prepared regex should escape regex delimiter as well.

This commit is contained in:
Justin Hileman
2010-06-11 16:36:40 -04:00
parent e709be6d90
commit b300b31752
2 changed files with 10 additions and 14 deletions
+8 -8
View File
@@ -164,8 +164,8 @@ class Mustache {
* @return string * @return string
*/ */
protected function _renderSection($template) { protected function _renderSection($template) {
$otag = preg_quote($this->_otag); $otag = preg_quote($this->_otag, '/');
$ctag = preg_quote($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 = preg_quote($this->_otag); $otag = preg_quote($this->_otag, '/');
$ctag = preg_quote($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);
} }
@@ -327,8 +327,8 @@ class Mustache {
$otag_orig = $this->_otag; $otag_orig = $this->_otag;
$ctag_orig = $this->_ctag; $ctag_orig = $this->_ctag;
$otag = preg_quote($this->_otag); $otag = preg_quote($this->_otag, '/');
$ctag = preg_quote($this->_ctag); $ctag = preg_quote($this->_ctag, '/');
$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/"; $this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
@@ -466,8 +466,8 @@ class Mustache {
$this->_otag = $tags[0]; $this->_otag = $tags[0];
$this->_ctag = $tags[1]; $this->_ctag = $tags[1];
$otag = preg_quote($this->_otag); $otag = preg_quote($this->_otag, '/');
$ctag = preg_quote($this->_ctag); $ctag = preg_quote($this->_ctag, '/');
$this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/"; $this->_tagRegEx = '/' . $otag . "([#\^\/=!>\\{&])?(.+?)\\1?" . $ctag . "+/";
return ''; return '';
} }
+2 -6
View File
@@ -293,15 +293,11 @@ class MustacheTest extends PHPUnit_Framework_TestCase {
public function testCrazyDelimiters() { public function testCrazyDelimiters() {
$m = new Mustache(null, array('result' => 'success')); $m = new Mustache(null, array('result' => 'success'));
$this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]')); $this->assertEquals('success', $m->render('{{=[[ ]]=}}[[ result ]]'));
$m = new Mustache(null, array('result' => 'success'));
$this->assertEquals('success', $m->render('{{=(( ))=}}(( result ))')); $this->assertEquals('success', $m->render('{{=(( ))=}}(( result ))'));
$m = new Mustache(null, array('result' => 'success'));
$this->assertEquals('success', $m->render('{{={$ $}=}}{$ result $}')); $this->assertEquals('success', $m->render('{{={$ $}=}}{$ result $}'));
$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 ^^'));
$this->assertEquals('success', $m->render('{{=// \\\\}}// result \\\\'));
} }
public function testResetDelimiters() { public function testResetDelimiters() {