From: Fabien Potencier Date: Sun, 27 Oct 2013 09:56:11 +0000 (+0100) Subject: allowed operators that contain whitespaces to have more than one whitespace X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=724dc61beabf2e71e2a4d6cb2ff76d09b1392447;p=konrad%2Ftwig.git allowed operators that contain whitespaces to have more than one whitespace --- diff --git a/CHANGELOG b/CHANGELOG index c0fa5d0..fdcf0ff 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ * 1.14.2 (2013-XX-XX) - * n/a + * allowed operators that contain whitespaces to have more than one whitespace * 1.14.1 (2013-10-15) diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index e34fdd7..72fbc66 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -228,7 +228,7 @@ class Twig_Lexer implements Twig_LexerInterface // operators if (preg_match($this->regexes['operator'], $this->code, $match, null, $this->cursor)) { - $this->pushToken(Twig_Token::OPERATOR_TYPE, $match[0]); + $this->pushToken(Twig_Token::OPERATOR_TYPE, preg_replace('/\s+/', ' ', $match[0])); $this->moveCursor($match[0]); } // names @@ -377,10 +377,15 @@ class Twig_Lexer implements Twig_LexerInterface // an operator that ends with a character must be followed by // a whitespace or a parenthesis if (ctype_alpha($operator[$length - 1])) { - $regex[] = preg_quote($operator, '/').'(?=[\s()])'; + $r = preg_quote($operator, '/').'(?=[\s()])'; } else { - $regex[] = preg_quote($operator, '/'); + $r = preg_quote($operator, '/'); } + + // an operator with a space can be any amount of whitespaces + $r = preg_replace('/\s+/', '\s+', $r); + + $regex[] = $r; } return '/'.implode('|', $regex).'/A'; diff --git a/test/Twig/Tests/Fixtures/expressions/starts_with.test b/test/Twig/Tests/Fixtures/expressions/starts_with.test index 84004ac..1ae4f86 100644 --- a/test/Twig/Tests/Fixtures/expressions/starts_with.test +++ b/test/Twig/Tests/Fixtures/expressions/starts_with.test @@ -4,9 +4,14 @@ Twig supports the "starts with" operator {{ 'foo' starts with 'f' ? 'OK' : 'KO' }} {{ not ('foo' starts with 'oo') ? 'OK' : 'KO' }} {{ not ('foo' starts with 'foowaytoolong') ? 'OK' : 'KO' }} +{{ 'foo' starts with 'f' ? 'OK' : 'KO' }} +{{ 'foo' starts +with 'f' ? 'OK' : 'KO' }} --DATA-- return array() --EXPECT-- OK OK OK +OK +OK