Bug report
Bug description:
Since gh-156665, Node.cloneNode(), Document.importNode() and Attr.cloneNode() raise xml.dom.NamespaceErr for DOM Level 1 nodes that have a colon in the name. Such nodes have no namespace and are created by createElement("svg:rect"), createAttribute("a:b"), setAttribute("xmlns:xsi", ...), or by parsing with namespace processing disabled.
from xml.dom import minidom
doc = minidom.Document()
root = doc.appendChild(doc.createElement("root"))
root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
root.appendChild(doc.createElement("svg:rect"))
for label, func in [
("cloneNode", lambda: root.cloneNode(True)),
("importNode", lambda: minidom.Document().importNode(root, True)),
("Attr.cloneNode", lambda: doc.createAttribute("a:b").cloneNode(True)),
]:
try:
node = func()
except Exception as exc:
print(f"{label}: {type(exc).__name__}: {exc}")
else:
print(f"{label}: {node.nodeName}")
Output on main:
cloneNode: NamespaceErr: cannot use the prefix 'xmlns' with an empty namespace
importNode: NamespaceErr: cannot use the prefix 'xmlns' with an empty namespace
Attr.cloneNode: NamespaceErr: cannot use the prefix 'a' with an empty namespace
Expected, as in 3.15 and earlier:
cloneNode: root
importNode: root
Attr.cloneNode: a:b
_clone_node() creates the clones with createElementNS(), setAttributeNS() and createAttributeNS(), and these now reject a prefixed name when the namespace is empty. A plain setAttribute("xmlns", ...) fails the same way, because the xmlns attribute is only allowed in the XMLNS namespace.
CPython versions tested on:
CPython main branch (3.13 and 3.12 are not affected)
Operating systems tested on:
Windows
Linked PRs
Bug report
Bug description:
Since gh-156665,
Node.cloneNode(),Document.importNode()andAttr.cloneNode()raisexml.dom.NamespaceErrfor DOM Level 1 nodes that have a colon in the name. Such nodes have no namespace and are created bycreateElement("svg:rect"),createAttribute("a:b"),setAttribute("xmlns:xsi", ...), or by parsing with namespace processing disabled.Output on main:
Expected, as in 3.15 and earlier:
_clone_node()creates the clones withcreateElementNS(),setAttributeNS()andcreateAttributeNS(), and these now reject a prefixed name when the namespace is empty. A plainsetAttribute("xmlns", ...)fails the same way, because thexmlnsattribute is only allowed in the XMLNS namespace.CPython versions tested on:
CPython main branch (3.13 and 3.12 are not affected)
Operating systems tested on:
Windows
Linked PRs