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

z_html: also strip html comments and the content of style/script tags #94

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/z_convert.erl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ to_bool_strict(undefined) -> false;
to_bool_strict(false) -> false;
to_bool_strict(0) -> false;
to_bool_strict(0.0) -> false;
to_bool_strict(-0.0) -> false;
to_bool_strict(<<>>) -> false;
to_bool_strict(<<0>>) -> false;
to_bool_strict([]) -> false;
Expand Down
20 changes: 20 additions & 0 deletions src/z_html.erl
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,26 @@ strip(<<"</span>",T/binary>>, Acc, N) ->
strip(T, Acc, N);
strip(<<"</a>",T/binary>>, Acc, N) ->
strip(T, Acc, N);
strip(<<"<script", T/binary>>, Acc, N) ->
case binary:split(T, <<"</script">>) of
[_] -> Acc;
[_, Rest] -> strip_tag(Rest, Acc, N)
end;
strip(<<"<noscript", T/binary>>, Acc, N) ->
case binary:split(T, <<"</noscript">>) of
[_] -> Acc;
[_, Rest] -> strip_tag(Rest, Acc, N)
end;
strip(<<"<style", T/binary>>, Acc, N) ->
case binary:split(T, <<"</style">>) of
[_] -> Acc;
[_, Rest] -> strip_tag(Rest, Acc, N)
end;
strip(<<"<!--",T/binary>>, Acc, N) ->
case binary:split(T, <<"-->">>) of
[_] -> Acc;
[_, Rest] -> strip(Rest, Acc, N)
end;
strip(<<"<",T/binary>>, Acc, N) ->
strip_tag(T, Acc, N);
strip(<<H/utf8,T/binary>>, Acc, N) ->
Expand Down
4 changes: 4 additions & 0 deletions src/z_mochinum.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ digits(N) when is_integer(N) ->
integer_to_list(N);
digits(0.0) ->
"0.0";
digits(-0.0) ->
"0.0";
digits(Float) ->
{Frac1, Exp1} = frexp_int(Float),
[Place0 | Digits0] = digits1(Float, Exp1, Frac1),
Expand Down Expand Up @@ -269,6 +271,8 @@ digits_test() ->
digits(0)),
?assertEqual("0.0",
digits(0.0)),
?assertEqual("0.0",
digits(-0.0)),
?assertEqual("1.0",
digits(1.0)),
?assertEqual("-1.0",
Expand Down
7 changes: 7 additions & 0 deletions test/z_html_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ strip_test() ->
?assertEqual(<<"1234\n5678">>, z_html:strip(<<"<p>1234\n<span>5678</span></p>">>)),
?assertEqual(<<"This is a list item.">>, z_html:strip(<<"<ul><li>This is a list item.</li></ul>">>)),
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234</p><p>5678</p>">>)),
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234</p><!-- Hello --><p>5678</p>">>)),
?assertEqual(<<"12345678">>, z_html:strip(<<"<p>1234<!-- Hello -->5678</p>">>)),
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234<style type=\"text/css\">foo: { a: x }</style>5678</p>">>)),
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234<script type=\"text/javascript\">foo();</script>5678</p>">>)),
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234<style>foo: { a: x }</style>5678</p>">>)),
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234<script>foo();</script>5678</p>">>)),
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234<noscript>foobar</noscript>5678</p>">>)),
ok.

abs_links_test() ->
Expand Down
Loading