Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify EscaperNodeVisitor code #4479

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading