Browse Source

Fix bug in truncatewords_html on closing tags

`truncatewords_html` works as tested, but wasn't tested against one
potential case of input: if there's a completed closed tag in place.
erlydtl was throwing `function_clause` errors because it expected the
very next characters after `/` to be `>`, when it will normally be the
tag's name (i.e. the characters "strong" in "</strong>").

This adds a test case where truncation doesn't occur, and several tags
are opened and closed, as well as the function clause necessary to parse
the tags correctly.
Pablo Meier 10 years ago
parent
commit
5ac3718a16
2 changed files with 6 additions and 1 deletions
  1. 3 1
      src/erlydtl_filters.erl
  2. 3 0
      test/erlydtl_test_defs.erl

+ 3 - 1
src/erlydtl_filters.erl

@@ -1117,7 +1117,9 @@ truncatewords_html_io([C|Rest], WordsLeft, Acc, Tags, tag) ->
 truncatewords_html_io([C|Rest], WordsLeft, Acc, Tags, attrs) when C =:= $> ->
 truncatewords_html_io([C|Rest], WordsLeft, Acc, Tags, attrs) when C =:= $> ->
     truncatewords_html_io(Rest, WordsLeft, [C|Acc], Tags, text);
     truncatewords_html_io(Rest, WordsLeft, [C|Acc], Tags, text);
 truncatewords_html_io([C|Rest], WordsLeft, Acc, [_Tag|RestOfTags], close_tag) when C =:= $> ->
 truncatewords_html_io([C|Rest], WordsLeft, Acc, [_Tag|RestOfTags], close_tag) when C =:= $> ->
-    truncatewords_html_io(Rest, WordsLeft, [C|Acc], RestOfTags, text).
+    truncatewords_html_io(Rest, WordsLeft, [C|Acc], RestOfTags, text);
+truncatewords_html_io([C|Rest], WordsLeft, Acc, Tags, close_tag) when C =/= $> ->
+    truncatewords_html_io(Rest, WordsLeft, [C|Acc], Tags, close_tag).
 
 
 wordcount([], Count) ->
 wordcount([], Count) ->
     Count;
     Count;

+ 3 - 0
test/erlydtl_test_defs.erl

@@ -1059,6 +1059,9 @@ all_test_defs() ->
        {"|truncatewords_html:4",
        {"|truncatewords_html:4",
         <<"{{ var1|truncatewords_html:4 }}">>, [{var1, "<p>The <strong>Long and <em>Winding</em> Road</strong> is too long</p>"}],
         <<"{{ var1|truncatewords_html:4 }}">>, [{var1, "<p>The <strong>Long and <em>Winding</em> Road</strong> is too long</p>"}],
         <<"<p>The <strong>Long and <em>Winding</em>...</strong></p>">>},
         <<"<p>The <strong>Long and <em>Winding</em>...</strong></p>">>},
+       {"|truncatewords_html:50",
+        <<"{{ var1|truncatewords_html:50 }}">>, [{var1, "<p>The <strong>Long and <em>Winding</em> Road</strong> is too long</p>"}],
+        <<"<p>The <strong>Long and <em>Winding</em> Road</strong> is too long</p>">>},
        {"|unordered_list",
        {"|unordered_list",
         <<"{{ var1|unordered_list }}">>, [{var1, ["States", ["Kansas", ["Lawrence", "Topeka"], "Illinois"]]}],
         <<"{{ var1|unordered_list }}">>, [{var1, ["States", ["Kansas", ["Lawrence", "Topeka"], "Illinois"]]}],
         <<"<li>States<ul><li>Kansas<ul><li>Lawrence</li><li>Topeka</li></ul></li><li>Illinois</li></ul></li>">>},
         <<"<li>States<ul><li>Kansas<ul><li>Lawrence</li><li>Topeka</li></ul></li><li>Illinois</li></ul></li>">>},