Selaa lähdekoodia

Cleaned up regex string preparation.

Justin Hileman 15 vuotta sitten
vanhempi
sitoutus
12096e0b8c
2 muutettua tiedostoa jossa 29 lisäystä ja 24 poistoa
  1. 8 24
      Mustache.php
  2. 21 0
      test/MustacheTest.php

+ 8 - 24
Mustache.php

@@ -164,8 +164,8 @@ class Mustache {
 	 * @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 @@ class Mustache {
 			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 @@ class Mustache {
 			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 @@ class Mustache {
 		$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 @@ class Mustache {
 	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);
-	}
 }
 
 

+ 21 - 0
test/MustacheTest.php

@@ -289,4 +289,25 @@ class MustacheTest extends PHPUnit_Framework_TestCase {
 		}
 		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 %>'));
+	}
 }