From fc8f7a01e3a908871d25dc7bdcd1c541d571b481 Mon Sep 17 00:00:00 2001 From: Vladimir Valikayev Date: Wed, 26 Mar 2025 14:58:40 +0700 Subject: [PATCH 1/2] issues/60038: Table counts wrong number of padding symbols in method renderCell(..) when cell contain unicode variant selector. --- src/Symfony/Component/Console/Helper/Table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 1f026dc504adb..df6807c015f06 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -565,7 +565,7 @@ private function renderCell(array $row, int $column, string $cellFormat): string // str_pad won't work properly with multi-byte strings, we need to fix the padding if (false !== $encoding = mb_detect_encoding($cell, null, true)) { - $width += \strlen($cell) - mb_strwidth($cell, $encoding); + $width += \strlen($cell) - mb_strwidth(str_replace(["\xef\xb8\x8f", "\xef\xb8\x8e", ], "", $cell), $encoding); } $style = $this->getColumnStyle($column); From 61b01a82ec373b7d1cc1275a7135aae743a0dff0 Mon Sep 17 00:00:00 2001 From: Vladimir Valikayev Date: Tue, 1 Apr 2025 23:21:26 +0700 Subject: [PATCH 2/2] issues/60038: Table counts wrong number of padding symbols in method renderCell(..) when cell contain unicode variant selector. With testcase. --- .../Component/Console/Helper/Table.php | 2 +- .../Console/Tests/Helper/TableTest.php | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index df6807c015f06..968110be088aa 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -565,7 +565,7 @@ private function renderCell(array $row, int $column, string $cellFormat): string // str_pad won't work properly with multi-byte strings, we need to fix the padding if (false !== $encoding = mb_detect_encoding($cell, null, true)) { - $width += \strlen($cell) - mb_strwidth(str_replace(["\xef\xb8\x8f", "\xef\xb8\x8e", ], "", $cell), $encoding); + $width += \strlen($cell) - mb_strwidth($cell, $encoding) + substr_count($cell, "\xef\xb8\x8f") + substr_count($cell, "\xef\xb8\x8e"); } $style = $this->getColumnStyle($column); diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index 608d23c210bef..aa125cb4a9429 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -2078,4 +2078,36 @@ public function testGithubIssue52101HorizontalFalse() $this->getOutputContent($output) ); } + + public function testGithubIssue60038WidthOfCellWithEmoji() + { + $table = (new Table($output = $this->getOutputStream())) + ->setHeaderTitle('Test Title') + ->setHeaders(['Title', 'Author']) + ->setRows([ + ["🎭 💫 ☯"." Divine Comedy", "Dante Alighieri"], + // the snowflake (e2 9d 84 ef b8 8f) has a variant selector + ["👑 ❄️ 🗡"." Game of Thrones", "George R.R. Martin"], + // the snowflake in text style (e2 9d 84 ef b8 8e) has a variant selector + ["❄︎❄︎❄︎ snowflake in text style ❄︎❄︎❄︎", ""], + ["And a very long line to show difference in previous lines", ""], + ]) + ; + $table->render(); + + $this->assertSame(<<getOutputContent($output) + ); + } }