From ee39215cc29d8cfde6ef2e7a62d9e8a4740eed75 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 5 Jan 2012 15:18:26 +0100 Subject: [PATCH] fixed regression in string parsing (closes #580) --- lib/Twig/ExpressionParser.php | 12 +++++++----- test/Twig/Tests/ExpressionParserTest.php | 12 ++++++++++++ test/Twig/Tests/Fixtures/expressions/strings.test | 2 ++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index e9ed43a..0e6a8df 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -170,23 +170,25 @@ class Twig_ExpressionParser $stream = $this->parser->getStream(); $nodes = array(); - + // a string cannot be followed by another string in a single expression + $nextCanBeString = true; while (true) { - if ($stream->test(Twig_Token::STRING_TYPE)) { + if ($stream->test(Twig_Token::STRING_TYPE) && $nextCanBeString) { $token = $stream->next(); $nodes[] = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine()); - } else if ($stream->test(Twig_Token::INTERPOLATION_START_TYPE)) { + $nextCanBeString = false; + } elseif ($stream->test(Twig_Token::INTERPOLATION_START_TYPE)) { $stream->next(); $nodes[] = $this->parseExpression(); $stream->expect(Twig_Token::INTERPOLATION_END_TYPE); + $nextCanBeString = true; } else { break; } } $expr = array_shift($nodes); - - foreach($nodes as $node) { + foreach ($nodes as $node) { $expr = new Twig_Node_Expression_Binary_Concat($expr, $node, $node->getLine()); } diff --git a/test/Twig/Tests/ExpressionParserTest.php b/test/Twig/Tests/ExpressionParserTest.php index bb7b38a..b3f300f 100644 --- a/test/Twig/Tests/ExpressionParserTest.php +++ b/test/Twig/Tests/ExpressionParserTest.php @@ -145,6 +145,18 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase } /** + * @expectedException Twig_Error_Syntax + */ + public function testStringExpressionDoesNotConcatenateTwoConsecutiveStrings() + { + $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0)); + $stream = $env->tokenize('{{ "a" "b" }}', 'index'); + $parser = new Twig_Parser($env); + + $parser->parse($stream); + } + + /** * @dataProvider getTestsForString */ public function testStringExpression($template, $expected) diff --git a/test/Twig/Tests/Fixtures/expressions/strings.test b/test/Twig/Tests/Fixtures/expressions/strings.test index a2bff10..a911661 100644 --- a/test/Twig/Tests/Fixtures/expressions/strings.test +++ b/test/Twig/Tests/Fixtures/expressions/strings.test @@ -2,7 +2,9 @@ Twig supports string interpolation --TEMPLATE-- {{ "foo #{"foo #{bar} baz"} baz" }} +{{ "foo #{bar}#{bar} baz" }} --DATA-- return array('bar' => 'BAR'); --EXPECT-- foo foo BAR baz baz +foo BARBAR baz -- 1.7.2.5