From 48f427ebd39607729ff7b271e438f642bb80edb6 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 9 May 2011 17:47:21 +0200 Subject: [PATCH] added a test for the raw tag --- lib/Twig/Lexer.php | 19 ++++++++++++------- test/Twig/Tests/Fixtures/tags/raw/basic.test | 10 ++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/tags/raw/basic.test diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index 184f8a8..e8fab6f 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -160,13 +160,7 @@ class Twig_Lexer implements Twig_LexerInterface // raw data? if (preg_match('/\s*raw\s*'.preg_quote($this->options['tag_block'][1], '/').'/As', $this->code, $match, null, $this->cursor)) { $this->moveCursor($match[0]); - if (!preg_match('/'.preg_quote($this->options['tag_block'][0], '/').'\s*endraw\s*'.preg_quote($this->options['tag_block'][1], '/').'/s', $this->code, $match, null, $this->cursor)) { - throw new Twig_Error_Syntax(sprintf('Unexpected end of file: Unclosed "block"')); - } - $pos = strpos($this->code, $match[0], $this->cursor); - $text = substr($this->code, $this->cursor, $pos - $this->cursor); - $this->pushToken(Twig_Token::TEXT_TYPE, $text); - $this->moveCursor($text.$match[0]); + $this->lexRawData(); $this->state = self::STATE_DATA; } else { $this->pushToken(Twig_Token::BLOCK_START_TYPE); @@ -267,6 +261,17 @@ class Twig_Lexer implements Twig_LexerInterface } } + protected function lexRawData() + { + if (!preg_match('/'.preg_quote($this->options['tag_block'][0], '/').'\s*endraw\s*'.preg_quote($this->options['tag_block'][1], '/').'/s', $this->code, $match, null, $this->cursor)) { + throw new Twig_Error_Syntax(sprintf('Unexpected end of file: Unclosed "block"')); + } + $pos = strpos($this->code, $match[0], $this->cursor); + $text = substr($this->code, $this->cursor, $pos - $this->cursor); + $this->pushToken(Twig_Token::TEXT_TYPE, $text); + $this->moveCursor($text.$match[0]); + } + protected function pushToken($type, $value = '') { // do not push empty text tokens diff --git a/test/Twig/Tests/Fixtures/tags/raw/basic.test b/test/Twig/Tests/Fixtures/tags/raw/basic.test new file mode 100644 index 0000000..0445e85 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/raw/basic.test @@ -0,0 +1,10 @@ +--TEST-- +"raw" tag +--TEMPLATE-- +{% raw %} +{{ foo }} +{% endraw %} +--DATA-- +return array() +--EXPECT-- +{{ foo }} -- 1.7.2.5