From: Mark Story Date: Thu, 14 Apr 2011 02:10:57 +0000 (-0400) Subject: Making trim tags consume all whitespace, both horizontal and vertical. X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=99e3246c497b05a998c041c6247bacf0a9e72309;p=web%2Fkonrad%2Ftwig.git Making trim tags consume all whitespace, both horizontal and vertical. --- diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index d03d14b..16dda51 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -138,7 +138,7 @@ class Twig_Lexer implements Twig_LexerInterface // push the template text first $text = $textContent = substr($this->code, $this->cursor, $pos - $this->cursor); if (true === $trimBlock) { - $text = rtrim($text, " \t"); + $text = rtrim($text); } $this->pushToken(Twig_Token::TEXT_TYPE, $text); $this->moveCursor($textContent . $token . $append); @@ -146,7 +146,7 @@ class Twig_Lexer implements Twig_LexerInterface switch ($token) { case $this->options['tag_comment'][0]: $commentEndRegex = '/.*?(?:' . preg_quote($this->options['whitespace_trim'], '/') - . preg_quote($this->options['tag_comment'][1], '/') . '\h*|' + . preg_quote($this->options['tag_comment'][1], '/') . '\s*|' . preg_quote($this->options['tag_comment'][1], '/') . ')\n?/As'; if (!preg_match($commentEndRegex, $this->code, $match, null, $this->cursor)) { @@ -180,7 +180,7 @@ class Twig_Lexer implements Twig_LexerInterface $trimTag = preg_quote($this->options['whitespace_trim'] . $this->options['tag_block'][1], '/'); $endTag = preg_quote($this->options['tag_block'][1], '/'); - if (empty($this->brackets) && preg_match('/\s*(?:' . $trimTag . '\h*|\s*' . $endTag . ')\n?/A', $this->code, $match, null, $this->cursor)) { + if (empty($this->brackets) && preg_match('/\s*(?:' . $trimTag . '\s*|\s*' . $endTag . ')\n?/A', $this->code, $match, null, $this->cursor)) { $this->pushToken(Twig_Token::BLOCK_END_TYPE); $this->moveCursor($match[0]); $this->state = self::STATE_DATA; @@ -195,7 +195,7 @@ class Twig_Lexer implements Twig_LexerInterface $trimTag = preg_quote($this->options['whitespace_trim'] . $this->options['tag_variable'][1], '/'); $endTag = preg_quote($this->options['tag_variable'][1], '/'); - if (empty($this->brackets) && preg_match('/\s*' . $trimTag . '\h*|\s*' . $endTag . '/A', $this->code, $match, null, $this->cursor)) { + if (empty($this->brackets) && preg_match('/\s*' . $trimTag . '\s*|\s*' . $endTag . '/A', $this->code, $match, null, $this->cursor)) { $this->pushToken(Twig_Token::VAR_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 index 8e31521..1d2273f 100644 --- a/test/Twig/Tests/Fixtures/tags/trim_block.test +++ b/test/Twig/Tests/Fixtures/tags/trim_block.test @@ -1,53 +1,74 @@ --TEST-- Whitespace trimming on tags. --TEMPLATE-- - {#- Comments can trim leading space #} - - -{# Comments can trim trailing space -#} - - - - - {#- Comments can trim both -#} - -after {{ 5 * '{#-'|length }} {{ '{{-'|length * 5 + '{%-'|length }} + +Trim on control tag: +{% for i in range(1, 9) -%} + {{ i }} +{%- endfor %} + + +Trim on output tag: +{% for i in range(1, 9) %} + {{- i -}} +{% endfor %} + + +Trim comments: + +{#- Invisible -#} + +After the comment. + +Trim leading space: +{% if leading %} + + {{- leading }} +{% endif %} + +{%- if leading %} + {{- leading }} + +{%- endif %} + + +Trim trailing space: +{% if trailing -%} + {{ trailing -}} + +{% endif -%} + +Combined: + +{%- if both -%} + + +{%- endif -%} + +end --DATA-- -return array('leading' => 'leading space', 'trailing' => 'trailing tabs', 'mixed' => 'mixed tags', 'both' => 'both') +return array('leading' => 'leading space', 'trailing' => 'trailing space', 'both' => 'both') --EXPECT-- - - - - - - - -after 15 18 + +Trim on control tag: +123456789 + +Trim on output tag: +123456789 + +Trim comments:After the comment. + +Trim leading space: +leading space +leading space + +Trim trailing space: +trailing spaceCombined:end