mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 10:56:38 +00:00
Fix custom escapers when using multiple Twig environments
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# 2.14.12 (2022-XX-XX)
|
# 2.14.12 (2022-XX-XX)
|
||||||
|
|
||||||
* n/a
|
* Fix custom escapers when using multiple Twig environments
|
||||||
|
|
||||||
# 2.14.11 (2022-02-04)
|
# 2.14.11 (2022-02-04)
|
||||||
|
|
||||||
|
|||||||
@@ -392,20 +392,18 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char
|
|||||||
return rawurlencode($string);
|
return rawurlencode($string);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
static $escapers;
|
// check the ones set on CoreExtension for BC (to be removed in 3.0)
|
||||||
|
$legacyEscapers = $env->getExtension(CoreExtension::class)->getEscapers(false);
|
||||||
if (null === $escapers) {
|
if (array_key_exists($strategy, $legacyEscapers)) {
|
||||||
// merge the ones set on CoreExtension for BC (to be removed in 3.0)
|
return $legacyEscapers[$strategy]($env, $string, $charset);
|
||||||
$escapers = array_merge(
|
|
||||||
$env->getExtension(CoreExtension::class)->getEscapers(false),
|
|
||||||
$env->getExtension(EscaperExtension::class)->getEscapers()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($escapers[$strategy])) {
|
$escapers = $env->getExtension(EscaperExtension::class)->getEscapers();
|
||||||
|
if (array_key_exists($strategy, $escapers)) {
|
||||||
return $escapers[$strategy]($env, $string, $charset);
|
return $escapers[$strategy]($env, $string, $charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$escapers = array_merge($legacyEscapers, $escapers);
|
||||||
$validStrategies = implode(', ', array_merge(['html', 'js', 'url', 'css', 'html_attr'], array_keys($escapers)));
|
$validStrategies = implode(', ', array_merge(['html', 'js', 'url', 'css', 'html_attr'], array_keys($escapers)));
|
||||||
|
|
||||||
throw new RuntimeError(sprintf('Invalid escaping strategy "%s" (valid ones: %s).', $strategy, $validStrategies));
|
throw new RuntimeError(sprintf('Invalid escaping strategy "%s" (valid ones: %s).', $strategy, $validStrategies));
|
||||||
|
|||||||
@@ -355,6 +355,13 @@ class Twig_Tests_Extension_EscaperTest extends TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testUnknownCustomEscaper()
|
||||||
|
{
|
||||||
|
$this->expectException(RuntimeError::class);
|
||||||
|
|
||||||
|
twig_escape_filter(new Environment($this->createMock(LoaderInterface::class)), 'foo', 'bar');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider provideCustomEscaperCases
|
* @dataProvider provideCustomEscaperCases
|
||||||
*/
|
*/
|
||||||
@@ -375,11 +382,15 @@ class Twig_Tests_Extension_EscaperTest extends TestCase
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUnknownCustomEscaper()
|
public function testCustomEscapersOnMultipleEnvs()
|
||||||
{
|
{
|
||||||
$this->expectException(RuntimeError::class);
|
$env1 = new Environment($this->createMock(LoaderInterface::class));
|
||||||
|
$env1->getExtension(EscaperExtension::class)->setEscaper('foo', 'Twig\Tests\foo_escaper_for_test');
|
||||||
|
$env2 = new Environment($this->createMock(LoaderInterface::class));
|
||||||
|
$env2->getExtension(EscaperExtension::class)->setEscaper('foo', 'Twig\Tests\foo_escaper_for_test1');
|
||||||
|
|
||||||
twig_escape_filter(new Environment($this->createMock(LoaderInterface::class)), 'foo', 'bar');
|
$this->assertSame('fooUTF-8', twig_escape_filter($env1, 'foo', 'foo'));
|
||||||
|
$this->assertSame('fooUTF-81', twig_escape_filter($env2, 'foo', 'foo'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -410,6 +421,11 @@ function foo_escaper_for_test(Environment $twig, $string, $charset)
|
|||||||
return $string.$charset;
|
return $string.$charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function foo_escaper_for_test1(Environment $twig, $string, $charset)
|
||||||
|
{
|
||||||
|
return $string.$charset.'1';
|
||||||
|
}
|
||||||
|
|
||||||
interface Extension_SafeHtmlInterface
|
interface Extension_SafeHtmlInterface
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user