#42282 [Bgs]: appendChild breaks loop when copying imported tree
| From: | TomTrnka at seznam dot cz | Date: | Tue, 14 Aug 2007 12:32:11 +0000 |
| Subject: | #42282 [Bgs]: appendChild breaks loop when copying imported tree | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-116070@lists.php.net to get a copy of this message | ||
ID: 42282
User updated by: TomTrnka at seznam dot cz
Reported By: TomTrnka at seznam dot cz
Status: Bogus
Bug Type: DOM XML related
Operating System: Linux
PHP Version: 5CVS-2007-08-13 (snap)
New Comment:
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...
Previous Comments:
------------------------------------------------------------------------
[2007-08-14 12:04:28] 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-13 09:44:05] 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.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=42282&edit=1