note 108600 added to domnode.removechild
| From: | Bernt | Date: | Wed, 09 May 2012 18:26:47 +0000 |
| Subject: | note 108600 added to domnode.removechild | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-187556@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 212.63.222.76
Probable Submitter: 79.136.92.250
----
Manual Page -- http://www.php.net/manual/en/domnode.removechild.php
Edit -- https://master.php.net/note/edit/108600
Del: integrated -- https://master.php.net/note/delete/108600/integrated
Del: useless -- https://master.php.net/note/delete/108600/useless
Del: bad code -- https://master.php.net/note/delete/108600/bad+code
Del: spam -- https://master.php.net/note/delete/108600/spam
Del: non-english -- https://master.php.net/note/delete/108600/non-english
Del: in docs -- https://master.php.net/note/delete/108600/in+docs
Del: other reasons-- https://master.php.net/note/delete/108600
Reject -- https://master.php.net/note/reject/108600
Search -- https://master.php.net/manage/user-notes.php