Skip to content

Commit 8676014

Browse files
authored
fix(Helpers): clamp position and handle float 1.0 in ellipsize() (#10809)
1 parent e78a203 commit 8676014

3 files changed

Lines changed: 33 additions & 8 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Release Date: Unreleased
3434
- **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.
3535
- **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.
3636
- **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.
37+
- **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`.
3738
- **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`.
3839
- **Helpers:** Fixed a bug where `format_number()` with `$precision = 0` appended a trailing comma in locales using a comma as the decimal separator.
3940
- **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.

‎system/Helpers/text_helper.php‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -437,14 +437,10 @@ function ellipsize(string $str, int $maxLength, $position = 1, string $ellipsis
437437
return $str;
438438
}
439439

440-
$beg = mb_substr($str, 0, (int) floor($maxLength * $position));
441-
$position = ($position > 1) ? 1 : $position;
442-
443-
if ($position === 1) {
444-
$end = mb_substr($str, 0, -($maxLength - mb_strlen($beg)));
445-
} else {
446-
$end = mb_substr($str, -($maxLength - mb_strlen($beg)));
447-
}
440+
$position = max(0.0, min(1.0, $position * 1.0));
441+
$beg = mb_substr($str, 0, (int) floor($maxLength * $position));
442+
$remaining = $maxLength - mb_strlen($beg);
443+
$end = $remaining > 0 ? mb_substr($str, -$remaining) : '';
448444

449445
return $beg . $ellipsis . $end;
450446
}

‎tests/system/Helpers/TextHelperTest.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use PHPUnit\Framework\Attributes\DataProvider;
1919
use PHPUnit\Framework\Attributes\Group;
20+
use TypeError;
2021

2122
/**
2223
* @internal
@@ -358,6 +359,33 @@ public function testEllipsize(): void
358359
}
359360
}
360361

362+
#[DataProvider('provideEllipsizePositionEdgeCases')]
363+
public function testEllipsizePositionEdgeCases(float|int $position, string $expected): void
364+
{
365+
$this->assertSame($expected, ellipsize('this is my string', 10, $position));
366+
}
367+
368+
/**
369+
* @return iterable<string, array{float|int, string}>
370+
*/
371+
public static function provideEllipsizePositionEdgeCases(): iterable
372+
{
373+
yield 'float one' => [1.0, 'this is my&hellip;'];
374+
375+
yield 'greater than one (int)' => [2, 'this is my&hellip;'];
376+
377+
yield 'greater than one (float)' => [1.5, 'this is my&hellip;'];
378+
379+
yield 'negative float' => [-0.5, '&hellip; my string'];
380+
}
381+
382+
public function testEllipsizeInvalidPositionThrowsTypeError(): void
383+
{
384+
$this->expectException(TypeError::class);
385+
386+
$this->assertSame('', ellipsize('this is my string', 10, 'invalid')); // @phpstan-ignore argument.type
387+
}
388+
361389
/**
362390
* A character limit below 2 used to make word_wrap() loop forever.
363391
*/

0 commit comments

Comments
 (0)