From 57b0a8b935ba9eed4eea6c01e51cd128f8e8805a Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 18 Apr 2019 09:50:46 +0200 Subject: [PATCH 1/2] fix major version constant --- src/Environment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Environment.php b/src/Environment.php index f6cf5c5ce..9e3fdf382 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -43,7 +43,7 @@ class Environment { const VERSION = '1.39.2-DEV'; const VERSION_ID = 13902; - const MAJOR_VERSION = 2; + const MAJOR_VERSION = 1; const MINOR_VERSION = 39; const RELEASE_VERSION = 2; const EXTRA_VERSION = 'DEV'; From 7b5851feeb62d7b91b033a1d5ec57d60b0927a74 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 21 Apr 2019 21:05:06 +0200 Subject: [PATCH 2/2] fixed Lexer when using custom options containing the # char --- CHANGELOG | 2 +- src/Lexer.php | 41 ++++++++++++++++++----------------- test/Twig/Tests/LexerTest.php | 26 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 683b16f12..dc177a7a0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ * 1.39.2 (2019-XX-XX) - * n/a + * fixed Lexer when using custom options containing the # char * 1.39.1 (2019-04-16) diff --git a/src/Lexer.php b/src/Lexer.php index 8cb63f388..81f3b5340 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -67,16 +67,17 @@ class Lexer implements \Twig_LexerInterface 'interpolation' => ['#{', '}'], ], $options); + // when PHP 7.3 is the min version, we will be able to remove the '#' part in preg_quote as it's part of the default $this->regexes = [ // }} 'lex_var' => '{ \s* (?:'. - preg_quote($this->options['whitespace_trim'].$this->options['tag_variable'][1]).'\s*'. // -}}\s* + preg_quote($this->options['whitespace_trim'].$this->options['tag_variable'][1], '#').'\s*'. // -}}\s* '|'. - preg_quote($this->options['whitespace_line_trim'].$this->options['tag_variable'][1]).'['.$this->options['whitespace_line_chars'].']*'. // ~}}[ \t\0\x0B]* + preg_quote($this->options['whitespace_line_trim'].$this->options['tag_variable'][1], '#').'['.$this->options['whitespace_line_chars'].']*'. // ~}}[ \t\0\x0B]* '|'. - preg_quote($this->options['tag_variable'][1]). // }} + preg_quote($this->options['tag_variable'][1], '#'). // }} ') }Ax', @@ -84,17 +85,17 @@ class Lexer implements \Twig_LexerInterface 'lex_block' => '{ \s* (?:'. - preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1]).'\s*\n?'. // -%}\s*\n? + preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '#').'\s*\n?'. // -%}\s*\n? '|'. - preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1]).'['.$this->options['whitespace_line_chars'].']*'. // ~%}[ \t\0\x0B]* + preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1], '#').'['.$this->options['whitespace_line_chars'].']*'. // ~%}[ \t\0\x0B]* '|'. - preg_quote($this->options['tag_block'][1]).'\n?'. // %}\n? + preg_quote($this->options['tag_block'][1], '#').'\n?'. // %}\n? ') }Ax', // {% endverbatim %} 'lex_raw_data' => '{'. - preg_quote($this->options['tag_block'][0]). // {% + preg_quote($this->options['tag_block'][0], '#'). // {% '('. $this->options['whitespace_trim']. // - '|'. @@ -103,11 +104,11 @@ class Lexer implements \Twig_LexerInterface '(?:end%s)'. // endraw or endverbatim '\s*'. '(?:'. - preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1]).'\s*'. // -%} + preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '#').'\s*'. // -%} '|'. - preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1]).'['.$this->options['whitespace_line_chars'].']*'. // ~%}[ \t\0\x0B]* + preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1], '#').'['.$this->options['whitespace_line_chars'].']*'. // ~%}[ \t\0\x0B]* '|'. - preg_quote($this->options['tag_block'][1]). // %} + preg_quote($this->options['tag_block'][1], '#'). // %} ') }sx', @@ -130,32 +131,32 @@ class Lexer implements \Twig_LexerInterface (raw|verbatim) \s* (?:'. - preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1]).'\s*'. // -%}\s* + preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '#').'\s*'. // -%}\s* '|'. - preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1]).'['.$this->options['whitespace_line_chars'].']*'. // ~%}[ \t\0\x0B]* + preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1], '#').'['.$this->options['whitespace_line_chars'].']*'. // ~%}[ \t\0\x0B]* '|'. - preg_quote($this->options['tag_block'][1]). // %} + preg_quote($this->options['tag_block'][1], '#'). // %} ') }Asx', - 'lex_block_line' => '{\s*line\s+(\d+)\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', // {{ or {% or {# 'lex_tokens_start' => '{ ('. - preg_quote($this->options['tag_variable'][0]). // {{ + preg_quote($this->options['tag_variable'][0], '#'). // {{ '|'. - preg_quote($this->options['tag_block'][0]). // {% + preg_quote($this->options['tag_block'][0], '#'). // {% '|'. preg_quote($this->options['tag_comment'][0], '#'). // {# ')('. - preg_quote($this->options['whitespace_trim']). // - + preg_quote($this->options['whitespace_trim'], '#'). // - '|'. - preg_quote($this->options['whitespace_line_trim']). // ~ + preg_quote($this->options['whitespace_line_trim'], '#'). // ~ ')? }sx', - 'interpolation_start' => '{'.preg_quote($this->options['interpolation'][0]).'\s*}A', - 'interpolation_end' => '{\s*'.preg_quote($this->options['interpolation'][1]).'}A', + 'interpolation_start' => '{'.preg_quote($this->options['interpolation'][0], '#').'\s*}A', + 'interpolation_end' => '{\s*'.preg_quote($this->options['interpolation'][1], '#').'}A', ]; } diff --git a/test/Twig/Tests/LexerTest.php b/test/Twig/Tests/LexerTest.php index 4055719d9..c1e62a667 100644 --- a/test/Twig/Tests/LexerTest.php +++ b/test/Twig/Tests/LexerTest.php @@ -350,4 +350,30 @@ bar $lexer = new Lexer(new Environment($this->getMockBuilder('\Twig\Loader\LoaderInterface')->getMock())); $lexer->tokenize(new Source($template, 'index')); } + + public function testOverridingSyntax() + { + $template = '[# comment #]{# variable #}/# if true #/true/# endif #/'; + $lexer = new Lexer(new Environment($this->getMockBuilder('\Twig\Loader\LoaderInterface')->getMock()), [ + 'tag_comment' => ['[#', '#]'], + 'tag_block' => ['/#', '#/'], + 'tag_variable' => ['{#', '#}'], + ]); + $stream = $lexer->tokenize(new Source($template, 'index')); + $stream->expect(Token::VAR_START_TYPE); + $stream->expect(Token::NAME_TYPE, 'variable'); + $stream->expect(Token::VAR_END_TYPE); + $stream->expect(Token::BLOCK_START_TYPE); + $stream->expect(Token::NAME_TYPE, 'if'); + $stream->expect(Token::NAME_TYPE, 'true'); + $stream->expect(Token::BLOCK_END_TYPE); + $stream->expect(Token::TEXT_TYPE, 'true'); + $stream->expect(Token::BLOCK_START_TYPE); + $stream->expect(Token::NAME_TYPE, 'endif'); + $stream->expect(Token::BLOCK_END_TYPE); + + // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above + // can be executed without throwing any exceptions + $this->addToAssertionCount(1); + } }