Skip to content

Fix #77686: Immediately remove ID from DOMDocument when removing child element #6936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions ext/dom/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,33 @@ static xmlNodePtr _php_dom_insert_fragment(xmlNodePtr nodep, xmlNodePtr prevsib,
}
/* }}} */

void dom_node_remove_ids_from_doc(xmlNodePtr node) /* {{{ */
{
xmlNodePtr child;
xmlAttrPtr attrp;

if (node->type == XML_DTD_NODE) {
return; // Skip DTD nodes, see bug #54601
}

child = node->children;
while (child) {
dom_node_remove_ids_from_doc(child);
child = child->next;
}

attrp = node->properties;
while (attrp) {
if (attrp->atype == XML_ATTRIBUTE_ID) {
xmlRemoveID(attrp->doc, attrp);
attrp->atype = 0;
break;
}
attrp = attrp->next;
}
}
/* }}} */

/* {{{ proto domnode dom_node_insert_before(DomNode newChild, DomNode refChild);
URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-952280727
Since:
Expand Down Expand Up @@ -1217,6 +1244,7 @@ PHP_FUNCTION(dom_node_remove_child)
while (children) {
if (children == child) {
xmlUnlinkNode(child);
dom_node_remove_ids_from_doc(child);
DOM_RET_OBJ(child, &ret, intern);
return;
}
Expand Down
18 changes: 18 additions & 0 deletions ext/dom/tests/bug77686.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Bug #77686 Removed elements are still returned by getElementById
--EXTENSIONS--
dom
--FILE--
<?php
$doc = new DOMDocument;
$doc->loadHTML('<html><body id="x"><div id="y"><span id="z"></span></div></body></html>');
$body = $doc->getElementById('x');
$div = $doc->getElementById('y');
$span = $doc->getElementById('z');
$body->removeChild($div);
var_dump($doc->getElementById('y'));
var_dump($doc->getElementById('z'));
?>
--EXPECT--
NULL
NULL