Skip to content

Commit

Permalink
bug twigphp#1248 allowed operators that contain whitespaces to have m…
Browse files Browse the repository at this point in the history
…ore than one whitespace (fabpot)

This PR was merged into the master branch.

Discussion
----------

allowed operators that contain whitespaces to have more than one whitespace

Commits
-------

724dc61 allowed operators that contain whitespaces to have more than one whitespace
  • Loading branch information
fabpot committed Oct 28, 2013
2 parents 5eb8373 + 724dc61 commit 5444c29
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion 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)

Expand Down
11 changes: 8 additions & 3 deletions lib/Twig/Lexer.php
Expand Up @@ -228,7 +228,7 @@ protected function lexExpression()

// 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
Expand Down Expand Up @@ -377,10 +377,15 @@ protected function getOperatorRegex()
// 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';
Expand Down
5 changes: 5 additions & 0 deletions test/Twig/Tests/Fixtures/expressions/starts_with.test
Expand Up @@ -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

0 comments on commit 5444c29

Please sign in to comment.