Fixed setfilter

This commit is contained in:
Simon Wisselink
2023-01-03 22:03:43 +01:00
parent 3fccb54093
commit 16c7fe1d4f
4 changed files with 16 additions and 7 deletions

View File

@@ -66,7 +66,6 @@ class PrintExpressionCompiler extends Base {
if (!$_attr['nofilter']) {
// default modifier
if ($compiler->smarty->getDefaultModifiers()) {
$modifierlist = [];
foreach ($compiler->smarty->getDefaultModifiers() as $key => $single_default_modifier) {
preg_match_all(

View File

@@ -20,8 +20,18 @@ class Setfilter extends Base {
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
$compiler->variable_filter_stack[] = $compiler->getSmarty()->getAutoModifiers();
$compiler->getSmarty()->setAutoModifiers((array) $parameter['modifier_list']);
$compiler->variable_filter_stack[] = $compiler->getSmarty()->getDefaultModifiers();
// The modifier_list is passed as an array of array's. The inner arrays have the modifier at index 0,
// and, possibly, parameters at subsequent indexes, e.g. [ ['escape','"mail"'] ]
// We will collapse them so the syntax is OK for ::setDefaultModifiers() as follows: [ 'escape:"mail"' ]
$newList = [];
foreach($parameter['modifier_list'] as $modifier) {
$newList[] = implode(':', $modifier);
}
$compiler->getSmarty()->setDefaultModifiers($newList);
// this tag does not return compiled code
$compiler->has_code = false;
return true;

View File

@@ -31,7 +31,7 @@ class SetfilterClose extends Base {
$this->getAttributes($compiler, $args);
// reset variable filter to previous state
$compiler->getSmarty()->setAutoModifiers(
$compiler->getSmarty()->setDefaultModifiers(
count($compiler->variable_filter_stack) ? array_pop($compiler->variable_filter_stack) : []
);

View File

@@ -33,14 +33,14 @@ class CompileSetfilterTest extends PHPUnit_Smarty
public function testNestedSetfilter()
{
$this->smarty->setCaching(1);
$tpl = $this->smarty->createTemplate('string:{$foo}{setfilter htmlspecialchars} {$foo}{$foo nocache}{setfilter escape:"mail"} {$foo}{$foo nocache}{/setfilter} {$foo}{/setfilter} {$foo}');
$tpl = $this->smarty->createTemplate('string:{$foo}{setfilter escape} {$foo}{$foo nocache}{setfilter escape:"mail"} {$foo}{$foo nocache}{/setfilter} {$foo}{/setfilter} {$foo}');
$tpl->assign('foo', '<a@b.c>');
$this->assertEquals("<a@b.c> &lt;a@b.c&gt;&lt;a@b.c&gt; <a [AT] b [DOT] c><a [AT] b [DOT] c> &lt;a@b.c&gt; <a@b.c>", $this->smarty->fetch($tpl));
}
public function testNestedSetfilter1()
{
$this->smarty->setCaching(1);
$tpl = $this->smarty->createTemplate('string:{$foo}{setfilter htmlspecialchars} {$foo}{$foo nocache}{setfilter escape:"mail"} {$foo}{$foo nocache}{/setfilter} {$foo}{/setfilter} {$foo}');
$tpl = $this->smarty->createTemplate('string:{$foo}{setfilter escape} {$foo}{$foo nocache}{setfilter escape:"mail"} {$foo}{$foo nocache}{/setfilter} {$foo}{/setfilter} {$foo}');
$tpl->assign('foo', '<e@f.d>');
$this->assertEquals("<a@b.c> &lt;a@b.c&gt;&lt;e@f.d&gt; <a [AT] b [DOT] c><e [AT] f [DOT] d> &lt;a@b.c&gt; <a@b.c>", $this->smarty->fetch($tpl));
}
@@ -50,7 +50,7 @@ class CompileSetfilterTest extends PHPUnit_Smarty
*/
public function testNestedSetfilter2()
{
$tpl = $this->smarty->createTemplate('string:{$foo}{setfilter htmlspecialchars} {$foo}{setfilter escape:"mail"} {$foo}{/setfilter} {$foo}{/setfilter} {$foo}');
$tpl = $this->smarty->createTemplate('string:{$foo}{setfilter escape} {$foo}{setfilter escape:"mail"} {$foo}{/setfilter} {$foo}{/setfilter} {$foo}');
$tpl->assign('foo', '<a@b.c>');
$this->assertEquals("<a@b.c> &lt;a@b.c&gt; <a [AT] b [DOT] c> &lt;a@b.c&gt; <a@b.c>", $this->smarty->fetch($tpl));
}