From 6783d5812bc2a89ccaae3c1fe251501cd26faf53 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 25 Jul 2011 15:51:24 +0200 Subject: [PATCH] fixed a parsing problem when a large chunk of text is enclosed in a comment tag --- CHANGELOG | 6 ++++++ lib/Twig/Lexer.php | 23 ++++++++++++++--------- test/Twig/Tests/LexerTest.php | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 0cbf633..2e6bd7c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,9 @@ +* 1.1.2 + +Changes: + + * fixed a parsing problem when a large chunk of text is enclosed in a comment tag + * 1.1.1 (2011-07-17) Changes: diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index 485de15..5a2489a 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -145,15 +145,7 @@ class Twig_Lexer implements Twig_LexerInterface switch ($token) { case $this->options['tag_comment'][0]: - $commentEndRegex = '/.*?(?:'.preg_quote($this->options['whitespace_trim'], '/') - .preg_quote($this->options['tag_comment'][1], '/').'\s*|' - .preg_quote($this->options['tag_comment'][1], '/').')\n?/As'; - - if (!preg_match($commentEndRegex, $this->code, $match, null, $this->cursor)) { - throw new Twig_Error_Syntax('Unclosed comment', $this->lineno, $this->filename); - } - - $this->moveCursor($match[0]); + $this->lexComment(); break; case $this->options['tag_block'][0]: @@ -276,6 +268,19 @@ class Twig_Lexer implements Twig_LexerInterface $this->moveCursor($text.$match[0][0]); } + protected function lexComment() + { + $commentEndRegex = '/(?:'.preg_quote($this->options['whitespace_trim'], '/') + .preg_quote($this->options['tag_comment'][1], '/').'\s*|' + .preg_quote($this->options['tag_comment'][1], '/').')\n?/s'; + + if (!preg_match($commentEndRegex, $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor)) { + throw new Twig_Error_Syntax('Unclosed comment', $this->lineno, $this->filename); + } + + $this->moveCursor(substr($this->code, $this->cursor, $match[0][1] - $this->cursor).$match[0][0]); + } + protected function pushToken($type, $value = '') { // do not push empty text tokens diff --git a/test/Twig/Tests/LexerTest.php b/test/Twig/Tests/LexerTest.php index ac6472a..f3df09b 100644 --- a/test/Twig/Tests/LexerTest.php +++ b/test/Twig/Tests/LexerTest.php @@ -76,4 +76,44 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase // baz $this->assertSame(11, $stream->expect(Twig_Token::NAME_TYPE)->getLine()); } + + public function testLongComments() + { + $template = '{# '.str_repeat('*', 100000).' #}'; + + $lexer = new Twig_Lexer(new Twig_Environment()); + $lexer->tokenize($template); + + // should not throw an exception + } + + public function testLongRaw() + { + $template = '{% raw %}'.str_repeat('*', 100000).'{% endraw %}'; + + $lexer = new Twig_Lexer(new Twig_Environment()); + $stream = $lexer->tokenize($template); + + // should not throw an exception + } + + public function testLongBlock() + { + $template = '{{ '.str_repeat('*', 100000).' }}'; + + $lexer = new Twig_Lexer(new Twig_Environment()); + $stream = $lexer->tokenize($template); + + // should not throw an exception + } + + public function testLongBlock1() + { + $template = '{% '.str_repeat('*', 100000).' %}'; + + $lexer = new Twig_Lexer(new Twig_Environment()); + $stream = $lexer->tokenize($template); + + // should not throw an exception + } } -- 1.7.2.5