]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
add chars option to trim 828/head
authorMatteo Ferrando <matteo.ferrando2@gmail.com>
Wed, 21 Mar 2018 16:47:46 +0000 (12:47 -0400)
committerDavid Lord <davidism@gmail.com>
Fri, 4 Oct 2019 18:46:04 +0000 (11:46 -0700)
CHANGES.rst
jinja2/filters.py
tests/test_filters.py

index 692a5161667169959c6d8ee6a625fa79178e63a2..fe99937daba3a3c58a6abe5666bd06bdbf2e2a59 100644 (file)
@@ -22,6 +22,8 @@ Unreleased
 -   Int and float literals can be written with the '_' separator for
     legibility, like 12_345. :pr:`923`
 -   Fix a bug causing deadlocks in ``LRUCache.setdefault``. :pr:`1000`
+-   The ``trim`` filter takes an optional string of characters to trim.
+    :pr:`828`
 
 
 Version 2.10.2
index 488aab56907440525f9fc4b8fc07548152e1bf25..40dbfcb3a358201536c5188b66573e47bf5c52cf 100644 (file)
@@ -757,9 +757,9 @@ def do_format(value, *args, **kwargs):
     return soft_unicode(value) % (kwargs or args)
 
 
-def do_trim(value):
-    """Strip leading and trailing whitespace."""
-    return soft_unicode(value).strip()
+def do_trim(value, chars=None):
+    """Strip leading and trailing characters, by default whitespace."""
+    return soft_unicode(value).strip(chars)
 
 
 def do_striptags(value):
index cfa1f43d73ffe532ca4a258d1b3a6b18de495897..33123f971a04e712648049428016db39c4805c07 100644 (file)
@@ -85,6 +85,14 @@ class TestFilter(object):
         out = tmpl.render()
         assert out == '&lt;&#34;&gt;&amp;'
 
+    @pytest.mark.parametrize(
+        ("chars", "expect"), [(None, "..stays.."), (".", "  ..stays"), (" .", "stays")]
+    )
+    def test_trim(self, env, chars, expect):
+        tmpl = env.from_string("{{ foo|trim(chars) }}")
+        out = tmpl.render(foo="  ..stays..", chars=chars)
+        assert out == expect
+
     def test_striptags(self, env):
         tmpl = env.from_string('''{{ foo|striptags }}''')
         out = tmpl.render(foo='  <p>just a small   \n <a href="#">'