Skip to content

Commit 2d862b1

Browse files
authored
gh-158978: Fix cloning of DOM Level 1 nodes in xml.dom.minidom (GH-158987)
Node.cloneNode() and Document.importNode() raised NamespaceErr for nodes without a namespace whose name contains a colon, for example created with createElement("svg:rect"). Such nodes are now cloned with the DOM Level 1 factory methods, which keeps also their prefix.
1 parent 72ba954 commit 2d862b1

2 files changed

Lines changed: 90 additions & 6 deletions

File tree

‎Lib/test/test_minidom.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from test import support
77
import unittest
88

9+
import xml.dom.expatbuilder
910
import xml.dom.minidom
1011

1112
from xml.dom.minidom import parse, Attr, Node, Document, Element, parseString
@@ -1277,6 +1278,52 @@ def testCloneElementDeep(self):
12771278
self.assertEqual(clone.toxml(), '<doc attr="value"><foo/></doc>')
12781279
dom.unlink()
12791280

1281+
def create_dom1_element(self):
1282+
# DOM Level 1 nodes have no namespace, but their names can contain
1283+
# a colon.
1284+
doc = Document()
1285+
root = doc.appendChild(doc.createElement("root"))
1286+
root.setAttribute("xmlns", "http://xml.python.org/default")
1287+
root.setAttribute("xmlns:xsi", "http://xml.python.org/xsi")
1288+
root.setAttribute("xsi:type", "t")
1289+
root.setAttributeNS("http://xml.python.org/ns", "p:a", "v")
1290+
root.appendChild(doc.createElement("svg:rect"))
1291+
root.appendChild(
1292+
doc.createElementNS("http://xml.python.org/ns", "p:b"))
1293+
return doc, root
1294+
1295+
def check_clone_names(self, clone, node):
1296+
for name in ("nodeName", "prefix", "localName", "namespaceURI"):
1297+
self.assertEqual(getattr(clone, name), getattr(node, name), name)
1298+
1299+
def check_clone_element(self, clone, elem, doc):
1300+
self.assertIs(clone.ownerDocument, doc)
1301+
self.assertIsNone(clone.parentNode)
1302+
self.check_clone_names(clone, elem)
1303+
self.assertEqual(len(clone.attributes), len(elem.attributes))
1304+
for attr in elem.attributes.values():
1305+
with self.subTest(attr=attr.name):
1306+
a = clone.getAttributeNode(attr.name)
1307+
self.check_clone_names(a, attr)
1308+
self.assertEqual(a.value, attr.value)
1309+
self.assertIs(a.ownerElement, clone)
1310+
self.assertEqual(len(clone.childNodes), len(elem.childNodes))
1311+
for c, child in zip(clone.childNodes, elem.childNodes):
1312+
self.check_clone_names(c, child)
1313+
1314+
def testCloneElementDOM1Names(self):
1315+
doc, root = self.create_dom1_element()
1316+
self.check_clone_element(root.cloneNode(True), root, doc)
1317+
doc.unlink()
1318+
1319+
def testCloneElementNoNamespaces(self):
1320+
doc = xml.dom.expatbuilder.parseString(
1321+
"<a:b xmlns:a='http://xml.python.org/a' a:x='1'><a:c/></a:b>",
1322+
namespaces=False)
1323+
root = doc.documentElement
1324+
self.check_clone_element(root.cloneNode(True), root, doc)
1325+
doc.unlink()
1326+
12801327
def testCloneDocumentShallow(self):
12811328
doc = parseString("<?xml version='1.0'?>\n"
12821329
"<!-- comment -->"
@@ -1395,6 +1442,13 @@ def testImportDocumentTypeDeep(self):
13951442
self.assertRaises(xml.dom.NotSupportedErr, target.importNode,
13961443
src.doctype, 1)
13971444

1445+
def testImportElementDOM1Names(self):
1446+
doc1, root = self.create_dom1_element()
1447+
doc2 = Document()
1448+
self.check_clone_element(doc2.importNode(root, True), root, doc2)
1449+
doc1.unlink()
1450+
doc2.unlink()
1451+
13981452
# Testing attribute clones uses a helper, and should always be deep,
13991453
# even if the argument to cloneNode is false.
14001454
def check_clone_attribute(self, deep, testName):
@@ -1417,6 +1471,24 @@ def testCloneAttributeShallow(self):
14171471
def testCloneAttributeDeep(self):
14181472
self.check_clone_attribute(1, "testCloneAttributeDeep")
14191473

1474+
def testCloneAttributeDOM1Names(self):
1475+
doc1 = Document()
1476+
attr = doc1.createAttribute("a:b")
1477+
attr.value = "v"
1478+
doc2 = Document()
1479+
for method, clone, owner in [
1480+
("cloneNode", attr.cloneNode(True), doc1),
1481+
("importNode", doc2.importNode(attr, True), doc2),
1482+
]:
1483+
with self.subTest(method=method):
1484+
self.check_clone_names(clone, attr)
1485+
self.assertEqual(clone.value, "v")
1486+
self.assertTrue(clone.specified)
1487+
self.assertIsNone(clone.ownerElement)
1488+
self.assertIs(clone.ownerDocument, owner)
1489+
doc1.unlink()
1490+
doc2.unlink()
1491+
14201492
def check_clone_pi(self, deep, testName):
14211493
doc = parseString("<?target data?><doc/>")
14221494
pi = doc.firstChild

‎Lib/xml/dom/minidom.py‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,11 +2170,20 @@ def _clone_node(node, deep, newOwnerDocument):
21702170
else:
21712171
operation = xml.dom.UserDataHandler.NODE_IMPORTED
21722172
if node.nodeType == Node.ELEMENT_NODE:
2173-
clone = newOwnerDocument.createElementNS(node.namespaceURI,
2174-
node.nodeName)
2173+
if node.namespaceURI is None:
2174+
clone = newOwnerDocument.createElement(node.nodeName)
2175+
else:
2176+
clone = newOwnerDocument.createElementNS(node.namespaceURI,
2177+
node.nodeName)
21752178
for attr in node.attributes.values():
2176-
clone.setAttributeNS(attr.namespaceURI, attr.nodeName, attr.value)
2177-
a = clone.getAttributeNodeNS(attr.namespaceURI, attr.localName)
2179+
if attr.namespaceURI is None:
2180+
clone.setAttribute(attr.nodeName, attr.value)
2181+
a = clone.getAttributeNode(attr.nodeName)
2182+
else:
2183+
clone.setAttributeNS(attr.namespaceURI, attr.nodeName,
2184+
attr.value)
2185+
a = clone.getAttributeNodeNS(attr.namespaceURI,
2186+
attr.localName)
21782187
a.specified = attr.specified
21792188

21802189
if deep:
@@ -2201,8 +2210,11 @@ def _clone_node(node, deep, newOwnerDocument):
22012210
elif node.nodeType == Node.ENTITY_REFERENCE_NODE:
22022211
clone = newOwnerDocument.createEntityReference(node.nodeName)
22032212
elif node.nodeType == Node.ATTRIBUTE_NODE:
2204-
clone = newOwnerDocument.createAttributeNS(node.namespaceURI,
2205-
node.nodeName)
2213+
if node.namespaceURI is None:
2214+
clone = newOwnerDocument.createAttribute(node.nodeName)
2215+
else:
2216+
clone = newOwnerDocument.createAttributeNS(node.namespaceURI,
2217+
node.nodeName)
22062218
clone.specified = True
22072219
clone.value = node.value
22082220
elif node.nodeType == Node.DOCUMENT_TYPE_NODE:

0 commit comments

Comments
 (0)