Skip to content

dom: Fix ID spec compliance strictness #19111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 1 addition & 2 deletions ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,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
5 changes: 3 additions & 2 deletions ext/dom/tests/bug79701/remove_attribute.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Bug #79701 (getElementById does not correctly work with duplicate definitions) -
dom
--FILE--
<?php
$dom = Dom\XMLDocument::createFromString(<<<XML
$dom = new DOMDocument;
$dom->loadXML(<<<XML
<root>
<test1 xml:id="x"/>
<test2 xml:id="x"/>
Expand All @@ -16,6 +17,6 @@ $dom->getElementById('x')->removeAttribute('xml:id');
var_dump($dom->getElementById('x')?->nodeName);
?>
--EXPECTF--
Warning: Dom\XMLDocument::createFromString(): ID x already defined in Entity, line: 3 in %s on line %d
Warning: DOMDocument::loadXML(): ID x already defined in Entity, line: 3 in %s on line %d
string(5) "test1"
NULL
3 changes: 2 additions & 1 deletion ext/dom/tests/bug79701/set_attr_value.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ dom
<?php
foreach (["value", "nodeValue"] as $property) {
echo "--- Testing property \$$property ---\n";
$dom = Dom\XMLDocument::createFromString(<<<XML
$dom = new DOMDocument;
$dom->loadXML(<<<XML
<root>
<test1 xml:id="x"/>
<test2 xml:id="y"/>
Expand Down
74 changes: 52 additions & 22 deletions ext/dom/tests/bug79701/set_attribute_xml.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ Bug #79701 (getElementById does not correctly work with duplicate definitions) -
dom
--FILE--
<?php
function test($dom, $fn) {
function test($dom, $prefix, $fn) {
$test1 = $dom->getElementById('x');
$test2 = $dom->getElementById('y');

echo "--- After resetting test1's id ---\n";

$fn($test1, 'xml:id', 'y');
$fn($test1, $prefix . 'id', 'y');
var_dump($dom->getElementById('x')?->nodeName);
var_dump($dom->getElementById('y')?->nodeName);

echo "--- After resetting test2's id ---\n";

$fn($test2, 'xml:id', 'x');
$fn($test2, $prefix . 'id', 'x');
var_dump($dom->getElementById('x')?->nodeName);
var_dump($dom->getElementById('y')?->nodeName);

echo "--- After resetting test1's id ---\n";

$fn($test1, 'xml:id', 'z');
$fn($test1, $prefix . 'id', 'z');
var_dump($dom->getElementById('x')?->nodeName);
var_dump($dom->getElementById('y')?->nodeName);

echo "--- After resetting test2's id ---\n";

$fn($test2, 'xml:id', 'z');
$fn($test2, $prefix . 'id', 'z');
var_dump($dom->getElementById('x')?->nodeName);
var_dump($dom->getElementById('y')?->nodeName);

Expand All @@ -51,30 +51,43 @@ $common_xml = <<<XML
</root>
XML;

echo "\n=== DOMDocument: setAttribute ===\n\n";
echo "\n=== DOMDocument: setAttribute (prefixed) ===\n\n";

$dom = new DOMDocument;
$dom->loadXML($common_xml);
test($dom, fn ($element, $name, $value) => $element->setAttribute($name, $value));
test($dom, "xml:", fn ($element, $name, $value) => $element->setAttribute($name, $value));

echo "\n=== DOMDocument: setAttributeNS ===\n\n";
echo "\n=== DOMDocument: setAttributeNS (prefixed) ===\n\n";

$dom = new DOMDocument;
$dom->loadXML($common_xml);
test($dom, fn ($element, $name, $value) => $element->setAttributeNS(getNamespace($name), $name, $value));
test($dom, "xml:", fn ($element, $name, $value) => $element->setAttributeNS(getNamespace($name), $name, $value));

echo "\n=== Dom\\XMLDocument: setAttribute ===\n\n";
echo "\n=== Dom\\XMLDocument: setAttribute (prefixed) ===\n\n";

$dom = Dom\XMLDocument::createFromString($common_xml);
test($dom, fn ($element, $name, $value) => $element->setAttribute($name, $value));
test($dom, "xml:", fn ($element, $name, $value) => $element?->setAttribute($name, $value));

echo "\n=== Dom\\XMLDocument: setAttributeNS (prefixed) ===\n\n";

$dom = Dom\XMLDocument::createFromString($common_xml);
test($dom, "xml:", fn ($element, $name, $value) => $element?->setAttribute(getNamespace($name), $name, $value));

echo "\n=== Dom\\XMLDocument: setAttribute ===\n\n";

echo "\n=== Dom\\XMLDocument: setAttributeNS ===\n\n";
$common_xml = <<<XML
<root>
<test1 id="x"/>
<test2 id="y"/>
</root>
XML;

$dom = Dom\XMLDocument::createFromString($common_xml);
test($dom, fn ($element, $name, $value) => $element->setAttributeNS(getNamespace($name), $name, $value));
test($dom, "", fn ($element, $name, $value) => $element?->setAttribute($name, $value));

?>
--EXPECT--
=== DOMDocument: setAttribute ===
=== DOMDocument: setAttribute (prefixed) ===

--- After resetting test1's id ---
NULL
Expand All @@ -91,7 +104,7 @@ NULL
--- Get id z ---
string(5) "test1"

=== DOMDocument: setAttributeNS ===
=== DOMDocument: setAttributeNS (prefixed) ===

--- After resetting test1's id ---
NULL
Expand All @@ -108,24 +121,41 @@ NULL
--- Get id z ---
string(5) "test1"

=== Dom\XMLDocument: setAttribute ===
=== Dom\XMLDocument: setAttribute (prefixed) ===

--- After resetting test1's id ---
NULL
string(5) "test1"
NULL
--- After resetting test2's id ---
string(5) "test2"
string(5) "test1"
NULL
NULL
--- After resetting test1's id ---
string(5) "test2"
NULL
NULL
--- After resetting test2's id ---
NULL
NULL
--- Get id z ---
string(5) "test1"
NULL

=== Dom\XMLDocument: setAttributeNS (prefixed) ===

=== Dom\XMLDocument: setAttributeNS ===
--- After resetting test1's id ---
NULL
NULL
--- After resetting test2's id ---
NULL
NULL
--- After resetting test1's id ---
NULL
NULL
--- After resetting test2's id ---
NULL
NULL
--- Get id z ---
NULL

=== Dom\XMLDocument: setAttribute ===

--- After resetting test1's id ---
NULL
Expand Down
8 changes: 4 additions & 4 deletions ext/dom/tests/modern/css_selectors/closest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ dom
$xml = <<<XML
<root>
<a/>
<div class="foo" xml:id="div1">
<div xml:id="div2">
<div class="bar" xml:id="div3"/>
<div class="foo" id="div1">
<div id="div2">
<div class="bar" id="div3"/>
</div>
</div>
</root>
Expand All @@ -20,7 +20,7 @@ $dom = DOM\XMLDocument::createFromString($xml);

function test($el, $selector) {
echo "--- Selector: $selector ---\n";
var_dump($el->closest($selector)?->getAttribute('xml:id'));
var_dump($el->closest($selector)?->getAttribute('id'));
}

test($dom->getElementById('div3'), 'div');
Expand Down
24 changes: 24 additions & 0 deletions ext/dom/tests/modern/xml/id_registration_strictness.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Strictness of ID registration
--EXTENSIONS--
dom
--FILE--
<?php

$dom = Dom\XMLDocument::createFromString('<root><child0 xml:id="a"/><child1 id="a"/><child2 id="b"/></root>');
var_dump($dom->getElementById('a')?->tagName);
var_dump($dom->getElementById('b')?->tagName);
$dom->documentElement->setAttribute('id', 'a');
var_dump($dom->getElementById('a')?->tagName);
$dom->documentElement->setAttribute('id', '');
var_dump($dom->getElementById('a')?->tagName);
$dom->documentElement->setAttribute('ID', 'a');
var_dump($dom->getElementById('a')?->tagName);

?>
--EXPECT--
string(6) "child1"
string(6) "child2"
string(4) "root"
string(6) "child1"
string(6) "child1"
16 changes: 16 additions & 0 deletions ext/dom/xml_document.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ void dom_mark_namespaces_as_attributes_too(php_dom_libxml_ns_mapper *ns_mapper,
xmlNodePtr node = doc->children;
while (node != NULL) {
if (node->type == XML_ELEMENT_NODE) {
/* Apply ID rules of WHATWG DOM. */
for (xmlAttrPtr attr = node->properties; attr != NULL; attr = attr->next) {
if (attr->atype == XML_ATTRIBUTE_ID) {
if (attr->ns != NULL || !xmlStrEqual(attr->name, BAD_CAST "id")) {
xmlRemoveID(doc, attr);
}
} else if (attr->atype == 0 && attr->ns == NULL && xmlStrEqual(attr->name, BAD_CAST "id")) {
bool should_free;
xmlChar *value = php_libxml_attr_value(attr, &should_free);
xmlAddID(NULL, doc, value, attr);
if (UNEXPECTED(should_free)) {
xmlFree(value);
}
}
}

php_dom_ns_compat_mark_attribute_list(ns_mapper, node);
}

Expand Down