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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2020 The PHP Group
All rights reserved.
Last updated: Wed Feb 26 14:01:28 2020 UTC