Skip to content

Commit df4b23d

Browse files
committed
fix truncating in WordBefore mode with length after last space
1 parent 35b97b9 commit df4b23d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Symfony/Component/String/AbstractString.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ public function truncate(int $length, string $ellipsis = '', bool|TruncateMode $
620620
}
621621

622622
$desiredLength = $length;
623-
if (TruncateMode::WordAfter === $cut || TruncateMode::WordBefore === $cut || !$cut) {
623+
if (TruncateMode::WordAfter === $cut || !$cut) {
624624
if (null === $length = $this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1)) {
625625
return clone $this;
626626
}

src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php

+12
Original file line numberDiff line numberDiff line change
@@ -1536,12 +1536,24 @@ public static function provideTruncate(): array
15361536
['foobar foo aar', 'foobar foo aar', 12, '...', false],
15371537
['foobar...', 'foobar foo', 6, '...', TruncateMode::WordAfter],
15381538
['foobar...', 'foobar foo', 7, '...', TruncateMode::WordAfter],
1539+
['foobar...', 'foobar foo a', 10, '...', TruncateMode::Char],
1540+
['foobar...', 'foobar foo a', 10, '...', TruncateMode::WordBefore],
15391541
['foobar foo...', 'foobar foo a', 10, '...', TruncateMode::WordAfter],
1542+
['foobar fo...', 'foobar foo aar', 12, '...', TruncateMode::Char],
15401543
['foobar foo aar', 'foobar foo aar', 12, '...', TruncateMode::WordAfter],
1544+
['foobar...', 'foobar foo aar', 12, '...', TruncateMode::WordBefore],
1545+
['foobar foo', 'foobar foo aar', 10, '', TruncateMode::Char],
15411546
['foobar foo', 'foobar foo aar', 10, '', TruncateMode::WordBefore],
1547+
['foobar foo', 'foobar foo aar', 10, '', TruncateMode::WordAfter],
1548+
['foobar...', 'foobar foo aar', 10, '...', TruncateMode::Char],
15421549
['foobar...', 'foobar foo aar', 10, '...', TruncateMode::WordBefore],
1550+
['foobar foo...', 'foobar foo aar', 10, '...', TruncateMode::WordAfter],
1551+
['Lorem ipsum do', 'Lorem ipsum dolor sit amet', 14, '', TruncateMode::Char],
15431552
['Lorem ipsum', 'Lorem ipsum dolor sit amet', 14, '', TruncateMode::WordBefore],
1553+
['Lorem ipsum dolor', 'Lorem ipsum dolor sit amet', 14, '', TruncateMode::WordAfter],
1554+
['Lorem i...', 'Lorem ipsum dolor sit amet', 10, '...', TruncateMode::Char],
15441555
['Lorem...', 'Lorem ipsum dolor sit amet', 10, '...', TruncateMode::WordBefore],
1556+
['Lorem ipsum...', 'Lorem ipsum dolor sit amet', 10, '...', TruncateMode::WordAfter],
15451557
];
15461558
}
15471559

0 commit comments

Comments
 (0)