|
@@ -37,13 +37,37 @@ coalesce([undefined|T]) -> coalesce(T);
|
|
|
coalesce([[]|T]) -> coalesce(T);
|
|
|
coalesce([H|_]) -> H.
|
|
|
|
|
|
-jse(X) -> js_escape(X).
|
|
|
hte(X) when erlang:is_binary(X) ->
|
|
|
nitro:to_binary(nitro_conv:html_encode(X));
|
|
|
hte(X) -> nitro_conv:html_encode(X).
|
|
|
|
|
|
-js_escape(Value) ->
|
|
|
- nitro_conv:js_escape(Value).
|
|
|
+
|
|
|
+%% JavaScript encode/decode
|
|
|
+
|
|
|
+js_escape(undefined) -> [];
|
|
|
+js_escape(Value) when erlang:is_list(Value) ->
|
|
|
+ erlang:binary_to_list(js_escape(erlang:iolist_to_binary(Value)));
|
|
|
+js_escape(Value) -> js_escape(Value, <<>>).
|
|
|
+
|
|
|
+js_escape(<<"\\", Rest/binary>>, Acc) -> %"
|
|
|
+ js_escape(Rest, <<Acc/binary, "\\\\">>); %"
|
|
|
+js_escape(<<"\r", Rest/binary>>, Acc) ->
|
|
|
+ js_escape(Rest, <<Acc/binary, "\\r">>);
|
|
|
+js_escape(<<"\n", Rest/binary>>, Acc) ->
|
|
|
+ js_escape(Rest, <<Acc/binary, "\\n">>);
|
|
|
+js_escape(<<"\"", Rest/binary>>, Acc) ->
|
|
|
+ js_escape(Rest, <<Acc/binary, "\\\"">>);
|
|
|
+js_escape(<<"'", Rest/binary>>, Acc) ->
|
|
|
+ js_escape(Rest, <<Acc/binary, "\\'">>);
|
|
|
+js_escape(<<"`", Rest/binary>>, Acc) ->
|
|
|
+ js_escape(Rest, <<Acc/binary, "\\`">>);
|
|
|
+js_escape(<<"<script", Rest/binary>>, Acc) ->
|
|
|
+ js_escape(Rest, <<Acc/binary, "<script">>);
|
|
|
+js_escape(<<"script>", Rest/binary>>, Acc) ->
|
|
|
+ js_escape(Rest, <<Acc/binary, "script>">>);
|
|
|
+js_escape(<<C, Rest/binary>>, Acc) ->
|
|
|
+ js_escape(Rest, <<Acc/binary, C>>);
|
|
|
+js_escape(<<>>, Acc) -> Acc.
|
|
|
|
|
|
|
|
|
-define(IS_STRING(Term),
|
|
@@ -101,6 +125,8 @@ unique_integer() -> erlang:unique_integer().
|
|
|
temp_id() ->
|
|
|
"auto" ++ integer_to_list(unique_integer() rem 1000000).
|
|
|
|
|
|
+
|
|
|
+%% html_encode(B, normal)
|
|
|
html_encode(L, Fun) when erlang:is_function(Fun) ->
|
|
|
Fun(L);
|
|
|
html_encode(L, EncType) when erlang:is_atom(L) ->
|
|
@@ -115,10 +141,16 @@ html_encode(L, true) ->
|
|
|
L;
|
|
|
html_encode(L, whites) ->
|
|
|
html_encode_whites(nitro:to_list(lists:flatten([L]))).
|
|
|
+
|
|
|
+
|
|
|
html_encode(<<>>) ->
|
|
|
[];
|
|
|
html_encode([]) ->
|
|
|
[];
|
|
|
+html_encode(B) when is_binary(B) ->
|
|
|
+ html_encode(erlang:binary_to_list(B)); % todo check utf8
|
|
|
+html_encode([$\n |T]) ->
|
|
|
+ "<br>" ++ html_encode(T);
|
|
|
html_encode([H|T]) ->
|
|
|
case H of
|
|
|
$< -> "<" ++ html_encode(T);
|
|
@@ -126,8 +158,11 @@ html_encode([H|T]) ->
|
|
|
$" -> """ ++ html_encode(T);
|
|
|
$' -> "'" ++ html_encode(T);
|
|
|
$& -> "&" ++ html_encode(T);
|
|
|
+ $\\ -> "\" ++ html_encode(T);
|
|
|
BigNum when erlang:is_integer(BigNum) andalso BigNum > 255 ->
|
|
|
- [$&,$# | nitro:to_list(BigNum)] ++ ";" ++ html_encode(T);
|
|
|
+ %% Any integers above 255 are converted to their HTML encode equivalent
|
|
|
+ %% Example: 7534 gets turned into ᵮ
|
|
|
+ [$&, $# | nitro:to_list(BigNum)] ++ ";" ++ html_encode(T);
|
|
|
Tup when erlang:is_tuple(Tup) ->
|
|
|
erlang:throw({html_encode, encountered_tuple, Tup});
|
|
|
_ ->
|
|
@@ -145,6 +180,7 @@ html_encode_whites([H|T]) ->
|
|
|
$' -> "'" ++ html_encode_whites(T);
|
|
|
$& -> "&" ++ html_encode_whites(T);
|
|
|
$\n -> "<br>" ++ html_encode_whites(T);
|
|
|
+ $\\ -> "\" ++ html_encode_whites(T);
|
|
|
_ -> [H|html_encode_whites(T)]
|
|
|
end.
|
|
|
|
|
@@ -258,7 +294,7 @@ compact(Tuple) when erlang:is_tuple(Tuple) ->
|
|
|
lists:sublist(erlang:tuple_to_list(Tuple), 1, Min)),
|
|
|
"{" ++ string:join([ io_lib:format("~s", [compact(F)]) || {_, F} <- Fields ], ",") ++ "}";
|
|
|
compact(T) ->
|
|
|
- nitro:jse(nitro:to_list(T)).
|
|
|
+ nitro:js_escape(nitro:to_list(T)).
|
|
|
|
|
|
meg(X) -> erlang:integer_to_list(X div 1000000) ++ "M".
|
|
|
|