Skip to content

Commit d07b5db

Browse files
committed
fix truncating in WordBefore mode with length after last space
1 parent 7484a85 commit d07b5db

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
@@ -19,7 +19,7 @@ String
1919
* `truncate` method now also accept `TruncateMode` enum instead of a boolean:
2020
* `TruncateMode::Char` is equivalent to `true` value ;
2121
* `TruncateMode::WordAfter` is equivalent to `false` value ;
22-
* `TruncateMode::Word` is a new mode that will cut the sentence on the last word before the limit is reached.
22+
* `TruncateMode::WordBefore` is a new mode that will cut the sentence on the last word before the limit is reached.
2323

2424
Yaml
2525
----

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
@@ -1534,14 +1534,42 @@ public static function provideTruncate(): array
15341534
['foobar...', 'foobar foo', 7, '...', false],
15351535
['foobar foo...', 'foobar foo a', 10, '...', false],
15361536
['foobar foo aar', 'foobar foo aar', 12, '...', false],
1537+
['foobar', 'foobar foo', 6, '', TruncateMode::Char],
1538+
['foobar', 'foobar foo', 6, '', TruncateMode::WordAfter],
1539+
['foobar', 'foobar foo', 6, '', TruncateMode::WordBefore],
1540+
['foo...', 'foobar foo', 6, '...', TruncateMode::Char],
15371541
['foobar...', 'foobar foo', 6, '...', TruncateMode::WordAfter],
1542+
['foobar...', 'foobar foo', 6, '...', TruncateMode::WordBefore],
1543+
['foobar ', 'foobar foo', 7, '', TruncateMode::Char],
1544+
['foobar', 'foobar foo', 7, '', TruncateMode::WordAfter],
1545+
['foobar', 'foobar foo', 7, '', TruncateMode::WordBefore],
1546+
['foob...', 'foobar foo', 7, '...', TruncateMode::Char],
15381547
['foobar...', 'foobar foo', 7, '...', TruncateMode::WordAfter],
1548+
['foobar...', 'foobar foo', 7, '...', TruncateMode::WordBefore],
1549+
['foobar foo', 'foobar foo a', 10, '', TruncateMode::Char],
1550+
['foobar foo', 'foobar foo a', 10, '', TruncateMode::WordAfter],
1551+
['foobar foo', 'foobar foo a', 10, '', TruncateMode::WordBefore],
1552+
['foobar...', 'foobar foo a', 10, '...', TruncateMode::Char],
15391553
['foobar foo...', 'foobar foo a', 10, '...', TruncateMode::WordAfter],
1554+
['foobar...', 'foobar foo a', 10, '...', TruncateMode::WordBefore],
1555+
['foobar foo a', 'foobar foo aar', 12, '', TruncateMode::Char],
1556+
['foobar foo aar', 'foobar foo aar', 12, '', TruncateMode::WordAfter],
1557+
['foobar foo', 'foobar foo aar', 12, '', TruncateMode::WordBefore],
1558+
['foobar fo...', 'foobar foo aar', 12, '...', TruncateMode::Char],
15401559
['foobar foo aar', 'foobar foo aar', 12, '...', TruncateMode::WordAfter],
1560+
['foobar...', 'foobar foo aar', 12, '...', TruncateMode::WordBefore],
1561+
['foobar foo', 'foobar foo aar', 10, '', TruncateMode::Char],
15411562
['foobar foo', 'foobar foo aar', 10, '', TruncateMode::WordBefore],
1563+
['foobar foo', 'foobar foo aar', 10, '', TruncateMode::WordAfter],
1564+
['foobar...', 'foobar foo aar', 10, '...', TruncateMode::Char],
15421565
['foobar...', 'foobar foo aar', 10, '...', TruncateMode::WordBefore],
1566+
['foobar foo...', 'foobar foo aar', 10, '...', TruncateMode::WordAfter],
1567+
['Lorem ipsum do', 'Lorem ipsum dolor sit amet', 14, '', TruncateMode::Char],
15431568
['Lorem ipsum', 'Lorem ipsum dolor sit amet', 14, '', TruncateMode::WordBefore],
1569+
['Lorem ipsum dolor', 'Lorem ipsum dolor sit amet', 14, '', TruncateMode::WordAfter],
1570+
['Lorem i...', 'Lorem ipsum dolor sit amet', 10, '...', TruncateMode::Char],
15441571
['Lorem...', 'Lorem ipsum dolor sit amet', 10, '...', TruncateMode::WordBefore],
1572+
['Lorem ipsum...', 'Lorem ipsum dolor sit amet', 10, '...', TruncateMode::WordAfter],
15451573
];
15461574
}
15471575

0 commit comments

Comments
 (0)