Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix strip_tags modifier for falsy input. #893

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libs/plugins/modifiercompiler.strip_tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] . ')';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Smarty PHPunit tests of modifier
*/

namespace UnitTests\TemplateSource\TagTests\PluginModifier;
use PHPUnit_Smarty;

/**
* class for modifier tests
*
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class PluginModifierStripTagsTest extends PHPUnit_Smarty {

public function setUp(): void {
$this->setUpSmarty(__DIR__);
}

public function testDefault() {
$tpl = $this->smarty->createTemplate('string:{$x|strip_tags}');
$tpl->assign('x', '<b>hi</b>');
$this->assertEquals(" hi ", $this->smarty->fetch($tpl));
}

public function testParam1() {
$tpl = $this->smarty->createTemplate('string:{$x|strip_tags:false}');
$tpl->assign('x', '<b>hi</b>');
$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));
}

}