From b725f05fdeba4e37e044ea672d0ade68bad93b68 Mon Sep 17 00:00:00 2001 From: Vladimir Valikayev Date: Wed, 26 Mar 2025 15:33:23 +0700 Subject: [PATCH 1/4] issues/60043: Table counts wrong column width when use colspan and setColumnMaxWidth(). --- .../Component/Console/Helper/Table.php | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 1f026dc504adb..9ac986f75b173 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -632,8 +632,48 @@ private function buildTableRows(array $rows): TableRows foreach ($rows[$rowKey] as $column => $cell) { $colspan = $cell instanceof TableCell ? $cell->getColspan() : 1; - if (isset($this->columnMaxWidths[$column]) && Helper::width(Helper::removeDecoration($formatter, $cell)) > $this->columnMaxWidths[$column]) { - $cell = $formatter->formatAndWrap($cell, $this->columnMaxWidths[$column] * $colspan); + $minWrappedWidth = 0; + $widthApplied = []; + $lengthColumnBorder = $this->getColumnSeparatorWidth() + Helper::width($this->style->getCellRowContentFormat()) - 2; + for ($i = $column; $i < ($column + $colspan); $i++) { + if (isset($this->columnMaxWidths[$i])) { + $minWrappedWidth += $this->columnMaxWidths[$i]; + $widthApplied[] = ['type' => 'max', 'column' => $i]; + } else if (($this->columnWidths[$i] ?? 0) > 0 && $colspan > 1) { + $minWrappedWidth += $this->columnWidths[$i]; + $widthApplied[] = ['type' => 'min', 'column' => $i]; + } + } + if (count($widthApplied) === 1) { + if ($colspan > 1) { + $minWrappedWidth = $minWrappedWidth * $colspan; // previous logic + } + } else if (count($widthApplied) > 1) { + $minWrappedWidth += (count($widthApplied) - 1) * $lengthColumnBorder; + } + + $cellWidth = Helper::width(Helper::removeDecoration($formatter, $cell)); + if ($minWrappedWidth && $cellWidth > $minWrappedWidth) { + $cell = $formatter->formatAndWrap($cell, $minWrappedWidth); + } + // update minimal columnWidths for spanned columns + if ($colspan > 1 && $minWrappedWidth > 0) { + $columnsMinWidthProcessed = []; + $cellWidth = min($cellWidth, $minWrappedWidth); + array_filter($widthApplied, function ($item) use (&$cellWidth, &$columnsMinWidthProcessed, $lengthColumnBorder) { + if ($item['type'] === 'max' && $cellWidth >= $this->columnMaxWidths[$item['column']]) { + $minWidthColumn = $this->columnMaxWidths[$item['column']]; + $this->columnWidths[$item['column']] = $minWidthColumn; + $columnsMinWidthProcessed[] = $item['column']; + $cellWidth -= $minWidthColumn + $lengthColumnBorder; + } + }); + for ($i = $column; $i < ($column + $colspan); $i++) { + if (in_array($i, $columnsMinWidthProcessed)) { + continue; + } + $this->columnWidths[$i] = $cellWidth + $lengthColumnBorder; + } } if (!str_contains($cell ?? '', "\n")) { continue; From a4b358f986b48a681fb08895447cd6da3e75ba10 Mon Sep 17 00:00:00 2001 From: Vladimir Valikayev Date: Wed, 26 Mar 2025 15:56:22 +0700 Subject: [PATCH 2/4] issues/60043: Table counts wrong column width when use colspan and setColumnMaxWidth(). --- src/Symfony/Component/Console/Helper/Table.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 9ac986f75b173..4f05f75e9f2cc 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -660,14 +660,14 @@ private function buildTableRows(array $rows): TableRows if ($colspan > 1 && $minWrappedWidth > 0) { $columnsMinWidthProcessed = []; $cellWidth = min($cellWidth, $minWrappedWidth); - array_filter($widthApplied, function ($item) use (&$cellWidth, &$columnsMinWidthProcessed, $lengthColumnBorder) { + foreach ($widthApplied as $item) { if ($item['type'] === 'max' && $cellWidth >= $this->columnMaxWidths[$item['column']]) { $minWidthColumn = $this->columnMaxWidths[$item['column']]; $this->columnWidths[$item['column']] = $minWidthColumn; $columnsMinWidthProcessed[] = $item['column']; $cellWidth -= $minWidthColumn + $lengthColumnBorder; } - }); + } for ($i = $column; $i < ($column + $colspan); $i++) { if (in_array($i, $columnsMinWidthProcessed)) { continue; From 6e298911281c330f5266b36f54116e08714db603 Mon Sep 17 00:00:00 2001 From: Vladimir Valikayev Date: Wed, 26 Mar 2025 16:08:05 +0700 Subject: [PATCH 3/4] issues/60043: Fix unit tests. --- .../Console/Tests/Helper/TableTest.php | 76 +++++++++---------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index 608d23c210bef..d25b66ccd180b 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -1576,17 +1576,17 @@ public function testWithColspanAndMaxWith() $expected = << [ << [ << [ << Date: Wed, 26 Mar 2025 16:17:47 +0700 Subject: [PATCH 4/4] issues/60043: Fix unit tests. --- .../Console/Tests/Helper/TableTest.php | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index d25b66ccd180b..065bf54be0e6e 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -1845,17 +1845,17 @@ public static function provideRenderVerticalTests(): \Traversable yield 'Borderless style' => [ << [ << [ <<