Skip to content

Incorrect parsing of self closing tr/td #239

@stefanfisk

Description

@stefanfisk

Parsing self closing <tr> and <td> in combination results in the rows being nested instead of siblings.

Here's a minimal test case:

<?php

use Masterminds\HTML5;
use PHPUnit\Framework\TestCase;

class SelfClosingTableTest extends TestCase
{
    public function testHtml5Spec(): void
    {
        $html5 = new HTML5();

        $html = '<table><tr><td>A<tr><td>B</table>';

        $doc = $html5->loadHTMLFragment($html);

        $this->assertSame(
            '<table><tr><td>A</td></tr><tr><td>B</td></tr></table>',
            $html5->saveHTML($doc),
        );
    }
}

Which outputs:

Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'<table><tr><td>A</td></tr><tr><td>B</td></tr></table>'
+'<table><tr><td>A<tr><td>B</td></tr></td></tr></table>'

I first encountered this issue when parsing the HTML spec 😸 https://html.spec.whatwg.org/multipage/indices.html#attributes-1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions