Skip to content

Commit

Permalink
Merge pull request #155 from xiCO2k/fix/truncate-with-w-full-bug
Browse files Browse the repository at this point in the history
fix: Truncate to work well with `w-full` or `w-division`.
  • Loading branch information
xiCO2k authored Oct 17, 2022
2 parents cd2d170 + 2c2123b commit 24ee35e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ValueObjects/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,15 @@ final public function truncate(int $limit = 0, string $end = '…'): self
{
$this->textModifiers[__METHOD__] = function ($text, $styles) use ($limit, $end): string {
$width = $styles['width'] ?? 0;

if (is_string($width)) {
$width = self::calcWidthFromFraction(
$width,
$styles,
$this->properties['parentStyles'] ?? []
);
}

[, $paddingRight, , $paddingLeft] = $this->getPaddings();
$width -= $paddingRight + $paddingLeft;

Expand Down
22 changes: 22 additions & 0 deletions tests/classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@
expect($html)->toBe(' te… ');
});

test('truncate with w-full', function () {
putenv('COLUMNS=5');

$html = parse(<<<'HTML'
<span class="w-full truncate">texttext</span>
HTML);

expect($html)->toBe('text…');
});

test('truncate with w-division', function () {
putenv('COLUMNS=6');

$html = parse(<<<'HTML'
<div class="w-full">
<span class="w-1/2 truncate">texttext</span>
</div>
HTML);

expect($html)->toBe('te… ');
});

test('w', function () {
$html = parse(<<<'HTML'
<span>
Expand Down

0 comments on commit 24ee35e

Please # to comment.