mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 11:27:00 +00:00
optimized macro calls when auto-escaping is on (closes #779)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
* 1.9.1 (2012-XX-XX)
|
||||
|
||||
* optimized macro calls when auto-escaping is on
|
||||
* fixed wrong parent class for Twig_Function_Node
|
||||
* made Twig_Loader_Chain more explicit about problems
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ class Twig_Node_Macro extends Twig_Node
|
||||
->write("throw \$e;\n")
|
||||
->outdent()
|
||||
->write("}\n\n")
|
||||
->write("return ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());\n")
|
||||
->write("return ob_get_clean();\n")
|
||||
->outdent()
|
||||
->write("}\n\n")
|
||||
;
|
||||
|
||||
@@ -22,6 +22,7 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface
|
||||
protected $safeAnalysis;
|
||||
protected $traverser;
|
||||
protected $defaultStrategy = false;
|
||||
protected $safeVars = array();
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -42,10 +43,13 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface
|
||||
if ($env->hasExtension('escaper') && $defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy($node->getAttribute('filename'))) {
|
||||
$this->defaultStrategy = $defaultStrategy;
|
||||
}
|
||||
$this->safeVars = array();
|
||||
} elseif ($node instanceof Twig_Node_AutoEscape) {
|
||||
$this->statusStack[] = $node->getAttribute('value');
|
||||
} elseif ($node instanceof Twig_Node_Block) {
|
||||
$this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env);
|
||||
} elseif ($node instanceof Twig_Node_Import) {
|
||||
$this->safeVars[] = $node->getNode('var')->getAttribute('name');
|
||||
}
|
||||
|
||||
return $node;
|
||||
@@ -63,6 +67,7 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface
|
||||
{
|
||||
if ($node instanceof Twig_Node_Module) {
|
||||
$this->defaultStrategy = false;
|
||||
$this->safeVars = array();
|
||||
} elseif ($node instanceof Twig_Node_Expression_Filter) {
|
||||
return $this->preEscapeFilterNode($node, $env);
|
||||
} elseif ($node instanceof Twig_Node_Print) {
|
||||
@@ -129,6 +134,9 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface
|
||||
if (null === $this->traverser) {
|
||||
$this->traverser = new Twig_NodeTraverser($env, array($this->safeAnalysis));
|
||||
}
|
||||
|
||||
$this->safeAnalysis->setSafeVars($this->safeVars);
|
||||
|
||||
$this->traverser->traverse($expression);
|
||||
$safe = $this->safeAnalysis->getSafe($expression);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,12 @@
|
||||
class Twig_NodeVisitor_SafeAnalysis implements Twig_NodeVisitorInterface
|
||||
{
|
||||
protected $data = array();
|
||||
protected $safeVars = array();
|
||||
|
||||
public function setSafeVars($safeVars)
|
||||
{
|
||||
$this->safeVars = $safeVars;
|
||||
}
|
||||
|
||||
public function getSafe(Twig_NodeInterface $node)
|
||||
{
|
||||
@@ -85,6 +91,14 @@ class Twig_NodeVisitor_SafeAnalysis implements Twig_NodeVisitorInterface
|
||||
} else {
|
||||
$this->setSafe($node, array());
|
||||
}
|
||||
} elseif ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name) {
|
||||
$name = $node->getNode('node')->getAttribute('name');
|
||||
// attributes on template instances are safe
|
||||
if ('_self' == $name || in_array($name, $this->safeVars)) {
|
||||
$this->setSafe($node, array('all'));
|
||||
} else {
|
||||
$this->setSafe($node, array());
|
||||
}
|
||||
} else {
|
||||
$this->setSafe($node, array());
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ public function getfoo(\$foo = null)
|
||||
throw \$e;
|
||||
}
|
||||
|
||||
return ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());
|
||||
return ob_get_clean();
|
||||
}
|
||||
EOF
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user