From 14e54729132b147a1c9cae95853f91cf08fa5fcd Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sat, 21 Nov 2026 20:13:40 +0700 Subject: [PATCH 1/2] fix(Helpers): clamp position and handle float 1.0 in ellipsize() --- system/Helpers/text_helper.php | 12 ++++-------- tests/system/Helpers/TextHelperTest.php | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/system/Helpers/text_helper.php b/system/Helpers/text_helper.php index 20abc3d5f7de..a56d3c0e3f90 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, (float) $position)); + $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..1def34176246 100644 --- a/tests/system/Helpers/TextHelperTest.php +++ b/tests/system/Helpers/TextHelperTest.php @@ -358,6 +358,26 @@ 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']; + } + /** * A character limit below 2 used to make word_wrap() loop forever. */ From 75e88814520003202d572c64ac3fbcf4c7dd30f8 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sun, 22 Nov 2026 08:51:32 +0700 Subject: [PATCH 2/2] fix(Helpers): preserve TypeError for non-numeric position and add changelog --- docs/changelogs/v4.7.7.md | 1 + system/Helpers/text_helper.php | 2 +- tests/system/Helpers/TextHelperTest.php | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) 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 a56d3c0e3f90..3d197b4e4818 100644 --- a/system/Helpers/text_helper.php +++ b/system/Helpers/text_helper.php @@ -437,7 +437,7 @@ function ellipsize(string $str, int $maxLength, $position = 1, string $ellipsis return $str; } - $position = max(0.0, min(1.0, (float) $position)); + $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) : ''; diff --git a/tests/system/Helpers/TextHelperTest.php b/tests/system/Helpers/TextHelperTest.php index 1def34176246..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 @@ -378,6 +379,13 @@ public static function provideEllipsizePositionEdgeCases(): iterable 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. */