From a3cbdc46fbee148f2e0a7c2bf8f0840e5ef2dce0 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Fri, 4 Aug 2023 22:40:19 +0200 Subject: [PATCH] Fix strip_tags modifier for falsy input. (#893) Fixes #890 --- CHANGELOG.md | 3 ++ libs/plugins/modifiercompiler.strip_tags.php | 2 +- .../PluginModifierStripTagsTest.php | 46 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierStripTagsTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 10c66c77..20c5b00e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- `|strip_tags` does not work if the input is 0 [#890](https://github.com/smarty-php/smarty/issues/890) + ## [4.3.2] - 2023-07-19 ### Fixed diff --git a/libs/plugins/modifiercompiler.strip_tags.php b/libs/plugins/modifiercompiler.strip_tags.php index bd866a61..fd6cc725 100644 --- a/libs/plugins/modifiercompiler.strip_tags.php +++ b/libs/plugins/modifiercompiler.strip_tags.php @@ -21,7 +21,7 @@ function smarty_modifiercompiler_strip_tags($params) { if (!isset($params[ 1 ]) || $params[ 1 ] === true || trim($params[ 1 ], '"') === 'true') { - return "preg_replace('!<[^>]*?>!', ' ', {$params[0]} ?: '')"; + return "preg_replace('!<[^>]*?>!', ' ', (string) {$params[0]})"; } else { return 'strip_tags((string) ' . $params[ 0 ] . ')'; } diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierStripTagsTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierStripTagsTest.php new file mode 100644 index 00000000..c0860a27 --- /dev/null +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierStripTagsTest.php @@ -0,0 +1,46 @@ +setUpSmarty(__DIR__); + } + + public function testDefault() { + $tpl = $this->smarty->createTemplate('string:{$x|strip_tags}'); + $tpl->assign('x', 'hi'); + $this->assertEquals(" hi ", $this->smarty->fetch($tpl)); + } + + public function testParam1() { + $tpl = $this->smarty->createTemplate('string:{$x|strip_tags:false}'); + $tpl->assign('x', 'hi'); + $this->assertEquals("hi", $this->smarty->fetch($tpl)); + } + + public function testInputIsFalsy0() { + $tpl = $this->smarty->createTemplate('string:{$x|strip_tags}'); + $tpl->assign('x', 0); + $this->assertEquals("0", $this->smarty->fetch($tpl)); + } + + public function testInputIsFalsy1() { + $tpl = $this->smarty->createTemplate('string:{$x|strip_tags}'); + $tpl->assign('x', ''); + $this->assertEquals("", $this->smarty->fetch($tpl)); + } + +}