bug #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
-------

ca11772e3f [Console] Table: support cells with newlines after a cell with colspan >= 2
This commit is contained in:
Fabien Potencier 2020-08-11 09:29:54 +02:00
commit 0f92b9a584
2 changed files with 51 additions and 4 deletions

View File

@ -574,6 +574,9 @@ class Table
if (0 === $lineKey) {
$rows[$rowKey][$column] = $line;
} else {
if (!\array_key_exists($rowKey, $unmergedRows) || !\array_key_exists($lineKey, $unmergedRows[$rowKey])) {
$unmergedRows[$rowKey][$lineKey] = $this->copyRow($rows, $rowKey);
}
$unmergedRows[$rowKey][$lineKey][$column] = $line;
}
}
@ -585,8 +588,8 @@ class Table
yield $this->fillCells($row);
if (isset($unmergedRows[$rowKey])) {
foreach ($unmergedRows[$rowKey] as $row) {
yield $row;
foreach ($unmergedRows[$rowKey] as $unmergedRow) {
yield $this->fillCells($unmergedRow);
}
}
}
@ -670,12 +673,17 @@ class Table
private function fillCells($row)
{
$newRow = [];
$newColumn = null;
foreach ($row as $column => $cell) {
$newRow[] = $cell;
if (null === $newColumn) {
$newColumn = $column;
}
$newRow[$newColumn++] = $cell;
if ($cell instanceof TableCell && $cell->getColspan() > 1) {
foreach (range($column + 1, $column + $cell->getColspan() - 1) as $position) {
// insert empty value at column position
$newRow[] = '';
$newRow[$newColumn++] = '';
}
}
}

View File

@ -333,6 +333,45 @@ TABLE
| Cupiditate dicta atque porro, tempora exercitationem modi animi nulla nemo vel nihil! |
+-------------------------------+-------------------------------+-----------------------------+
TABLE
],
'Cell after colspan contains new line break' => [
['Foo', 'Bar', 'Baz'],
[
[
new TableCell("foo\nbar", ['colspan' => 2]),
"baz\nqux",
],
],
'default',
<<<'TABLE'
+-----+-----+-----+
| Foo | Bar | Baz |
+-----+-----+-----+
| foo | baz |
| bar | qux |
+-----+-----+-----+
TABLE
],
'Cell after colspan contains multiple new lines' => [
['Foo', 'Bar', 'Baz'],
[
[
new TableCell("foo\nbar", ['colspan' => 2]),
"baz\nqux\nquux",
],
],
'default',
<<<'TABLE'
+-----+-----+------+
| Foo | Bar | Baz |
+-----+-----+------+
| foo | baz |
| bar | qux |
| | quux |
+-----+-----+------+
TABLE
],
'Cell with rowspan' => [