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

Avoid using trim() on null #380

Merged
merged 3 commits into from
Dec 13, 2021
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
6 changes: 3 additions & 3 deletions src/Mustache/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -107,7 +107,7 @@ public function scan($text, $delimiters = null)

$this->reset();

if ($delimiters = trim($delimiters)) {
if (is_string($delimiters) && $delimiters = trim($delimiters)) {
$this->setDelimiters($delimiters);
}

Expand Down
48 changes: 48 additions & 0 deletions test/Mustache/Test/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
),
),
);
}
}