diff --git a/src/ValueObjects/Styles.php b/src/ValueObjects/Styles.php index 3dd1e76..91b1b1c 100644 --- a/src/ValueObjects/Styles.php +++ b/src/ValueObjects/Styles.php @@ -644,7 +644,7 @@ final public function contentRepeat(string $string): self { $string = preg_replace("/\[?'?([^'|\]]+)'?\]?/", '$1', $string) ?? ''; - $this->textModifiers[__METHOD__] = static fn (): string => str_repeat($string, (int) floor(terminal()->width() / mb_strlen($string, 'UTF-8'))); + $this->textModifiers[__METHOD__] = static fn (): string => str_repeat($string, (int) floor(terminal()->width() / mb_strwidth($string, 'UTF-8'))); return $this->with(['styles' => [ 'contentRepeat' => true, @@ -855,7 +855,7 @@ private function applyWidth(string $content): string preg_match_all("/\n+/", $content, $matches); $width *= count($matches[0] ?? []) + 1; - $width += mb_strlen($matches[0][0] ?? '', 'UTF-8'); + $width += mb_strwidth($matches[0][0] ?? '', 'UTF-8'); if ($length <= $width) { $space = $width - $length; @@ -942,7 +942,7 @@ private function applyStyling(string $content): string */ public function getLength(?string $text = null): int { - return mb_strlen(preg_replace( + return mb_strwidth(preg_replace( self::STYLING_REGEX, '', $text ?? $this->element?->toString() ?? '' diff --git a/tests/render.php b/tests/render.php index a690059..176ec62 100644 --- a/tests/render.php +++ b/tests/render.php @@ -259,7 +259,37 @@ HTML); - expect($html)->toBe(" \n\n ⚽️ A "); + expect($html)->toBe(" \n\n ⚽️ A "); +}); + +it('renders East Asian characters chains of text-center with spaces correctly', function () { + $html = parse(<<<'HTML' +
+
你好 世界
+
ハローワールド
+
+ HTML); + + expect($html)->toBe(" 你好 世界 \n ハローワールド "); +}); + +it('renders East Asian characters chains of space between with spaces correctly', function () { + $html = parse(<<<'HTML' +
+
+ Termwind + + 你好 世界 +
+
+ Termwind + + ハローワールド +
+
+ HTML); + + expect($html)->toBe(" Termwind 你好 世界 \n Termwind ハローワールド "); }); it('renders multiple chains of w-full with margins and text-alignment', function () {