Skip to content

Commit 69c37c0

Browse files
bug #37054 [String] Fix ellipsis of truncate when not using cut option (DuboisS)
This PR was merged into the 5.1 branch. Discussion ---------- [String] Fix ellipsis of truncate when not using cut option | Q | A | ------------- | --- | Branch? | 5.1 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | - <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | - <!-- required for new features --> [Since 5.1](https://symfony.com/blog/new-in-symfony-5-1-string-improvements#keep-the-last-word-when-truncating), we can use a cut option on truncate. But with this option, we don't have the expected behavior when the entire chain is returned. Currently: `u('Lorem Ipsum')->truncate(8, '…', false); // 'Lorem Ipsum...'` Instead of: `u('Lorem Ipsum')->truncate(8, '…', false); // 'Lorem Ipsum'` Thanks to @jmsche for his help. Commits ------- a2ee6c6 [String] Fix ellipsis of truncate when not using cut option
2 parents 8169581 + a2ee6c6 commit 69c37c0

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/Symfony/Component/String/AbstractString.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,11 @@ public function truncate(int $length, string $ellipsis = '', bool $cut = true):
643643
}
644644

645645
if (!$cut) {
646-
$length = $ellipsisLength + ($this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1) ?? $stringLength);
646+
if (null === $length = $this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1)) {
647+
return clone $this;
648+
}
649+
650+
$length += $ellipsisLength;
647651
}
648652

649653
$str = $this->slice(0, $length - $ellipsisLength);

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

+1
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,7 @@ public static function provideTruncate()
14501450
['foobar...', 'foobar foo', 6, '...', false],
14511451
['foobar...', 'foobar foo', 7, '...', false],
14521452
['foobar foo...', 'foobar foo a', 10, '...', false],
1453+
['foobar foo aar', 'foobar foo aar', 12, '...', false],
14531454
];
14541455
}
14551456

0 commit comments

Comments
 (0)