Skip to content

Commit 424c265

Browse files
vicvolkcmb69
authored andcommitted
Fix #81433: DOMElement::setIdAttribute() called twice may remove ID
We must only remove the attribute id, if the user requested that. Closes GH-7482.
1 parent 95a943e commit 424c265

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 7.4.25
44

5+
- DOM:
6+
. Fixed bug #81433 (DOMElement::setIdAttribute() called twice may remove ID).
7+
(Viktor Volkov)
58

69
23 Dep 2021, PHP 7.4.24
710

ext/dom/element.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,11 +1157,9 @@ static void php_set_attribute_id(xmlAttrPtr attrp, zend_bool is_id) /* {{{ */
11571157
xmlAddID(NULL, attrp->doc, id_val, attrp);
11581158
xmlFree(id_val);
11591159
}
1160-
} else {
1161-
if (attrp->atype == XML_ATTRIBUTE_ID) {
1162-
xmlRemoveID(attrp->doc, attrp);
1163-
attrp->atype = 0;
1164-
}
1160+
} else if (is_id == 0 && attrp->atype == XML_ATTRIBUTE_ID) {
1161+
xmlRemoveID(attrp->doc, attrp);
1162+
attrp->atype = 0;
11651163
}
11661164
}
11671165
/* }}} */

ext/dom/tests/bug81433.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Bug #81433 (DOMElement::setIdAttribute(attr, true) called twice removes ID)
3+
--SKIPIF--
4+
<?php
5+
require_once('skipif.inc');
6+
?>
7+
--FILE--
8+
<?php
9+
$dom = new DOMDocument('1.0', 'utf-8');
10+
11+
$element = $dom->createElement('test', 'root');
12+
13+
$dom->appendChild($element);
14+
15+
$element->setAttribute("id", 123);
16+
$element->setIdAttribute("id", true);
17+
18+
$node = $element->getAttributeNode("id");
19+
var_dump($node->isId());
20+
21+
$element->setIdAttribute("id", true);
22+
var_dump($node->isId());
23+
?>
24+
--EXPECT--
25+
bool(true)
26+
bool(true)

0 commit comments

Comments
 (0)