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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ PHP NEWS
. Fixed Dom\HTMLDocument giving attributes the namespace of their element
when a fragment is parsed with an xlink, xml or xmlns context element.
(Ilia Alshanetsky)
. getElementById() now finds elements whose id attribute was set with
setAttribute()/setAttributeNS() on Dom\XMLDocument. (Ilia Alshanetsky)

- Fileinfo:
. Upgrade to file 5.48. (Weilin Du)
Expand Down
3 changes: 1 addition & 2 deletions ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--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);

?>
--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
Loading