From: Mark Story Date: Sat, 2 Apr 2011 14:08:51 +0000 (-0400) Subject: Applying changes suggested by nikic to simplify how end of comments are processed. X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=d0b40aff672c3905455c84a8ce6677c5a0c1e469;p=web%2Fkonrad%2Ftwig.git Applying changes suggested by nikic to simplify how end of comments are processed. --- diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index ba7e457..0664e8d 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -144,27 +144,15 @@ class Twig_Lexer implements Twig_LexerInterface switch ($token) { case $this->options['tag_comment'][0]: - $endPos = strpos($this->code, $this->options['tag_comment'][1], $this->cursor); - if (false === $endPos) { - throw new Twig_Error_Syntax('unclosed comment', $this->lineno, $this->filename); - } - $trimLen = strlen($this->options['whitespace_trim']); - if (strpos($this->code, $this->options['whitespace_trim'], $endPos - $trimLen) === $endPos - $trimLen) { - $endTag = preg_quote($this->options['tag_comment'][1], '/'); - preg_match('/' . $endTag . '(\h*)/', $this->code, $match, null, $this->cursor); - if (isset($match[1])) { - $endPos += strlen($match[1]); - } - } + $commentEndRegex = '/.*?(?:' . preg_quote($this->options['whitespace_trim'], '/') + . preg_quote($this->options['tag_comment'][1], '/') . '\h*|' + . preg_quote($this->options['tag_comment'][1], '/') . ')\n?/As'; - $this->moveCursor(substr($this->code, $this->cursor, $endPos - $this->cursor) . $this->options['tag_comment'][1]); - - // mimics the behavior of PHP by removing the newline that follows instructions if present - if ("\n" === substr($this->code, $this->cursor, 1)) { - ++$this->cursor; - ++$this->lineno; + if (!preg_match($commentEndRegex, $this->code, $match, null, $this->cursor)) { + throw new Twig_Error_Syntax('unclosed comment', $this->lineno, $this->filename); } + $this->moveCursor($match[0]); break; case $this->options['tag_block'][0]: