diff --git a/CHANGELOG b/CHANGELOG index fa3a1f646..178061dc2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ * 2.2.0 (2017-XX-XX) * added a PSR-11 compatible runtime loader + * added `side` argument to `trim` to allow left or right trimming only. * 2.1.0 (2017-01-11) @@ -31,6 +32,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 815895b91..860417858 100644 --- a/doc/filters/trim.rst +++ b/doc/filters/trim.rst @@ -14,13 +14,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 e5ee86ce3..1df0b65e6 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -155,7 +155,7 @@ final class Twig_Extension_Core extends Twig_Extension new Twig_Filter('upper', 'twig_upper_filter', array('needs_environment' => true)), new Twig_Filter('lower', 'twig_lower_filter', array('needs_environment' => true)), new Twig_Filter('striptags', 'strip_tags'), - new Twig_Filter('trim', 'trim'), + new Twig_Filter('trim', 'twig_trim_filter'), new Twig_Filter('nl2br', 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))), // array helpers @@ -852,6 +852,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.