Просмотр исходного кода

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 лет назад
Родитель
Сommit
fb7c74c5aa
2 измененных файлов с 6 добавлено и 1 удалено
  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(Rest, WordsLeft, [C|Acc], Tags, text);
 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) ->
     Count;

+ 3 - 0
test/erlydtl_test_defs.erl

@@ -1059,6 +1059,9 @@ all_test_defs() ->
        {"|truncatewords_html:4",
         <<"{{ 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>">>},
+       {"|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",
         <<"{{ 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>">>},