From 076209772dda21cbfdbf5ef99d202a0937da4826 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Fri, 21 Jan 2022 00:45:13 -0500 Subject: [PATCH] Explicitly treat `null` as an empty string when interpolating. Fixes a deprecation warning on PHP 8.1+, and saves us some unnecessary escaping calls! Thanks for your help @scr4bble :) Fixes #383 --- src/Mustache/Compiler.php | 2 +- test/Mustache/Test/CompilerTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Mustache/Compiler.php b/src/Mustache/Compiler.php index 43fedccb..2fa1a736 100644 --- a/src/Mustache/Compiler.php +++ b/src/Mustache/Compiler.php @@ -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); '; /** diff --git a/test/Mustache/Test/CompilerTest.php b/test/Mustache/Test/CompilerTest.php index 6fd1986f..c07a0d45 100644 --- a/test/Mustache/Test/CompilerTest.php +++ b/test/Mustache/Test/CompilerTest.php @@ -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;', ), ), @@ -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;', ), ), @@ -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;', ), ), @@ -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;',