Skip to content

ext/dom: Register id attributes set via setAttribute() in XML documents - #23801

Open
iliaal wants to merge 1 commit into
php:masterfrom
iliaal:fix/dom-setattribute-id-master
Open

iliaal wants to merge 1 commit into
php:masterfrom
iliaal:fix/dom-setattribute-id-master

Conversation

@iliaal

@iliaal iliaal commented Sep 20, 2026

Copy link
Copy Markdown
Member

dom_check_register_attribute_id() sets XML_ATTRIBUTE_ID only when the document is XML_HTML_DOCUMENT_NODE, so Dom\XMLDocument::getElementById() returns null after setAttribute("id", ...) even though the id property registered fine. Dropping the document-type restriction lets modern XML documents register an un-namespaced id attribute, setIdAttribute(false) still unregisters, and legacy DOMDocument is unchanged because every helper call site sits behind spec mode. All three call sites were audited.

Comment thread ext/dom/element.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- a/ext/dom/element.c
+++ b/ext/dom/element.c
@@ -446,9 +446,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) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied, and added a test case for it: a plain id that came in from parsing is never flagged XML_ATTRIBUTE_ID, and the existing-attribute branch left it that way, so getElementById() missed it after a setAttribute().

iliaal added a commit to iliaal/php-src that referenced this pull request Sep 22, 2026
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 phpGH-23801
@iliaal
iliaal force-pushed the fix/dom-setattribute-id-master branch from a4cf1b6 to 3feb465 Compare September 22, 2026 12:54
@iliaal
iliaal requested a review from devnexen September 22, 2026 13:01
@devnexen

Copy link
Copy Markdown
Member

I m not entirely sure about the direction, cc @ndossche see below

--TEST--
Dom\XMLDocument::getElementById() after renaming an id attribute set with setAttribute()
--EXTENSIONS--
dom
--FILE--
<?php

$dom = Dom\XMLDocument::createFromString('<root><a/></root>');
$a = $dom->documentElement->firstElementChild;

$a->setAttribute("id", "x");
var_dump($dom->getElementById("x")?->nodeName);

$a->getAttributeNode("id")->rename(null, "foo");
var_dump($a->getAttribute("id"));
var_dump($a->getAttribute("foo"));
var_dump($dom->getElementById("x")?->nodeName);

?>
--EXPECT--
string(1) "a"
NULL
string(1) "x"
NULL

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. Dom\Attr::rename() kept the flag when renaming away from id and
never set it when renaming to id; it now re-evaluates the flag whenever
the local name or namespace changes.

Closes phpGH-23801
@iliaal

iliaal commented Nov 27, 2026

Copy link
Copy Markdown
Member Author

The spec defines an element's ID as its id attribute's value in any document, so I'd keep the direction. Your case also failed on Dom\HTMLDocument before this PR because Attr::rename() never updated the ID flag. Fixed that and added your test with an HTMLDocument variant.

@iliaal
iliaal force-pushed the fix/dom-setattribute-id-master branch from 3feb465 to 77c7acd Compare November 27, 2026 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants