forbid mixed usage of raw/verbatim
authorFabien Potencier <fabien.potencier@gmail.com>
Tue, 8 Jan 2013 12:28:45 +0000 (13:28 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Tue, 8 Jan 2013 12:28:45 +0000 (13:28 +0100)
lib/Twig/Lexer.php
test/Twig/Tests/Fixtures/tags/raw/mixed_usage_with_raw.test [new file with mode: 0644]
test/Twig/Tests/Fixtures/tags/verbatim/mixed_usage_with_raw.test [new file with mode: 0644]

index 86d9639..3be7215 100644 (file)
@@ -61,10 +61,10 @@ class Twig_Lexer implements Twig_LexerInterface
         $this->regexes = array(
             'lex_var'             => '/\s*'.preg_quote($this->options['whitespace_trim'].$this->options['tag_variable'][1], '/').'\s*|\s*'.preg_quote($this->options['tag_variable'][1], '/').'/A',
             'lex_block'           => '/\s*(?:'.preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '/').'\s*|\s*'.preg_quote($this->options['tag_block'][1], '/').')\n?/A',
-            'lex_raw_data'        => '/('.preg_quote($this->options['tag_block'][0].$this->options['whitespace_trim'], '/').'|'.preg_quote($this->options['tag_block'][0], '/').')\s*(?:endraw|endverbatim)\s*(?:'.preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '/').'\s*|\s*'.preg_quote($this->options['tag_block'][1], '/').')/s',
+            'lex_raw_data'        => '/('.preg_quote($this->options['tag_block'][0].$this->options['whitespace_trim'], '/').'|'.preg_quote($this->options['tag_block'][0], '/').')\s*(?:end%s)\s*(?:'.preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '/').'\s*|\s*'.preg_quote($this->options['tag_block'][1], '/').')/s',
             'operator'            => $this->getOperatorRegex(),
             'lex_comment'         => '/(?:'.preg_quote($this->options['whitespace_trim'], '/').preg_quote($this->options['tag_comment'][1], '/').'\s*|'.preg_quote($this->options['tag_comment'][1], '/').')\n?/s',
-            'lex_block_raw'       => '/\s*(?:raw|verbatim)\s*(?:'.preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '/').'\s*|\s*'.preg_quote($this->options['tag_block'][1], '/').')/As',
+            'lex_block_raw'       => '/\s*(raw|verbatim)\s*(?:'.preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '/').'\s*|\s*'.preg_quote($this->options['tag_block'][1], '/').')/As',
             'lex_block_line'      => '/\s*line\s+(\d+)\s*'.preg_quote($this->options['tag_block'][1], '/').'/As',
             'lex_tokens_start'    => '/('.preg_quote($this->options['tag_variable'][0], '/').'|'.preg_quote($this->options['tag_block'][0], '/').'|'.preg_quote($this->options['tag_comment'][0], '/').')('.preg_quote($this->options['whitespace_trim'], '/').')?/s',
             'interpolation_start' => '/'.preg_quote($this->options['interpolation'][0], '/').'\s*/A',
@@ -178,7 +178,7 @@ class Twig_Lexer implements Twig_LexerInterface
                 // raw data?
                 if (preg_match($this->regexes['lex_block_raw'], $this->code, $match, null, $this->cursor)) {
                     $this->moveCursor($match[0]);
-                    $this->lexRawData();
+                    $this->lexRawData($match[1]);
                 // {% line \d+ %}
                 } elseif (preg_match($this->regexes['lex_block_line'], $this->code, $match, null, $this->cursor)) {
                     $this->moveCursor($match[0]);
@@ -286,10 +286,10 @@ class Twig_Lexer implements Twig_LexerInterface
         }
     }
 
-    protected function lexRawData()
+    protected function lexRawData($tag)
     {
-        if (!preg_match($this->regexes['lex_raw_data'], $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor)) {
-            throw new Twig_Error_Syntax(sprintf('Unexpected end of file: Unclosed "block"'), $this->lineno, $this->filename);
+        if (!preg_match(str_replace('%s', $tag, $this->regexes['lex_raw_data']), $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor)) {
+            throw new Twig_Error_Syntax(sprintf('Unexpected end of file: Unclosed "%s" block', $tag), $this->lineno, $this->filename);
         }
 
         $text = substr($this->code, $this->cursor, $match[0][1] - $this->cursor);
diff --git a/test/Twig/Tests/Fixtures/tags/raw/mixed_usage_with_raw.test b/test/Twig/Tests/Fixtures/tags/raw/mixed_usage_with_raw.test
new file mode 100644 (file)
index 0000000..2fd9fb2
--- /dev/null
@@ -0,0 +1,10 @@
+--TEST--
+"raw" tag
+--TEMPLATE--
+{% raw %}
+{{ foo }}
+{% endverbatim %}
+--DATA--
+return array()
+--EXCEPTION--
+Twig_Error_Syntax: Unexpected end of file: Unclosed "raw" block in "index.twig" at line 2
diff --git a/test/Twig/Tests/Fixtures/tags/verbatim/mixed_usage_with_raw.test b/test/Twig/Tests/Fixtures/tags/verbatim/mixed_usage_with_raw.test
new file mode 100644 (file)
index 0000000..941dddc
--- /dev/null
@@ -0,0 +1,10 @@
+--TEST--
+"verbatim" tag
+--TEMPLATE--
+{% verbatim %}
+{{ foo }}
+{% endraw %}
+--DATA--
+return array()
+--EXCEPTION--
+Twig_Error_Syntax: Unexpected end of file: Unclosed "verbatim" block in "index.twig" at line 2