diff --git a/src/Compile/Base.php b/src/Compile/Base.php index 2d5c0c0ef..f028e893e 100644 --- a/src/Compile/Base.php +++ b/src/Compile/Base.php @@ -69,6 +69,17 @@ protected function formatParamsArray(array $_attr): array { return $_paramsArray; } + /** + * Returns attribute index for unnamed ("shorthand") attribute, or null if not allowed. + * + * @param string|int|null $key Index of the argument. Type should probably be narrowed to int + * + * @return string|int|null + */ + protected function getShorthandOrder($key) { + return $this->shorttag_order[$key] ?? null; + } + /** * This function checks if the attributes passed are valid * The attributes passed for the tag to compile are checked against the list of required and @@ -91,8 +102,8 @@ protected function getAttributes($compiler, $attributes) { if (isset($options[trim($mixed, '\'"')])) { $_indexed_attr[trim($mixed, '\'"')] = true; // shorthand attribute ? - } elseif (isset($this->shorttag_order[$key])) { - $_indexed_attr[$this->shorttag_order[$key]] = $mixed; + } elseif (!is_null($this->getShorthandOrder($key))) { + $_indexed_attr[$this->getShorthandOrder($key)] = $mixed; } else { // too many shorthands $compiler->trigger_template_error('too many shorthand attributes', null, true); diff --git a/src/Compile/Tag/BCPluginWrapper.php b/src/Compile/Tag/BCPluginWrapper.php index abd89f78f..e69634093 100644 --- a/src/Compile/Tag/BCPluginWrapper.php +++ b/src/Compile/Tag/BCPluginWrapper.php @@ -21,6 +21,20 @@ public function __construct($callback, bool $cacheable = true) { $this->cacheable = $cacheable; } + /** + * Returns attribute index for unnamed ("shorthand") attribute, or null if not allowed. + * + * For compiler plugins, we allow arbitrarily many unnamed attributes, + * and just make them accessible in the order they are set. + * + * @param string|int|null $key Index of the argument. Type should probably be narrowed to int + * + * @return string|int|null + */ + protected function getShorthandOrder($key) { + return $key; + } + /** * @inheritDoc */ @@ -28,4 +42,4 @@ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = { return call_user_func($this->callback, $this->getAttributes($compiler, $args), $compiler->getSmarty()); } -} \ No newline at end of file +} diff --git a/tests/UnitTests/SmartyMethodsTests/RegisterCompiler/RegisterCompilerFunctionTest.php b/tests/UnitTests/SmartyMethodsTests/RegisterCompiler/RegisterCompilerFunctionTest.php index ec2287adf..1ce743436 100644 --- a/tests/UnitTests/SmartyMethodsTests/RegisterCompiler/RegisterCompilerFunctionTest.php +++ b/tests/UnitTests/SmartyMethodsTests/RegisterCompiler/RegisterCompilerFunctionTest.php @@ -34,7 +34,7 @@ public function testRegisterCompilerFunction() { $this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', 'mycompilerfunction'); $this->assertEquals('mycompilerfunction', $this->smarty->getRegisteredPlugin('compiler', 'testcompilerfunction')[0]); - $this->assertEquals('hello world 1', $this->smarty->fetch('eval:{testcompilerfunction var=1}')); + $this->assertEquals('hello world 1 2', $this->smarty->fetch('eval:{testcompilerfunction 1 var=2}')); } /** @@ -99,7 +99,7 @@ public function testUnregisterCompilerFunctionOtherRegistered() function mycompilerfunction($params, $smarty) { - return ""; + return ""; } function mycompilerfunctionopen($params, $smarty)