php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42282 appendChild breaks loop when copying imported tree
Submitted: 2007-08-13 09:44 UTC Modified: 2007-08-14 12:32 UTC
From: TomTrnka at seznam dot cz Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5CVS-2007-08-13 (snap) OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: TomTrnka at seznam dot cz
New email:
PHP Version: OS:

 

 [2007-08-13 09:44 UTC] TomTrnka at seznam dot cz
Description:
------------
When a node tree is copied from one DOMDocument to another using 
recursive importNode on the tree root and then copying the 
childNodes in a loop (doesn't matter whether for or foreach) using 
e.g. appendChild, the loop is no more repeated after copying the 
first element, that means, other child elements do not get copied.
Additionally, the source childNodes->length gets decremented.

The opposite approach - iterating through the source elements and 
importing&appending one at a time works flawlessly.

On commenting out the appendChild call the iteration goes normally, 
too.

Reproduce code:
---------------
<?php
error_reporting(E_ALL | E_STRICT);
$srcdoc = new DOMDocument("1.0", "UTF-8");
$src = $srcdoc->appendChild($srcdoc->createElement("srcroot"));
$src->appendChild($srcdoc->createElement("elem1", "1"));
$src->appendChild($srcdoc->createElement("elem2", "2"));

echo $srcdoc->saveXML();

$dstdoc = new DOMDocument("1.0", "UTF-8");
$dst = $dstdoc->appendChild($dstdoc->createElement("dstroot"));


$tmp = $dstdoc->importNode($src, TRUE);
echo $tmp->childNodes->length;

foreach ($tmp->childNodes as $child) {
	echo ".";
	$dst->appendChild($child);
}
echo "\n";

echo $dstdoc->saveXML();
?>

Expected result:
----------------
The output should look like this (both elements copied):

<?xml version="1.0" encoding="UTF-8"?>
<srcroot><elem1>1</elem1><elem2>2</elem2></srcroot>
2..
<?xml version="1.0" encoding="UTF-8"?>
<dstroot><elem1>1</elem1><elem2>2</elem2></dstroot>


Actual result:
--------------
The output looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<srcroot><elem1>1</elem1><elem2>2</elem2></srcroot>
2.
<?xml version="1.0" encoding="UTF-8"?>
<dstroot><elem1>1</elem1></dstroot>

That means the elem2 did not get copied. There is also only one dot 
at the "2." row, meaning the second iteration of the loop hasn't 
even started.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-14 12:04 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

nodelists are live and updated as nodes are removed from them
 [2007-08-14 12:32 UTC] TomTrnka at seznam dot cz
Well, that's nice, but why the hell does any change to the nodelist 
break the loop? That doesn't seem like "acceptable behaviour" to 
me...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 14:01:28 2024 UTC