Merge pull request #148 from pinterface/dev

Allow passing in the $type parameter of htmlspecialchars()
This commit is contained in:
Justin Hileman
2013-04-27 15:58:07 -07:00
3 changed files with 56 additions and 11 deletions
+6 -3
View File
@@ -21,6 +21,7 @@ class Mustache_Compiler
private $source; private $source;
private $indentNextLine; private $indentNextLine;
private $customEscape; private $customEscape;
private $entity_flags;
private $charset; private $charset;
private $strictCallables; private $strictCallables;
private $pragmas; private $pragmas;
@@ -32,18 +33,20 @@ class Mustache_Compiler
* @param string $tree Parse tree of Mustache tokens * @param string $tree Parse tree of Mustache tokens
* @param string $name Mustache Template class name * @param string $name Mustache Template class name
* @param bool $customEscape (default: false) * @param bool $customEscape (default: false)
* @param int $entity_flags (default: ENT_COMPAT)
* @param string $charset (default: 'UTF-8') * @param string $charset (default: 'UTF-8')
* @param bool $strictCallables (default: false) * @param bool $strictCallables (default: false)
* *
* @return string Generated PHP source code * @return string Generated PHP source code
*/ */
public function compile($source, array $tree, $name, $customEscape = false, $charset = 'UTF-8', $strictCallables = false) public function compile($source, array $tree, $name, $customEscape = false, $charset = 'UTF-8', $strictCallables = false, $entity_flags = ENT_COMPAT)
{ {
$this->pragmas = array(); $this->pragmas = array();
$this->sections = array(); $this->sections = array();
$this->source = $source; $this->source = $source;
$this->indentNextLine = true; $this->indentNextLine = true;
$this->customEscape = $customEscape; $this->customEscape = $customEscape;
$this->entity_flags = $entity_flags;
$this->charset = $charset; $this->charset = $charset;
$this->strictCallables = $strictCallables; $this->strictCallables = $strictCallables;
@@ -402,7 +405,7 @@ class Mustache_Compiler
return preg_replace("/\n( {8})?/", "\n".str_repeat(" ", $bonus * 4), $text); return preg_replace("/\n( {8})?/", "\n".str_repeat(" ", $bonus * 4), $text);
} }
const DEFAULT_ESCAPE = 'htmlspecialchars(%s, ENT_COMPAT, %s)'; const DEFAULT_ESCAPE = 'htmlspecialchars(%s, %s, %s)';
const CUSTOM_ESCAPE = 'call_user_func($this->mustache->getEscape(), %s)'; const CUSTOM_ESCAPE = 'call_user_func($this->mustache->getEscape(), %s)';
/** /**
@@ -417,7 +420,7 @@ class Mustache_Compiler
if ($this->customEscape) { if ($this->customEscape) {
return sprintf(self::CUSTOM_ESCAPE, $value); return sprintf(self::CUSTOM_ESCAPE, $value);
} else { } else {
return sprintf(self::DEFAULT_ESCAPE, $value, var_export($this->charset, true)); return sprintf(self::DEFAULT_ESCAPE, $value, var_export($this->entity_flags, true), var_export($this->charset, true));
} }
} }
+21 -2
View File
@@ -39,6 +39,7 @@ class Mustache_Engine
private $partialsLoader; private $partialsLoader;
private $helpers; private $helpers;
private $escape; private $escape;
private $entity_flags = ENT_COMPAT;
private $charset = 'UTF-8'; private $charset = 'UTF-8';
private $logger; private $logger;
private $strictCallables = false; private $strictCallables = false;
@@ -81,6 +82,9 @@ class Mustache_Engine
* return htmlspecialchars($buffer, ENT_COMPAT, 'UTF-8'); * return htmlspecialchars($buffer, ENT_COMPAT, 'UTF-8');
* }, * },
* *
* // Type argument for `htmlspecialchars`. Defaults to ENT_COMPAT. You may prefer ENT_QUOTES.
* 'entity_flags' => ENT_QUOTES,
*
* // Character set for `htmlspecialchars`. Defaults to 'UTF-8'. Use 'UTF-8'. * // Character set for `htmlspecialchars`. Defaults to 'UTF-8'. Use 'UTF-8'.
* 'charset' => 'ISO-8859-1', * 'charset' => 'ISO-8859-1',
* *
@@ -139,6 +143,10 @@ class Mustache_Engine
$this->escape = $options['escape']; $this->escape = $options['escape'];
} }
if (isset($options['entity_flags'])) {
$this->entity_flags = $options['entity_flags'];
}
if (isset($options['charset'])) { if (isset($options['charset'])) {
$this->charset = $options['charset']; $this->charset = $options['charset'];
} }
@@ -180,6 +188,16 @@ class Mustache_Engine
return $this->escape; return $this->escape;
} }
/**
* Get the current Mustache entitity type to escape.
*
* @return int
*/
public function getEntityFlags()
{
return $this->entity_flags;
}
/** /**
* Get the current Mustache character set. * Get the current Mustache character set.
* *
@@ -471,9 +489,10 @@ class Mustache_Engine
public function getTemplateClassName($source) public function getTemplateClassName($source)
{ {
return $this->templateClassPrefix . md5(sprintf( return $this->templateClassPrefix . md5(sprintf(
'version:%s,escape:%s,charset:%s,strict_callables:%s,source:%s', 'version:%s,escape:%s,entity_flags:%i,charset:%s,strict_callables:%s,source:%s',
self::VERSION, self::VERSION,
isset($this->escape) ? 'custom' : 'default', isset($this->escape) ? 'custom' : 'default',
$this->entity_flags,
$this->charset, $this->charset,
$this->strictCallables ? 'true' : 'false', $this->strictCallables ? 'true' : 'false',
$source $source
@@ -644,7 +663,7 @@ class Mustache_Engine
array('className' => $name) array('className' => $name)
); );
return $this->getCompiler()->compile($source, $tree, $name, isset($this->escape), $this->charset, $this->strictCallables); return $this->getCompiler()->compile($source, $tree, $name, isset($this->escape), $this->charset, $this->strictCallables, $this->entity_flags);
} }
/** /**
+29 -6
View File
@@ -18,11 +18,11 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase
/** /**
* @dataProvider getCompileValues * @dataProvider getCompileValues
*/ */
public function testCompile($source, array $tree, $name, $customEscaper, $charset, $expected) public function testCompile($source, array $tree, $name, $customEscaper, $entity_flags, $charset, $expected)
{ {
$compiler = new Mustache_Compiler; $compiler = new Mustache_Compiler;
$compiled = $compiler->compile($source, $tree, $name, $customEscaper, $charset); $compiled = $compiler->compile($source, $tree, $name, $customEscaper, $charset, false, $entity_flags);
foreach ($expected as $contains) { foreach ($expected as $contains) {
$this->assertContains($contains, $compiled); $this->assertContains($contains, $compiled);
} }
@@ -31,12 +31,12 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase
public function getCompileValues() public function getCompileValues()
{ {
return array( return array(
array('', array(), 'Banana', false, 'ISO-8859-1', array( array('', array(), 'Banana', false, ENT_COMPAT, 'ISO-8859-1', array(
"\nclass Banana extends Mustache_Template", "\nclass Banana extends Mustache_Template",
'return $buffer;', 'return $buffer;',
)), )),
array('', array($this->createTextToken('TEXT')), 'Monkey', false, 'UTF-8', array( array('', array($this->createTextToken('TEXT')), 'Monkey', false, ENT_COMPAT, 'UTF-8', array(
"\nclass Monkey extends Mustache_Template", "\nclass Monkey extends Mustache_Template",
'$buffer .= $indent . \'TEXT\';', '$buffer .= $indent . \'TEXT\';',
'return $buffer;', 'return $buffer;',
@@ -52,6 +52,7 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase
), ),
'Monkey', 'Monkey',
true, true,
ENT_COMPAT,
'ISO-8859-1', 'ISO-8859-1',
array( array(
"\nclass Monkey extends Mustache_Template", "\nclass Monkey extends Mustache_Template",
@@ -71,11 +72,32 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase
), ),
'Monkey', 'Monkey',
false, false,
ENT_COMPAT,
'ISO-8859-1', 'ISO-8859-1',
array( array(
"\nclass Monkey extends Mustache_Template", "\nclass Monkey extends Mustache_Template",
'$value = $this->resolveValue($context->find(\'name\'), $context, $indent);', '$value = $this->resolveValue($context->find(\'name\'), $context, $indent);',
'$buffer .= $indent . htmlspecialchars($value, ENT_COMPAT, \'ISO-8859-1\');', '$buffer .= $indent . htmlspecialchars($value, '.ENT_COMPAT.', \'ISO-8859-1\');',
'return $buffer;',
)
),
array(
'',
array(
array(
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => 'name',
)
),
'Monkey',
false,
ENT_QUOTES,
'ISO-8859-1',
array(
"\nclass Monkey extends Mustache_Template",
'$value = $this->resolveValue($context->find(\'name\'), $context, $indent);',
'$buffer .= $indent . htmlspecialchars($value, '.ENT_QUOTES.', \'ISO-8859-1\');',
'return $buffer;', 'return $buffer;',
) )
), ),
@@ -97,13 +119,14 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase
), ),
'Monkey', 'Monkey',
false, false,
ENT_COMPAT,
'UTF-8', 'UTF-8',
array( array(
"\nclass Monkey extends Mustache_Template", "\nclass Monkey extends Mustache_Template",
'$buffer .= $indent . \'foo\'', '$buffer .= $indent . \'foo\'',
'$buffer .= "\n"', '$buffer .= "\n"',
'$value = $this->resolveValue($context->find(\'name\'), $context, $indent);', '$value = $this->resolveValue($context->find(\'name\'), $context, $indent);',
'$buffer .= htmlspecialchars($value, ENT_COMPAT, \'UTF-8\');', '$buffer .= htmlspecialchars($value, '.ENT_COMPAT.', \'UTF-8\');',
'$value = $this->resolveValue($context->last(), $context, $indent);', '$value = $this->resolveValue($context->last(), $context, $indent);',
'$buffer .= \'\\\'bar\\\'\';', '$buffer .= \'\\\'bar\\\'\';',
'return $buffer;', 'return $buffer;',