diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 121dae00153c..fb25fc67d3a4 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -34,6 +34,7 @@ Release Date: Unreleased - **Database:** Fixed a bug where `SQLite3\Connection::_foreignKeyData()` did not escape table identifiers in `PRAGMA foreign_key_list`, causing syntax errors on table names containing hyphens or schemas. - **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters. - **Debug Toolbar:** Fixed a bug where `History::setFiles()` emitted a PHP warning when attempting to delete a debugbar history file that had already been removed. +- **Helpers:** Fixed a bug in `ellipsize()` where `$position` was clamped after slicing the beginning of the string and where float `1.0` was not treated identically to integer `1`. - **Helpers:** Fixed a bug where `excerpt()` threw a `TypeError` when `$phrase` was not found in `$text` or when `$phrase` was `null` and `$radius` was an odd integer. When `$phrase` is not found, `excerpt()` now falls back to extracting an excerpt from the beginning of `$text`. - **Helpers:** Fixed a bug where `format_number()` with `$precision = 0` appended a trailing comma in locales using a comma as the decimal separator. - **Helpers:** Fixed a bug where `parse_subdomain()` incorrectly treated hostnames with ports (such as IP addresses and two-part TLD domains) as having unexpected subdomains. diff --git a/system/Helpers/text_helper.php b/system/Helpers/text_helper.php index 20abc3d5f7de..3d197b4e4818 100644 --- a/system/Helpers/text_helper.php +++ b/system/Helpers/text_helper.php @@ -437,14 +437,10 @@ function ellipsize(string $str, int $maxLength, $position = 1, string $ellipsis return $str; } - $beg = mb_substr($str, 0, (int) floor($maxLength * $position)); - $position = ($position > 1) ? 1 : $position; - - if ($position === 1) { - $end = mb_substr($str, 0, -($maxLength - mb_strlen($beg))); - } else { - $end = mb_substr($str, -($maxLength - mb_strlen($beg))); - } + $position = max(0.0, min(1.0, $position * 1.0)); + $beg = mb_substr($str, 0, (int) floor($maxLength * $position)); + $remaining = $maxLength - mb_strlen($beg); + $end = $remaining > 0 ? mb_substr($str, -$remaining) : ''; return $beg . $ellipsis . $end; } diff --git a/tests/system/Helpers/TextHelperTest.php b/tests/system/Helpers/TextHelperTest.php index ec81c921c097..ce3a14bb99b0 100644 --- a/tests/system/Helpers/TextHelperTest.php +++ b/tests/system/Helpers/TextHelperTest.php @@ -17,6 +17,7 @@ use CodeIgniter\Test\CIUnitTestCase; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; +use TypeError; /** * @internal @@ -358,6 +359,33 @@ public function testEllipsize(): void } } + #[DataProvider('provideEllipsizePositionEdgeCases')] + public function testEllipsizePositionEdgeCases(float|int $position, string $expected): void + { + $this->assertSame($expected, ellipsize('this is my string', 10, $position)); + } + + /** + * @return iterable + */ + public static function provideEllipsizePositionEdgeCases(): iterable + { + yield 'float one' => [1.0, 'this is my…']; + + yield 'greater than one (int)' => [2, 'this is my…']; + + yield 'greater than one (float)' => [1.5, 'this is my…']; + + yield 'negative float' => [-0.5, '… my string']; + } + + public function testEllipsizeInvalidPositionThrowsTypeError(): void + { + $this->expectException(TypeError::class); + + $this->assertSame('', ellipsize('this is my string', 10, 'invalid')); // @phpstan-ignore argument.type + } + /** * A character limit below 2 used to make word_wrap() loop forever. */