Skip to content

Fix GH-25998: Dom\Element::setIdAttributeNS() rejects null for $namespace - #25999

Closed
thunderer wants to merge 1 commit into
php:PHP-8.4from
thunderer:fix/dom-setidattributens-null
Closed

thunderer wants to merge 1 commit into
php:PHP-8.4from
thunderer:fix/dom-setidattributens-null

Conversation

@thunderer

@thunderer thunderer commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes GH-25998.

Dom\Element::setIdAttributeNS() now has its own method entry that parses $namespace as a nullable string, so null means no namespace, as the stub declares. Like the other *NS methods of Dom\Element, it also treats "" as no namespace. Before, null threw a TypeError in strict mode and was otherwise coerced to "" with a deprecation, and "" matched only an attribute whose namespace is the empty string, for example a DTD default attribute parsed with LIBXML_DTDATTR or an attribute of an imported legacy node. DOMElement::setIdAttributeNS() is unchanged.

Comment thread ext/dom/element.c
}

DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);

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.

nit

diff --git a/ext/dom/element.c b/ext/dom/element.c
--- a/ext/dom/element.c
+++ b/ext/dom/element.c
@@ -1417,6 +1417,10 @@ static void dom_element_set_id_attribute_ns(INTERNAL_FUNCTION_PARAMETERS, bool modern)

        DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);

+       if (modern && uri_len == 0) {
+               uri = NULL;
+       }
+
        attrp = xmlHasNsProp(elemp, BAD_CAST name, BAD_CAST uri);
        if (attrp == NULL || attrp->type == XML_ATTRIBUTE_DECL) {
                php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document));

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.

I trust you ll find a test case for it ;)

@thunderer thunderer Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @devnexen, thank you for your suggestion. I applied it and added another test assertion for that case. I also updated the PR description for the empty string.

…mespace

The method was an implementation alias of
DOMElement::setIdAttributeNS(), which parses the namespace as a
non-nullable string, although the stub declares ?string. Give it its
own entry that accepts null as no namespace and, like the other *NS
methods of Dom\Element, treats an empty namespace as no namespace too.
@thunderer
thunderer force-pushed the fix/dom-setidattributens-null branch from fae958f to 060ea2a Compare December 1, 2026 18:11
@thunderer
thunderer requested a review from devnexen December 1, 2026 18:17
@devnexen devnexen closed this in 793f0ee Dec 1, 2026
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