Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b93bcc1

Browse filesBrowse files
jaytaphaitboudad
authored andcommitted
Fixed colspan issues with multiple render() calls
1 parent 5f36605 commit b93bcc1
Copy full SHA for b93bcc1

File tree

Expand file treeCollapse file tree

2 files changed

+52
-24
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+52
-24
lines changed
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Helper/Table.php
+20-19Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -205,24 +205,26 @@ public function setRow($column, array $row)
205205
public function render()
206206
{
207207
$this->calculateNumberOfColumns();
208-
$this->rows = $this->buildTableRows($this->rows);
209-
$this->headers = $this->buildTableRows($this->headers);
208+
$rows = $this->buildTableRows($this->rows);
209+
$headers = $this->buildTableRows($this->headers);
210+
211+
$this->calculateColumnsWidth(array_merge($headers, $rows));
210212

211213
$this->renderRowSeparator();
212-
if (!empty($this->headers)) {
213-
foreach ($this->headers as $header) {
214+
if (!empty($headers)) {
215+
foreach ($headers as $header) {
214216
$this->renderRow($header, $this->style->getCellHeaderFormat());
215217
$this->renderRowSeparator();
216218
}
217219
}
218-
foreach ($this->rows as $row) {
220+
foreach ($rows as $row) {
219221
if ($row instanceof TableSeparator) {
220222
$this->renderRowSeparator();
221223
} else {
222224
$this->renderRow($row, $this->style->getCellRowFormat());
223225
}
224226
}
225-
if (!empty($this->rows)) {
227+
if (!empty($rows)) {
226228
$this->renderRowSeparator();
227229
}
228230

@@ -246,7 +248,7 @@ private function renderRowSeparator()
246248

247249
$markup = $this->style->getCrossingChar();
248250
for ($column = 0; $column < $count; $column++) {
249-
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->getColumnWidth($column)).$this->style->getCrossingChar();
251+
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->columnWidths[$column]).$this->style->getCrossingChar();
250252
}
251253

252254
$this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
@@ -292,11 +294,11 @@ private function renderRow(array $row, $cellFormat)
292294
private function renderCell(array $row, $column, $cellFormat)
293295
{
294296
$cell = isset($row[$column]) ? $row[$column] : '';
295-
$width = $this->getColumnWidth($column);
297+
$width = $this->columnWidths[$column];
296298
if ($cell instanceof TableCell && $cell->getColspan() > 1) {
297299
// add the width of the following columns(numbers of colspan).
298300
foreach (range($column + 1, $column + $cell->getColspan() - 1) as $nextColumn) {
299-
$width += $this->getColumnSeparatorWidth() + $this->getColumnWidth($nextColumn);
301+
$width += $this->getColumnSeparatorWidth() + $this->columnWidths[$nextColumn];
300302
}
301303
}
302304

@@ -509,21 +511,20 @@ private function getRowColumns($row)
509511
*
510512
* @return int
511513
*/
512-
private function getColumnWidth($column)
514+
private function calculateColumnsWidth($rows)
513515
{
514-
if (isset($this->columnWidths[$column])) {
515-
return $this->columnWidths[$column];
516-
}
516+
for ($column = 0; $column < $this->numberOfColumns; $column++) {
517+
$lengths = array();
518+
foreach ($rows as $row) {
519+
if ($row instanceof TableSeparator) {
520+
continue;
521+
}
517522

518-
foreach (array_merge($this->headers, $this->rows) as $row) {
519-
if ($row instanceof TableSeparator) {
520-
continue;
523+
$lengths[] = $this->getCellWidth($row, $column);
521524
}
522525

523-
$lengths[] = $this->getCellWidth($row, $column);
526+
$this->columnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2;
524527
}
525-
526-
return $this->columnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2;
527528
}
528529

529530
/**
Collapse file

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Helper/TableTest.php
+32-5Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ public function testRenderProvider()
180180
array(
181181
array('ISBN', 'Title', 'Author'),
182182
array(
183-
array("99921-58-10-7", "Divine\nComedy", "Dante Alighieri"),
184-
array("9971-5-0210-2", "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
185-
array("9971-5-0210-2", "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
186-
array("960-425-059-0", "The Lord of the Rings", "J. R. R.\nTolkien"),
183+
array('99921-58-10-7', "Divine\nComedy", 'Dante Alighieri'),
184+
array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
185+
array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
186+
array('960-425-059-0', 'The Lord of the Rings', "J. R. R.\nTolkien"),
187187
),
188188
'default',
189189
<<<TABLE
@@ -427,7 +427,7 @@ public function testRenderProvider()
427427
array('ISBN', 'Author'),
428428
array(
429429
array(
430-
new TableCell("9971-5-0210-0", array('rowspan' => 3, 'colspan' => 1)),
430+
new TableCell('9971-5-0210-0', array('rowspan' => 3, 'colspan' => 1)),
431431
'Dante Alighieri',
432432
),
433433
array(new TableSeparator()),
@@ -547,6 +547,33 @@ public function testRowSeparator()
547547
| Bar3 |
548548
+------+
549549
550+
TABLE;
551+
552+
$this->assertEquals($expected, $this->getOutputContent($output));
553+
}
554+
555+
public function testRenderMultiCalls()
556+
{
557+
$table = new Table($output = $this->getOutputStream());
558+
$table->setRows(array(
559+
array(new TableCell('foo', array('colspan' => 2))),
560+
));
561+
$table->render();
562+
$table->render();
563+
$table->render();
564+
565+
$expected =
566+
<<<TABLE
567+
+---+--+
568+
| foo |
569+
+---+--+
570+
+---+--+
571+
| foo |
572+
+---+--+
573+
+---+--+
574+
| foo |
575+
+---+--+
576+
550577
TABLE;
551578

552579
$this->assertEquals($expected, $this->getOutputContent($output));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.