Skip to content

DOMElement::replaceWith causes crash #11290

Closed
@fluffycondor

Description

@fluffycondor

Description

This is a longstanding one (sorry for the php-dom day!). Sometimes the same code from #11288 causes crash on every 8.x PHP version. The code is the same, but the html markup is slightly differs.

Live reproducing:
https://3v4l.org/KjJgQ // On every PHP 8.x version throws DOMException: Not Found Error, hangs for 1-15 seconds, then crashes.
https://3v4l.org/FdOZR // Hangs for 1-15 seconds, crashes on every PHP versions but PHP 8.2.6 and 8.1.19.

It's kinda related to #11289 and #11288, but this is a longstanding one. I guess this is a part of a big picture, so I hope this would help to investigate the main cause of weird php-dom behavior.

The following code:

<?php
$html = <<<HTML
<!DOCTYPE HTML>
<html>
<body>
    <p><span class="unwrap_me">Lorem</span><span class="unwrap_me">ipsum</span><span class="unwrap_me">dolor</span></p>
</body>
</html>
HTML;

$dom = new DOMDocument();
$dom->loadHTML($html);

$spans = iterator_to_array($dom->getElementsByTagName('span')->getIterator());
foreach ($spans as $span) {
    if ('unwrap_me' === $span->getAttribute('class')) {
        $span->replaceWith(...$span->childNodes);
    }
}

results in crash on every PHP 8.x version.

PHP Version

PHP 8.x

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions