Skip to content

Commit 1774419

Browse files
committed
Fix #77686: Immediately remove ID from document when removing child
Prior a removed node was still returned from DOMDocument::getElementById() until the DOMElement object was destroyed. Now null is returned from getElementById() even if a reference remains to the removed DOMElement object.
1 parent 42c72ef commit 1774419

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

ext/dom/node.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,7 @@ PHP_FUNCTION(dom_node_remove_child)
11861186
xmlNodePtr children, child, nodep;
11871187
dom_object *intern, *childobj;
11881188
int ret, stricterror;
1189+
xmlAttrPtr attrp;
11891190

11901191
id = ZEND_THIS;
11911192
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &node, dom_node_class_entry) == FAILURE) {
@@ -1217,6 +1218,17 @@ PHP_FUNCTION(dom_node_remove_child)
12171218
while (children) {
12181219
if (children == child) {
12191220
xmlUnlinkNode(child);
1221+
1222+
attrp = child->properties;
1223+
while (attrp) {
1224+
if (attrp->type == XML_ATTRIBUTE_ID) {
1225+
xmlRemoveID(attrp->doc, attrp);
1226+
attrp->atype = 0;
1227+
break;
1228+
}
1229+
attrp = attrp->next;
1230+
}
1231+
12201232
DOM_RET_OBJ(child, &ret, intern);
12211233
return;
12221234
}

ext/dom/tests/bug77686.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Bug #77686 Removed elements are still returned by getElementById
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$doc = new DOMDocument;
8+
$doc->loadHTML('<html><body id="x"><div id="y"></div></body></html>');
9+
$body = $doc->getElementById('x');
10+
$div = $doc->getElementById('y');
11+
$body->removeChild($div);
12+
var_dump($doc->getElementById('y'));
13+
14+
?>
15+
--EXPECT--
16+
NULL

0 commit comments

Comments
 (0)