From a2ee6c6cf4d8370a701d307c4d0883c4aeef16e8 Mon Sep 17 00:00:00 2001 From: Steven Dubois <29716703+DuboisS@users.noreply.github.com> Date: Mon, 1 Jun 2020 23:16:32 +0200 Subject: [PATCH] [String] Fix ellipsis of truncate when not using cut option --- src/Symfony/Component/String/AbstractString.php | 6 +++++- .../Component/String/Tests/AbstractAsciiTestCase.php | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/String/AbstractString.php b/src/Symfony/Component/String/AbstractString.php index c9ba147b319c..f2754a56d0b6 100644 --- a/src/Symfony/Component/String/AbstractString.php +++ b/src/Symfony/Component/String/AbstractString.php @@ -643,7 +643,11 @@ public function truncate(int $length, string $ellipsis = '', bool $cut = true): } if (!$cut) { - $length = $ellipsisLength + ($this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1) ?? $stringLength); + if (null === $length = $this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1)) { + return clone $this; + } + + $length += $ellipsisLength; } $str = $this->slice(0, $length - $ellipsisLength); diff --git a/src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php b/src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php index b333c74a252b..44f14c18af3a 100644 --- a/src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php +++ b/src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php @@ -1450,6 +1450,7 @@ public static function provideTruncate() ['foobar...', 'foobar foo', 6, '...', false], ['foobar...', 'foobar foo', 7, '...', false], ['foobar foo...', 'foobar foo a', 10, '...', false], + ['foobar foo aar', 'foobar foo aar', 12, '...', false], ]; }