Skip to content

Consecutive self-closing tags are parsed as children instead of siblings with \Dom\HTMLDocument  #16960

@brendt

Description

@brendt

Description

The following code:

<?php

$html = <<<HTML
<b>
    <c>
        Hello
    </c>
    <d />
    <e />
</b>
HTML;

$dom = HTMLDocument::createFromString($html);

$b = $dom->getElementsByTagName('b')[0];
$childrenOfB = $b->childNodes;

var_dump($childrenOfB[1]->tagName); // C
var_dump($childrenOfB[3]->tagName); // D
var_dump($childrenOfB[5]->tagName); // null, this should be E
var_dump($childrenOfB[3]->childNodes[1]->tagName); // E is somehow a child of D

Resulted in this output: E is a child of D, while it should be a sibling of D and a child of B

Changing the input to the following, does generate the correct result:

$html = <<<HTML
<b>
    <c>
        Hello
    </c>
    <d></d>
    <e></e>
</b>
HTML;

PHP Version

PHP 8.4.1

Operating System

Mac 15.0.1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions