/src/utils/ewgi_util_html.erl
Erlang | 44 lines | 22 code | 7 blank | 15 comment | 0 complexity | c06da82f8999aee2e979b0bf62afd172 MD5 | raw file
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 14-module(ewgi_util_html). 15-author('Hunter Morris <hunter.morris@smarkets.com>'). 16 17-export([escape/1]). 18 19-include("ewgi.hrl"). 20 21%% @spec escape(binary() | string()) -> binary() | string() 22%% @doc Replace the special characters '<', '>', '&', and '"' 23%% to HTML entity sequences. 24-spec(escape/1 :: (binary() | string()) -> binary() | string()). 25 26escape(Bin) when is_binary(Bin) -> 27 list_to_binary(escape(binary_to_list(Bin), [])); 28escape(S) when is_list(S) -> 29 escape(S, []). 30 31-spec(escape/2 :: (string(), list()) -> string()). 32 33escape([], Acc) -> 34 lists:reverse(Acc); 35escape([$<|Rest], Acc) -> 36 escape(Rest, lists:reverse("<", Acc)); 37escape([$>|Rest], Acc) -> 38 escape(Rest, lists:reverse(">", Acc)); 39escape([$&|Rest], Acc) -> 40 escape(Rest, lists:reverse("&", Acc)); 41escape([$\"|Rest], Acc) -> 42 escape(Rest, lists:reverse(""", Acc)); 43escape([C|Rest], Acc) -> 44 escape(Rest, [C|Acc]).