diff --git a/src/Compile/Modifier/CountCharactersModifierCompiler.php b/src/Compile/Modifier/CountCharactersModifierCompiler.php index fb5f5ca33..dfc7bc7f3 100644 --- a/src/Compile/Modifier/CountCharactersModifierCompiler.php +++ b/src/Compile/Modifier/CountCharactersModifierCompiler.php @@ -13,7 +13,7 @@ class CountCharactersModifierCompiler extends Base { public function compile($params, \Smarty\Compiler\Template $compiler) { if (!isset($params[ 1 ]) || $params[ 1 ] !== 'true') { - return 'preg_match_all(\'/[^\s]/' . \Smarty\Smarty::$_UTF8_MODIFIER . '\',' . $params[ 0 ] . ', $tmp)'; + return 'preg_match_all(\'/[^\s]/' . \Smarty\Smarty::$_UTF8_MODIFIER . '\', (string)' . $params[ 0 ] . ', $tmp)'; } return 'mb_strlen((string) ' . $params[ 0 ] . ', \'' . addslashes(\Smarty\Smarty::$_CHARSET) . '\')'; } diff --git a/src/Compile/Modifier/CountParagraphsModifierCompiler.php b/src/Compile/Modifier/CountParagraphsModifierCompiler.php index 76552e073..1dd4602a3 100644 --- a/src/Compile/Modifier/CountParagraphsModifierCompiler.php +++ b/src/Compile/Modifier/CountParagraphsModifierCompiler.php @@ -13,7 +13,7 @@ class CountParagraphsModifierCompiler extends Base { public function compile($params, \Smarty\Compiler\Template $compiler) { // count \r or \n characters - return '(preg_match_all(\'#[\r\n]+#\', ' . $params[ 0 ] . ', $tmp)+1)'; + return '(preg_match_all(\'#[\r\n]+#\', (string)' . $params[ 0 ] . ', $tmp)+1)'; } } \ No newline at end of file diff --git a/src/Compile/Modifier/CountSentencesModifierCompiler.php b/src/Compile/Modifier/CountSentencesModifierCompiler.php index bc7c43e1c..25803c5d2 100644 --- a/src/Compile/Modifier/CountSentencesModifierCompiler.php +++ b/src/Compile/Modifier/CountSentencesModifierCompiler.php @@ -13,7 +13,7 @@ class CountSentencesModifierCompiler extends Base { public function compile($params, \Smarty\Compiler\Template $compiler) { // find periods, question marks, exclamation marks with a word before but not after. - return 'preg_match_all("#\w[\.\?\!](\W|$)#S' . \Smarty\Smarty::$_UTF8_MODIFIER . '", ' . $params[ 0 ] . ', $tmp)'; + return 'preg_match_all("#\w[\.\?\!](\W|$)#S' . \Smarty\Smarty::$_UTF8_MODIFIER . '", (string)' . $params[ 0 ] . ', $tmp)'; } } \ No newline at end of file diff --git a/src/Compile/Modifier/CountWordsModifierCompiler.php b/src/Compile/Modifier/CountWordsModifierCompiler.php index c11d546d4..6ed1982c6 100644 --- a/src/Compile/Modifier/CountWordsModifierCompiler.php +++ b/src/Compile/Modifier/CountWordsModifierCompiler.php @@ -14,7 +14,7 @@ class CountWordsModifierCompiler extends Base { public function compile($params, \Smarty\Compiler\Template $compiler) { // expression taken from http://de.php.net/manual/en/function.str-word-count.php#85592 return 'preg_match_all(\'/\p{L}[\p{L}\p{Mn}\p{Pd}\\\'\x{2019}]*/' . \Smarty\Smarty::$_UTF8_MODIFIER . '\', ' . - $params[ 0 ] . ', $tmp)'; + '(string)' . $params[ 0 ] . ', $tmp)'; } } \ No newline at end of file diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountCharactersTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountCharactersTest.php index 7e4ebe6c0..b10d3200b 100644 --- a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountCharactersTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountCharactersTest.php @@ -48,4 +48,12 @@ public function testUmlautsSpaces() $this->assertEquals(str_replace("\r", '', $result), $this->smarty->fetch($tpl)); } + public function testNull() + { + $tpl = $this->smarty->createTemplate('string:{null|count_characters}'); + $this->assertEquals("0", $this->smarty->fetch($tpl)); + $tpl = $this->smarty->createTemplate('string:{null|count_characters:true}'); + $this->assertEquals("0", $this->smarty->fetch($tpl)); + } + } diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountParagraphsTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountParagraphsTest.php new file mode 100644 index 000000000..5b8451359 --- /dev/null +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountParagraphsTest.php @@ -0,0 +1,48 @@ +setUpSmarty(__DIR__); + } + + public function testInit() + { + $this->cleanDirs(); + } + + public function testDefault() + { + $tpl = $this->smarty->createTemplate('string:{"One paragraph only."|count_paragraphs}'); + $this->assertEquals("1", $this->smarty->fetch($tpl)); + $tpl = $this->smarty->createTemplate('string:{"First line.\nSecond line."|count_paragraphs}'); + $this->assertEquals("2", $this->smarty->fetch($tpl)); + $tpl = $this->smarty->createTemplate('string:{"Paragraph1\nParagraph2\nParagraph3"|count_paragraphs}'); + $this->assertEquals("3", $this->smarty->fetch($tpl)); + $tpl = $this->smarty->createTemplate('string:{"Paragraph1\n\nParagraph2"|count_paragraphs}'); + $this->assertEquals("2", $this->smarty->fetch($tpl)); + $tpl = $this->smarty->createTemplate('string:{"Paragraph1\r\nParagraph2\nParagraph3\rParagraph4"|count_paragraphs}'); + $this->assertEquals("4", $this->smarty->fetch($tpl)); + $tpl = $this->smarty->createTemplate('string:{"End with newline\n"|count_paragraphs}'); + $this->assertEquals("2", $this->smarty->fetch($tpl)); + $tpl = $this->smarty->createTemplate('string:{"\nStart with newline"|count_paragraphs}'); + $this->assertEquals("2", $this->smarty->fetch($tpl)); + } + + public function testNull() + { + $tpl = $this->smarty->createTemplate('string:{null|count_paragraphs}'); + $this->assertEquals("1", $this->smarty->fetch($tpl)); + } +} diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountSentencesTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountSentencesTest.php index caec76304..02cc2cbee 100644 --- a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountSentencesTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountSentencesTest.php @@ -48,4 +48,10 @@ public function testUmlauts() $tpl = $this->smarty->createTemplate('string:{"hello world.ärong"|count_sentences}'); $this->assertEquals("0", $this->smarty->fetch($tpl)); } + + public function testNull() + { + $tpl = $this->smarty->createTemplate('string:{null|count_sentences}'); + $this->assertEquals("0", $this->smarty->fetch($tpl)); + } } diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountWordsTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountWordsTest.php index 97a3cc537..6e2bd09b1 100644 --- a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountWordsTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountWordsTest.php @@ -41,4 +41,10 @@ public function testUmlauts() $this->assertEquals(str_replace("\r", '', $result), $this->smarty->fetch($tpl)); } + public function testNull() + { + $result = "0"; + $tpl = $this->smarty->createTemplate('string:{null|count_words}'); + $this->assertEquals(str_replace("\r", '', $result), $this->smarty->fetch($tpl)); + } }