]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Fixed #320
authorArmin Ronacher <armin.ronacher@active-4.com>
Fri, 6 Jun 2014 16:31:00 +0000 (22:31 +0600)
committerArmin Ronacher <armin.ronacher@active-4.com>
Fri, 6 Jun 2014 16:31:00 +0000 (22:31 +0600)
jinja2/filters.py
jinja2/testsuite/filters.py

index 882f915b9bd72461a77cc199b67eaa02abfcfcc7..5c96b085068586e13366ee63958734858970b0a1 100644 (file)
@@ -473,7 +473,7 @@ def do_truncate(s, length=255, killwords=False, end='...'):
             -> "foo ..."
 
     """
-    if len(s) <= (length + len(end)):
+    if len(s) <= length:
         return s
     elif killwords:
         return s[:length - len(end)] + end
@@ -483,6 +483,7 @@ def do_truncate(s, length=255, killwords=False, end='...'):
         result += ' '
     return result + end
 
+
 @environmentfilter
 def do_wordwrap(environment, s, width=79, break_long_words=True,
                 wrapstring=None):
index 3cb4fd427800c1b1557213f0c6a41ca4514a5c4f..a32db374c498651673c0b22566df203af012623b 100644 (file)
@@ -230,6 +230,14 @@ class FilterTestCase(JinjaTestCase):
         msg = 'Current output: %s' % out
         assert out == 'foobar baz b>>>|foobar baz >>>|foobar baz bar', msg
 
+    def test_truncate_very_short(self):
+        tmpl = env.from_string(
+            '{{ "foo bar baz"|truncate(9) }}|'
+            '{{ "foo bar baz"|truncate(9, true) }}'
+        )
+        out = tmpl.render()
+        assert out == 'foo ...|foo ba...', out
+
     def test_truncate_end_length(self):
         tmpl = env.from_string('{{ "Joel is a slug"|truncate(9, true) }}')
         out = tmpl.render()