From 7d8e75504cd9905e4466b94b5baa100d17c5230f Mon Sep 17 00:00:00 2001 From: Antony D'Andrea Date: Sat, 18 Feb 2017 19:25:35 +0000 Subject: [PATCH 1/2] #1802 left/right trim --- CHANGELOG | 1 + doc/filters/trim.rst | 18 ++++++++++++++- lib/Twig/Extension/Core.php | 27 +++++++++++++++++++++- test/Twig/Tests/Fixtures/filters/trim.test | 12 ++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 452e013cf..880a07f88 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ * 1.32.0 (2017-XX-XX) * added a PSR-11 compatible runtime loader + * added `side` argument to `trim` to allow left or right trimming only. * 1.31.0 (2017-01-11) diff --git a/doc/filters/trim.rst b/doc/filters/trim.rst index 4ddb2083b..f9c52f2a9 100644 --- a/doc/filters/trim.rst +++ b/doc/filters/trim.rst @@ -1,5 +1,8 @@ ``trim`` ======== +.. versionadded:: 1.32 + + The ``side`` argument was added in Twig 1.32. .. versionadded:: 1.6.2 The ``trim`` filter was added in Twig 1.6.2. @@ -17,13 +20,26 @@ and end of a string: {# outputs ' I like Twig' #} + {{ ' I like Twig. '|trim(side='left') }} + + {# outputs 'I like Twig. ' #} + + {{ ' I like Twig. '|trim(' ', 'right') }} + + {# outputs ' I like Twig.' #} + .. note:: - Internally, Twig uses the PHP `trim`_ function. + Internally, Twig uses the PHP `trim`_, `ltrim`_, and `rtrim`_ functions. Arguments --------- * ``character_mask``: The characters to strip +* ``side``: The default is to strip from the left and the right (`both`) sides, but `left` + and `right` will strip from either the left side or right side only + .. _`trim`: http://php.net/trim +.. _`ltrim`: http://php.net/ltrim +.. _`rtrim`: http://php.net/rtrim diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 0b99d64b2..d044c0cc0 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -163,7 +163,7 @@ class Twig_Extension_Core extends Twig_Extension new Twig_SimpleFilter('upper', 'strtoupper'), new Twig_SimpleFilter('lower', 'strtolower'), new Twig_SimpleFilter('striptags', 'strip_tags'), - new Twig_SimpleFilter('trim', 'trim'), + new Twig_SimpleFilter('trim', 'twig_trim_filter'), new Twig_SimpleFilter('nl2br', 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))), // array helpers @@ -945,6 +945,31 @@ function twig_in_filter($value, $compare) return false; } +/** + * Returns a trimmed string. + * + * @return string + * + * @throws Twig_Error_Runtime When an invalid trimming side is used (not a string or not 'left', 'right' or 'both') + */ +function twig_trim_filter($string, $characterMask = null, $side = 'both') +{ + if (null === $characterMask) { + $characterMask = " \t\n\r\0\x0B"; + } + + switch ($side) { + case 'both': + return trim($string, $characterMask); + case 'left': + return ltrim($string, $characterMask); + case 'right': + return rtrim($string, $characterMask); + default: + throw new Twig_Error_Runtime('Trimming side must be "left", "right" or "both".'); + } +} + /** * Escapes a string. * diff --git a/test/Twig/Tests/Fixtures/filters/trim.test b/test/Twig/Tests/Fixtures/filters/trim.test index 319206258..ce4035b29 100644 --- a/test/Twig/Tests/Fixtures/filters/trim.test +++ b/test/Twig/Tests/Fixtures/filters/trim.test @@ -4,9 +4,21 @@ {{ " I like Twig. "|trim }} {{ text|trim }} {{ " foo/"|trim("/") }} +{{ " I like Twig. "|trim(side="left") }} +{{ " I like Twig. "|trim(side="right") }} +{{ " I like Twig. "|trim(null, "right") }} +{{ "/ foo/"|trim("/", "left") }} +{{ "/ foo/"|trim(character_mask="/", side="left") }} +{{ " do nothing. "|trim("", "right") }} --DATA-- return array('text' => " If you have some HTML it will be escaped. ") --EXPECT-- I like Twig. If you have some <strong>HTML</strong> it will be escaped. foo +I like Twig. + I like Twig. + I like Twig. + foo/ + foo/ + do nothing. From af0d787c432cec78705d3796eff8268c51f327c4 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 23 Feb 2017 06:54:27 -0800 Subject: [PATCH 2/2] fixed CS --- doc/filters/trim.rst | 2 +- lib/Twig/Extension/Core.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/filters/trim.rst b/doc/filters/trim.rst index f9c52f2a9..b598363c2 100644 --- a/doc/filters/trim.rst +++ b/doc/filters/trim.rst @@ -1,7 +1,7 @@ ``trim`` ======== -.. versionadded:: 1.32 +.. versionadded:: 1.32 The ``side`` argument was added in Twig 1.32. .. versionadded:: 1.6.2 diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index d044c0cc0..b52198ae3 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -950,7 +950,7 @@ function twig_in_filter($value, $compare) * * @return string * - * @throws Twig_Error_Runtime When an invalid trimming side is used (not a string or not 'left', 'right' or 'both') + * @throws Twig_Error_Runtime When an invalid trimming side is used (not a string or not 'left', 'right', or 'both') */ function twig_trim_filter($string, $characterMask = null, $side = 'both') {