mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-07 07:57:01 +00:00
Remove magic handling of the raw filter
This commit is contained in:
@@ -14,6 +14,7 @@ namespace Twig\Extension;
|
||||
use Twig\Environment;
|
||||
use Twig\FileExtensionEscapingStrategy;
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Node\Expression\Filter\RawFilter;
|
||||
use Twig\Node\Node;
|
||||
use Twig\NodeVisitor\EscaperNodeVisitor;
|
||||
use Twig\Runtime\EscaperRuntime;
|
||||
@@ -52,7 +53,7 @@ final class EscaperExtension extends AbstractExtension
|
||||
return [
|
||||
new TwigFilter('escape', [EscaperRuntime::class, 'escape'], ['is_safe_callback' => [self::class, 'escapeFilterIsSafe']]),
|
||||
new TwigFilter('e', [EscaperRuntime::class, 'escape'], ['is_safe_callback' => [self::class, 'escapeFilterIsSafe']]),
|
||||
new TwigFilter('raw', [self::class, 'raw'], ['is_safe' => ['all']]),
|
||||
new TwigFilter('raw', null, ['is_safe' => ['all'], 'node_class' => RawFilter::class]),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -180,18 +181,6 @@ final class EscaperExtension extends AbstractExtension
|
||||
$this->escaper->addSafeClass($class, $strategies);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks a variable as being safe.
|
||||
*
|
||||
* @param string $string A PHP variable
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public static function raw($string)
|
||||
{
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Twig\Node\Expression\Filter;
|
||||
|
||||
use Twig\Compiler;
|
||||
use Twig\Node\Expression\FilterExpression;
|
||||
|
||||
/**
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class RawFilter extends FilterExpression
|
||||
{
|
||||
public function compile(Compiler $compiler): void
|
||||
{
|
||||
$compiler->subcompile($this->getNode('node'));
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,6 @@ use Twig\Environment;
|
||||
use Twig\Node\BlockReferenceNode;
|
||||
use Twig\Node\Expression\BlockReferenceExpression;
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Node\Expression\FilterExpression;
|
||||
use Twig\Node\Expression\FunctionExpression;
|
||||
use Twig\Node\Expression\GetAttrExpression;
|
||||
use Twig\Node\Expression\NameExpression;
|
||||
@@ -59,6 +58,10 @@ final class OptimizerNodeVisitor implements NodeVisitorInterface
|
||||
throw new \InvalidArgumentException(\sprintf('Optimizer mode "%s" is not valid.', $optimizers));
|
||||
}
|
||||
|
||||
if (-1 !== $optimizers && self::OPTIMIZE_RAW_FILTER === (self::OPTIMIZE_RAW_FILTER & $optimizers)) {
|
||||
trigger_deprecation('twig/twig', '3.11', 'The "Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_RAW_FILTER" option is deprecated and does nothing.');
|
||||
}
|
||||
|
||||
$this->optimizers = $optimizers;
|
||||
}
|
||||
|
||||
@@ -77,10 +80,6 @@ final class OptimizerNodeVisitor implements NodeVisitorInterface
|
||||
$this->leaveOptimizeFor($node);
|
||||
}
|
||||
|
||||
if (self::OPTIMIZE_RAW_FILTER === (self::OPTIMIZE_RAW_FILTER & $this->optimizers)) {
|
||||
$node = $this->optimizeRawFilter($node);
|
||||
}
|
||||
|
||||
$node = $this->optimizePrintNode($node);
|
||||
|
||||
if (self::OPTIMIZE_TEXT_NODES === (self::OPTIMIZE_TEXT_NODES & $this->optimizers)) {
|
||||
@@ -153,18 +152,6 @@ final class OptimizerNodeVisitor implements NodeVisitorInterface
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes "raw" filters.
|
||||
*/
|
||||
private function optimizeRawFilter(Node $node): Node
|
||||
{
|
||||
if ($node instanceof FilterExpression && 'raw' == $node->getNode('filter')->getAttribute('value')) {
|
||||
return $node->getNode('node');
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimizes "for" tag by removing the "loop" variable creation whenever possible.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
--TEST--
|
||||
"raw" filter excludes a variable from being escaped
|
||||
--TEMPLATE--
|
||||
{{ br|raw }}
|
||||
--DATA--
|
||||
return ['br' => '<br>']
|
||||
--EXPECT--
|
||||
<br>
|
||||
@@ -29,7 +29,6 @@ class OptimizerTest extends TestCase
|
||||
$this->expectNotToPerformAssertions();
|
||||
new OptimizerNodeVisitor(
|
||||
OptimizerNodeVisitor::OPTIMIZE_FOR
|
||||
| OptimizerNodeVisitor::OPTIMIZE_RAW_FILTER
|
||||
| OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user