From: Fabien Potencier Date: Sat, 28 Jan 2012 16:45:32 +0000 (+0100) Subject: fixed raw blocks when used with the whitespace trim option (closes #617) X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=310020ac9815d90da553c6981ed2dc777ef27c5a;p=web%2Fkonrad%2Ftwig.git fixed raw blocks when used with the whitespace trim option (closes #617) --- diff --git a/CHANGELOG b/CHANGELOG index 6ee60ab..6011029 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.6.0-DEV + * fixed raw blocks when used with the whitespace trim option * made a speed optimization to macro calls when imported via the "from" tag * fixed globals, parsers, visitors, filters, tests, and functions management in Twig_Environment when a new one or new extension is added * fixed the attribute function when passing arguments diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index 9810ab2..e7068bb 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -59,10 +59,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], '/').'\s*endraw\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*endraw\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\s*'.preg_quote($this->options['tag_block'][1], '/').'/As', + 'lex_block_raw' => '/\s*raw\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', @@ -288,9 +288,15 @@ class Twig_Lexer implements Twig_LexerInterface 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"')); } + $text = substr($this->code, $this->cursor, $match[0][1] - $this->cursor); - $this->pushToken(Twig_Token::TEXT_TYPE, $text); $this->moveCursor($text.$match[0][0]); + + if (false !== strpos($match[1][0], $this->options['whitespace_trim'])) { + $text = rtrim($text); + } + + $this->pushToken(Twig_Token::TEXT_TYPE, $text); } protected function lexComment() diff --git a/test/Twig/Tests/Fixtures/tags/raw/whitespace_control.test b/test/Twig/Tests/Fixtures/tags/raw/whitespace_control.test new file mode 100644 index 0000000..352bb18 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/raw/whitespace_control.test @@ -0,0 +1,56 @@ +--TEST-- +"raw" tag +--TEMPLATE-- +1*** + +{%- raw %} + {{ 'bla' }} +{% endraw %} + +1*** +2*** + +{%- raw -%} + {{ 'bla' }} +{% endraw %} + +2*** +3*** + +{%- raw -%} + {{ 'bla' }} +{% endraw -%} + +3*** +4*** + +{%- raw -%} + {{ 'bla' }} +{%- endraw %} + +4*** +5*** + +{%- raw -%} + {{ 'bla' }} +{%- endraw -%} + +5*** +--DATA-- +return array() +--EXPECT-- +1*** + {{ 'bla' }} + + +1*** +2***{{ 'bla' }} + + +2*** +3***{{ 'bla' }} +3*** +4***{{ 'bla' }} + +4*** +5***{{ 'bla' }}5***