Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Couldn't fetch DOMElement introduced in 8.2.6, 8.1.19 #11288

Closed
fluffycondor opened this issue May 22, 2023 · 1 comment · Fixed by ThePHPF/thephp.foundation#90
Closed

Comments

@fluffycondor
Copy link

Description

Live reproduce: https://3v4l.org/iPl83

The following code:

<?php
$html = <<<HTML
<!DOCTYPE HTML>
<html>
<span class="unwrap_me"><i><span class="foo">Lorem</span></i><span class="foo">ipsum</span></span>
</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')) {
        $fragment = $dom->createDocumentFragment();
        $fragment->append(...$span->childNodes);
        $span->parentNode?->replaceChild($fragment, $span);
    }
}

worked successfully as for 8.2.5, 8.1.18 and earlier.
But as for 8.2.6 and 8.1.19 it throws Error: Couldn't fetch DOMElement because one of DOMElement's state is corrupted:

object(DOMElement)#142 (1) {
  ["schemaTypeInfo"]=>
  NULL
}
DOMElement Object
(
    [schemaTypeInfo] =>
)

PHP Version

PHP 8.2.6, 8.1.19

Operating System

FreeBSD 12.4, Windows 10 x64

@fluffycondor
Copy link
Author

Same with DOMElement::replaceWith without using DOMDocumentFragment: https://3v4l.org/YogrE

nielsdos added a commit to nielsdos/php-src that referenced this issue May 22, 2023
This replaces the implementation of before and after with one following
the spec very strictly, instead of trying to figure out the state we're
in by looking at the pointers. Also relaxes the condition on text node
copying to prevent working on a stale node pointer.
nielsdos added a commit to nielsdos/php-src that referenced this issue May 22, 2023
…placeWith

This replaces the implementation of before and after with one following
the spec very strictly, instead of trying to figure out the state we're
in by looking at the pointers. Also relaxes the condition on text node
copying to prevent working on a stale node pointer.
nielsdos added a commit to nielsdos/php-src that referenced this issue May 22, 2023
…ception with replaceWith

This replaces the implementation of before and after with one following
the spec very strictly, instead of trying to figure out the state we're
in by looking at the pointers. Also relaxes the condition on text node
copying to prevent working on a stale node pointer.
nielsdos added a commit that referenced this issue May 25, 2023
* PHP-8.1:
  Fix GH-11288 and GH-11289 and GH-11290 and GH-9142: DOMExceptions and segfaults with replaceWith
nielsdos added a commit that referenced this issue May 25, 2023
* PHP-8.2:
  Fix GH-11288 and GH-11289 and GH-11290 and GH-9142: DOMExceptions and segfaults with replaceWith
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants