Add custom variable escapers.

cf. #86

 * Add `escape` Mustache constructor option. Expects something callable.
 * Update Compiler to either inline `htmlspecialchars` (old way) or a call to the custom escaper callback.
 * Update Compiler to statically compile charset.
 * Update template classname to account for charset and custom compilers.
This commit is contained in:
Justin Hileman
2012-03-14 20:07:16 -07:00
parent 2bd3804b1c
commit 23a4bb04f2
4 changed files with 94 additions and 14 deletions
+17 -5
View File
@@ -22,10 +22,10 @@ class CompilerTest extends \PHPUnit_Framework_TestCase {
/**
* @dataProvider getCompileValues
*/
public function testCompile($source, array $tree, $name, $expected) {
public function testCompile($source, array $tree, $name, $customEscaper, $charset, $expected) {
$compiler = new Compiler;
$compiled = $compiler->compile($source, $tree, $name);
$compiled = $compiler->compile($source, $tree, $name, $customEscaper, $charset);
foreach ($expected as $contains) {
$this->assertContains($contains, $compiled);
}
@@ -33,14 +33,23 @@ class CompilerTest extends \PHPUnit_Framework_TestCase {
public function getCompileValues() {
return array(
array('', array(), 'Banana', array(
array('', array(), 'Banana', false, 'ISO-8859-1', array(
"\nclass Banana extends \Mustache\Template",
'return htmlspecialchars($buffer, ENT_COMPAT, \'ISO-8859-1\');',
'return $buffer;',
)),
array('', array('TEXT'), 'Monkey', array(
array('', array('TEXT'), 'Monkey', false, 'UTF-8', array(
"\nclass Monkey extends \Mustache\Template",
'return htmlspecialchars($buffer, ENT_COMPAT, \'UTF-8\');',
'$buffer .= $indent . \'TEXT\';',
'return $buffer;',
)),
array('', array('TEXT'), 'Monkey', true, 'ISO-8859-1', array(
"\nclass Monkey extends \Mustache\Template",
'$buffer .= $indent . \'TEXT\';',
'return call_user_func($this->mustache->getEscape(), $buffer);',
'return $buffer;',
)),
@@ -60,14 +69,17 @@ class CompilerTest extends \PHPUnit_Framework_TestCase {
"'bar'",
),
'Monkey',
false,
'UTF-8',
array(
"\nclass Monkey extends \Mustache\Template",
'$buffer .= $indent . \'foo\'',
'$buffer .= "\n"',
'$value = $context->find(\'name\');',
'$buffer .= htmlspecialchars($value, ENT_COMPAT, $this->mustache->getCharset());',
'$buffer .= htmlspecialchars($value, ENT_COMPAT, \'UTF-8\');',
'$value = $context->last();',
'$buffer .= \'\\\'bar\\\'\';',
'return htmlspecialchars($buffer, ENT_COMPAT, \'UTF-8\');',
'return $buffer;',
)
),