Skip to content

PHP 8.3.14: New DOMText ownerDocument is null after being added to a document #16967

@ssddanbrown

Description

@ssddanbrown

Description

The following code:

<?php
 
$html = <<<CDATA
<p>World!</p>
CDATA;

$doc = new DOMDocument();
$doc->loadHTML($html);
$children = $doc->childNodes;

$pTag = $doc->getElementsByTagName('p')[0];
$worldTextNode = $pTag->childNodes[0];

$helloTextNode = new DOMText('Hello ');
$worldTextNode->parentNode->insertBefore($helloTextNode, $worldTextNode);

echo is_null($helloTextNode->ownerDocument) ? 'Missing owner doc' : 'Has owner doc';

Resulted in this output:

Missing owner doc

But I expected this output instead:

Has owner doc

Only seems to be apparent in PHP 8.3.14.
Here's the 3v4l view of this: https://3v4l.org/30pdS
This can easily be worked around by adopting the DOMText, or creating it through the target document ($doc->createTextNode('text')).

Not sure if it's actually an issue, or the consequence of a fix in PHP 8.3.14, but just raising if it is an unintended change, since it caught me out and produced an error in my code, so could be a compatibility issue in other cases too.
The same does not occur for DOMElement which leads me to think this could be a bug.

PHP Version

PHP 8.3.14

Operating System

Fedora 41

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