Skip to content
Open
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
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 14 additions & 5 deletions ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
54 changes: 54 additions & 0 deletions ext/dom/tests/modern/common/Attr_rename_id.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--TEST--
Dom\Attr::rename() updates the id used by getElementById()
--EXTENSIONS--
dom
--FILE--
<?php

foreach ([
Dom\XMLDocument::createFromString('<root><a/><b/></root>'),
Dom\HTMLDocument::createFromString('<!DOCTYPE html><a></a><b></b>', 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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--TEST--
Dom\XMLDocument::getElementById() after setting id with setAttribute()
--EXTENSIONS--
dom
--FILE--
<?php

$dom = Dom\XMLDocument::createEmpty();
$root = $dom->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('<root><b id="p"/></root>');
$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"
Loading