Skip to content

Commit

Permalink
feature #4479 Simplify EscaperNodeVisitor code (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Simplify EscaperNodeVisitor code

This PR simplifies how we escape the ternary operator, and simplifies the whole strategy.
Instead of replacing the `PrintNode`s, we instead "just" wrap the inner expressions.

For the ternary operator, the `InlinePrint` expression is useless and even weird as printing something in the middle of an expression looks very wrong. This is not done anymore and the node is deprecated.

Overall, this PR makes fewer changes to the Node tree which should make things use a bit less memory.

Commits
-------

02cec77 Simplify EscaperNodeVisitor code
  • Loading branch information
fabpot committed Nov 28, 2024
2 parents c384fb4 + 02cec77 commit ae82e8b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 3.16.0 (2024-XX-XX)

* Deprecate `InlinePrint`
* Fix having macro variables starting with an underscore
* Deprecate not passing a `Source` instance to `TokenStream`
* Deprecate returning `null` from `TwigFilter::getSafe()` and `TwigFunction::getSafe()`, return `[]` instead
Expand Down
2 changes: 2 additions & 0 deletions doc/deprecated.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ Nodes
made ready for ``yield``; the ``use_yield`` Environment option can be turned
on when all nodes use the ``#[\Twig\Attribute\YieldReady]`` attribute.

* The ``InlinePrint`` class is deprecated as of Twig 3.16 with no replacement.

Node Visitors
-------------

Expand Down
4 changes: 1 addition & 3 deletions src/Node/Expression/InlinePrint.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ final class InlinePrint extends AbstractExpression
*/
public function __construct(Node $node, int $lineno)
{
if (!$node instanceof AbstractExpression) {
trigger_deprecation('twig/twig', '3.15', 'Not passing a "%s" instance to the "node" argument of "%s" is deprecated ("%s" given).', AbstractExpression::class, static::class, get_class($node));
}
trigger_deprecation('twig/twig', '3.16', \sprintf('The "%s" class is deprecated with no replacement.', static::class));

parent::__construct(['node' => $node], [], $lineno);
}
Expand Down
44 changes: 11 additions & 33 deletions src/NodeVisitor/EscaperNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
use Twig\Node\AutoEscapeNode;
use Twig\Node\BlockNode;
use Twig\Node\BlockReferenceNode;
use Twig\Node\DoNode;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\ConditionalExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\InlinePrint;
use Twig\Node\ImportNode;
use Twig\Node\ModuleNode;
use Twig\Node\Node;
Expand Down Expand Up @@ -78,10 +76,12 @@ public function leaveNode(Node $node, Environment $env): ?Node
} elseif ($node instanceof PrintNode && false !== $type = $this->needEscaping()) {
$expression = $node->getNode('expr');
if ($expression instanceof ConditionalExpression) {
return new DoNode($this->unwrapConditional($expression, $env, $type), $expression->getTemplateLine());
$node->setNode('expr', $this->escapeConditional($expression, $env, $type));
} else {
$node->setNode('expr', $this->escapeExpression($expression, $env, $type));
}

return $this->escapePrintNode($node, $env, $type);
return $node;
}

if ($node instanceof AutoEscapeNode || $node instanceof BlockNode) {
Expand All @@ -93,22 +93,21 @@ public function leaveNode(Node $node, Environment $env): ?Node
return $node;
}

private function unwrapConditional(ConditionalExpression $expression, Environment $env, string $type): ConditionalExpression
private function escapeConditional(ConditionalExpression $expression, Environment $env, string $type): ConditionalExpression
{
// convert "echo a ? b : c" to "a ? echo b : echo c" recursively
/** @var AbstractExpression $expr2 */
$expr2 = $expression->getNode('expr2');
if ($expr2 instanceof ConditionalExpression) {
$expr2 = $this->unwrapConditional($expr2, $env, $type);
$expr2 = $this->escapeConditional($expr2, $env, $type);
} else {
$expr2 = $this->escapeInlinePrintNode(new InlinePrint($expr2, $expr2->getTemplateLine()), $env, $type);
$expr2 = $this->escapeExpression($expr2, $env, $type);
}
/** @var AbstractExpression $expr3 */
$expr3 = $expression->getNode('expr3');
if ($expr3 instanceof ConditionalExpression) {
$expr3 = $this->unwrapConditional($expr3, $env, $type);
$expr3 = $this->escapeConditional($expr3, $env, $type);
} else {
$expr3 = $this->escapeInlinePrintNode(new InlinePrint($expr3, $expr3->getTemplateLine()), $env, $type);
$expr3 = $this->escapeExpression($expr3, $env, $type);
}

/** @var AbstractExpression $expr1 */
Expand All @@ -117,30 +116,9 @@ private function unwrapConditional(ConditionalExpression $expression, Environmen
return new ConditionalExpression($expr1, $expr2, $expr3, $expression->getTemplateLine());
}

private function escapeInlinePrintNode(InlinePrint $node, Environment $env, string $type): AbstractExpression
private function escapeExpression(AbstractExpression $expression, Environment $env, string $type): AbstractExpression
{
/** @var AbstractExpression $expression */
$expression = $node->getNode('node');

if ($this->isSafeFor($type, $expression, $env)) {
return $node;
}

return new InlinePrint($this->getEscaperFilter($env, $type, $expression), $node->getTemplateLine());
}

private function escapePrintNode(PrintNode $node, Environment $env, string $type): Node
{
/** @var AbstractExpression $expression */
$expression = $node->getNode('expr');

if ($this->isSafeFor($type, $expression, $env)) {
return $node;
}

$class = \get_class($node);

return new $class($this->getEscaperFilter($env, $type, $expression), $node->getTemplateLine());
return $this->isSafeFor($type, $expression, $env) ? $expression : $this->getEscaperFilter($env, $type, $expression);
}

private function preEscapeFilterNode(FilterExpression $filter, Environment $env): FilterExpression
Expand Down

0 comments on commit ae82e8b

Please sign in to comment.