Browse Source

rm jse wrapper; html_encode

221V 3 years ago
parent
commit
8f6f056974

+ 4 - 4
src/elements/combo/element_comboLookup.erl

@@ -16,15 +16,15 @@ render_element(#comboLookup{id=Id, style=Style, value = Val, bind = Object, feed
     #panel{id=form:atom([lookup, Id]), class=[dropdown],
            body=[#input{id=Id, disabled = Disabled, type="comboLookup",
                         autocomplete = "off",
-                        onkeyup = nitro:jse("comboLookupKeyup('"
+                        onkeyup = nitro:js_escape("comboLookupKeyup('"
                                ++ nitro:to_list(Id) ++ "','"
                                ++ nitro:to_list(Feed) ++ "','"
                                ++ nitro:to_list(Module) ++ "')"),
-                        onkeydown= nitro:jse("comboLookupKeydown('"
+                        onkeydown = nitro:js_escape("comboLookupKeydown('"
                                ++ nitro:to_list(Id) ++ "','"
                                ++ nitro:to_list(Feed) ++ "','"
                                ++ nitro:to_list(Module) ++ "')"),
-                        onchange= nitro:jse("comboLookupChange('"
+                        onchange = nitro:js_escape("comboLookupChange('"
                                ++ nitro:to_list(Id) ++ "')"),
                         bind = Object,
                         value = Val, style = Style, class = column},
@@ -33,7 +33,7 @@ render_element(#comboLookup{id=Id, style=Style, value = Val, bind = Object, feed
                         onclick =
                           case Disabled of
                             true -> [];
-                            _ -> nitro:jse("comboLookupClick('"
+                            _ -> nitro:js_escape("comboLookupClick('"
                               ++ nitro:to_list(Id) ++ "','"
                               ++ nitro:to_list(Feed) ++ "','"
                               ++ nitro:to_list(Module) ++ "')")

+ 1 - 1
src/elements/combo/element_comboLookupEdit.erl

@@ -27,7 +27,7 @@ render_element(#comboLookupEdit{id=Id, input=Input, disabled=Disabled, validatio
                   #link{
                     class = [button, sgreen],
                     style = "min-width: 40px; text-align: center; height: fit-content; margin-left: 5px;",
-                    onclick = nitro:jse("addSortableItemFrom('#" ++ ListId ++ "', '" ++ InputId ++ "')"),
+                    onclick = nitro:js_escape("addSortableItemFrom('#" ++ ListId ++ "', '" ++ InputId ++ "')"),
                     body = <<"+">>};
                 false -> []
               end,

+ 1 - 1
src/elements/combo/element_comboLookupVec.erl

@@ -27,7 +27,7 @@ render_element(#comboLookupVec{id=Id, input=Input, disabled=Disabled, validation
                   #link{
                     class = [button, sgreen],
                     style = "min-width: 40px; text-align: center; height: fit-content; margin-left: 5px;",
-                    onclick = nitro:jse("addSortableItemFrom('#" ++ ListId ++ "', '" ++ InputId ++ "')"),
+                    onclick = nitro:js_escape("addSortableItemFrom('#" ++ ListId ++ "', '" ++ InputId ++ "')"),
                     body = <<"+">>} ]
             end
         },

+ 1 - 1
src/elements/combo/element_sortable_item.erl

@@ -30,7 +30,7 @@ render_element(#sortable_item{list_id=ListId, value=Value, bind=Bind, closeable=
             true -> 
               #panel{
                 class = <<"list__item-close">>,
-                onclick = nitro:jse("removeSortableItem('#" ++ ListId ++ "', this.parentNode);")};
+                onclick = nitro:js_escape("removeSortableItem('#" ++ ListId ++ "', this.parentNode);")};
             _ -> []
           end,
           #panel{

+ 41 - 5
src/nitro.erl

@@ -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
     $< -> "&lt;" ++ html_encode(T);
@@ -126,8 +158,11 @@ html_encode([H|T]) ->
     $" -> "&quot;" ++ html_encode(T);
     $' -> "&#39;" ++ html_encode(T);
     $& -> "&amp;" ++ html_encode(T);
+    $\\ -> "&#92;" ++ 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 &#7534;
+      [$&, $# | 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]) ->
     $' -> "&#39;" ++ html_encode_whites(T);
     $& -> "&amp;" ++ html_encode_whites(T);
     $\n -> "<br>" ++ html_encode_whites(T);
+    $\\ -> "&#92;" ++ 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".
 

+ 0 - 28
src/nitro_conv.erl

@@ -152,34 +152,6 @@ qs_revdecode([Lo, Hi, ?PERCENT | Rest], Acc) when ?IS_HEX(Lo), ?IS_HEX(Hi) ->
   qs_revdecode(Rest, [(unhexdigit(Lo) bor (unhexdigit(Hi) bsl 4)) | Acc]);
 qs_revdecode([C | Rest], Acc) -> qs_revdecode(Rest, [C | Acc]).
 
-
-%% 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.
-
-
 % JOIN
 
 join([],_) -> [];