Skip to content

Commit d3fce38

Browse files
committed
Merge branch '2.3' into 2.4
* 2.3: fix some cs use restore_error_handler instead of set_error_handler($previous) fix #9321 Crawler::addHtmlContent add gbk encoding support [Console] fixed column width when using the Table helper with some decoration in cells [Security] Fixed problem with losing ROLE_PREVIOUS_ADMIN role. Fix for cache-key conflict when having a \Traversable as choices [Security] removed obsolete comment Conflicts: src/Symfony/Component/Console/Helper/TableHelper.php src/Symfony/Component/Security/Tests/Http/Firewall/ExceptionListenerTest.php
2 parents 4f7c45b + 941075d commit d3fce38

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

Helper/TableHelper.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ private function renderCell(array $row, $column, $cellFormat)
427427
$width += strlen($cell) - mb_strlen($cell, $encoding);
428428
}
429429

430+
$width += $this->strlen($cell) - $this->computeLengthWithoutDecoration($cell);
431+
430432
$content = sprintf($this->cellRowContentFormat, $cell);
431433

432434
$this->output->write(sprintf($cellFormat, str_pad($content, $width, $this->paddingChar, $this->padType)));
@@ -484,15 +486,7 @@ private function getColumnWidth($column)
484486
*/
485487
private function getCellWidth(array $row, $column)
486488
{
487-
if ($column < 0) {
488-
return 0;
489-
}
490-
491-
if (isset($row[$column])) {
492-
return $this->strlen($row[$column]);
493-
}
494-
495-
return $this->getCellWidth($row, $column - 1);
489+
return isset($row[$column]) ? $this->computeLengthWithoutDecoration($row[$column]) : 0;
496490
}
497491

498492
/**
@@ -504,6 +498,18 @@ private function cleanup()
504498
$this->numberOfColumns = null;
505499
}
506500

501+
private function computeLengthWithoutDecoration($string)
502+
{
503+
$formatter = $this->output->getFormatter();
504+
$isDecorated = $formatter->isDecorated();
505+
$formatter->setDecorated(false);
506+
507+
$string = $formatter->format($string);
508+
$formatter->setDecorated($isDecorated);
509+
510+
return $this->strlen($string);
511+
}
512+
507513
/**
508514
* {@inheritDoc}
509515
*/

Tests/Helper/TableHelperTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,40 @@ public function testRenderProvider()
216216
TableHelper::LAYOUT_DEFAULT,
217217
'',
218218
),
219+
'Cell text with tags used for Output styling' => array(
220+
array('ISBN', 'Title', 'Author'),
221+
array(
222+
array('<info>99921-58-10-7</info>', '<error>Divine Comedy</error>', '<fg=blue;bg=white>Dante Alighieri</fg=blue;bg=white>'),
223+
array('9971-5-0210-0', 'A Tale of Two Cities', '<info>Charles Dickens</>'),
224+
),
225+
TableHelper::LAYOUT_DEFAULT,
226+
<<<TABLE
227+
+---------------+----------------------+-----------------+
228+
| ISBN | Title | Author |
229+
+---------------+----------------------+-----------------+
230+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
231+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
232+
+---------------+----------------------+-----------------+
233+
234+
TABLE
235+
),
236+
'Cell text with tags not used for Output styling' => array(
237+
array('ISBN', 'Title', 'Author'),
238+
array(
239+
array('<strong>99921-58-10-700</strong>', '<f>Divine Com</f>', 'Dante Alighieri'),
240+
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
241+
),
242+
TableHelper::LAYOUT_DEFAULT,
243+
<<<TABLE
244+
+----------------------------------+----------------------+-----------------+
245+
| ISBN | Title | Author |
246+
+----------------------------------+----------------------+-----------------+
247+
| <strong>99921-58-10-700</strong> | <f>Divine Com</f> | Dante Alighieri |
248+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
249+
+----------------------------------+----------------------+-----------------+
250+
251+
TABLE
252+
),
219253
);
220254
}
221255

0 commit comments

Comments
 (0)