Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Cleaned up regex string preparation.
  • Loading branch information
bobthecow committed Jun 11, 2010
1 parent a46d000 commit 12096e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
32 changes: 8 additions & 24 deletions Mustache.php
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 . "+/";

Expand Down Expand Up @@ -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 '';
}
Expand Down Expand Up @@ -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);
}
}


Expand Down
21 changes: 21 additions & 0 deletions test/MustacheTest.php
Expand Up @@ -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 %>'));
}
}

0 comments on commit 12096e0

Please sign in to comment.