Allow null for nl2br, striptags and format filters

Same as https://github.com/twigphp/Twig/pull/3617
This commit is contained in:
Ruud Kamphuis
2022-01-03 14:00:34 +01:00
parent e155d8dcfc
commit e9cd55def5
5 changed files with 66 additions and 4 deletions
+41 -3
View File
@@ -213,7 +213,7 @@ final class CoreExtension extends AbstractExtension
// formatting filters
new TwigFilter('date', 'twig_date_format_filter', ['needs_environment' => true]),
new TwigFilter('date_modify', 'twig_date_modify_filter', ['needs_environment' => true]),
new TwigFilter('format', 'sprintf'),
new TwigFilter('format', 'twig_sprintf'),
new TwigFilter('replace', 'twig_replace_filter'),
new TwigFilter('number_format', 'twig_number_format_filter', ['needs_environment' => true]),
new TwigFilter('abs', 'abs'),
@@ -229,9 +229,9 @@ final class CoreExtension extends AbstractExtension
new TwigFilter('capitalize', 'twig_capitalize_string_filter', ['needs_environment' => true]),
new TwigFilter('upper', 'twig_upper_filter', ['needs_environment' => true]),
new TwigFilter('lower', 'twig_lower_filter', ['needs_environment' => true]),
new TwigFilter('striptags', 'strip_tags'),
new TwigFilter('striptags', 'twig_striptags'),
new TwigFilter('trim', 'twig_trim_filter'),
new TwigFilter('nl2br', 'nl2br', ['pre_escape' => 'html', 'is_safe' => ['html']]),
new TwigFilter('nl2br', 'twig_nl2br', ['pre_escape' => 'html', 'is_safe' => ['html']]),
new TwigFilter('spaceless', 'twig_spaceless', ['is_safe' => ['html']]),
// array helpers
@@ -481,6 +481,19 @@ function twig_date_modify_filter(Environment $env, $date, $modifier)
return $date->modify($modifier);
}
/**
* Returns a formatted string.
*
* @param string|null $format
* @param ...$values
*
* @return string
*/
function twig_sprintf($format, ...$values)
{
return sprintf($format ?? '', ...$values);
}
/**
* Converts an input to a \DateTime instance.
*
@@ -996,6 +1009,18 @@ function twig_trim_filter($string, $characterMask = null, $side = 'both')
}
}
/**
* Inserts HTML line breaks before all newlines in a string.
*
* @param string|null $string
*
* @return string
*/
function twig_nl2br($string)
{
return nl2br($string ?? '');
}
/**
* Removes whitespaces between HTML tags.
*
@@ -1080,6 +1105,19 @@ function twig_lower_filter(Environment $env, $string)
return mb_strtolower($string ?? '', $env->getCharset());
}
/**
* Strips HTML and PHP tags from a string.
*
* @param string|null $string
* @param string[]|string|null $string
*
* @return string
*/
function twig_striptags($string, $allowable_tags = null)
{
return strip_tags($string ?? '', $allowable_tags);
}
/**
* Returns a titlecased string.
*
+4
View File
@@ -2,7 +2,11 @@
"format" filter
--TEMPLATE--
{{ string|format(foo, 3) }}
*{{ ""|format(foo, 3) }}*
*{{ null|format(foo, 3) }}*
--DATA--
return ['string' => '%s/%d', 'foo' => 'bar']
--EXPECT--
bar/3
**
**
+4
View File
@@ -3,6 +3,8 @@
--TEMPLATE--
{{ "I like Twig.\nYou will like it too.\n\nEverybody like it!"|nl2br }}
{{ text|nl2br }}
*{{ ''|nl2br }}*
*{{ null|nl2br }}*
--DATA--
return ['text' => "If you have some <strong>HTML</strong>\nit will be escaped."]
--EXPECT--
@@ -12,3 +14,5 @@ You will like it too.<br />
Everybody like it!
If you have some &lt;strong&gt;HTML&lt;/strong&gt;<br />
it will be escaped.
**
**
+16
View File
@@ -0,0 +1,16 @@
--TEST--
"striptags" filter
--TEMPLATE--
{{ "Hello, <strong>World</strong>!"|striptags }}
{{ text|striptags }}
{{ text|striptags('<strong>')|raw }}
*{{ ''|striptags }}*
*{{ null|striptags }}*
--DATA--
return ['text' => "<p>Hello, <strong>World</strong>!</p>"]
--EXPECT--
Hello, World!
Hello, World!
Hello, <strong>World</strong>!
**
**
+1 -1
View File
@@ -229,7 +229,7 @@ class TwigTestExtension extends AbstractExtension
{
// not secure if $value contains html tags (not only entities)
// don't use
return str_replace("\n", "$sep\n", $value);
return str_replace("\n", "$sep\n", $value ?? '');
}
public function dynamic_path($element, $item)