From 22cf53fdfa3dc34e7e612f8eebba77a16ef91ace Mon Sep 17 00:00:00 2001 From: Mark Story Date: Tue, 22 Mar 2011 22:35:54 -0400 Subject: [PATCH] Implementing {%- which trims non-newline whitespace preceding it. Refs --- lib/Twig/Lexer.php | 19 ++++++++++++++++--- test/Twig/Tests/Fixtures/tags/trim_block.test | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/tags/trim_block.test diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index 93647f6..6d66e87 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -47,6 +47,7 @@ class Twig_Lexer implements Twig_LexerInterface $this->options = array_merge(array( 'tag_comment' => array('{#', '#}'), 'tag_block' => array('{%', '%}'), + 'tag_trim_block' => array('{%-', '-%}'), 'tag_variable' => array('{{', '}}'), ), $options); } @@ -109,6 +110,7 @@ class Twig_Lexer implements Twig_LexerInterface protected function lexData() { + $trimBlock = false; $pos = $this->end; if (false !== ($tmpPos = strpos($this->code, $this->options['tag_comment'][0], $this->cursor)) && $tmpPos < $pos) { $pos = $tmpPos; @@ -118,6 +120,11 @@ class Twig_Lexer implements Twig_LexerInterface $pos = $tmpPos; $token = $this->options['tag_variable'][0]; } + if (false !== ($tmpPos = strpos($this->code, $this->options['tag_trim_block'][0], $this->cursor)) && $tmpPos < $pos) { + $pos = $tmpPos; + $token = $this->options['tag_trim_block'][0]; + $trimBlock = true; + } if (false !== ($tmpPos = strpos($this->code, $this->options['tag_block'][0], $this->cursor)) && $tmpPos < $pos) { $pos = $tmpPos; $token = $this->options['tag_block'][0]; @@ -131,9 +138,12 @@ class Twig_Lexer implements Twig_LexerInterface } // push the template text first - $text = substr($this->code, $this->cursor, $pos - $this->cursor); + $text = $textContent = substr($this->code, $this->cursor, $pos - $this->cursor); + if (true === $trimBlock) { + $text = rtrim($text, " \t"); + } $this->pushToken(Twig_Token::TEXT_TYPE, $text); - $this->moveCursor($text.$token); + $this->moveCursor($textContent . $token); switch ($token) { case $this->options['tag_comment'][0]: @@ -152,6 +162,7 @@ class Twig_Lexer implements Twig_LexerInterface break; case $this->options['tag_block'][0]: + case $this->options['tag_trim_block'][0]: // raw data? if (preg_match('/\s*raw\s*'.preg_quote($this->options['tag_block'][1], '/').'(.*?)'.preg_quote($this->options['tag_block'][0], '/').'\s*endraw\s*'.preg_quote($this->options['tag_block'][1], '/').'/As', $this->code, $match, null, $this->cursor)) { $this->pushToken(Twig_Token::TEXT_TYPE, $match[1]); @@ -172,7 +183,9 @@ class Twig_Lexer implements Twig_LexerInterface protected function lexBlock() { - if (empty($this->brackets) && preg_match('/\s*'.preg_quote($this->options['tag_block'][1], '/').'/A', $this->code, $match, null, $this->cursor)) { + $endTag = preg_quote($this->options['tag_block'][1], '/'); + if (empty($this->brackets) && preg_match('/\s*'. $endTag.'/A', $this->code, $match, null, $this->cursor)) { + $this->pushToken(Twig_Token::BLOCK_END_TYPE); $this->moveCursor($match[0]); $this->state = self::STATE_DATA; diff --git a/test/Twig/Tests/Fixtures/tags/trim_block.test b/test/Twig/Tests/Fixtures/tags/trim_block.test new file mode 100644 index 0000000..de26a2c --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/trim_block.test @@ -0,0 +1,14 @@ +--TEST-- +Whitespace trimming on tags. +--TEMPLATE-- + +--DATA-- +return array('string' => 'a value') +--EXPECT-- + -- 1.7.2.5