Skip to content

Commit

Permalink
bug #37054 [String] Fix ellipsis of truncate when not using cut optio…
Browse files Browse the repository at this point in the history
…n (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
  • Loading branch information
nicolas-grekas committed Jun 11, 2020
2 parents 8169581 + a2ee6c6 commit 69c37c0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Symfony/Component/String/AbstractString.php
Expand Up @@ -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);
Expand Down
Expand Up @@ -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],
];
}

Expand Down

0 comments on commit 69c37c0

Please sign in to comment.