From d5499fc8fcb953251b8477af02d0e113deb56e2e Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 6 Dec 2021 16:08:55 -0500 Subject: [PATCH 1/3] Avoid using trim() on null --- src/Mustache/Tokenizer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mustache/Tokenizer.php b/src/Mustache/Tokenizer.php index 6dbe0cdf..4a2f06cf 100644 --- a/src/Mustache/Tokenizer.php +++ b/src/Mustache/Tokenizer.php @@ -88,11 +88,11 @@ class Mustache_Tokenizer * @throws Mustache_Exception_InvalidArgumentException when $delimiters string is invalid * * @param string $text Mustache template source to tokenize - * @param string $delimiters Optionally, pass initial opening and closing delimiters (default: null) + * @param string $delimiters Optionally, pass initial opening and closing delimiters (default: empty string) * * @return array Set of Mustache tokens */ - public function scan($text, $delimiters = null) + public function scan($text, $delimiters = '') { // Setting mbstring.func_overload makes things *really* slow. // Let's do everyone a favor and scan this string as ASCII instead. From 09adfdec7667c432198b8a9d13bccab7892fd3c5 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Sun, 12 Dec 2021 08:37:04 -0500 Subject: [PATCH 2/3] Ensure $delimiters is a string before trimming --- src/Mustache/Tokenizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mustache/Tokenizer.php b/src/Mustache/Tokenizer.php index 4a2f06cf..1846675f 100644 --- a/src/Mustache/Tokenizer.php +++ b/src/Mustache/Tokenizer.php @@ -107,7 +107,7 @@ public function scan($text, $delimiters = '') $this->reset(); - if ($delimiters = trim($delimiters)) { + if (is_string($delimiters) && $delimiters = trim($delimiters)) { $this->setDelimiters($delimiters); } From 10a6c7ce52e9596e929fac08050b09f4d9b33596 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Sun, 12 Dec 2021 08:51:15 -0500 Subject: [PATCH 3/3] Add tests --- test/Mustache/Test/TokenizerTest.php | 48 ++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/Mustache/Test/TokenizerTest.php b/test/Mustache/Test/TokenizerTest.php index 38f16dc4..29568e0b 100644 --- a/test/Mustache/Test/TokenizerTest.php +++ b/test/Mustache/Test/TokenizerTest.php @@ -301,6 +301,54 @@ public function getTokens() ), ), ), + + // Delimiters are trimmed + array( + '<% name %>', + ' <% %> ', + array( + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED, + Mustache_Tokenizer::NAME => 'name', + Mustache_Tokenizer::OTAG => '<%', + Mustache_Tokenizer::CTAG => '%>', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::INDEX => 10, + ), + ), + ), + + // An empty string makes delimiters fall back to default + array( + '{{ name }}', + '', + array( + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED, + Mustache_Tokenizer::NAME => 'name', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::INDEX => 10, + ), + ), + ), + + // A bad delimiter type makes delimiters fall back to default + array( + '{{ name }}', + 42, + array( + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED, + Mustache_Tokenizer::NAME => 'name', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::INDEX => 10, + ), + ), + ), ); } }