Skip to content

Commit c97bd94

Browse files
committed
bug symfony#57778 [String] fix truncating in WordBefore mode with length after last space (xabbuh)
This PR was merged into the 7.2 branch. Discussion ---------- [String] fix truncating in WordBefore mode with length after last space | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT Commits ------- d07b5db fix truncating in WordBefore mode with length after last space
2 parents 6c7a6ff + d07b5db commit c97bd94

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

UPGRADE-7.2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ String
2525
* `truncate` method now also accept `TruncateMode` enum instead of a boolean:
2626
* `TruncateMode::Char` is equivalent to `true` value ;
2727
* `TruncateMode::WordAfter` is equivalent to `false` value ;
28-
* `TruncateMode::Word` is a new mode that will cut the sentence on the last word before the limit is reached.
28+
* `TruncateMode::WordBefore` is a new mode that will cut the sentence on the last word before the limit is reached.
2929

3030
Yaml
3131
----

src/Symfony/Component/String/AbstractString.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,13 @@ 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
}
627627

628+
$length += $ellipsisLength;
629+
} elseif (TruncateMode::WordBefore === $cut && null !== $this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1)) {
628630
$length += $ellipsisLength;
629631
}
630632

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,14 +1535,42 @@ public static function provideTruncate(): array
15351535
['foobar...', 'foobar foo', 7, '...', false],
15361536
['foobar foo...', 'foobar foo a', 10, '...', false],
15371537
['foobar foo aar', 'foobar foo aar', 12, '...', false],
1538+
['foobar', 'foobar foo', 6, '', TruncateMode::Char],
1539+
['foobar', 'foobar foo', 6, '', TruncateMode::WordAfter],
1540+
['foobar', 'foobar foo', 6, '', TruncateMode::WordBefore],
1541+
['foo...', 'foobar foo', 6, '...', TruncateMode::Char],
15381542
['foobar...', 'foobar foo', 6, '...', TruncateMode::WordAfter],
1543+
['foobar...', 'foobar foo', 6, '...', TruncateMode::WordBefore],
1544+
['foobar ', 'foobar foo', 7, '', TruncateMode::Char],
1545+
['foobar', 'foobar foo', 7, '', TruncateMode::WordAfter],
1546+
['foobar', 'foobar foo', 7, '', TruncateMode::WordBefore],
1547+
['foob...', 'foobar foo', 7, '...', TruncateMode::Char],
15391548
['foobar...', 'foobar foo', 7, '...', TruncateMode::WordAfter],
1549+
['foobar...', 'foobar foo', 7, '...', TruncateMode::WordBefore],
1550+
['foobar foo', 'foobar foo a', 10, '', TruncateMode::Char],
1551+
['foobar foo', 'foobar foo a', 10, '', TruncateMode::WordAfter],
1552+
['foobar foo', 'foobar foo a', 10, '', TruncateMode::WordBefore],
1553+
['foobar...', 'foobar foo a', 10, '...', TruncateMode::Char],
15401554
['foobar foo...', 'foobar foo a', 10, '...', TruncateMode::WordAfter],
1555+
['foobar...', 'foobar foo a', 10, '...', TruncateMode::WordBefore],
1556+
['foobar foo a', 'foobar foo aar', 12, '', TruncateMode::Char],
1557+
['foobar foo aar', 'foobar foo aar', 12, '', TruncateMode::WordAfter],
1558+
['foobar foo', 'foobar foo aar', 12, '', TruncateMode::WordBefore],
1559+
['foobar fo...', 'foobar foo aar', 12, '...', TruncateMode::Char],
15411560
['foobar foo aar', 'foobar foo aar', 12, '...', TruncateMode::WordAfter],
1561+
['foobar...', 'foobar foo aar', 12, '...', TruncateMode::WordBefore],
1562+
['foobar foo', 'foobar foo aar', 10, '', TruncateMode::Char],
15421563
['foobar foo', 'foobar foo aar', 10, '', TruncateMode::WordBefore],
1564+
['foobar foo', 'foobar foo aar', 10, '', TruncateMode::WordAfter],
1565+
['foobar...', 'foobar foo aar', 10, '...', TruncateMode::Char],
15431566
['foobar...', 'foobar foo aar', 10, '...', TruncateMode::WordBefore],
1567+
['foobar foo...', 'foobar foo aar', 10, '...', TruncateMode::WordAfter],
1568+
['Lorem ipsum do', 'Lorem ipsum dolor sit amet', 14, '', TruncateMode::Char],
15441569
['Lorem ipsum', 'Lorem ipsum dolor sit amet', 14, '', TruncateMode::WordBefore],
1570+
['Lorem ipsum dolor', 'Lorem ipsum dolor sit amet', 14, '', TruncateMode::WordAfter],
1571+
['Lorem i...', 'Lorem ipsum dolor sit amet', 10, '...', TruncateMode::Char],
15451572
['Lorem...', 'Lorem ipsum dolor sit amet', 10, '...', TruncateMode::WordBefore],
1573+
['Lorem ipsum...', 'Lorem ipsum dolor sit amet', 10, '...', TruncateMode::WordAfter],
15461574
];
15471575
}
15481576

0 commit comments

Comments
 (0)