From 12096e0b8cad822edbec58eec1fdeae1cb116ee1 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Fri, 11 Jun 2010 16:24:09 -0400 Subject: [PATCH] Cleaned up regex string preparation. --- Mustache.php | 32 ++++++++------------------------ test/MustacheTest.php | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/Mustache.php b/Mustache.php index 202fdc97..6d798ef3 100644 --- a/Mustache.php +++ b/Mustache.php @@ -164,8 +164,8 @@ protected function _renderTemplate($template) { * @return string */ protected function _renderSection($template) { - $otag = $this->_prepareRegEx($this->_otag); - $ctag = $this->_prepareRegEx($this->_ctag); + $otag = preg_quote($this->_otag); + $ctag = preg_quote($this->_ctag); $regex = '/' . $otag . '(\\^|\\#)\\s*(.+?)\\s*' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\s*\\2\\s*' . $ctag . '\\s*/m'; $matches = array(); @@ -227,8 +227,8 @@ protected function _renderPragmas($template) { return $template; } - $otag = $this->_prepareRegEx($this->_otag); - $ctag = $this->_prepareRegEx($this->_ctag); + $otag = preg_quote($this->_otag); + $ctag = preg_quote($this->_ctag); $regex = '/' . $otag . '%\\s*([\\w_-]+)((?: [\\w]+=[\\w]+)*)\\s*' . $ctag . '\\n?/'; return preg_replace_callback($regex, array($this, '_renderPragma'), $template); } @@ -324,8 +324,8 @@ protected function _renderTags($template) { return $template; } - $otag = $this->_prepareRegEx($this->_otag); - $ctag = $this->_prepareRegEx($this->_ctag); + $otag = preg_quote($this->_otag); + $ctag = preg_quote($this->_ctag); $this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/"; @@ -460,8 +460,8 @@ protected function _changeDelimiter($tag_name) { $this->_otag = $tags[0]; $this->_ctag = $tags[1]; - $otag = $this->_prepareRegEx($this->_otag); - $ctag = $this->_prepareRegEx($this->_ctag); + $otag = preg_quote($this->_otag); + $ctag = preg_quote($this->_ctag); $this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/"; return ''; } @@ -593,22 +593,6 @@ protected function _getPartial($tag_name) { protected function _varIsIterable($var) { return is_object($var) || (is_array($var) && !array_diff_key($var, array_keys(array_keys($var)))); } - - /** - * Prepare a string to be used in a regular expression. - * - * @access protected - * @param string $str - * @return string - */ - protected function _prepareRegEx($str) { - $replace = array( - '\\' => '\\\\', '^' => '\^', '.' => '\.', '$' => '\$', '|' => '\|', '(' => '\(', - ')' => '\)', '[' => '\[', ']' => '\]', '*' => '\*', '+' => '\+', '?' => '\?', - '{' => '\{', '}' => '\}', ',' => '\,' - ); - return strtr($str, $replace); - } } diff --git a/test/MustacheTest.php b/test/MustacheTest.php index c787f37a..9ab97261 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -289,4 +289,25 @@ public function getExamples() { } return $ret; } + + public function testCrazyDelimiters() { + $m = new Mustache(null, array('result' => 'success')); + $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]')); + + $m = new Mustache(null, array('result' => 'success')); + $this->assertEquals('success', $m->render('{{=(( ))=}}(( result ))')); + + $m = new Mustache(null, array('result' => 'success')); + $this->assertEquals('success', $m->render('{{={$ $}=}}{$ result $}')); + + $m = new Mustache(null, array('result' => 'success')); + $this->assertEquals('success', $m->render('{{=<.. ..>=}}<.. result ..>')); + } + + public function testResetDelimiters() { + $m = new Mustache(null, array('result' => 'success')); + $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]')); + $this->assertEquals('success', $m->render('{{=<< >>=}}<< result >>')); + $this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>')); + } } \ No newline at end of file