/src/utils/ewgi_util_html.erl
Erlang | 44 lines | 22 code | 7 blank | 15 comment | 0 complexity | c06da82f8999aee2e979b0bf62afd172 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
- %% @author Hunter Morris <hunter.morris@smarkets.com>
- %% @copyright 2009 Smarkets Limited.
- %%
- %% @doc Smak HTML utility methods.
- %% @end
- %%
- %% Licensed under the MIT license:
- %% http://www.opensource.org/licenses/mit-license.php
- %%
- %% Some code is based on the Python Paste Project which is copyright Ian
- %% Bicking, Clark C. Evans, and contributors and released under the MIT
- %% license. See: http://pythonpaste.org/
- -module(ewgi_util_html).
- -author('Hunter Morris <hunter.morris@smarkets.com>').
- -export([escape/1]).
- -include("ewgi.hrl").
- %% @spec escape(binary() | string()) -> binary() | string()
- %% @doc Replace the special characters '<', '>', '&', and '"'
- %% to HTML entity sequences.
- -spec(escape/1 :: (binary() | string()) -> binary() | string()).
-
- escape(Bin) when is_binary(Bin) ->
- list_to_binary(escape(binary_to_list(Bin), []));
- escape(S) when is_list(S) ->
- escape(S, []).
- -spec(escape/2 :: (string(), list()) -> string()).
- escape([], Acc) ->
- lists:reverse(Acc);
- escape([$<|Rest], Acc) ->
- escape(Rest, lists:reverse("<", Acc));
- escape([$>|Rest], Acc) ->
- escape(Rest, lists:reverse(">", Acc));
- escape([$&|Rest], Acc) ->
- escape(Rest, lists:reverse("&", Acc));
- escape([$\"|Rest], Acc) ->
- escape(Rest, lists:reverse(""", Acc));
- escape([C|Rest], Acc) ->
- escape(Rest, [C|Acc]).