Cleaned up regex string preparation.
This commit is contained in:
+8
-24
@@ -164,8 +164,8 @@ class Mustache {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function _renderSection($template) {
|
protected function _renderSection($template) {
|
||||||
$otag = $this->_prepareRegEx($this->_otag);
|
$otag = preg_quote($this->_otag);
|
||||||
$ctag = $this->_prepareRegEx($this->_ctag);
|
$ctag = preg_quote($this->_ctag);
|
||||||
$regex = '/' . $otag . '(\\^|\\#)\\s*(.+?)\\s*' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\s*\\2\\s*' . $ctag . '\\s*/m';
|
$regex = '/' . $otag . '(\\^|\\#)\\s*(.+?)\\s*' . $ctag . '\\s*([\\s\\S]+?)' . $otag . '\\/\\s*\\2\\s*' . $ctag . '\\s*/m';
|
||||||
|
|
||||||
$matches = array();
|
$matches = array();
|
||||||
@@ -227,8 +227,8 @@ class Mustache {
|
|||||||
return $template;
|
return $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
$otag = $this->_prepareRegEx($this->_otag);
|
$otag = preg_quote($this->_otag);
|
||||||
$ctag = $this->_prepareRegEx($this->_ctag);
|
$ctag = preg_quote($this->_ctag);
|
||||||
$regex = '/' . $otag . '%\\s*([\\w_-]+)((?: [\\w]+=[\\w]+)*)\\s*' . $ctag . '\\n?/';
|
$regex = '/' . $otag . '%\\s*([\\w_-]+)((?: [\\w]+=[\\w]+)*)\\s*' . $ctag . '\\n?/';
|
||||||
return preg_replace_callback($regex, array($this, '_renderPragma'), $template);
|
return preg_replace_callback($regex, array($this, '_renderPragma'), $template);
|
||||||
}
|
}
|
||||||
@@ -324,8 +324,8 @@ class Mustache {
|
|||||||
return $template;
|
return $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
$otag = $this->_prepareRegEx($this->_otag);
|
$otag = preg_quote($this->_otag);
|
||||||
$ctag = $this->_prepareRegEx($this->_ctag);
|
$ctag = preg_quote($this->_ctag);
|
||||||
|
|
||||||
$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
|
$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
|
||||||
|
|
||||||
@@ -460,8 +460,8 @@ class Mustache {
|
|||||||
$this->_otag = $tags[0];
|
$this->_otag = $tags[0];
|
||||||
$this->_ctag = $tags[1];
|
$this->_ctag = $tags[1];
|
||||||
|
|
||||||
$otag = $this->_prepareRegEx($this->_otag);
|
$otag = preg_quote($this->_otag);
|
||||||
$ctag = $this->_prepareRegEx($this->_ctag);
|
$ctag = preg_quote($this->_ctag);
|
||||||
$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
|
$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@@ -593,22 +593,6 @@ class Mustache {
|
|||||||
protected function _varIsIterable($var) {
|
protected function _varIsIterable($var) {
|
||||||
return is_object($var) || (is_array($var) && !array_diff_key($var, array_keys(array_keys($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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -289,4 +289,25 @@ class MustacheTest extends PHPUnit_Framework_TestCase {
|
|||||||
}
|
}
|
||||||
return $ret;
|
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 %>'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user