Skip to content

Commit 563e1f2

Browse files
authored
z_html: also strip html comments and the content of style/script tags (#94)
* z_html: also strip html comments and the content of style/script tags * Add noscript test
1 parent c42d132 commit 563e1f2

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/z_convert.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ to_bool_strict(undefined) -> false;
176176
to_bool_strict(false) -> false;
177177
to_bool_strict(0) -> false;
178178
to_bool_strict(0.0) -> false;
179+
to_bool_strict(-0.0) -> false;
179180
to_bool_strict(<<>>) -> false;
180181
to_bool_strict(<<0>>) -> false;
181182
to_bool_strict([]) -> false;

src/z_html.erl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,26 @@ strip(<<"</span>",T/binary>>, Acc, N) ->
634634
strip(T, Acc, N);
635635
strip(<<"</a>",T/binary>>, Acc, N) ->
636636
strip(T, Acc, N);
637+
strip(<<"<script", T/binary>>, Acc, N) ->
638+
case binary:split(T, <<"</script">>) of
639+
[_] -> Acc;
640+
[_, Rest] -> strip_tag(Rest, Acc, N)
641+
end;
642+
strip(<<"<noscript", T/binary>>, Acc, N) ->
643+
case binary:split(T, <<"</noscript">>) of
644+
[_] -> Acc;
645+
[_, Rest] -> strip_tag(Rest, Acc, N)
646+
end;
647+
strip(<<"<style", T/binary>>, Acc, N) ->
648+
case binary:split(T, <<"</style">>) of
649+
[_] -> Acc;
650+
[_, Rest] -> strip_tag(Rest, Acc, N)
651+
end;
652+
strip(<<"<!--",T/binary>>, Acc, N) ->
653+
case binary:split(T, <<"-->">>) of
654+
[_] -> Acc;
655+
[_, Rest] -> strip(Rest, Acc, N)
656+
end;
637657
strip(<<"<",T/binary>>, Acc, N) ->
638658
strip_tag(T, Acc, N);
639659
strip(<<H/utf8,T/binary>>, Acc, N) ->

src/z_mochinum.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ digits(N) when is_integer(N) ->
2828
integer_to_list(N);
2929
digits(0.0) ->
3030
"0.0";
31+
digits(-0.0) ->
32+
"0.0";
3133
digits(Float) ->
3234
{Frac1, Exp1} = frexp_int(Float),
3335
[Place0 | Digits0] = digits1(Float, Exp1, Frac1),
@@ -269,6 +271,8 @@ digits_test() ->
269271
digits(0)),
270272
?assertEqual("0.0",
271273
digits(0.0)),
274+
?assertEqual("0.0",
275+
digits(-0.0)),
272276
?assertEqual("1.0",
273277
digits(1.0)),
274278
?assertEqual("-1.0",

test/z_html_test.erl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ strip_test() ->
6868
?assertEqual(<<"1234\n5678">>, z_html:strip(<<"<p>1234\n<span>5678</span></p>">>)),
6969
?assertEqual(<<"This is a list item.">>, z_html:strip(<<"<ul><li>This is a list item.</li></ul>">>)),
7070
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234</p><p>5678</p>">>)),
71+
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234</p><!-- Hello --><p>5678</p>">>)),
72+
?assertEqual(<<"12345678">>, z_html:strip(<<"<p>1234<!-- Hello -->5678</p>">>)),
73+
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234<style type=\"text/css\">foo: { a: x }</style>5678</p>">>)),
74+
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234<script type=\"text/javascript\">foo();</script>5678</p>">>)),
75+
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234<style>foo: { a: x }</style>5678</p>">>)),
76+
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234<script>foo();</script>5678</p>">>)),
77+
?assertEqual(<<"1234 5678">>, z_html:strip(<<"<p>1234<noscript>foobar</noscript>5678</p>">>)),
7178
ok.
7279

7380
abs_links_test() ->

0 commit comments

Comments
 (0)