Skip to content

Commit 3feb465

Browse files
committed
ext/dom: Register id attributes set via setAttribute() in XML documents
getElementById() relies on the XML_ATTRIBUTE_ID flag, which dom_check_register_attribute_id() set only for HTML documents, leaving Dom\XMLDocument::getElementById() blind to ids assigned through setAttribute(), setAttributeNS() and setAttributeNode(). Drop the document-type restriction so modern XML documents register an un-namespaced id attribute; setIdAttribute(false) still unregisters and legacy DOMDocument is unchanged because every call site sits behind spec mode. setAttribute() reached the registration only when it created the attribute, so an id carried in from parsing stayed unregistered; it now runs for both branches. Closes GH-23801
1 parent 3c857cc commit 3feb465

3 files changed

Lines changed: 75 additions & 5 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ PHP NEWS
3030
. Fixed Dom\HTMLDocument giving attributes the namespace of their element
3131
when a fragment is parsed with an xlink, xml or xmlns context element.
3232
(Ilia Alshanetsky)
33+
. getElementById() now finds elements whose id attribute was set with
34+
setAttribute()/setAttributeNS() on Dom\XMLDocument. (Ilia Alshanetsky)
3335

3436
- Fileinfo:
3537
. Upgrade to file 5.48. (Weilin Du)

‎ext/dom/element.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,7 @@ static void dom_check_register_attribute_id(xmlAttrPtr attr, php_libxml_ref_obj
393393
{
394394
dom_mark_ids_modified(document);
395395

396-
if (attr->atype != XML_ATTRIBUTE_ID && attr->doc->type == XML_HTML_DOCUMENT_NODE && attr->ns == NULL && xmlStrEqual(attr->name, BAD_CAST "id")) {
397-
/* To respect XML's ID behaviour, we only do this registration for HTML documents. */
396+
if (attr->atype != XML_ATTRIBUTE_ID && attr->ns == NULL && xmlStrEqual(attr->name, BAD_CAST "id")) {
398397
attr->atype = XML_ATTRIBUTE_ID;
399398
}
400399
}
@@ -447,9 +446,10 @@ PHP_METHOD(DOMElement, setAttribute)
447446
xmlAddChild((xmlNodePtr) attr, node);
448447
} else {
449448
attr = xmlSetNsProp(nodep, NULL, name_processed, BAD_CAST value);
450-
if (EXPECTED(attr != NULL)) {
451-
dom_check_register_attribute_id(attr, intern->document);
452-
}
449+
}
450+
451+
if (EXPECTED(attr != NULL)) {
452+
dom_check_register_attribute_id(attr, intern->document);
453453
}
454454

455455
if (name_processed != BAD_CAST name) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--TEST--
2+
Dom\XMLDocument::getElementById() after setting id with setAttribute()
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = Dom\XMLDocument::createEmpty();
9+
$root = $dom->appendChild($dom->createElement("root"));
10+
$a = $dom->createElement("a");
11+
$dom->documentElement->appendChild($a);
12+
13+
echo "--- After parsing ---\n";
14+
var_dump($dom->getElementById("x")?->nodeName);
15+
16+
echo "--- After setAttribute ---\n";
17+
$a->setAttribute("id", "x");
18+
var_dump($dom->getElementById("x")?->nodeName);
19+
var_dump($dom->getElementById("y")?->nodeName);
20+
21+
echo "--- After changing the id value with setAttribute ---\n";
22+
$a->setAttribute("id", "y");
23+
var_dump($dom->getElementById("x")?->nodeName);
24+
var_dump($dom->getElementById("y")?->nodeName);
25+
26+
echo "--- After setAttributeNS ---\n";
27+
$a->setAttributeNS(null, "id", "z");
28+
var_dump($dom->getElementById("y")?->nodeName);
29+
var_dump($dom->getElementById("z")?->nodeName);
30+
31+
echo "--- After removing the attribute ---\n";
32+
$a->removeAttribute("id");
33+
var_dump($dom->getElementById("z")?->nodeName);
34+
35+
echo "--- After setIdAttribute ---\n";
36+
$a->setAttribute("id", "w");
37+
$a->setIdAttribute("id", false);
38+
var_dump($dom->getElementById("w")?->nodeName);
39+
40+
echo "--- Parsed id attribute changed with setAttribute ---\n";
41+
$parsed = Dom\XMLDocument::createFromString('<root><b id="p"/></root>');
42+
$b = $parsed->documentElement->firstElementChild;
43+
var_dump($parsed->getElementById("p")?->nodeName);
44+
$b->setAttribute("id", "q");
45+
var_dump($parsed->getElementById("p")?->nodeName);
46+
var_dump($parsed->getElementById("q")?->nodeName);
47+
48+
?>
49+
--EXPECT--
50+
--- After parsing ---
51+
NULL
52+
--- After setAttribute ---
53+
string(1) "a"
54+
NULL
55+
--- After changing the id value with setAttribute ---
56+
NULL
57+
string(1) "a"
58+
--- After setAttributeNS ---
59+
NULL
60+
string(1) "a"
61+
--- After removing the attribute ---
62+
NULL
63+
--- After setIdAttribute ---
64+
NULL
65+
--- Parsed id attribute changed with setAttribute ---
66+
NULL
67+
NULL
68+
string(1) "b"

0 commit comments

Comments
 (0)