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:
@@ -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;',
|
||||
)
|
||||
),
|
||||
|
||||
@@ -47,6 +47,7 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
|
||||
'foo' => function() { return 'foo'; },
|
||||
'bar' => 'BAR',
|
||||
),
|
||||
'escape' => 'strtoupper',
|
||||
'charset' => 'ISO-8859-1',
|
||||
));
|
||||
|
||||
@@ -54,6 +55,7 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
|
||||
$this->assertSame($partialsLoader, $mustache->getPartialsLoader());
|
||||
$this->assertEquals('{{ foo }}', $partialsLoader->load('foo'));
|
||||
$this->assertContains('__whot__', $mustache->getTemplateClassName('{{ foo }}'));
|
||||
$this->assertEquals('strtoupper', $mustache->getEscape());
|
||||
$this->assertEquals('ISO-8859-1', $mustache->getCharset());
|
||||
$this->assertTrue($mustache->hasHelper('foo'));
|
||||
$this->assertTrue($mustache->hasHelper('bar'));
|
||||
@@ -127,6 +129,21 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
|
||||
$this->assertContains("\nclass $className extends \Mustache\Template", file_get_contents($fileName));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @dataProvider getBadEscapers
|
||||
*/
|
||||
public function testNonCallableEscapeThrowsException($escape) {
|
||||
new Mustache(array('escape' => $escape));
|
||||
}
|
||||
|
||||
public function getBadEscapers() {
|
||||
return array(
|
||||
array('nothing'),
|
||||
array('foo', 'bar'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group functional
|
||||
* @expectedException \RuntimeException
|
||||
|
||||
Reference in New Issue
Block a user