Skip to content

Consecutive calls to DOMNode::before and DOMNode::remove unlink wrong nodes #11682

Closed
@theseer

Description

@theseer

Description

The following code:

<?php declare(strict_types = 1);

$dom = new DOMDocument;
$dom->loadXML('<?xml version="1.0" ?><root><node /></root>');

$list = [
    $dom->createElement('foo1'),
    $dom->createElement('foo2'),
    $dom->createElement('foo3')
];

$node = $dom->getElementsByTagName('node')->item(0);

$node->before(...$list);
$node->remove();

echo $dom->saveXML();

Resulted in this output:

<?xml version="1.0"?>
<root><foo1/></root>

But I expected this output instead:

<?xml version="1.0"?>
<root><foo1/><foo2/><foo3/></root>

Observations:

  • When the $node->before() call gets replaced by $node->after(), the code works as expected.
  • When the $node->remove() call is commented out, the result is as expected, apparently avoid the bug , as the below output shows
  • When PHP 8.0.29 is used, the code works as expected, regardless whether ::before or ::after is used
  • For the record: PHP 7.4 doesn't implement ::before, so it cannot be used

Ouput with ::before and no ::remove:

<?xml version="1.0"?>
<root><foo1/><foo2/><foo3/><node/></root>

Ouput with ::after and no ::remove:

<?xml version="1.0"?>
<root><node/><foo1/><foo2/><foo3/></root>

PHP Version

PHP 8.2.8 / PHP 8.1.21 / PHP 8.3.0-alpha3

Operating System

Fedora Linux 38 x86_64

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