Skip to content

Commit 7615331

Browse files
committed
Fix GH-11404: DOMDocument::savexml and friends ommit xmlns="" declaration for null namespace, creating incorrect xml representation of the DOM
The NULL namespace is only correct when there is no default namespace override. When there is, we need to manually set it to the empty string namespace.
1 parent e9d6023 commit 7615331

File tree

6 files changed

+65
-20
lines changed

6 files changed

+65
-20
lines changed

ext/dom/document.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,10 @@ PHP_METHOD(DOMDocument, createElementNS)
878878

879879
if (errorcode == 0) {
880880
if (xmlValidateName((xmlChar *) localname, 0) == 0) {
881+
/* https://dom.spec.whatwg.org/#validate-and-extract: demands us to set an empty string uri to NULL */
882+
if (uri_len == 0) {
883+
uri = NULL;
884+
}
881885
nodep = xmlNewDocNode(docp, NULL, (xmlChar *) localname, (xmlChar *) value);
882886
if (nodep != NULL && uri != NULL) {
883887
nsptr = xmlSearchNsByHref(nodep->doc, nodep, (xmlChar *) uri);

ext/dom/element.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ PHP_METHOD(DOMElement, __construct)
5656
if (uri_len > 0) {
5757
errorcode = dom_check_qname(name, &localname, &prefix, uri_len, name_len);
5858
if (errorcode == 0) {
59+
/* https://dom.spec.whatwg.org/#validate-and-extract: demands us to set an empty string uri to NULL */
60+
if (uri_len == 0) {
61+
uri = NULL;
62+
}
5963
nodep = xmlNewNode (NULL, (xmlChar *)localname);
6064
if (nodep != NULL && uri != NULL) {
6165
nsptr = dom_get_ns(nodep, uri, &errorcode, prefix);

ext/dom/node.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@ Since: DOM Level 2
531531
int dom_node_namespace_uri_read(dom_object *obj, zval *retval)
532532
{
533533
xmlNode *nodep = dom_object_get_node(obj);
534-
char *str = NULL;
535534

536535
if (nodep == NULL) {
537536
php_dom_throw_error(INVALID_STATE_ERR, 1);
@@ -543,20 +542,19 @@ int dom_node_namespace_uri_read(dom_object *obj, zval *retval)
543542
case XML_ATTRIBUTE_NODE:
544543
case XML_NAMESPACE_DECL:
545544
if (nodep->ns != NULL) {
546-
str = (char *) nodep->ns->href;
545+
char *str = (char *) nodep->ns->href;
546+
/* https://dom.spec.whatwg.org/#concept-attribute: namespaceUri is "null or a non-empty string" */
547+
if (str != NULL && str[0] != '\0') {
548+
ZVAL_STRING(retval, str);
549+
return SUCCESS;
550+
}
547551
}
548552
break;
549553
default:
550-
str = NULL;
551554
break;
552555
}
553556

554-
if (str != NULL) {
555-
ZVAL_STRING(retval, str);
556-
} else {
557-
ZVAL_NULL(retval);
558-
}
559-
557+
ZVAL_NULL(retval);
560558
return SUCCESS;
561559
}
562560

ext/dom/php_dom.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,13 +1478,34 @@ static void dom_libxml_reconcile_ensure_namespaces_are_declared(xmlNodePtr nodep
14781478
xmlDOMWrapReconcileNamespaces(&dummy_ctxt, nodep, /* options */ 0);
14791479
}
14801480

1481+
static bool dom_must_replace_namespace_by_empty_default(xmlDocPtr doc, xmlNodePtr nodep)
1482+
{
1483+
xmlNsPtr default_ns = xmlSearchNs(doc, nodep->parent, NULL);
1484+
return default_ns != NULL && default_ns->href != NULL && default_ns->href[0] != '\0';
1485+
}
1486+
1487+
static void dom_replace_namespace_by_empty_default(xmlDocPtr doc, xmlNodePtr nodep)
1488+
{
1489+
if (nodep->ns == NULL) {
1490+
/* The node uses the default empty namespace, but the current default namespace is non-empty.
1491+
* We can't unconditionally do this because otherwise libxml2 creates an xmlns="" declaration.
1492+
* Note: there's no point searching the oldNs list, because we haven't found it in the tree anyway.
1493+
* Ideally this would be pre-allocated but unfortunately libxml2 doesn't offer such a functionality. */
1494+
xmlSetNs(nodep, xmlNewNs(nodep, (const xmlChar *) "", NULL));
1495+
}
1496+
}
1497+
14811498
void dom_reconcile_ns(xmlDocPtr doc, xmlNodePtr nodep) /* {{{ */
14821499
{
14831500
/* Although the node type will be checked by the libxml2 API,
14841501
* we still want to do the internal reconciliation conditionally. */
14851502
if (nodep->type == XML_ELEMENT_NODE) {
14861503
dom_reconcile_ns_internal(doc, nodep, nodep->parent);
14871504
dom_libxml_reconcile_ensure_namespaces_are_declared(nodep);
1505+
/* Check nodep->ns first to avoid an expensive lookup. */
1506+
if (nodep->ns == NULL && dom_must_replace_namespace_by_empty_default(doc, nodep)) {
1507+
dom_replace_namespace_by_empty_default(doc, nodep);
1508+
}
14881509
}
14891510
}
14901511
/* }}} */
@@ -1508,12 +1529,30 @@ static void dom_reconcile_ns_list_internal(xmlDocPtr doc, xmlNodePtr nodep, xmlN
15081529

15091530
void dom_reconcile_ns_list(xmlDocPtr doc, xmlNodePtr nodep, xmlNodePtr last)
15101531
{
1532+
bool did_compute_must_replace_namespace_by_empty_default = false;
1533+
bool must_replace_namespace_by_empty_default = false;
1534+
15111535
dom_reconcile_ns_list_internal(doc, nodep, last, nodep->parent);
1536+
15121537
/* The loop is outside of the recursion in the above call because
15131538
* dom_libxml_reconcile_ensure_namespaces_are_declared() performs its own recursion. */
15141539
while (true) {
15151540
/* The internal libxml2 call will already check the node type, no need for us to do it here. */
15161541
dom_libxml_reconcile_ensure_namespaces_are_declared(nodep);
1542+
1543+
/* We don't have to handle the children, because if their ns's are NULL they'll just take on the default
1544+
* which should've been reconciled before. */
1545+
if (nodep->ns == NULL) {
1546+
/* This is an optimistic approach: we assume that most of the time we don't need the result of the computation. */
1547+
if (!did_compute_must_replace_namespace_by_empty_default) {
1548+
did_compute_must_replace_namespace_by_empty_default = true;
1549+
must_replace_namespace_by_empty_default = dom_must_replace_namespace_by_empty_default(doc, nodep);
1550+
}
1551+
if (must_replace_namespace_by_empty_default) {
1552+
dom_replace_namespace_by_empty_default(doc, nodep);
1553+
}
1554+
}
1555+
15171556
if (nodep == last) {
15181557
break;
15191558
}

ext/dom/tests/bug47530.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ test_appendChild_with_shadowing();
121121
<html xmlns="https://php.net/something" xmlns:ns="https://php.net/whatever"><element ns:foo="https://php.net/bar"/></html>
122122
-- Test document fragment without import --
123123
<?xml version="1.0"?>
124-
<html xmlns=""><element xmlns:foo="https://php.net/bar"><foo:bar/><bar xmlns=""/></element></html>
124+
<html xmlns=""><element xmlns:foo="https://php.net/bar"><foo:bar/><bar/></element></html>
125125
string(7) "foo:bar"
126126
string(19) "https://php.net/bar"
127127
-- Test document import --

ext/dom/tests/gh11404.phpt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,27 +111,27 @@ var_dump($dom1->firstElementChild->firstElementChild->namespaceURI);
111111
-- Test append and attributes: with default namespace variation --
112112
NULL
113113
NULL
114-
string(0) ""
114+
NULL
115115
NULL
116116
string(7) "some:ns"
117117
<?xml version="1.0"?>
118-
<with xmlns="some:ns"><a/><b/><c xmlns=""/><d xmlns:x="some:ns" x:attrib="val"/><e attrib1="val" attrib2="val"/></with>
118+
<with xmlns="some:ns"><a xmlns=""/><b xmlns=""/><c xmlns=""/><d xmlns:x="some:ns" xmlns="" x:attrib="val"/><e attrib1="val" attrib2="val"/></with>
119119
<?xml version="1.0"?>
120-
<with xmlns="some:ns"><a/><b/><c xmlns=""/><d xmlns:x="some:ns" x:attrib="val"/><e attrib1="val" attrib2="val"/><subtree><subtreechild1 xmlns="some:ns"><subtreechild2/></subtreechild1></subtree></with>
120+
<with xmlns="some:ns"><a xmlns=""/><b xmlns=""/><c xmlns=""/><d xmlns:x="some:ns" xmlns="" x:attrib="val"/><e attrib1="val" attrib2="val"/><subtree xmlns=""><subtreechild1 xmlns="some:ns"><subtreechild2 xmlns=""/></subtreechild1></subtree></with>
121121
<?xml version="1.0"?>
122-
<with xmlns="some:ns"><a/><b/><c xmlns=""/><d xmlns:x="some:ns" x:attrib="val"/><e attrib1="val" attrib2="val"/><subtree><subtreechild1 xmlns="some:ns"><subtreechild2/></subtreechild1></subtree><child3><child4/></child3><child5/></with>
122+
<with xmlns="some:ns"><a xmlns=""/><b xmlns=""/><c xmlns=""/><d xmlns:x="some:ns" xmlns="" x:attrib="val"/><e attrib1="val" attrib2="val"/><subtree xmlns=""><subtreechild1 xmlns="some:ns"><subtreechild2 xmlns=""/></subtreechild1></subtree><child3 xmlns=""><child4/></child3><child5 xmlns=""/></with>
123123
-- Test append and attributes: without default namespace variation --
124124
NULL
125125
NULL
126-
string(0) ""
126+
NULL
127127
NULL
128128
string(7) "some:ns"
129129
<?xml version="1.0"?>
130-
<with><a/><b/><c xmlns=""/><d xmlns:x="some:ns" x:attrib="val"/><e xmlns="some:ns" attrib1="val" attrib2="val"/></with>
130+
<with><a/><b/><c/><d xmlns:x="some:ns" x:attrib="val"/><e xmlns="some:ns" attrib1="val" attrib2="val"/></with>
131131
<?xml version="1.0"?>
132-
<with><a/><b/><c xmlns=""/><d xmlns:x="some:ns" x:attrib="val"/><e xmlns="some:ns" attrib1="val" attrib2="val"/><subtree><subtreechild1 xmlns="some:ns"><subtreechild2/></subtreechild1></subtree></with>
132+
<with><a/><b/><c/><d xmlns:x="some:ns" x:attrib="val"/><e xmlns="some:ns" attrib1="val" attrib2="val"/><subtree><subtreechild1 xmlns="some:ns"><subtreechild2 xmlns=""/></subtreechild1></subtree></with>
133133
<?xml version="1.0"?>
134-
<with><a/><b/><c xmlns=""/><d xmlns:x="some:ns" x:attrib="val"/><e xmlns="some:ns" attrib1="val" attrib2="val"/><subtree><subtreechild1 xmlns="some:ns"><subtreechild2/></subtreechild1></subtree><child3><child4/></child3><child5/></with>
134+
<with><a/><b/><c/><d xmlns:x="some:ns" x:attrib="val"/><e xmlns="some:ns" attrib1="val" attrib2="val"/><subtree><subtreechild1 xmlns="some:ns"><subtreechild2 xmlns=""/></subtreechild1></subtree><child3><child4/></child3><child5/></with>
135135
-- Test import --
136136
NULL
137137
<?xml version="1.0"?>
@@ -141,7 +141,7 @@ NULL
141141
<with xmlns=""><none/></with>
142142
NULL
143143
<?xml version="1.0"?>
144-
<with xmlns="some:ns"><none/></with>
144+
<with xmlns="some:ns"><none xmlns=""/></with>
145145
NULL
146146
<?xml version="1.0"?>
147147
<with xmlns=""><none><div xmlns="some:ns"/></none></with>
@@ -155,6 +155,6 @@ string(3) "a:b"
155155
string(3) "a:b"
156156
NULL
157157
<?xml version="1.0"?>
158-
<parent xmlns="a:b"><child1/><child2/></parent>
158+
<parent xmlns="a:b"><child1/><child2 xmlns=""/></parent>
159159
string(3) "a:b"
160160
string(3) "a:b"

0 commit comments

Comments
 (0)