Skip to content
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
7 changes: 7 additions & 0 deletions src/Node/Expression/Binary/AndBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@

use Twig\Compiler;
use Twig\Node\Expression\ReturnBoolInterface;
use Twig\Node\Expression\Test\TrueTest;
use Twig\Node\Node;

class AndBinary extends AbstractBinary implements ReturnBoolInterface
{
public function __construct(Node $left, Node $right, int $lineno)
{
parent::__construct(TrueTest::wrap($left), TrueTest::wrap($right), $lineno);
}

public function operator(Compiler $compiler): Compiler
{
return $compiler->raw('&&');
Expand Down
3 changes: 2 additions & 1 deletion src/Node/Expression/Binary/ElvisBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\OperatorEscapeInterface;
use Twig\Node\Expression\Test\TrueTest;
use Twig\Node\Node;

final class ElvisBinary extends AbstractBinary implements OperatorEscapeInterface
Expand All @@ -26,7 +27,7 @@ public function __construct(Node $left, Node $right, int $lineno)
{
parent::__construct($left, $right, $lineno);

$this->setNode('test', clone $left);
$this->setNode('test', TrueTest::wrap(clone $left));
$left->setAttribute('always_defined', true);
}

Expand Down
7 changes: 7 additions & 0 deletions src/Node/Expression/Binary/OrBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@

use Twig\Compiler;
use Twig\Node\Expression\ReturnBoolInterface;
use Twig\Node\Expression\Test\TrueTest;
use Twig\Node\Node;

class OrBinary extends AbstractBinary implements ReturnBoolInterface
{
public function __construct(Node $left, Node $right, int $lineno)
{
parent::__construct(TrueTest::wrap($left), TrueTest::wrap($right), $lineno);
}

public function operator(Compiler $compiler): Compiler
{
return $compiler->raw('||');
Expand Down
7 changes: 7 additions & 0 deletions src/Node/Expression/Binary/XorBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@

use Twig\Compiler;
use Twig\Node\Expression\ReturnBoolInterface;
use Twig\Node\Expression\Test\TrueTest;
use Twig\Node\Node;

class XorBinary extends AbstractBinary implements ReturnBoolInterface
{
public function __construct(Node $left, Node $right, int $lineno)
{
parent::__construct(TrueTest::wrap($left), TrueTest::wrap($right), $lineno);
}

public function operator(Compiler $compiler): Compiler
{
return $compiler->raw('xor');
Expand Down
8 changes: 1 addition & 7 deletions src/Node/Expression/Ternary/ConditionalTernary.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\OperatorEscapeInterface;
use Twig\Node\Expression\ReturnPrimitiveTypeInterface;
use Twig\Node\Expression\Test\TrueTest;
use Twig\TwigTest;

final class ConditionalTernary extends AbstractExpression implements OperatorEscapeInterface
{
public function __construct(AbstractExpression $test, AbstractExpression $left, AbstractExpression $right, int $lineno)
{
if (!$test instanceof ReturnPrimitiveTypeInterface) {
$test = new TrueTest($test, new TwigTest('true', null, ['always_allowed_in_sandbox' => true]), null, $test->getTemplateLine());
}

parent::__construct(['test' => $test, 'left' => $left, 'right' => $right], [], $lineno);
parent::__construct(['test' => TrueTest::wrap($test), 'left' => $left, 'right' => $right], [], $lineno);
}

public function compile(Compiler $compiler): void
Expand Down
12 changes: 12 additions & 0 deletions src/Node/Expression/Test/TrueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
namespace Twig\Node\Expression\Test;

use Twig\Compiler;
use Twig\Node\Expression\ReturnPrimitiveTypeInterface;
use Twig\Node\Expression\TestExpression;
use Twig\Node\Node;
use Twig\TwigTest;

/**
* Checks that an expression is true.
Expand All @@ -23,6 +26,15 @@
*/
class TrueTest extends TestExpression
{
public static function wrap(Node $node): Node
{
if ($node instanceof ReturnPrimitiveTypeInterface) {
return $node;
}

return new self($node, new TwigTest('true', null, ['always_allowed_in_sandbox' => true]), null, $node->getTemplateLine());
}

public function compile(Compiler $compiler): void
{
$compiler
Expand Down
10 changes: 9 additions & 1 deletion src/Node/Expression/Unary/NotUnary.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@
namespace Twig\Node\Expression\Unary;

use Twig\Compiler;
use Twig\Node\Expression\ReturnBoolInterface;
use Twig\Node\Expression\Test\TrueTest;
use Twig\Node\Node;

class NotUnary extends AbstractUnary
class NotUnary extends AbstractUnary implements ReturnBoolInterface
{
public function __construct(Node $node, int $lineno)
{
parent::__construct(TrueTest::wrap($node), $lineno);
}

public function operator(Compiler $compiler): Compiler
{
return $compiler->raw('!');
Expand Down
7 changes: 1 addition & 6 deletions src/Node/IfNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\Expression\ReturnPrimitiveTypeInterface;
use Twig\Node\Expression\Test\TrueTest;
use Twig\TwigTest;

/**
* Represents an if node.
Expand All @@ -29,10 +27,7 @@ class IfNode extends Node
public function __construct(Node $tests, ?Node $else, int $lineno)
{
for ($i = 0, $count = \count($tests); $i < $count; $i += 2) {
$test = $tests->getNode((string) $i);
if (!$test instanceof ReturnPrimitiveTypeInterface) {
$tests->setNode($i, new TrueTest($test, new TwigTest('true', null, ['always_allowed_in_sandbox' => true]), null, $test->getTemplateLine()));
}
$tests->setNode($i, TrueTest::wrap($tests->getNode((string) $i)));
}
$nodes = ['tests' => $tests];
if (null !== $else) {
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixtures/regression/markup_test.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ Twig outputs 0 nodes correctly
{% if spaces|trim %}KO{% else %}ok{% endif %}

{% set bar %} {% endset %}{{ bar|trim ? 'KO' : 'ok' }}
{% if bar|trim and bar|trim %}KO{% else %}ok{% endif %}{{- "\n" -}}
{% if bar|trim or empty|trim %}KO{% else %}ok{% endif %}{{- "\n" -}}
{% if bar|trim xor empty|trim %}KO{% else %}ok{% endif %}{{- "\n" -}}
{% if not bar|trim %}ok{% else %}KO{% endif %}{{- "\n" -}}
{{ bar|trim ?: 'ok' }}
{{ (bar|trim and bar|trim) ? 'KO' : 'ok' }}
{{ (bar|trim and bar|trim) ?: 'ok' }}
{{ (bar|trim and bar|trim) ?: (bar|trim ?: 'ok') }}
--DATA--
return ['spaces' => new Twig\Markup(' ', 'UTF-8'), 'empty' => new Twig\Markup('', 'UTF-8')]
--EXPECT--
Expand All @@ -16,3 +24,11 @@ ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
Loading