Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 4 additions & 8 deletions system/Helpers/text_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/system/Helpers/TextHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CodeIgniter\Test\CIUnitTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use TypeError;

/**
* @internal
Expand Down Expand Up @@ -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<string, array{float|int, string}>
*/
public static function provideEllipsizePositionEdgeCases(): iterable
{
yield 'float one' => [1.0, 'this is my&hellip;'];

yield 'greater than one (int)' => [2, 'this is my&hellip;'];

yield 'greater than one (float)' => [1.5, 'this is my&hellip;'];

yield 'negative float' => [-0.5, '&hellip; 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.
*/
Expand Down
Loading