diff --git a/NEWS b/NEWS index e96c1d882c9f..6bc73ad7e1c6 100644 --- a/NEWS +++ b/NEWS @@ -29,6 +29,10 @@ PHP NEWS . Fixed use-after-free when a php:function() callback of DOMXPath or Dom\XPath modifies nodes that are still being evaluated; such modifications now throw a DOMException or Error. (Ilia Alshanetsky) + . getElementById() now finds elements whose id attribute was set with + setAttribute()/setAttributeNS() on Dom\XMLDocument. (Ilia Alshanetsky) + . Fixed getElementById() ignoring an id attribute renamed with + Dom\Attr::rename(). (Ilia Alshanetsky) - FPM: . Use recommended TYPE, HELP order for OpenMetrics metadata. diff --git a/ext/dom/element.c b/ext/dom/element.c index ad27fc2e6c9e..ab3111390f63 100644 --- a/ext/dom/element.c +++ b/ext/dom/element.c @@ -397,8 +397,7 @@ static void dom_check_register_attribute_id(xmlAttrPtr attr, php_libxml_ref_obj { dom_mark_ids_modified(document); - if (attr->atype != XML_ATTRIBUTE_ID && attr->doc->type == XML_HTML_DOCUMENT_NODE && attr->ns == NULL && xmlStrEqual(attr->name, BAD_CAST "id")) { - /* To respect XML's ID behaviour, we only do this registration for HTML documents. */ + if (attr->atype != XML_ATTRIBUTE_ID && attr->ns == NULL && xmlStrEqual(attr->name, BAD_CAST "id")) { attr->atype = XML_ATTRIBUTE_ID; } } @@ -455,9 +454,10 @@ PHP_METHOD(DOMElement, setAttribute) xmlAddChild((xmlNodePtr) attr, node); } else { attr = xmlSetNsProp(nodep, NULL, name_processed, BAD_CAST value); - if (EXPECTED(attr != NULL)) { - dom_check_register_attribute_id(attr, intern->document); - } + } + + if (EXPECTED(attr != NULL)) { + dom_check_register_attribute_id(attr, intern->document); } if (name_processed != BAD_CAST name) { @@ -2293,6 +2293,9 @@ PHP_METHOD(Dom_Element, rename) php_dom_libxml_ns_mapper *ns_mapper = php_dom_get_ns_mapper(intern); + const xmlChar *old_ns_href = nodep->ns ? nodep->ns->href : NULL; + bool name_changed = !xmlStrEqual(nodep->name, localname); + /* Update namespace uri + prefix by querying the namespace mapper */ /* prefix can be NULL here, but that is taken care of by the called APIs. */ nodep->ns = php_dom_libxml_ns_mapper_get_ns_raw_prefix_string(ns_mapper, prefix, xmlStrlen(prefix), namespace_uri); @@ -2309,6 +2312,12 @@ PHP_METHOD(Dom_Element, rename) localname = NULL; } + if (nodep->type == XML_ATTRIBUTE_NODE + && (name_changed || !xmlStrEqual(old_ns_href, nodep->ns ? nodep->ns->href : NULL))) { + php_set_attribute_id((xmlAttrPtr) nodep, false, intern->document); + dom_check_register_attribute_id((xmlAttrPtr) nodep, intern->document); + } + cleanup: xmlFree(localname); xmlFree(prefix); diff --git a/ext/dom/tests/modern/common/Attr_rename_id.phpt b/ext/dom/tests/modern/common/Attr_rename_id.phpt new file mode 100644 index 000000000000..caffb7eb34f4 --- /dev/null +++ b/ext/dom/tests/modern/common/Attr_rename_id.phpt @@ -0,0 +1,54 @@ +--TEST-- +Dom\Attr::rename() updates the id used by getElementById() +--EXTENSIONS-- +dom +--FILE-- +'), + Dom\HTMLDocument::createFromString('', LIBXML_NOERROR), +] as $dom) { + echo "--- ", $dom::class, " ---\n"; + $a = $dom->getElementsByTagName("a")->item(0); + $b = $dom->getElementsByTagName("b")->item(0); + + $a->setAttribute("id", "x"); + var_dump($dom->getElementById("x")?->localName); + + $a->getAttributeNode("id")->rename(null, "foo"); + var_dump($a->getAttribute("id")); + var_dump($a->getAttribute("foo")); + var_dump($dom->getElementById("x")?->localName); + + $b->setAttribute("foo", "y"); + $b->getAttributeNode("foo")->rename(null, "id"); + var_dump($dom->getElementById("y")?->localName); + + $b->getAttributeNode("id")->rename("urn:test", "id"); + var_dump($dom->getElementById("y")?->localName); + + $b->setAttribute("key", "k"); + $b->setIdAttribute("key", true); + $b->getAttributeNode("key")->rename(null, "key"); + var_dump($dom->getElementById("k")?->localName); +} + +?> +--EXPECT-- +--- Dom\XMLDocument --- +string(1) "a" +NULL +string(1) "x" +NULL +string(1) "b" +NULL +string(1) "b" +--- Dom\HTMLDocument --- +string(1) "a" +NULL +string(1) "x" +NULL +string(1) "b" +NULL +string(1) "b" diff --git a/ext/dom/tests/modern/xml/Document_getElementById_setAttribute.phpt b/ext/dom/tests/modern/xml/Document_getElementById_setAttribute.phpt new file mode 100644 index 000000000000..91edde084b93 --- /dev/null +++ b/ext/dom/tests/modern/xml/Document_getElementById_setAttribute.phpt @@ -0,0 +1,68 @@ +--TEST-- +Dom\XMLDocument::getElementById() after setting id with setAttribute() +--EXTENSIONS-- +dom +--FILE-- +appendChild($dom->createElement("root")); +$a = $dom->createElement("a"); +$dom->documentElement->appendChild($a); + +echo "--- After parsing ---\n"; +var_dump($dom->getElementById("x")?->nodeName); + +echo "--- After setAttribute ---\n"; +$a->setAttribute("id", "x"); +var_dump($dom->getElementById("x")?->nodeName); +var_dump($dom->getElementById("y")?->nodeName); + +echo "--- After changing the id value with setAttribute ---\n"; +$a->setAttribute("id", "y"); +var_dump($dom->getElementById("x")?->nodeName); +var_dump($dom->getElementById("y")?->nodeName); + +echo "--- After setAttributeNS ---\n"; +$a->setAttributeNS(null, "id", "z"); +var_dump($dom->getElementById("y")?->nodeName); +var_dump($dom->getElementById("z")?->nodeName); + +echo "--- After removing the attribute ---\n"; +$a->removeAttribute("id"); +var_dump($dom->getElementById("z")?->nodeName); + +echo "--- After setIdAttribute ---\n"; +$a->setAttribute("id", "w"); +$a->setIdAttribute("id", false); +var_dump($dom->getElementById("w")?->nodeName); + +echo "--- Parsed id attribute changed with setAttribute ---\n"; +$parsed = Dom\XMLDocument::createFromString(''); +$b = $parsed->documentElement->firstElementChild; +var_dump($parsed->getElementById("p")?->nodeName); +$b->setAttribute("id", "q"); +var_dump($parsed->getElementById("p")?->nodeName); +var_dump($parsed->getElementById("q")?->nodeName); + +?> +--EXPECT-- +--- After parsing --- +NULL +--- After setAttribute --- +string(1) "a" +NULL +--- After changing the id value with setAttribute --- +NULL +string(1) "a" +--- After setAttributeNS --- +NULL +string(1) "a" +--- After removing the attribute --- +NULL +--- After setIdAttribute --- +NULL +--- Parsed id attribute changed with setAttribute --- +NULL +NULL +string(1) "b"