From 98ba85dcd60ed357ff78f9d4dac87153350ddd42 Mon Sep 17 00:00:00 2001 From: Jaapio Date: Wed, 4 Sep 2024 21:47:23 +0200 Subject: [PATCH] [BUGFIX] do not trim per line Table cells may contain multiple lines as we did trim the cell content before the table parsing was completed we did mis empty lines. As empty lines where missing we did break the parsing process of a table cell. (cherry picked from commit 5e6d683e44f4c72ff0efcd6a149bf297d2ba4520) --- .../Productions/Table/GridTableBuilder.php | 25 +++++++++++++++++++ .../Parser/Productions/GridTableRuleTest.php | 4 +-- .../guides/src/Nodes/Table/TableColumn.php | 2 +- packages/guides/src/Nodes/Table/TableRow.php | 6 +++-- .../grid-table-with-list/expected/index.html | 14 +++++++---- .../tables/grid-table-with-list/input/skip | 0 6 files changed, 41 insertions(+), 10 deletions(-) delete mode 100644 tests/Integration/tests/tables/grid-table-with-list/input/skip diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/Table/GridTableBuilder.php b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/Table/GridTableBuilder.php index 91195dc30..8a17cd9ed 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/Table/GridTableBuilder.php +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/Table/GridTableBuilder.php @@ -24,6 +24,7 @@ use phpDocumentor\Guides\RestructuredText\Parser\Productions\RuleContainer; use Psr\Log\LoggerInterface; +use function array_map; use function array_reverse; use function count; use function mb_strlen; @@ -45,6 +46,7 @@ protected function compile(ParserContext $context): TableNode { $rows = $this->extractTableRows($context); $rows = $this->concatenateTableRows($rows, $context); + $rows = $this->trimTableCellContents($rows); $headers = $this->extractHeaderRows($rows, $context); return new TableNode($rows, $headers); @@ -407,4 +409,27 @@ private function hasRowSpan(string $line): bool { return preg_match('/\+[-]+\+/', $line) === 1; } + + /** + * @param array $rows + * + * @return array + */ + private function trimTableCellContents(array $rows): array + { + return array_map( + static fn (TableRow $row) => new TableRow( + array_map( + static fn (TableColumn $column) => new TableColumn( + trim($column->getContent()), + $column->getColSpan(), + [], + $column->getRowSpan(), + ), + $row->getColumns(), + ), + ), + $rows, + ); + } } diff --git a/packages/guides-restructured-text/tests/unit/Parser/Productions/GridTableRuleTest.php b/packages/guides-restructured-text/tests/unit/Parser/Productions/GridTableRuleTest.php index 050266f2d..386f866e4 100644 --- a/packages/guides-restructured-text/tests/unit/Parser/Productions/GridTableRuleTest.php +++ b/packages/guides-restructured-text/tests/unit/Parser/Productions/GridTableRuleTest.php @@ -205,7 +205,7 @@ public static function gridTableWithRowSpanProvider(): Generator +===================================+===============+ | description | string | +-----------------------------------+ | -| author | | +| author | test | +-----------------------------------+---------------+ | keywords | string | +-----------------------------------+---------------+ @@ -217,7 +217,7 @@ public static function gridTableWithRowSpanProvider(): Generator $row1 = new TableRow(); $row1->addColumn(self::createColumnNode('description')); - $rowSpan = self::createColumnNode('string'); + $rowSpan = self::createColumnNode("string\n\ntest"); $rowSpan->incrementRowSpan(); $row1->addColumn($rowSpan); diff --git a/packages/guides/src/Nodes/Table/TableColumn.php b/packages/guides/src/Nodes/Table/TableColumn.php index 34afc2fbb..35970b47a 100644 --- a/packages/guides/src/Nodes/Table/TableColumn.php +++ b/packages/guides/src/Nodes/Table/TableColumn.php @@ -54,7 +54,7 @@ public function getRowSpan(): int public function addContent(string $content): void { - $this->content = trim($this->content . $content); + $this->content .= $content; } public function incrementRowSpan(): void diff --git a/packages/guides/src/Nodes/Table/TableRow.php b/packages/guides/src/Nodes/Table/TableRow.php index 81e004484..8cc37bbc6 100644 --- a/packages/guides/src/Nodes/Table/TableRow.php +++ b/packages/guides/src/Nodes/Table/TableRow.php @@ -23,8 +23,10 @@ final class TableRow { - /** @var TableColumn[] */ - private array $columns = []; + /** @param TableColumn[] $columns */ + public function __construct(private array $columns = []) + { + } public function addColumn(TableColumn $tableColumn): void { diff --git a/tests/Integration/tests/tables/grid-table-with-list/expected/index.html b/tests/Integration/tests/tables/grid-table-with-list/expected/index.html index 5ee31a040..fb598182b 100644 --- a/tests/Integration/tests/tables/grid-table-with-list/expected/index.html +++ b/tests/Integration/tests/tables/grid-table-with-list/expected/index.html @@ -6,19 +6,23 @@

table

Paragraphs -

Paragraph 1

-

Paragraph 2

+ +

Paragraph 1

+

Paragraph 2

+ Lists -

See the list

+ +

See the list

  • Item 1
  • Item 2
-

A paragraph

+

A paragraph

+ - \ No newline at end of file + diff --git a/tests/Integration/tests/tables/grid-table-with-list/input/skip b/tests/Integration/tests/tables/grid-table-with-list/input/skip deleted file mode 100644 index e69de29bb..000000000