From 724dc61beabf2e71e2a4d6cb2ff76d09b1392447 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 27 Oct 2013 10:56:11 +0100 Subject: [PATCH] allowed operators that contain whitespaces to have more than one whitespace --- CHANGELOG | 2 +- lib/Twig/Lexer.php | 11 ++++++++--- .../Tests/Fixtures/expressions/starts_with.test | 5 +++++ 3 files changed, 14 insertions(+), 4 deletions(-) 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 -- 1.7.2.5