Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: z_html: add property filter callback #95

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 151 additions & 48 deletions src/z_html.erl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
%% @author Marc Worrell <[email protected]>
%% @copyright 2009-2022 Marc Worrell
%% @copyright 2009-2024 Marc Worrell
%% @doc Utility functions for html processing. Also used for property filtering (by m_rsc_update).
%% @end

%% Copyright 2009-2022 Marc Worrell
%% Copyright 2009-2024 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,7 +46,29 @@
abs_links/2
]).

-type text() :: iodata() | {trans, list( {atom(), binary()} )}.
-type unsafe_props() :: #{ props_key() => unsafe_props_value() }
| [ {props_key(), unsafe_props_value()} | props_key() ].
-type unsafe_props_value() :: unsafe_props()
| number()
| atom()
| list( unsafe_props_value() )
| unsafe_text()
| boolean().

-type props() :: #{ props_key() => props_value() }
| [ {props_key(), props_value()} | props_key() ].
-type props_key() :: atom() | binary().
-type props_value() :: props()
| number()
| atom()
| list( props_value() )
| trans()
| boolean()
| binary().

-type text() :: iodata() | trans().
-type trans() :: {trans, list( {atom()|binary(), text()} )}.

-type unsafe_text() :: iodata()
| {trans, list( {atom(), iodata()} )}
| {trans, list( {binary(), iodata()} )}
Expand All @@ -59,10 +81,19 @@
-type sanitize_options() :: [ sanitize_option() ].
-type sanitize_option() :: {elt_extra, list( binary() )}
| {attr_extra, list( binary() )}
| {element, function()}.
| {element, function()}
| {property, function()}.

-export_type([
unsafe_props/0,
unsafe_props_value/0,

props/0,
props_key/0,
props_value/0,

text/0,
trans/0,
unsafe_text/0,
maybe_text/0,
maybe_unsafe_text/0,
Expand All @@ -84,24 +115,47 @@
]).


%% @doc Escape all properties used for an update statement. Only leaves the body property intact.
-spec escape_props(list() | map()) -> list() | map().
%% @doc Escape all properties used for an update statement.
-spec escape_props(Props) -> Props1 when
Props:: unsafe_props(),
Props1 :: props().
escape_props(Props) ->
escape_props(Props, []).

-spec escape_props(list() | map(), Options::list()) -> list() | map().
%% @doc Escape all properties used for an update statement.
-spec escape_props(Props, Options) -> Props1 when
Props :: unsafe_props(),
Options :: sanitize_options(),
Props1 :: props().
escape_props(Props, Options) when is_list(Props) ->
lists:map(
fun({P, V}) ->
V1 = escape_props1(z_convert:to_binary(P), V, Options),
{P, V1}
lists:filtermap(
fun
({P, V}) ->
V1 = escape_props1(z_convert:to_binary(P), V, Options),
case property_filter_cb(P, V1, Options) of
true -> {true, {P, V1}};
PV -> PV
end;
(P) when is_atom(P) ->
case property_filter_cb(P, true, Options) of
true -> {true, {P, true}};
PV -> PV
end
end,
Props);
escape_props(Props, Options) when is_map(Props) ->
maps:fold(
fun(K, V, Acc) ->
K1 = z_convert:to_binary(K),
Acc#{ K1 => escape_props1(K1, V, Options)}
V1 = escape_props1(K1, V, Options),
case property_filter_cb(K1, V1, Options) of
{true, {K2, V2}} ->
Acc#{ K2 => V2 };
true ->
Acc#{ K1 => V1 };
false ->
Acc
end
end,
#{},
Props).
Expand Down Expand Up @@ -155,21 +209,24 @@ sanitize_type(_, Ks, V, Options) when is_list(V) -> sanitize_list(Ks, V, Options
sanitize_type(_, _Ks, V, _Options) -> escape_value(V).

sanitize_list(Ks, L, Options) when is_list(L) ->
lists:map(
lists:filtermap(
fun
({P, V}) ->
P1 = z_convert:to_binary(P),
V1 = escape_props1(P1, V, Options),
{P1, V1};
case property_filter_cb(P1, V1, Options) of
true -> {true, P, V1};
PV -> PV
end;
(V) when is_list(V), Ks =:= [] ->
escape_props(V, Options);
{true, escape_props(V, Options)};
(V) when is_map(V) ->
escape_props(V, Options);
{true, escape_props(V, Options)};
(V) when Ks =:= [] ->
escape_value(V);
{true, escape_value(V)};
(V) ->
[Type|Ks1] = Ks,
sanitize_type(Type, Ks1, V, Options)
{true, sanitize_type(Type, Ks1, V, Options)}
end,
L);
sanitize_list(Ks, Map, Options) when is_map(Map) ->
Expand Down Expand Up @@ -210,28 +267,47 @@ escape_value(V) ->
V.

%% @doc Checks if all properties are properly escaped
-spec escape_props_check(list() | map()) -> list() | map().
-spec escape_props_check(Props) -> Props1 when
Props :: unsafe_props(),
Props1 :: props().
escape_props_check(Props) ->
escape_props_check(Props, []).

-spec escape_props_check(list() | map(), Options::list()) -> list() | map().
-spec escape_props_check(Props, Options) -> Props1 when
Props :: unsafe_props(),
Options :: sanitize_options(),
Props1 :: props().
escape_props_check(Props, Options) when is_list(Props) ->
lists:map(
lists:filtermap(
fun
({P, V}) ->
V1 = escape_props_check1(z_convert:to_binary(P), V, Options),
{P, V1};
(V) when is_list(V); is_map(V)->
escape_props_check(V, Options);
(V) ->
escape_value_check(V)
case property_filter_cb(P, V1, Options) of
true -> {true, {P, V1}};
PV -> PV
end;
(P) when is_atom(P) ->
case property_filter_cb(P, true, Options) of
true -> {true, {P, true}};
PV -> PV
end
end,
Props);
escape_props_check(Props, Options) when is_map(Props) ->
maps:map(
fun(P, V) ->
escape_props_check1(z_convert:to_binary(P), V, Options)
maps:fold(
fun(K, V, Acc) ->
K1 = z_convert:to_binary(K),
V1 = escape_props_check1(K1, V, Options),
case property_filter_cb(K1, V1, Options) of
{true, {K2, V2}} ->
Acc#{ K2 => V2 };
true ->
Acc#{ K1 => V1 };
false ->
Acc
end
end,
#{},
Props).


Expand Down Expand Up @@ -272,21 +348,24 @@ sanitize_type_check(<<"list">>, V, Options) -> sanitize_list_check(V, Options);
sanitize_type_check(<<"int">>, V, _Options) -> sanitize_int(V);
sanitize_type_check(<<"unsafe">>, V, _Options) -> V;
sanitize_type_check(_, V, Options) when is_map(V) -> escape_props_check(V, Options);
sanitize_type_check(_, V, Options) when is_list(V) -> escape_props_check(V, Options);
sanitize_type_check(_, V, Options) when is_list(V) -> sanitize_list_check(V, Options);
sanitize_type_check(_, V, _Options) -> escape_value_check(V).


sanitize_list_check(L, Options) when is_list(L) ->
lists:map(
lists:filtermap(
fun
({P, V}) ->
P1 = z_convert:to_binary(P),
V1 = escape_props_check1(P1, V, Options),
{P1, V1};
case property_filter_cb(P1, V1, Options) of
true -> {true, {P, V1}};
PV -> PV
end;
(V) when is_list(V); is_map(V)->
escape_props_check(V, Options);
{true, escape_props_check(V, Options)};
(V) ->
escape_value_check(V)
{true, escape_value_check(V)}
end,
L);
sanitize_list_check(Map, Options) when is_map(Map) ->
Expand Down Expand Up @@ -326,14 +405,12 @@ escape_value_check(V) ->
escape({trans, Tr}) when is_list(Tr) ->
Tr1 = lists:filtermap(
fun
({Lang, V}) when is_atom(Lang) ->
V1 = z_convert:to_binary(V),
{true, {Lang, escape(V1)}};
({Lang, V}) when is_binary(Lang) ->
({Lang, V}) when is_binary(Lang); is_atom(Lang) ->
try
Lang1 = binary_to_existing_atom(Lang, utf8),
Lang1 = sanitize_iso639_1(Lang),
Lang2 = binary_to_existing_atom(Lang1, utf8),
V1 = z_convert:to_binary(V),
{true, {Lang1, escape(V1)}}
{true, {Lang2, escape(V1)}}
catch _:_ ->
false
end;
Expand All @@ -360,7 +437,7 @@ escape(B) when is_binary(B) ->
escape1(<<>>, Acc) ->
Acc;
escape1(<<"&euro;", T/binary>>, Acc) ->
escape1(T, <<Acc/binary, "€">>);
escape1(T, <<Acc/binary, "€"/utf8>>);
escape1(<<$&, T/binary>>, Acc) ->
escape1(T, <<Acc/binary, "&amp;">>);
escape1(<<$<, T/binary>>, Acc) ->
Expand All @@ -380,14 +457,12 @@ escape1(<<C, T/binary>>, Acc) ->
escape_check({trans, Tr}) when is_list(Tr) ->
Tr1 = lists:filtermap(
fun
({Lang, V}) when is_atom(Lang) ->
V1 = z_convert:to_binary(V),
{true, {Lang, escape_check(V1)}};
({Lang, V}) when is_binary(Lang) ->
({Lang, V}) when is_binary(Lang); is_atom(Lang) ->
try
Lang1 = binary_to_existing_atom(Lang, utf8),
Lang1 = sanitize_iso639_1(Lang),
Lang2 = binary_to_existing_atom(Lang1, utf8),
V1 = z_convert:to_binary(V),
{true, {Lang1, escape_check(V1)}}
{true, {Lang2, escape_check(V1)}}
catch _:_ ->
false
end;
Expand All @@ -413,10 +488,38 @@ escape_check(B) when is_binary(B) ->
escape_check(Other) ->
Other.

property_filter_cb(K, V, Options) ->
case lists:keyfind(property, 1, Options) of
{property, F} ->
F(K, V);
false ->
true
end.

-define(is_lower_alpha(C), (C >= $a andalso C =< $z)).
-define(is_alpha(C), ((C >= $a andalso C =< $z) orelse (C >= $A andalso C =< $Z))).

% Sanitize a language code, so that "it looks like" an ISO639-1 code.
sanitize_iso639_1(Lang) when is_binary(Lang) ->
case Lang of
<<A, B>> when ?is_lower_alpha(A), ?is_lower_alpha(B) ->
Lang;
<<A, B, $-, Rest/binary>> when ?is_lower_alpha(A), ?is_lower_alpha(B) ->
Rest1 = << <<C>> || <<C>> <= Rest, ?is_alpha(C) >>,
<<A, B, $-, Rest1/binary>>;
<<$x, $-, Rest/binary>> ->
Rest1 = << <<C>> || <<C>> <= Rest, ?is_alpha(C) >>,
<<$x, $-, Rest1/binary>>;
_ ->
<<"x-other">>
end;
sanitize_iso639_1(Lang) when is_atom(Lang) ->
sanitize_iso639_1(atom_to_binary(Lang, utf8)).

escape_check1(<<>>, Acc) ->
Acc;
escape_check1(<<"&euro;", T/binary>>, Acc) ->
escape_check1(T, <<Acc/binary, "€">>);
escape_check1(T, <<Acc/binary, "€"/utf8>>);
escape_check1(<<"&amp;", T/binary>>, Acc) ->
escape_check1(T, <<Acc/binary, "&amp;">>);
escape_check1(<<"&lt;", T/binary>>, Acc) ->
Expand Down
Loading