Improve how trim behaves

This commit is contained in:
Fabien Potencier
2024-09-18 09:41:27 +02:00
parent 70886ec75e
commit 10c3142d3b
3 changed files with 37 additions and 19 deletions
+17 -16
View File
@@ -93,6 +93,8 @@ use Twig\Util\CallableArgumentsExtractor;
final class CoreExtension extends AbstractExtension
{
private const DEFAULT_TRIM_CHARS = " \t\n\r\0\x0B";
private $dateFormats = ['F j, Y H:i', '%d days'];
private $numberFormat = [0, '.', ','];
private $timezone = null;
@@ -1116,30 +1118,29 @@ final class CoreExtension extends AbstractExtension
/**
* Returns a trimmed string.
*
* @param string|null $string
* @param string|null $characterMask
* @param string $side
* @param string|\Stringable|null $string
* @param string|null $characterMask
* @param string $side left, right, or both
*
* @throws RuntimeError When an invalid trimming side is used (not a string or not 'left', 'right', or 'both')
* @throws RuntimeError When an invalid trimming side is used
*
* @internal
*/
public static function trim($string, $characterMask = null, $side = 'both'): string
public static function trim($string, $characterMask = null, $side = 'both'): string|\Stringable
{
if (null === $characterMask) {
$characterMask = " \t\n\r\0\x0B";
$characterMask = self::DEFAULT_TRIM_CHARS;
}
switch ($side) {
case 'both':
return trim($string ?? '', $characterMask);
case 'left':
return ltrim($string ?? '', $characterMask);
case 'right':
return rtrim($string ?? '', $characterMask);
default:
throw new RuntimeError('Trimming side must be "left", "right" or "both".');
}
$trimmed = match ($side) {
'both' => trim($string ?? '', $characterMask),
'left' => ltrim($string ?? '', $characterMask),
'right' => rtrim($string ?? '', $characterMask),
default => throw new RuntimeError('Trimming side must be "left", "right" or "both".'),
};
// trimming a safe string with the default character mask always returns a safe string (independently of the context)
return $string instanceof Markup && self::DEFAULT_TRIM_CHARS === $characterMask ? new Markup($trimmed, $string->getCharset()) : $trimmed;
}
/**
+5
View File
@@ -32,6 +32,11 @@ class Markup implements \Countable, \JsonSerializable, \Stringable
return $this->content;
}
public function getCharset(): string
{
return $this->charset;
}
/**
* @return int
*/
+15 -3
View File
@@ -4,11 +4,11 @@
{{ " I like Twig. "|trim }}
{{ text|trim }}
{{ " foo/"|trim("/") }}
{{ "xxxI like Twig.xxx"|trim(character_mask="x", side="left") }}
{{ "xxxI like Twig.xxx"|trim(side="right", character_mask="x") }}
{{ "xxxI like Twig.xxx"|trim(character_mask: "x", side: "left") }}
{{ "xxxI like Twig.xxx"|trim(side: "right", character_mask: "x") }}
{{ "xxxI like Twig.xxx"|trim("x", "right") }}
{{ "/ foo/"|trim("/", "left") }}
{{ "/ foo/"|trim(character_mask="/", side="left") }}
{{ "/ foo/"|trim(character_mask: "/", side: "left") }}
{{ " do nothing. "|trim("", "right") }}
*{{ ""|trim }}*
*{{ ""|trim("", "left") }}*
@@ -16,6 +16,14 @@
*{{ null|trim }}*
*{{ null|trim("", "left") }}*
*{{ null|trim("", "right") }}*
{% set myhtml %}
Here is<br>my HTML
{% endset %}
{% set myunsafestring = " I <3 u " %}
{{ myhtml | trim }}
{{ myunsafestring | trim }}
{{ myhtml | trim(character_mask: "f") }}
--DATA--
return ['text' => " If you have some <strong>HTML</strong> it will be escaped. "]
--EXPECT--
@@ -34,3 +42,7 @@ xxxI like Twig.
**
**
**
Here is<br>my HTML
I &lt;3 u
Here is&lt;br&gt;my HTML