PageRenderTime 33ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/utils/ewgi_util_html.erl

http://github.com/skarab/ewgi
Erlang | 44 lines | 22 code | 7 blank | 15 comment | 0 complexity | c06da82f8999aee2e979b0bf62afd172 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. %% @author Hunter Morris <hunter.morris@smarkets.com>
  2. %% @copyright 2009 Smarkets Limited.
  3. %%
  4. %% @doc Smak HTML utility methods.
  5. %% @end
  6. %%
  7. %% Licensed under the MIT license:
  8. %% http://www.opensource.org/licenses/mit-license.php
  9. %%
  10. %% Some code is based on the Python Paste Project which is copyright Ian
  11. %% Bicking, Clark C. Evans, and contributors and released under the MIT
  12. %% license. See: http://pythonpaste.org/
  13. -module(ewgi_util_html).
  14. -author('Hunter Morris <hunter.morris@smarkets.com>').
  15. -export([escape/1]).
  16. -include("ewgi.hrl").
  17. %% @spec escape(binary() | string()) -> binary() | string()
  18. %% @doc Replace the special characters '&lt;', '&gt;', '&amp;', and '&quot;'
  19. %% to HTML entity sequences.
  20. -spec(escape/1 :: (binary() | string()) -> binary() | string()).
  21. escape(Bin) when is_binary(Bin) ->
  22. list_to_binary(escape(binary_to_list(Bin), []));
  23. escape(S) when is_list(S) ->
  24. escape(S, []).
  25. -spec(escape/2 :: (string(), list()) -> string()).
  26. escape([], Acc) ->
  27. lists:reverse(Acc);
  28. escape([$<|Rest], Acc) ->
  29. escape(Rest, lists:reverse("&lt;", Acc));
  30. escape([$>|Rest], Acc) ->
  31. escape(Rest, lists:reverse("&gt;", Acc));
  32. escape([$&|Rest], Acc) ->
  33. escape(Rest, lists:reverse("&amp;", Acc));
  34. escape([$\"|Rest], Acc) ->
  35. escape(Rest, lists:reverse("&quot;", Acc));
  36. escape([C|Rest], Acc) ->
  37. escape(Rest, [C|Acc]).