From 2c2123ba4781ec83f2c27daaaaffa6044067aa32 Mon Sep 17 00:00:00 2001 From: Francisco Madeira Date: Wed, 5 Oct 2022 01:22:42 +0100 Subject: [PATCH] fix: Truncate to work well with `w-full` or `w-division`. --- src/ValueObjects/Styles.php | 9 +++++++++ tests/classes.php | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/ValueObjects/Styles.php b/src/ValueObjects/Styles.php index 3ed976e..617ed56 100644 --- a/src/ValueObjects/Styles.php +++ b/src/ValueObjects/Styles.php @@ -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; diff --git a/tests/classes.php b/tests/classes.php index ac7b07b..05aa9b5 100644 --- a/tests/classes.php +++ b/tests/classes.php @@ -186,6 +186,28 @@ expect($html)->toBe(' te… '); }); +test('truncate with w-full', function () { + putenv('COLUMNS=5'); + + $html = parse(<<<'HTML' + texttext + HTML); + + expect($html)->toBe('text…'); +}); + +test('truncate with w-division', function () { + putenv('COLUMNS=6'); + + $html = parse(<<<'HTML' +
+ texttext +
+ HTML); + + expect($html)->toBe('te… '); +}); + test('w', function () { $html = parse(<<<'HTML'