fixed a parsing problem when a large chunk of text is enclosed in a comment tag
authorFabien Potencier <fabien.potencier@gmail.com>
Mon, 25 Jul 2011 13:51:24 +0000 (15:51 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Mon, 25 Jul 2011 13:51:24 +0000 (15:51 +0200)
CHANGELOG
lib/Twig/Lexer.php
test/Twig/Tests/LexerTest.php

index 0cbf633..2e6bd7c 100644 (file)
--- 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:
index 485de15..5a2489a 100644 (file)
@@ -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
index ac6472a..f3df09b 100644 (file)
@@ -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
+    }
 }