Skip to content

Commit 5e6d683

Browse files
committed
[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.
1 parent 28819a4 commit 5e6d683

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/Table/GridTableBuilder.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use phpDocumentor\Guides\RestructuredText\Parser\Productions\RuleContainer;
2525
use Psr\Log\LoggerInterface;
2626

27+
use function array_map;
2728
use function array_reverse;
2829
use function count;
2930
use function mb_strlen;
@@ -45,6 +46,7 @@ protected function compile(ParserContext $context): TableNode
4546
{
4647
$rows = $this->extractTableRows($context);
4748
$rows = $this->concatenateTableRows($rows, $context);
49+
$rows = $this->trimTableCellContents($rows);
4850
$headers = $this->extractHeaderRows($rows, $context);
4951

5052
return new TableNode($rows, $headers);
@@ -407,4 +409,27 @@ private function hasRowSpan(string $line): bool
407409
{
408410
return preg_match('/\+[-]+\+/', $line) === 1;
409411
}
412+
413+
/**
414+
* @param array<int, TableRow> $rows
415+
*
416+
* @return array<int, TableRow>
417+
*/
418+
private function trimTableCellContents(array $rows): array
419+
{
420+
return array_map(
421+
static fn (TableRow $row) => new TableRow(
422+
array_map(
423+
static fn (TableColumn $column) => new TableColumn(
424+
trim($column->getContent()),
425+
$column->getColSpan(),
426+
[],
427+
$column->getRowSpan(),
428+
),
429+
$row->getColumns(),
430+
),
431+
),
432+
$rows,
433+
);
434+
}
410435
}

packages/guides-restructured-text/tests/unit/Parser/Productions/GridTableRuleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public static function gridTableWithRowSpanProvider(): Generator
205205
+===================================+===============+
206206
| description | string |
207207
+-----------------------------------+ |
208-
| author | |
208+
| author | test |
209209
+-----------------------------------+---------------+
210210
| keywords | string |
211211
+-----------------------------------+---------------+
@@ -217,7 +217,7 @@ public static function gridTableWithRowSpanProvider(): Generator
217217

218218
$row1 = new TableRow();
219219
$row1->addColumn(self::createColumnNode('description'));
220-
$rowSpan = self::createColumnNode('string');
220+
$rowSpan = self::createColumnNode("string\n\ntest");
221221
$rowSpan->incrementRowSpan();
222222
$row1->addColumn($rowSpan);
223223

packages/guides/src/Nodes/Table/TableColumn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function getRowSpan(): int
5454

5555
public function addContent(string $content): void
5656
{
57-
$this->content = trim($this->content . $content);
57+
$this->content .= $content;
5858
}
5959

6060
public function incrementRowSpan(): void

packages/guides/src/Nodes/Table/TableRow.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323

2424
final class TableRow
2525
{
26-
/** @var TableColumn[] */
27-
private array $columns = [];
26+
/** @param TableColumn[] $columns */
27+
public function __construct(private array $columns = [])
28+
{
29+
}
2830

2931
public function addColumn(TableColumn $tableColumn): void
3032
{

tests/Integration/tests/tables/grid-table-with-list/expected/index.html

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@ <h1>table</h1>
66
<tbody>
77
<tr>
88
<td><strong>Paragraphs</strong></td>
9-
<td><p>Paragraph 1</p>
10-
<p>Paragraph 2</p></td>
9+
<td>
10+
<p>Paragraph 1</p>
11+
<p>Paragraph 2</p>
12+
</td>
1113
</tr>
1214
<tr>
1315
<td><strong>Lists</strong></td>
14-
<td><p>See the list</p>
16+
<td>
17+
<p>See the list</p>
1518
<ul>
1619
<li>Item 1</li>
1720
<li>Item 2</li>
1821
</ul>
19-
<p>A paragraph</p></td>
22+
<p>A paragraph</p>
23+
</td>
2024
</tr>
2125
</tbody>
2226
</table>
2327
</div>
24-
<!-- content end -->
28+
<!-- content end -->

tests/Integration/tests/tables/grid-table-with-list/input/skip

Whitespace-only changes.

0 commit comments

Comments
 (0)