Skip to content

Commit

Permalink
Explicitly treat null as an empty string when interpolating.
Browse files Browse the repository at this point in the history
Fixes a deprecation warning on PHP 8.1+, and saves us some unnecessary escaping calls!

Thanks for your help @scr4bble :)

Fixes #383
  • Loading branch information
bobthecow committed Jan 21, 2022
1 parent 7fd191f commit 0762097
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Mustache/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ private static function onlyBlockArgs(array $node)

const VARIABLE = '
$value = $this->resolveValue($context->%s(%s), $context);%s
$buffer .= %s%s;
$buffer .= %s($value === null ? \'\' : %s);
';

/**
Expand Down
8 changes: 4 additions & 4 deletions test/Mustache/Test/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getCompileValues()
array(
"\nclass Monkey extends Mustache_Template",
'$value = $this->resolveValue($context->find(\'name\'), $context);',
'$buffer .= $indent . call_user_func($this->mustache->getEscape(), $value);',
'$buffer .= $indent . ($value === null ? \'\' : call_user_func($this->mustache->getEscape(), $value));',
'return $buffer;',
),
),
Expand All @@ -76,7 +76,7 @@ public function getCompileValues()
array(
"\nclass Monkey extends Mustache_Template",
'$value = $this->resolveValue($context->find(\'name\'), $context);',
'$buffer .= $indent . htmlspecialchars($value, ' . ENT_COMPAT . ', \'ISO-8859-1\');',
'$buffer .= $indent . ($value === null ? \'\' : htmlspecialchars($value, ' . ENT_COMPAT . ', \'ISO-8859-1\'));',
'return $buffer;',
),
),
Expand All @@ -96,7 +96,7 @@ public function getCompileValues()
array(
"\nclass Monkey extends Mustache_Template",
'$value = $this->resolveValue($context->find(\'name\'), $context);',
'$buffer .= $indent . htmlspecialchars($value, ' . ENT_QUOTES . ', \'ISO-8859-1\');',
'$buffer .= $indent . ($value === null ? \'\' : htmlspecialchars($value, ' . ENT_QUOTES . ', \'ISO-8859-1\'));',
'return $buffer;',
),
),
Expand All @@ -123,7 +123,7 @@ public function getCompileValues()
"\nclass Monkey extends Mustache_Template",
"\$buffer .= \$indent . 'foo\n';",
'$value = $this->resolveValue($context->find(\'name\'), $context);',
'$buffer .= htmlspecialchars($value, ' . ENT_COMPAT . ', \'UTF-8\');',
'$buffer .= ($value === null ? \'\' : htmlspecialchars($value, ' . ENT_COMPAT . ', \'UTF-8\'));',
'$value = $this->resolveValue($context->last(), $context);',
'$buffer .= \'\\\'bar\\\'\';',
'return $buffer;',
Expand Down

0 comments on commit 0762097

Please sign in to comment.