Skip to content

Commit 0f92b9a

Browse files
committed
bug symfony#37731 [Console] Table: support cells with newlines after a cell with colspan >= 2 (GMTA)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Console] Table: support cells with newlines after a cell with colspan >= 2 | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A When rendering a table with a cell containing newlines after a cell with colspan set to at least 2, every line in the cell with newlines except the first one fails to render. This case is fixed by calling `->fillCells()` on the unmerged rows and implementing support for rows that start with a non-zero index for the columns. While fixing this, I discovered another issue with colspan: if a cell following a colspanned cell contains enough newlines to make the contents extend further than the colspanned cell's contents, the cells become misaligned. This is now also fixed. Commits ------- ca11772 [Console] Table: support cells with newlines after a cell with colspan >= 2
2 parents b2e99e2 + ca11772 commit 0f92b9a

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/Symfony/Component/Console/Helper/Table.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ private function buildTableRows(array $rows): TableRows
574574
if (0 === $lineKey) {
575575
$rows[$rowKey][$column] = $line;
576576
} else {
577+
if (!\array_key_exists($rowKey, $unmergedRows) || !\array_key_exists($lineKey, $unmergedRows[$rowKey])) {
578+
$unmergedRows[$rowKey][$lineKey] = $this->copyRow($rows, $rowKey);
579+
}
577580
$unmergedRows[$rowKey][$lineKey][$column] = $line;
578581
}
579582
}
@@ -585,8 +588,8 @@ private function buildTableRows(array $rows): TableRows
585588
yield $this->fillCells($row);
586589

587590
if (isset($unmergedRows[$rowKey])) {
588-
foreach ($unmergedRows[$rowKey] as $row) {
589-
yield $row;
591+
foreach ($unmergedRows[$rowKey] as $unmergedRow) {
592+
yield $this->fillCells($unmergedRow);
590593
}
591594
}
592595
}
@@ -670,12 +673,17 @@ private function fillNextRows(array $rows, int $line): array
670673
private function fillCells($row)
671674
{
672675
$newRow = [];
676+
677+
$newColumn = null;
673678
foreach ($row as $column => $cell) {
674-
$newRow[] = $cell;
679+
if (null === $newColumn) {
680+
$newColumn = $column;
681+
}
682+
$newRow[$newColumn++] = $cell;
675683
if ($cell instanceof TableCell && $cell->getColspan() > 1) {
676684
foreach (range($column + 1, $column + $cell->getColspan() - 1) as $position) {
677685
// insert empty value at column position
678-
$newRow[] = '';
686+
$newRow[$newColumn++] = '';
679687
}
680688
}
681689
}

src/Symfony/Component/Console/Tests/Helper/TableTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,45 @@ public function renderProvider()
333333
| Cupiditate dicta atque porro, tempora exercitationem modi animi nulla nemo vel nihil! |
334334
+-------------------------------+-------------------------------+-----------------------------+
335335

336+
TABLE
337+
],
338+
'Cell after colspan contains new line break' => [
339+
['Foo', 'Bar', 'Baz'],
340+
[
341+
[
342+
new TableCell("foo\nbar", ['colspan' => 2]),
343+
"baz\nqux",
344+
],
345+
],
346+
'default',
347+
<<<'TABLE'
348+
+-----+-----+-----+
349+
| Foo | Bar | Baz |
350+
+-----+-----+-----+
351+
| foo | baz |
352+
| bar | qux |
353+
+-----+-----+-----+
354+
355+
TABLE
356+
],
357+
'Cell after colspan contains multiple new lines' => [
358+
['Foo', 'Bar', 'Baz'],
359+
[
360+
[
361+
new TableCell("foo\nbar", ['colspan' => 2]),
362+
"baz\nqux\nquux",
363+
],
364+
],
365+
'default',
366+
<<<'TABLE'
367+
+-----+-----+------+
368+
| Foo | Bar | Baz |
369+
+-----+-----+------+
370+
| foo | baz |
371+
| bar | qux |
372+
| | quux |
373+
+-----+-----+------+
374+
336375
TABLE
337376
],
338377
'Cell with rowspan' => [

0 commit comments

Comments
 (0)