note 108600 deleted from domnode.removechild by salathe
| From: | salathe@php.net | Date: | Thu, 10 May 2012 08:13:12 +0000 |
| Subject: | note 108600 deleted from domnode.removechild by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187564@lists.php.net to get a copy of this message | ||
Note Submitter: Bernt
----
I needed to replace an element node with its children nodes. Had to figure out how to loop over a
list I was destroying and insert at the right place. Also found out that insertBefore with null as
second argument behaves same as without second argument, same as appendChild, which made code
simpler.
<?php
// $node is the node to collapse
// remember where to move children before we start changing parents link chain list
$ref = $node->nextSibling;
// loop over children by using the link chain, take care to use the next that existed before the
move
for ($child = $node->firstChild;
$child !== null;
$child = $next) {
// remember the next sibling before we move child
$next = $child->nextSibling;
// move the child to before $ref in parent
$node->parentNode->insertBefore($child, $ref);
}
// and finally remove node itself
$node->parentNode->removeChild($node);
?>
Hope this helps some one else how think when manipulating DOM trees.