php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #53890 DOMDocumentFragment and AppendChild
Submitted: 2011-01-31 12:21 UTC Modified: 2011-02-11 14:19 UTC
From: giosh94mhz at gmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: giosh94mhz at gmail dot com
New email:
PHP Version: OS:

 

 [2011-01-31 12:21 UTC] giosh94mhz at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/class.domdocumentfragment
---
Undocumented behaviour for appendChild using a DOMDocumentFragment


Test script:
---------------
$doc=new DOMDocument();
$frag=$doc->createDocumentFragment();

$root=$doc->appendChild( $doc->createElement('root') );

$frag->appendChild( $doc->createElement('fragNode') );

echo $frag->childNodes->length;

$root->appendChild($frag);

echo $frag->childNodes->length;




Expected result:
----------------
Documented behaviour.

Actual result:
--------------
1
0

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-10 13:09 UTC] vrana@php.net
-Status: Open +Status: Bogus
 [2011-02-10 13:09 UTC] vrana@php.net
Works as expected. The appendChild() method actually moves the node if it is already somewhere else.
 [2011-02-11 14:19 UTC] giosh94mhz at gmail dot com
Correct.
I know that appending a node to another node change parentNode reference.

Anyway, DOMDocumentFragment is a "special" node; using

 $parent->appendChild($fragment);

doesn't change the parent of fragment, but the parent of all the child nodes, so it is equivalent to:

foreach( $fragment->childNodes as $child )
     $parent->appendChild( $child );

So, "$fragment->childNodes->length" change after appending the fragment. This may seem obvious, but note that a DOMDocumentFragment is an instance of DOMNode: the user must be informed of that "unexpected" behaviour.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 22 04:01:28 2024 UTC