Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/guide/converters.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ $djot = $converter->convert($html);
| `<figure>` + `<img>` + `<figcaption>` | Image with `^ caption` |
| `<figure>` + `<blockquote>` + `<figcaption>` | Block quote with `^ caption` |

Djot can only spell a leading header row, so a `<th>` elsewhere imports as a data cell.

**File Operations:**

```php
Expand Down
12 changes: 5 additions & 7 deletions src/Converter/HtmlToDjot.php
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,6 @@ protected function processTable(DOMElement $node): string
{
$rows = [];
$headerRow = null;
$headerRowAttrs = '';
$columnCount = 0;
$captionText = '';
$alignments = [];
Expand All @@ -1606,9 +1605,9 @@ protected function processTable(DOMElement $node): string
// Find all rows
$trElements = $this->getDirectTableRows($node);

foreach ($trElements as $tr) {
foreach ($trElements as $rowIndex => $tr) {
$cells = [];
$isHeader = false;
$allCellsAreHeaders = true;

$columnIndex = 0;
foreach ($tr->childNodes as $cell) {
Expand All @@ -1624,8 +1623,8 @@ protected function processTable(DOMElement $node): string
} else {
$cells[] = $cellContent;
}
if ($tag === 'th') {
$isHeader = true;
if ($tag !== 'th') {
$allCellsAreHeaders = false;
}
if (!isset($alignments[$columnIndex])) {
$alignments[$columnIndex] = $this->extractTableCellAlignment($cell);
Expand All @@ -1644,9 +1643,8 @@ protected function processTable(DOMElement $node): string

$row = '| ' . implode(' | ', $cells) . ' |' . $rowAttrSuffix;

if ($isHeader && $headerRow === null) {
if ($rowIndex === 0 && $allCellsAreHeaders) {
$headerRow = $row;
$headerRowAttrs = $rowAttrSuffix;
} else {
$rows[] = $row;
}
Expand Down
47 changes: 47 additions & 0 deletions tests/TestCase/Converter/HtmlToDjotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,53 @@ public function testTable(): void
$this->assertStringContainsString('| Alice | 30 |', $result);
}

public function testTableHeaderCellInThirdRowDoesNotReorderRows(): void
{
$html = '<table><tr><td>a</td></tr><tr><td>b</td></tr><tr><th>H</th></tr></table>';

$djot = $this->converter->convert($html);

$this->assertSame("| a |\n| b |\n| H |\n", $djot);
$this->assertSame(
"<table>\n<tr>\n<td>a</td>\n</tr>\n<tr>\n<td>b</td>\n</tr>\n<tr>\n<td>H</td>\n</tr>\n</table>\n",
(new DjotConverter())->convert($djot),
);
}

public function testTableMixedFirstRowImportsAsDataCells(): void
{
$html = '<table><tr><th>R</th><td>1</td></tr></table>';

$djot = $this->converter->convert($html);

$this->assertSame("| R | 1 |\n", $djot);
$this->assertSame(
"<table>\n<tr>\n<td>R</td>\n<td>1</td>\n</tr>\n</table>\n",
(new DjotConverter())->convert($djot),
);
}

public function testTableLeadingAllHeaderRowImportsAsHeader(): void
{
$html = '<table><tr><th>A</th><th>B</th></tr><tr><td>1</td><td>2</td></tr></table>';

$this->assertSame("| A | B |\n|---|---|\n| 1 | 2 |\n", $this->converter->convert($html));
}

public function testTableSectionsPreserveLeadingHeaderAndBodyRows(): void
{
$html = '<table><thead><tr><th>A</th></tr></thead><tbody><tr><td>1</td></tr></tbody></table>';

$this->assertSame("| A |\n|---|\n| 1 |\n", $this->converter->convert($html));
}

public function testTableWithoutHeaderCellsHasNoSeparator(): void
{
$html = '<table><tr><td>a</td></tr><tr><td>b</td></tr></table>';

$this->assertSame("| a |\n| b |\n", $this->converter->convert($html));
}

public function testNestedTableDoesNotLeakInnerRowsIntoOuterTable(): void
{
$html = '<table><tr><td>outer <table><tr><td>inner</td></tr></table></td></tr></table>';
Expand Down
Loading