66from test import support
77import unittest
88
9+ import xml .dom .expatbuilder
910import xml .dom .minidom
1011
1112from 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
0 commit comments