From cb6fab73c64f339ca95cafa2eeeb0395395d43f4 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Fri, 6 Jun 2014 22:31:00 +0600 Subject: [PATCH] Fixed #320 --- jinja2/filters.py | 3 ++- jinja2/testsuite/filters.py | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/jinja2/filters.py b/jinja2/filters.py index 882f915b..5c96b085 100644 --- a/jinja2/filters.py +++ b/jinja2/filters.py @@ -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): diff --git a/jinja2/testsuite/filters.py b/jinja2/testsuite/filters.py index 3cb4fd42..a32db374 100644 --- a/jinja2/testsuite/filters.py +++ b/jinja2/testsuite/filters.py @@ -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() -- 2.47.2