Skip to content

Commit acbdfd2

Browse files
committed
Fix DOMElement->prefix with empty string creates bogus prefix
Closes GH-12770.
1 parent cc5a1ba commit acbdfd2

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

ext/dom/node.c

+6
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,14 @@ zend_result dom_node_prefix_write(dom_object *obj, zval *newval)
674674
prefix_str = Z_STR_P(newval);
675675

676676
prefix = ZSTR_VAL(prefix_str);
677+
if (*prefix == '\0') {
678+
/* The empty string namespace prefix does not exist.
679+
* We should fall back to the default namespace in this case. */
680+
prefix = NULL;
681+
}
677682
if (nsnode && nodep->ns != NULL && !xmlStrEqual(nodep->ns->prefix, (xmlChar *)prefix)) {
678683
strURI = (char *) nodep->ns->href;
684+
/* Validate namespace naming constraints */
679685
if (strURI == NULL ||
680686
(zend_string_equals_literal(prefix_str, "xml") && strcmp(strURI, (char *) XML_XML_NAMESPACE)) ||
681687
(nodep->type == XML_ATTRIBUTE_NODE && zend_string_equals_literal(prefix_str, "xmlns") &&
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
DOMElement->prefix with empty string creates bogus prefix
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = new DOMDocument;
9+
$dom->loadXML("<hello:container xmlns:conflict=\"urn:foo\" xmlns:hello=\"http://www.w3.org/1999/xhtml\"/>");
10+
11+
$container = $dom->documentElement;
12+
13+
echo "--- Changing the prefix to an empty string ---\n";
14+
15+
$container->prefix = "";
16+
echo $dom->saveXML();
17+
18+
echo "--- Changing the prefix to an empty C-style string ---\n";
19+
20+
$container->prefix = "\0foobar";
21+
echo $dom->saveXML();
22+
23+
echo "--- Changing the prefix to \"hello\" ---\n";
24+
25+
$container->prefix = "hello";
26+
echo $dom->saveXML();
27+
28+
echo "--- Changing the prefix to that of a conflicting namespace (\"conflict\") ---\n";
29+
30+
try {
31+
$container->prefix = "conflict";
32+
} catch (DOMException $e) {
33+
echo $e->getMessage(), "\n";
34+
}
35+
echo $dom->saveXML();
36+
37+
?>
38+
--EXPECT--
39+
--- Changing the prefix to an empty string ---
40+
<?xml version="1.0"?>
41+
<container xmlns:conflict="urn:foo" xmlns:hello="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"/>
42+
--- Changing the prefix to an empty C-style string ---
43+
<?xml version="1.0"?>
44+
<container xmlns:conflict="urn:foo" xmlns:hello="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"/>
45+
--- Changing the prefix to "hello" ---
46+
<?xml version="1.0"?>
47+
<hello:container xmlns:conflict="urn:foo" xmlns:hello="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"/>
48+
--- Changing the prefix to that of a conflicting namespace ("conflict") ---
49+
Namespace Error
50+
<?xml version="1.0"?>
51+
<hello:container xmlns:conflict="urn:foo" xmlns:hello="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"/>

0 commit comments

Comments
 (0)