Debian Codegraph @

lib-python/3/test/test_minidom.py

raw symbols SCIP
1 # test for xml.dom.minidom
2
3 import copy
4 import pickle
5 import time
6 import io
7 from test import support
8 import unittest
9
10 import pyexpat
11 import xml.dom.minidom
12
13 from xml.dom.minidom import parse, Attr, Node, Document, Element, parseString
14 from xml.dom.minidom import getDOMImplementation
15 from xml.parsers.expat import ExpatError
16
17
18 tstfile = support.findfile("test.xml", subdir="xmltestdata")
19 sample = ("<?xml version='1.0' encoding='us-ascii'?>\n"
20 "<!DOCTYPE doc PUBLIC 'http://xml.python.org/public'"
21 " 'http://xml.python.org/system' [\n"
22 " <!ELEMENT e EMPTY>\n"
23 " <!ENTITY ent SYSTEM 'http://xml.python.org/entity'>\n"
24 "]><doc attr='value'> text\n"
25 "<?pi sample?> <!-- comment --> <e/> </doc>")
26
27 # The tests of DocumentType importing use these helpers to construct
28 # the documents to work with, since not all DOM builders actually
29 # create the DocumentType nodes.
30 def create_doc_without_doctype(doctype=None):
31 return getDOMImplementation().createDocument(None, "doc", doctype)
32
33 def create_nonempty_doctype():
34 doctype = getDOMImplementation().createDocumentType("doc", None, None)
35 doctype.entities._seq = []
36 doctype.notations._seq = []
37 notation = xml.dom.minidom.Notation("my-notation", None,
38 "http://xml.python.org/notations/my")
39 doctype.notations._seq.append(notation)
40 entity = xml.dom.minidom.Entity("my-entity", None,
41 "http://xml.python.org/entities/my",
42 "my-notation")
43 entity.version = "1.0"
44 entity.encoding = "utf-8"
45 entity.actualEncoding = "us-ascii"
46 doctype.entities._seq.append(entity)
47 return doctype
48
49 def create_doc_with_doctype():
50 doctype = create_nonempty_doctype()
51 doc = create_doc_without_doctype(doctype)
52 doctype.entities.item(0).ownerDocument = doc
53 doctype.notations.item(0).ownerDocument = doc
54 return doc
55
56 class MinidomTest(unittest.TestCase):
57 def confirm(self, test, testname = "Test"):
58 self.assertTrue(test, testname)
59
60 def checkWholeText(self, node, s):
61 t = node.wholeText
62 self.confirm(t == s, "looking for %r, found %r" % (s, t))
63
64 def testDocumentAsyncAttr(self):
65 doc = Document()
66 self.assertFalse(doc.async_)
67 self.assertFalse(Document.async_)
68
69 def testParseFromBinaryFile(self):
70 with open(tstfile, 'rb') as file:
71 dom = parse(file)
72 dom.unlink()
73 self.confirm(isinstance(dom, Document))
74
75 def testParseFromTextFile(self):
76 with open(tstfile, 'r', encoding='iso-8859-1') as file:
77 dom = parse(file)
78 dom.unlink()
79 self.confirm(isinstance(dom, Document))
80
81 def testAttrModeSetsParamsAsAttrs(self):
82 attr = Attr("qName", "namespaceURI", "localName", "prefix")
83 self.assertEqual(attr.name, "qName")
84 self.assertEqual(attr.namespaceURI, "namespaceURI")
85 self.assertEqual(attr.prefix, "prefix")
86 self.assertEqual(attr.localName, "localName")
87
88 def testAttrModeSetsNonOptionalAttrs(self):
89 attr = Attr("qName", "namespaceURI", None, "prefix")
90 self.assertEqual(attr.name, "qName")
91 self.assertEqual(attr.namespaceURI, "namespaceURI")
92 self.assertEqual(attr.prefix, "prefix")
93 self.assertEqual(attr.localName, attr.name)
94
95 def testGetElementsByTagName(self):
96 dom = parse(tstfile)
97 self.confirm(dom.getElementsByTagName("LI") == \
98 dom.documentElement.getElementsByTagName("LI"))
99 dom.unlink()
100
101 def testInsertBefore(self):
102 dom = parseString("<doc><foo/></doc>")
103 root = dom.documentElement
104 elem = root.childNodes[0]
105 nelem = dom.createElement("element")
106 root.insertBefore(nelem, elem)
107 self.confirm(len(root.childNodes) == 2
108 and root.childNodes.length == 2
109 and root.childNodes[0] is nelem
110 and root.childNodes.item(0) is nelem
111 and root.childNodes[1] is elem
112 and root.childNodes.item(1) is elem
113 and root.firstChild is nelem
114 and root.lastChild is elem
115 and root.toxml() == "<doc><element/><foo/></doc>"
116 , "testInsertBefore -- node properly placed in tree")
117 nelem = dom.createElement("element")
118 root.insertBefore(nelem, None)
119 self.confirm(len(root.childNodes) == 3
120 and root.childNodes.length == 3
121 and root.childNodes[1] is elem
122 and root.childNodes.item(1) is elem
123 and root.childNodes[2] is nelem
124 and root.childNodes.item(2) is nelem
125 and root.lastChild is nelem
126 and nelem.previousSibling is elem
127 and root.toxml() == "<doc><element/><foo/><element/></doc>"
128 , "testInsertBefore -- node properly placed in tree")
129 nelem2 = dom.createElement("bar")
130 root.insertBefore(nelem2, nelem)
131 self.confirm(len(root.childNodes) == 4
132 and root.childNodes.length == 4
133 and root.childNodes[2] is nelem2
134 and root.childNodes.item(2) is nelem2
135 and root.childNodes[3] is nelem
136 and root.childNodes.item(3) is nelem
137 and nelem2.nextSibling is nelem
138 and nelem.previousSibling is nelem2
139 and root.toxml() ==
140 "<doc><element/><foo/><bar/><element/></doc>"
141 , "testInsertBefore -- node properly placed in tree")
142 dom.unlink()
143
144 def _create_fragment_test_nodes(self):
145 dom = parseString("<doc/>")
146 orig = dom.createTextNode("original")
147 c1 = dom.createTextNode("foo")
148 c2 = dom.createTextNode("bar")
149 c3 = dom.createTextNode("bat")
150 dom.documentElement.appendChild(orig)
151 frag = dom.createDocumentFragment()
152 frag.appendChild(c1)
153 frag.appendChild(c2)
154 frag.appendChild(c3)
155 return dom, orig, c1, c2, c3, frag
156
157 def testInsertBeforeFragment(self):
158 dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()
159 dom.documentElement.insertBefore(frag, None)
160 self.confirm(tuple(dom.documentElement.childNodes) ==
161 (orig, c1, c2, c3),
162 "insertBefore(<fragment>, None)")
163 frag.unlink()
164 dom.unlink()
165
166 dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()
167 dom.documentElement.insertBefore(frag, orig)
168 self.confirm(tuple(dom.documentElement.childNodes) ==
169 (c1, c2, c3, orig),
170 "insertBefore(<fragment>, orig)")
171 frag.unlink()
172 dom.unlink()
173
174 def testAppendChild(self):
175 dom = parse(tstfile)
176 dom.documentElement.appendChild(dom.createComment("Hello"))
177 self.confirm(dom.documentElement.childNodes[-1].nodeName == "#comment")
178 self.confirm(dom.documentElement.childNodes[-1].data == "Hello")
179 dom.unlink()
180
181 @support.requires_resource('cpu')
182 def testAppendChildNoQuadraticComplexity(self):
183 impl = getDOMImplementation()
184
185 newdoc = impl.createDocument(None, "some_tag", None)
186 top_element = newdoc.documentElement
187 children = [newdoc.createElement(f"child-{i}") for i in range(1, 2 ** 15 + 1)]
188 element = top_element
189
190 start = time.monotonic()
191 for child in children:
192 element.appendChild(child)
193 element = child
194 end = time.monotonic()
195
196 # This example used to take at least 30 seconds.
197 # Conservative assertion due to the wide variety of systems and
198 # build configs timing based tests wind up run under.
199 # A --with-address-sanitizer --with-pydebug build on a rpi5 still
200 # completes this loop in <0.5 seconds.
201 self.assertLess(end - start, 4)
202
203 def testSetAttributeNodeWithoutOwnerDocument(self):
204 # regression test for gh-142754
205 elem = Element("test")
206 attr = Attr("id")
207 attr.value = "test-id"
208 elem.setAttributeNode(attr)
209 self.assertEqual(elem.getAttribute("id"), "test-id")
210
211 def testAppendChildFragment(self):
212 dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()
213 dom.documentElement.appendChild(frag)
214 self.confirm(tuple(dom.documentElement.childNodes) ==
215 (orig, c1, c2, c3),
216 "appendChild(<fragment>)")
217 frag.unlink()
218 dom.unlink()
219
220 def testReplaceChildFragment(self):
221 dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()
222 dom.documentElement.replaceChild(frag, orig)
223 orig.unlink()
224 self.confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3),
225 "replaceChild(<fragment>)")
226 frag.unlink()
227 dom.unlink()
228
229 def testLegalChildren(self):
230 dom = Document()
231 elem = dom.createElement('element')
232 text = dom.createTextNode('text')
233 self.assertRaises(xml.dom.HierarchyRequestErr, dom.appendChild, text)
234
235 dom.appendChild(elem)
236 self.assertRaises(xml.dom.HierarchyRequestErr, dom.insertBefore, text,
237 elem)
238 self.assertRaises(xml.dom.HierarchyRequestErr, dom.replaceChild, text,
239 elem)
240
241 nodemap = elem.attributes
242 self.assertRaises(xml.dom.HierarchyRequestErr, nodemap.setNamedItem,
243 text)
244 self.assertRaises(xml.dom.HierarchyRequestErr, nodemap.setNamedItemNS,
245 text)
246
247 elem.appendChild(text)
248 dom.unlink()
249
250 def testNamedNodeMapSetItem(self):
251 dom = Document()
252 elem = dom.createElement('element')
253 attrs = elem.attributes
254 attrs["foo"] = "bar"
255 a = attrs.item(0)
256 self.confirm(a.ownerDocument is dom,
257 "NamedNodeMap.__setitem__() sets ownerDocument")
258 self.confirm(a.ownerElement is elem,
259 "NamedNodeMap.__setitem__() sets ownerElement")
260 self.confirm(a.value == "bar",
261 "NamedNodeMap.__setitem__() sets value")
262 self.confirm(a.nodeValue == "bar",
263 "NamedNodeMap.__setitem__() sets nodeValue")
264 elem.unlink()
265 dom.unlink()
266
267 def testNonZero(self):
268 dom = parse(tstfile)
269 self.confirm(dom)# should not be zero
270 dom.appendChild(dom.createComment("foo"))
271 self.confirm(not dom.childNodes[-1].childNodes)
272 dom.unlink()
273
274 def testUnlink(self):
275 dom = parse(tstfile)
276 self.assertTrue(dom.childNodes)
277 dom.unlink()
278 self.assertFalse(dom.childNodes)
279
280 def testContext(self):
281 with parse(tstfile) as dom:
282 self.assertTrue(dom.childNodes)
283 self.assertFalse(dom.childNodes)
284
285 def testElement(self):
286 dom = Document()
287 dom.appendChild(dom.createElement("abc"))
288 self.confirm(dom.documentElement)
289 dom.unlink()
290
291 def testAAA(self):
292 dom = parseString("<abc/>")
293 el = dom.documentElement
294 el.setAttribute("spam", "jam2")
295 self.confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA")
296 a = el.getAttributeNode("spam")
297 self.confirm(a.ownerDocument is dom,
298 "setAttribute() sets ownerDocument")
299 self.confirm(a.ownerElement is dom.documentElement,
300 "setAttribute() sets ownerElement")
301 dom.unlink()
302
303 def testAAB(self):
304 dom = parseString("<abc/>")
305 el = dom.documentElement
306 el.setAttribute("spam", "jam")
307 el.setAttribute("spam", "jam2")
308 self.confirm(el.toxml() == '<abc spam="jam2"/>', "testAAB")
309 dom.unlink()
310
311 def testAddAttr(self):
312 dom = Document()
313 child = dom.appendChild(dom.createElement("abc"))
314
315 child.setAttribute("def", "ghi")
316 self.confirm(child.getAttribute("def") == "ghi")
317 self.confirm(child.attributes["def"].value == "ghi")
318
319 child.setAttribute("jkl", "mno")
320 self.confirm(child.getAttribute("jkl") == "mno")
321 self.confirm(child.attributes["jkl"].value == "mno")
322
323 self.confirm(len(child.attributes) == 2)
324
325 child.setAttribute("def", "newval")
326 self.confirm(child.getAttribute("def") == "newval")
327 self.confirm(child.attributes["def"].value == "newval")
328
329 self.confirm(len(child.attributes) == 2)
330 dom.unlink()
331
332 def testDeleteAttr(self):
333 dom = Document()
334 child = dom.appendChild(dom.createElement("abc"))
335
336 self.confirm(len(child.attributes) == 0)
337 child.setAttribute("def", "ghi")
338 self.confirm(len(child.attributes) == 1)
339 del child.attributes["def"]
340 self.confirm(len(child.attributes) == 0)
341 dom.unlink()
342
343 def testRemoveAttr(self):
344 dom = Document()
345 child = dom.appendChild(dom.createElement("abc"))
346
347 child.setAttribute("def", "ghi")
348 self.confirm(len(child.attributes) == 1)
349 self.assertRaises(xml.dom.NotFoundErr, child.removeAttribute, "foo")
350 child.removeAttribute("def")
351 self.confirm(len(child.attributes) == 0)
352 dom.unlink()
353
354 def testRemoveAttrNS(self):
355 dom = Document()
356 child = dom.appendChild(
357 dom.createElementNS("http://www.python.org", "python:abc"))
358 child.setAttributeNS("http://www.w3.org", "xmlns:python",
359 "http://www.python.org")
360 child.setAttributeNS("http://www.python.org", "python:abcattr", "foo")
361 self.assertRaises(xml.dom.NotFoundErr, child.removeAttributeNS,
362 "foo", "http://www.python.org")
363 self.confirm(len(child.attributes) == 2)
364 child.removeAttributeNS("http://www.python.org", "abcattr")
365 self.confirm(len(child.attributes) == 1)
366 dom.unlink()
367
368 def testRemoveAttributeNode(self):
369 dom = Document()
370 child = dom.appendChild(dom.createElement("foo"))
371 child.setAttribute("spam", "jam")
372 self.confirm(len(child.attributes) == 1)
373 node = child.getAttributeNode("spam")
374 self.assertRaises(xml.dom.NotFoundErr, child.removeAttributeNode,
375 None)
376 self.assertIs(node, child.removeAttributeNode(node))
377 self.confirm(len(child.attributes) == 0
378 and child.getAttributeNode("spam") is None)
379 dom2 = Document()
380 child2 = dom2.appendChild(dom2.createElement("foo"))
381 node2 = child2.getAttributeNode("spam")
382 self.assertRaises(xml.dom.NotFoundErr, child2.removeAttributeNode,
383 node2)
384 dom.unlink()
385
386 def testHasAttribute(self):
387 dom = Document()
388 child = dom.appendChild(dom.createElement("foo"))
389 child.setAttribute("spam", "jam")
390 self.confirm(child.hasAttribute("spam"))
391
392 def testChangeAttr(self):
393 dom = parseString("<abc/>")
394 el = dom.documentElement
395 el.setAttribute("spam", "jam")
396 self.confirm(len(el.attributes) == 1)
397 el.setAttribute("spam", "bam")
398 # Set this attribute to be an ID and make sure that doesn't change
399 # when changing the value:
400 el.setIdAttribute("spam")
401 self.confirm(len(el.attributes) == 1
402 and el.attributes["spam"].value == "bam"
403 and el.attributes["spam"].nodeValue == "bam"
404 and el.getAttribute("spam") == "bam"
405 and el.getAttributeNode("spam").isId)
406 el.attributes["spam"] = "ham"
407 self.confirm(len(el.attributes) == 1
408 and el.attributes["spam"].value == "ham"
409 and el.attributes["spam"].nodeValue == "ham"
410 and el.getAttribute("spam") == "ham"
411 and el.attributes["spam"].isId)
412 el.setAttribute("spam2", "bam")
413 self.confirm(len(el.attributes) == 2
414 and el.attributes["spam"].value == "ham"
415 and el.attributes["spam"].nodeValue == "ham"
416 and el.getAttribute("spam") == "ham"
417 and el.attributes["spam2"].value == "bam"
418 and el.attributes["spam2"].nodeValue == "bam"
419 and el.getAttribute("spam2") == "bam")
420 el.attributes["spam2"] = "bam2"
421 self.confirm(len(el.attributes) == 2
422 and el.attributes["spam"].value == "ham"
423 and el.attributes["spam"].nodeValue == "ham"
424 and el.getAttribute("spam") == "ham"
425 and el.attributes["spam2"].value == "bam2"
426 and el.attributes["spam2"].nodeValue == "bam2"
427 and el.getAttribute("spam2") == "bam2")
428 dom.unlink()
429
430 def testGetAttrList(self):
431 pass
432
433 def testGetAttrValues(self):
434 pass
435
436 def testGetAttrLength(self):
437 pass
438
439 def testGetAttribute(self):
440 dom = Document()
441 child = dom.appendChild(
442 dom.createElementNS("http://www.python.org", "python:abc"))
443 self.assertEqual(child.getAttribute('missing'), '')
444
445 def testGetAttributeNS(self):
446 dom = Document()
447 child = dom.appendChild(
448 dom.createElementNS("http://www.python.org", "python:abc"))
449 child.setAttributeNS("http://www.w3.org", "xmlns:python",
450 "http://www.python.org")
451 self.assertEqual(child.getAttributeNS("http://www.w3.org", "python"),
452 'http://www.python.org')
453 self.assertEqual(child.getAttributeNS("http://www.w3.org", "other"),
454 '')
455 child2 = child.appendChild(dom.createElement('abc'))
456 self.assertEqual(child2.getAttributeNS("http://www.python.org", "missing"),
457 '')
458
459 def testGetAttributeNode(self): pass
460
461 def testGetElementsByTagNameNS(self):
462 d="""<foo xmlns:minidom='http://pyxml.sf.net/minidom'>
463 <minidom:myelem/>
464 </foo>"""
465 dom = parseString(d)
466 elems = dom.getElementsByTagNameNS("http://pyxml.sf.net/minidom",
467 "myelem")
468 self.confirm(len(elems) == 1
469 and elems[0].namespaceURI == "http://pyxml.sf.net/minidom"
470 and elems[0].localName == "myelem"
471 and elems[0].prefix == "minidom"
472 and elems[0].tagName == "minidom:myelem"
473 and elems[0].nodeName == "minidom:myelem")
474 dom.unlink()
475
476 def get_empty_nodelist_from_elements_by_tagName_ns_helper(self, doc, nsuri,
477 lname):
478 nodelist = doc.getElementsByTagNameNS(nsuri, lname)
479 self.confirm(len(nodelist) == 0)
480
481 def testGetEmptyNodeListFromElementsByTagNameNS(self):
482 doc = parseString('<doc/>')
483 self.get_empty_nodelist_from_elements_by_tagName_ns_helper(
484 doc, 'http://xml.python.org/namespaces/a', 'localname')
485 self.get_empty_nodelist_from_elements_by_tagName_ns_helper(
486 doc, '*', 'splat')
487 self.get_empty_nodelist_from_elements_by_tagName_ns_helper(
488 doc, 'http://xml.python.org/namespaces/a', '*')
489
490 doc = parseString('<doc xmlns="http://xml.python.org/splat"><e/></doc>')
491 self.get_empty_nodelist_from_elements_by_tagName_ns_helper(
492 doc, "http://xml.python.org/splat", "not-there")
493 self.get_empty_nodelist_from_elements_by_tagName_ns_helper(
494 doc, "*", "not-there")
495 self.get_empty_nodelist_from_elements_by_tagName_ns_helper(
496 doc, "http://somewhere.else.net/not-there", "e")
497
498 def testElementReprAndStr(self):
499 dom = Document()
500 el = dom.appendChild(dom.createElement("abc"))
501 string1 = repr(el)
502 string2 = str(el)
503 self.confirm(string1 == string2)
504 dom.unlink()
505
506 def testElementReprAndStrUnicode(self):
507 dom = Document()
508 el = dom.appendChild(dom.createElement("abc"))
509 string1 = repr(el)
510 string2 = str(el)
511 self.confirm(string1 == string2)
512 dom.unlink()
513
514 def testElementReprAndStrUnicodeNS(self):
515 dom = Document()
516 el = dom.appendChild(
517 dom.createElementNS("http://www.slashdot.org", "slash:abc"))
518 string1 = repr(el)
519 string2 = str(el)
520 self.confirm(string1 == string2)
521 self.confirm("slash:abc" in string1)
522 dom.unlink()
523
524 def testAttributeRepr(self):
525 dom = Document()
526 el = dom.appendChild(dom.createElement("abc"))
527 node = el.setAttribute("abc", "def")
528 self.confirm(str(node) == repr(node))
529 dom.unlink()
530
531 def testTextNodeRepr(self): pass
532
533 def testWriteXML(self):
534 str = '<?xml version="1.0" ?><a b="c"/>'
535 dom = parseString(str)
536 domstr = dom.toxml()
537 dom.unlink()
538 self.confirm(str == domstr)
539
540 def testAltNewline(self):
541 str = '<?xml version="1.0" ?>\n<a b="c"/>\n'
542 dom = parseString(str)
543 domstr = dom.toprettyxml(newl="\r\n")
544 dom.unlink()
545 self.confirm(domstr == str.replace("\n", "\r\n"))
546
547 def test_toprettyxml_with_text_nodes(self):
548 # see issue #4147, text nodes are not indented
549 decl = '<?xml version="1.0" ?>\n'
550 self.assertEqual(parseString('<B>A</B>').toprettyxml(),
551 decl + '<B>A</B>\n')
552 self.assertEqual(parseString('<C>A<B>A</B></C>').toprettyxml(),
553 decl + '<C>\n\tA\n\t<B>A</B>\n</C>\n')
554 self.assertEqual(parseString('<C><B>A</B>A</C>').toprettyxml(),
555 decl + '<C>\n\t<B>A</B>\n\tA\n</C>\n')
556 self.assertEqual(parseString('<C><B>A</B><B>A</B></C>').toprettyxml(),
557 decl + '<C>\n\t<B>A</B>\n\t<B>A</B>\n</C>\n')
558 self.assertEqual(parseString('<C><B>A</B>A<B>A</B></C>').toprettyxml(),
559 decl + '<C>\n\t<B>A</B>\n\tA\n\t<B>A</B>\n</C>\n')
560
561 def test_toprettyxml_with_adjacent_text_nodes(self):
562 # see issue #4147, adjacent text nodes are indented normally
563 dom = Document()
564 elem = dom.createElement('elem')
565 elem.appendChild(dom.createTextNode('TEXT'))
566 elem.appendChild(dom.createTextNode('TEXT'))
567 dom.appendChild(elem)
568 decl = '<?xml version="1.0" ?>\n'
569 self.assertEqual(dom.toprettyxml(),
570 decl + '<elem>\n\tTEXT\n\tTEXT\n</elem>\n')
571
572 def test_toprettyxml_preserves_content_of_text_node(self):
573 # see issue #4147
574 for str in ('<B>A</B>', '<A><B>C</B></A>'):
575 dom = parseString(str)
576 dom2 = parseString(dom.toprettyxml())
577 self.assertEqual(
578 dom.getElementsByTagName('B')[0].childNodes[0].toxml(),
579 dom2.getElementsByTagName('B')[0].childNodes[0].toxml())
580
581 def testProcessingInstruction(self):
582 dom = parseString('<e><?mypi \t\n data \t\n ?></e>')
583 pi = dom.documentElement.firstChild
584 self.confirm(pi.target == "mypi"
585 and pi.data == "data \t\n "
586 and pi.nodeName == "mypi"
587 and pi.nodeType == Node.PROCESSING_INSTRUCTION_NODE
588 and pi.attributes is None
589 and not pi.hasChildNodes()
590 and len(pi.childNodes) == 0
591 and pi.firstChild is None
592 and pi.lastChild is None
593 and pi.localName is None
594 and pi.namespaceURI == xml.dom.EMPTY_NAMESPACE)
595
596 def testProcessingInstructionRepr(self): pass
597
598 def testTextRepr(self): pass
599
600 def testWriteText(self): pass
601
602 def testDocumentElement(self): pass
603
604 def testTooManyDocumentElements(self):
605 doc = parseString("<doc/>")
606 elem = doc.createElement("extra")
607 # Should raise an exception when adding an extra document element.
608 self.assertRaises(xml.dom.HierarchyRequestErr, doc.appendChild, elem)
609 elem.unlink()
610 doc.unlink()
611
612 def testCreateElementNS(self): pass
613
614 def testCreateAttributeNS(self): pass
615
616 def testParse(self): pass
617
618 def testParseString(self): pass
619
620 def testComment(self): pass
621
622 def testAttrListItem(self): pass
623
624 def testAttrListItems(self): pass
625
626 def testAttrListItemNS(self): pass
627
628 def testAttrListKeys(self): pass
629
630 def testAttrListKeysNS(self): pass
631
632 def testRemoveNamedItem(self):
633 doc = parseString("<doc a=''/>")
634 e = doc.documentElement
635 attrs = e.attributes
636 a1 = e.getAttributeNode("a")
637 a2 = attrs.removeNamedItem("a")
638 self.confirm(a1.isSameNode(a2))
639 self.assertRaises(xml.dom.NotFoundErr, attrs.removeNamedItem, "a")
640
641 def testRemoveNamedItemNS(self):
642 doc = parseString("<doc xmlns:a='http://xml.python.org/' a:b=''/>")
643 e = doc.documentElement
644 attrs = e.attributes
645 a1 = e.getAttributeNodeNS("http://xml.python.org/", "b")
646 a2 = attrs.removeNamedItemNS("http://xml.python.org/", "b")
647 self.confirm(a1.isSameNode(a2))
648 self.assertRaises(xml.dom.NotFoundErr, attrs.removeNamedItemNS,
649 "http://xml.python.org/", "b")
650
651 def testAttrListValues(self): pass
652
653 def testAttrListLength(self): pass
654
655 def testAttrList__getitem__(self): pass
656
657 def testAttrList__setitem__(self): pass
658
659 def testSetAttrValueandNodeValue(self): pass
660
661 def testParseElement(self): pass
662
663 def testParseAttributes(self): pass
664
665 def testParseElementNamespaces(self): pass
666
667 def testParseAttributeNamespaces(self): pass
668
669 def testParseProcessingInstructions(self): pass
670
671 def testChildNodes(self): pass
672
673 def testFirstChild(self): pass
674
675 def testHasChildNodes(self):
676 dom = parseString("<doc><foo/></doc>")
677 doc = dom.documentElement
678 self.assertTrue(doc.hasChildNodes())
679 dom2 = parseString("<doc/>")
680 doc2 = dom2.documentElement
681 self.assertFalse(doc2.hasChildNodes())
682
683 def _testCloneElementCopiesAttributes(self, e1, e2, test):
684 attrs1 = e1.attributes
685 attrs2 = e2.attributes
686 keys1 = list(attrs1.keys())
687 keys2 = list(attrs2.keys())
688 keys1.sort()
689 keys2.sort()
690 self.confirm(keys1 == keys2, "clone of element has same attribute keys")
691 for i in range(len(keys1)):
692 a1 = attrs1.item(i)
693 a2 = attrs2.item(i)
694 self.confirm(a1 is not a2
695 and a1.value == a2.value
696 and a1.nodeValue == a2.nodeValue
697 and a1.namespaceURI == a2.namespaceURI
698 and a1.localName == a2.localName
699 , "clone of attribute node has proper attribute values")
700 self.confirm(a2.ownerElement is e2,
701 "clone of attribute node correctly owned")
702
703 def _setupCloneElement(self, deep):
704 dom = parseString("<doc attr='value'><foo/></doc>")
705 root = dom.documentElement
706 clone = root.cloneNode(deep)
707 self._testCloneElementCopiesAttributes(
708 root, clone, "testCloneElement" + (deep and "Deep" or "Shallow"))
709 # mutilate the original so shared data is detected
710 root.tagName = root.nodeName = "MODIFIED"
711 root.setAttribute("attr", "NEW VALUE")
712 root.setAttribute("added", "VALUE")
713 return dom, clone
714
715 def testCloneElementShallow(self):
716 dom, clone = self._setupCloneElement(0)
717 self.confirm(len(clone.childNodes) == 0
718 and clone.childNodes.length == 0
719 and clone.parentNode is None
720 and clone.toxml() == '<doc attr="value"/>'
721 , "testCloneElementShallow")
722 dom.unlink()
723
724 def testCloneElementDeep(self):
725 dom, clone = self._setupCloneElement(1)
726 self.confirm(len(clone.childNodes) == 1
727 and clone.childNodes.length == 1
728 and clone.parentNode is None
729 and clone.toxml() == '<doc attr="value"><foo/></doc>'
730 , "testCloneElementDeep")
731 dom.unlink()
732
733 def testCloneDocumentShallow(self):
734 doc = parseString("<?xml version='1.0'?>\n"
735 "<!-- comment -->"
736 "<!DOCTYPE doc [\n"
737 "<!NOTATION notation SYSTEM 'http://xml.python.org/'>\n"
738 "]>\n"
739 "<doc attr='value'/>")
740 doc2 = doc.cloneNode(0)
741 self.confirm(doc2 is None,
742 "testCloneDocumentShallow:"
743 " shallow cloning of documents makes no sense!")
744
745 def testCloneDocumentDeep(self):
746 doc = parseString("<?xml version='1.0'?>\n"
747 "<!-- comment -->"
748 "<!DOCTYPE doc [\n"
749 "<!NOTATION notation SYSTEM 'http://xml.python.org/'>\n"
750 "]>\n"
751 "<doc attr='value'/>")
752 doc2 = doc.cloneNode(1)
753 self.confirm(not (doc.isSameNode(doc2) or doc2.isSameNode(doc)),
754 "testCloneDocumentDeep: document objects not distinct")
755 self.confirm(len(doc.childNodes) == len(doc2.childNodes),
756 "testCloneDocumentDeep: wrong number of Document children")
757 self.confirm(doc2.documentElement.nodeType == Node.ELEMENT_NODE,
758 "testCloneDocumentDeep: documentElement not an ELEMENT_NODE")
759 self.confirm(doc2.documentElement.ownerDocument.isSameNode(doc2),
760 "testCloneDocumentDeep: documentElement owner is not new document")
761 self.confirm(not doc.documentElement.isSameNode(doc2.documentElement),
762 "testCloneDocumentDeep: documentElement should not be shared")
763 if doc.doctype is not None:
764 # check the doctype iff the original DOM maintained it
765 self.confirm(doc2.doctype.nodeType == Node.DOCUMENT_TYPE_NODE,
766 "testCloneDocumentDeep: doctype not a DOCUMENT_TYPE_NODE")
767 self.confirm(doc2.doctype.ownerDocument.isSameNode(doc2))
768 self.confirm(not doc.doctype.isSameNode(doc2.doctype))
769
770 def testCloneDocumentTypeDeepOk(self):
771 doctype = create_nonempty_doctype()
772 clone = doctype.cloneNode(1)
773 self.confirm(clone is not None
774 and clone.nodeName == doctype.nodeName
775 and clone.name == doctype.name
776 and clone.publicId == doctype.publicId
777 and clone.systemId == doctype.systemId
778 and len(clone.entities) == len(doctype.entities)
779 and clone.entities.item(len(clone.entities)) is None
780 and len(clone.notations) == len(doctype.notations)
781 and clone.notations.item(len(clone.notations)) is None
782 and len(clone.childNodes) == 0)
783 for i in range(len(doctype.entities)):
784 se = doctype.entities.item(i)
785 ce = clone.entities.item(i)
786 self.confirm((not se.isSameNode(ce))
787 and (not ce.isSameNode(se))
788 and ce.nodeName == se.nodeName
789 and ce.notationName == se.notationName
790 and ce.publicId == se.publicId
791 and ce.systemId == se.systemId
792 and ce.encoding == se.encoding
793 and ce.actualEncoding == se.actualEncoding
794 and ce.version == se.version)
795 for i in range(len(doctype.notations)):
796 sn = doctype.notations.item(i)
797 cn = clone.notations.item(i)
798 self.confirm((not sn.isSameNode(cn))
799 and (not cn.isSameNode(sn))
800 and cn.nodeName == sn.nodeName
801 and cn.publicId == sn.publicId
802 and cn.systemId == sn.systemId)
803
804 def testCloneDocumentTypeDeepNotOk(self):
805 doc = create_doc_with_doctype()
806 clone = doc.doctype.cloneNode(1)
807 self.confirm(clone is None, "testCloneDocumentTypeDeepNotOk")
808
809 def testCloneDocumentTypeShallowOk(self):
810 doctype = create_nonempty_doctype()
811 clone = doctype.cloneNode(0)
812 self.confirm(clone is not None
813 and clone.nodeName == doctype.nodeName
814 and clone.name == doctype.name
815 and clone.publicId == doctype.publicId
816 and clone.systemId == doctype.systemId
817 and len(clone.entities) == 0
818 and clone.entities.item(0) is None
819 and len(clone.notations) == 0
820 and clone.notations.item(0) is None
821 and len(clone.childNodes) == 0)
822
823 def testCloneDocumentTypeShallowNotOk(self):
824 doc = create_doc_with_doctype()
825 clone = doc.doctype.cloneNode(0)
826 self.confirm(clone is None, "testCloneDocumentTypeShallowNotOk")
827
828 def check_import_document(self, deep, testName):
829 doc1 = parseString("<doc/>")
830 doc2 = parseString("<doc/>")
831 self.assertRaises(xml.dom.NotSupportedErr, doc1.importNode, doc2, deep)
832
833 def testImportDocumentShallow(self):
834 self.check_import_document(0, "testImportDocumentShallow")
835
836 def testImportDocumentDeep(self):
837 self.check_import_document(1, "testImportDocumentDeep")
838
839 def testImportDocumentTypeShallow(self):
840 src = create_doc_with_doctype()
841 target = create_doc_without_doctype()
842 self.assertRaises(xml.dom.NotSupportedErr, target.importNode,
843 src.doctype, 0)
844
845 def testImportDocumentTypeDeep(self):
846 src = create_doc_with_doctype()
847 target = create_doc_without_doctype()
848 self.assertRaises(xml.dom.NotSupportedErr, target.importNode,
849 src.doctype, 1)
850
851 # Testing attribute clones uses a helper, and should always be deep,
852 # even if the argument to cloneNode is false.
853 def check_clone_attribute(self, deep, testName):
854 doc = parseString("<doc attr='value'/>")
855 attr = doc.documentElement.getAttributeNode("attr")
856 self.assertNotEqual(attr, None)
857 clone = attr.cloneNode(deep)
858 self.confirm(not clone.isSameNode(attr))
859 self.confirm(not attr.isSameNode(clone))
860 self.confirm(clone.ownerElement is None,
861 testName + ": ownerElement should be None")
862 self.confirm(clone.ownerDocument.isSameNode(attr.ownerDocument),
863 testName + ": ownerDocument does not match")
864 self.confirm(clone.specified,
865 testName + ": cloned attribute must have specified == True")
866
867 def testCloneAttributeShallow(self):
868 self.check_clone_attribute(0, "testCloneAttributeShallow")
869
870 def testCloneAttributeDeep(self):
871 self.check_clone_attribute(1, "testCloneAttributeDeep")
872
873 def check_clone_pi(self, deep, testName):
874 doc = parseString("<?target data?><doc/>")
875 pi = doc.firstChild
876 self.assertEqual(pi.nodeType, Node.PROCESSING_INSTRUCTION_NODE)
877 clone = pi.cloneNode(deep)
878 self.confirm(clone.target == pi.target
879 and clone.data == pi.data)
880
881 def testClonePIShallow(self):
882 self.check_clone_pi(0, "testClonePIShallow")
883
884 def testClonePIDeep(self):
885 self.check_clone_pi(1, "testClonePIDeep")
886
887 def check_clone_node_entity(self, clone_document):
888 # bpo-35052: Test user data handler in cloneNode() on a document with
889 # an entity
890 document = xml.dom.minidom.parseString("""
891 <?xml version="1.0" ?>
892 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
893 "http://www.w3.org/TR/html4/strict.dtd"
894 [ <!ENTITY smile "☺"> ]
895 >
896 <doc>Don't let entities make you frown &smile;</doc>
897 """.strip())
898
899 class Handler:
900 def handle(self, operation, key, data, src, dst):
901 self.operation = operation
902 self.key = key
903 self.data = data
904 self.src = src
905 self.dst = dst
906
907 handler = Handler()
908 doctype = document.doctype
909 entity = doctype.entities['smile']
910 entity.setUserData("key", "data", handler)
911
912 if clone_document:
913 # clone Document
914 clone = document.cloneNode(deep=True)
915
916 self.assertEqual(clone.documentElement.firstChild.wholeText,
917 "Don't let entities make you frown ☺")
918 operation = xml.dom.UserDataHandler.NODE_IMPORTED
919 dst = clone.doctype.entities['smile']
920 else:
921 # clone DocumentType
922 with support.swap_attr(doctype, 'ownerDocument', None):
923 clone = doctype.cloneNode(deep=True)
924
925 operation = xml.dom.UserDataHandler.NODE_CLONED
926 dst = clone.entities['smile']
927
928 self.assertEqual(handler.operation, operation)
929 self.assertEqual(handler.key, "key")
930 self.assertEqual(handler.data, "data")
931 self.assertIs(handler.src, entity)
932 self.assertIs(handler.dst, dst)
933
934 def testCloneNodeEntity(self):
935 self.check_clone_node_entity(False)
936 self.check_clone_node_entity(True)
937
938 def testNormalize(self):
939 doc = parseString("<doc/>")
940 root = doc.documentElement
941 root.appendChild(doc.createTextNode("first"))
942 root.appendChild(doc.createTextNode("second"))
943 self.confirm(len(root.childNodes) == 2
944 and root.childNodes.length == 2,
945 "testNormalize -- preparation")
946 doc.normalize()
947 self.confirm(len(root.childNodes) == 1
948 and root.childNodes.length == 1
949 and root.firstChild is root.lastChild
950 and root.firstChild.data == "firstsecond"
951 , "testNormalize -- result")
952 doc.unlink()
953
954 doc = parseString("<doc/>")
955 root = doc.documentElement
956 root.appendChild(doc.createTextNode(""))
957 doc.normalize()
958 self.confirm(len(root.childNodes) == 0
959 and root.childNodes.length == 0,
960 "testNormalize -- single empty node removed")
961 doc.unlink()
962
963 def testNormalizeCombineAndNextSibling(self):
964 doc = parseString("<doc/>")
965 root = doc.documentElement
966 root.appendChild(doc.createTextNode("first"))
967 root.appendChild(doc.createTextNode("second"))
968 root.appendChild(doc.createElement("i"))
969 self.confirm(len(root.childNodes) == 3
970 and root.childNodes.length == 3,
971 "testNormalizeCombineAndNextSibling -- preparation")
972 doc.normalize()
973 self.confirm(len(root.childNodes) == 2
974 and root.childNodes.length == 2
975 and root.firstChild.data == "firstsecond"
976 and root.firstChild is not root.lastChild
977 and root.firstChild.nextSibling is root.lastChild
978 and root.firstChild.previousSibling is None
979 and root.lastChild.previousSibling is root.firstChild
980 and root.lastChild.nextSibling is None
981 , "testNormalizeCombinedAndNextSibling -- result")
982 doc.unlink()
983
984 def testNormalizeDeleteWithPrevSibling(self):
985 doc = parseString("<doc/>")
986 root = doc.documentElement
987 root.appendChild(doc.createTextNode("first"))
988 root.appendChild(doc.createTextNode(""))
989 self.confirm(len(root.childNodes) == 2
990 and root.childNodes.length == 2,
991 "testNormalizeDeleteWithPrevSibling -- preparation")
992 doc.normalize()
993 self.confirm(len(root.childNodes) == 1
994 and root.childNodes.length == 1
995 and root.firstChild.data == "first"
996 and root.firstChild is root.lastChild
997 and root.firstChild.nextSibling is None
998 and root.firstChild.previousSibling is None
999 , "testNormalizeDeleteWithPrevSibling -- result")
1000 doc.unlink()
1001
1002 def testNormalizeDeleteWithNextSibling(self):
1003 doc = parseString("<doc/>")
1004 root = doc.documentElement
1005 root.appendChild(doc.createTextNode(""))
1006 root.appendChild(doc.createTextNode("second"))
1007 self.confirm(len(root.childNodes) == 2
1008 and root.childNodes.length == 2,
1009 "testNormalizeDeleteWithNextSibling -- preparation")
1010 doc.normalize()
1011 self.confirm(len(root.childNodes) == 1
1012 and root.childNodes.length == 1
1013 and root.firstChild.data == "second"
1014 and root.firstChild is root.lastChild
1015 and root.firstChild.nextSibling is None
1016 and root.firstChild.previousSibling is None
1017 , "testNormalizeDeleteWithNextSibling -- result")
1018 doc.unlink()
1019
1020 def testNormalizeDeleteWithTwoNonTextSiblings(self):
1021 doc = parseString("<doc/>")
1022 root = doc.documentElement
1023 root.appendChild(doc.createElement("i"))
1024 root.appendChild(doc.createTextNode(""))
1025 root.appendChild(doc.createElement("i"))
1026 self.confirm(len(root.childNodes) == 3
1027 and root.childNodes.length == 3,
1028 "testNormalizeDeleteWithTwoSiblings -- preparation")
1029 doc.normalize()
1030 self.confirm(len(root.childNodes) == 2
1031 and root.childNodes.length == 2
1032 and root.firstChild is not root.lastChild
1033 and root.firstChild.nextSibling is root.lastChild
1034 and root.firstChild.previousSibling is None
1035 and root.lastChild.previousSibling is root.firstChild
1036 and root.lastChild.nextSibling is None
1037 , "testNormalizeDeleteWithTwoSiblings -- result")
1038 doc.unlink()
1039
1040 def testNormalizeDeleteAndCombine(self):
1041 doc = parseString("<doc/>")
1042 root = doc.documentElement
1043 root.appendChild(doc.createTextNode(""))
1044 root.appendChild(doc.createTextNode("second"))
1045 root.appendChild(doc.createTextNode(""))
1046 root.appendChild(doc.createTextNode("fourth"))
1047 root.appendChild(doc.createTextNode(""))
1048 self.confirm(len(root.childNodes) == 5
1049 and root.childNodes.length == 5,
1050 "testNormalizeDeleteAndCombine -- preparation")
1051 doc.normalize()
1052 self.confirm(len(root.childNodes) == 1
1053 and root.childNodes.length == 1
1054 and root.firstChild is root.lastChild
1055 and root.firstChild.data == "secondfourth"
1056 and root.firstChild.previousSibling is None
1057 and root.firstChild.nextSibling is None
1058 , "testNormalizeDeleteAndCombine -- result")
1059 doc.unlink()
1060
1061 def testNormalizeRecursion(self):
1062 doc = parseString("<doc>"
1063 "<o>"
1064 "<i/>"
1065 "t"
1066 #
1067 #x
1068 "</o>"
1069 "<o>"
1070 "<o>"
1071 "t2"
1072 #x2
1073 "</o>"
1074 "t3"
1075 #x3
1076 "</o>"
1077 #
1078 "</doc>")
1079 root = doc.documentElement
1080 root.childNodes[0].appendChild(doc.createTextNode(""))
1081 root.childNodes[0].appendChild(doc.createTextNode("x"))
1082 root.childNodes[1].childNodes[0].appendChild(doc.createTextNode("x2"))
1083 root.childNodes[1].appendChild(doc.createTextNode("x3"))
1084 root.appendChild(doc.createTextNode(""))
1085 self.confirm(len(root.childNodes) == 3
1086 and root.childNodes.length == 3
1087 and len(root.childNodes[0].childNodes) == 4
1088 and root.childNodes[0].childNodes.length == 4
1089 and len(root.childNodes[1].childNodes) == 3
1090 and root.childNodes[1].childNodes.length == 3
1091 and len(root.childNodes[1].childNodes[0].childNodes) == 2
1092 and root.childNodes[1].childNodes[0].childNodes.length == 2
1093 , "testNormalize2 -- preparation")
1094 doc.normalize()
1095 self.confirm(len(root.childNodes) == 2
1096 and root.childNodes.length == 2
1097 and len(root.childNodes[0].childNodes) == 2
1098 and root.childNodes[0].childNodes.length == 2
1099 and len(root.childNodes[1].childNodes) == 2
1100 and root.childNodes[1].childNodes.length == 2
1101 and len(root.childNodes[1].childNodes[0].childNodes) == 1
1102 and root.childNodes[1].childNodes[0].childNodes.length == 1
1103 , "testNormalize2 -- childNodes lengths")
1104 self.confirm(root.childNodes[0].childNodes[1].data == "tx"
1105 and root.childNodes[1].childNodes[0].childNodes[0].data == "t2x2"
1106 and root.childNodes[1].childNodes[1].data == "t3x3"
1107 , "testNormalize2 -- joined text fields")
1108 self.confirm(root.childNodes[0].childNodes[1].nextSibling is None
1109 and root.childNodes[0].childNodes[1].previousSibling
1110 is root.childNodes[0].childNodes[0]
1111 and root.childNodes[0].childNodes[0].previousSibling is None
1112 and root.childNodes[0].childNodes[0].nextSibling
1113 is root.childNodes[0].childNodes[1]
1114 and root.childNodes[1].childNodes[1].nextSibling is None
1115 and root.childNodes[1].childNodes[1].previousSibling
1116 is root.childNodes[1].childNodes[0]
1117 and root.childNodes[1].childNodes[0].previousSibling is None
1118 and root.childNodes[1].childNodes[0].nextSibling
1119 is root.childNodes[1].childNodes[1]
1120 , "testNormalize2 -- sibling pointers")
1121 doc.unlink()
1122
1123
1124 def testBug0777884(self):
1125 doc = parseString("<o>text</o>")
1126 text = doc.documentElement.childNodes[0]
1127 self.assertEqual(text.nodeType, Node.TEXT_NODE)
1128 # Should run quietly, doing nothing.
1129 text.normalize()
1130 doc.unlink()
1131
1132 def testBug1433694(self):
1133 doc = parseString("<o><i/>t</o>")
1134 node = doc.documentElement
1135 node.childNodes[1].nodeValue = ""
1136 node.normalize()
1137 self.confirm(node.childNodes[-1].nextSibling is None,
1138 "Final child's .nextSibling should be None")
1139
1140 def testSiblings(self):
1141 doc = parseString("<doc><?pi?>text?<elm/></doc>")
1142 root = doc.documentElement
1143 (pi, text, elm) = root.childNodes
1144
1145 self.confirm(pi.nextSibling is text and
1146 pi.previousSibling is None and
1147 text.nextSibling is elm and
1148 text.previousSibling is pi and
1149 elm.nextSibling is None and
1150 elm.previousSibling is text, "testSiblings")
1151
1152 doc.unlink()
1153
1154 def testParents(self):
1155 doc = parseString(
1156 "<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>")
1157 root = doc.documentElement
1158 elm1 = root.childNodes[0]
1159 (elm2a, elm2b) = elm1.childNodes
1160 elm3 = elm2b.childNodes[0]
1161
1162 self.confirm(root.parentNode is doc and
1163 elm1.parentNode is root and
1164 elm2a.parentNode is elm1 and
1165 elm2b.parentNode is elm1 and
1166 elm3.parentNode is elm2b, "testParents")
1167 doc.unlink()
1168
1169 def testNodeListItem(self):
1170 doc = parseString("<doc><e/><e/></doc>")
1171 children = doc.childNodes
1172 docelem = children[0]
1173 self.confirm(children[0] is children.item(0)
1174 and children.item(1) is None
1175 and docelem.childNodes.item(0) is docelem.childNodes[0]
1176 and docelem.childNodes.item(1) is docelem.childNodes[1]
1177 and docelem.childNodes.item(0).childNodes.item(0) is None,
1178 "test NodeList.item()")
1179 doc.unlink()
1180
1181 def testEncodings(self):
1182 doc = parseString('<foo>&#x20ac;</foo>')
1183 self.assertEqual(doc.toxml(),
1184 '<?xml version="1.0" ?><foo>\u20ac</foo>')
1185 self.assertEqual(doc.toxml('utf-8'),
1186 b'<?xml version="1.0" encoding="utf-8"?><foo>\xe2\x82\xac</foo>')
1187 self.assertEqual(doc.toxml('iso-8859-15'),
1188 b'<?xml version="1.0" encoding="iso-8859-15"?><foo>\xa4</foo>')
1189 self.assertEqual(doc.toxml('us-ascii'),
1190 b'<?xml version="1.0" encoding="us-ascii"?><foo>&#8364;</foo>')
1191 self.assertEqual(doc.toxml('utf-16'),
1192 '<?xml version="1.0" encoding="utf-16"?>'
1193 '<foo>\u20ac</foo>'.encode('utf-16'))
1194
1195 # Verify that character decoding errors raise exceptions instead
1196 # of crashing
1197 if pyexpat.version_info >= (2, 4, 5):
1198 self.assertRaises(ExpatError, parseString,
1199 b'<fran\xe7ais></fran\xe7ais>')
1200 self.assertRaises(ExpatError, parseString,
1201 b'<franais>Comment \xe7a va ? Tr\xe8s bien ?</franais>')
1202 else:
1203 self.assertRaises(UnicodeDecodeError, parseString,
1204 b'<fran\xe7ais>Comment \xe7a va ? Tr\xe8s bien ?</fran\xe7ais>')
1205
1206 doc.unlink()
1207
1208 def testStandalone(self):
1209 doc = parseString('<foo>&#x20ac;</foo>')
1210 self.assertEqual(doc.toxml(),
1211 '<?xml version="1.0" ?><foo>\u20ac</foo>')
1212 self.assertEqual(doc.toxml(standalone=None),
1213 '<?xml version="1.0" ?><foo>\u20ac</foo>')
1214 self.assertEqual(doc.toxml(standalone=True),
1215 '<?xml version="1.0" standalone="yes"?><foo>\u20ac</foo>')
1216 self.assertEqual(doc.toxml(standalone=False),
1217 '<?xml version="1.0" standalone="no"?><foo>\u20ac</foo>')
1218 self.assertEqual(doc.toxml('utf-8', True),
1219 b'<?xml version="1.0" encoding="utf-8" standalone="yes"?>'
1220 b'<foo>\xe2\x82\xac</foo>')
1221
1222 doc.unlink()
1223
1224 class UserDataHandler:
1225 called = 0
1226 def handle(self, operation, key, data, src, dst):
1227 dst.setUserData(key, data + 1, self)
1228 src.setUserData(key, None, None)
1229 self.called = 1
1230
1231 def testUserData(self):
1232 dom = Document()
1233 n = dom.createElement('e')
1234 self.confirm(n.getUserData("foo") is None)
1235 n.setUserData("foo", None, None)
1236 self.confirm(n.getUserData("foo") is None)
1237 n.setUserData("foo", 12, 12)
1238 n.setUserData("bar", 13, 13)
1239 self.confirm(n.getUserData("foo") == 12)
1240 self.confirm(n.getUserData("bar") == 13)
1241 n.setUserData("foo", None, None)
1242 self.confirm(n.getUserData("foo") is None)
1243 self.confirm(n.getUserData("bar") == 13)
1244
1245 handler = self.UserDataHandler()
1246 n.setUserData("bar", 12, handler)
1247 c = n.cloneNode(1)
1248 self.confirm(handler.called
1249 and n.getUserData("bar") is None
1250 and c.getUserData("bar") == 13)
1251 n.unlink()
1252 c.unlink()
1253 dom.unlink()
1254
1255 def checkRenameNodeSharedConstraints(self, doc, node):
1256 # Make sure illegal NS usage is detected:
1257 self.assertRaises(xml.dom.NamespaceErr, doc.renameNode, node,
1258 "http://xml.python.org/ns", "xmlns:foo")
1259 doc2 = parseString("<doc/>")
1260 self.assertRaises(xml.dom.WrongDocumentErr, doc2.renameNode, node,
1261 xml.dom.EMPTY_NAMESPACE, "foo")
1262
1263 def testRenameAttribute(self):
1264 doc = parseString("<doc a='v'/>")
1265 elem = doc.documentElement
1266 attrmap = elem.attributes
1267 attr = elem.attributes['a']
1268
1269 # Simple renaming
1270 attr = doc.renameNode(attr, xml.dom.EMPTY_NAMESPACE, "b")
1271 self.confirm(attr.name == "b"
1272 and attr.nodeName == "b"
1273 and attr.localName is None
1274 and attr.namespaceURI == xml.dom.EMPTY_NAMESPACE
1275 and attr.prefix is None
1276 and attr.value == "v"
1277 and elem.getAttributeNode("a") is None
1278 and elem.getAttributeNode("b").isSameNode(attr)
1279 and attrmap["b"].isSameNode(attr)
1280 and attr.ownerDocument.isSameNode(doc)
1281 and attr.ownerElement.isSameNode(elem))
1282
1283 # Rename to have a namespace, no prefix
1284 attr = doc.renameNode(attr, "http://xml.python.org/ns", "c")
1285 self.confirm(attr.name == "c"
1286 and attr.nodeName == "c"
1287 and attr.localName == "c"
1288 and attr.namespaceURI == "http://xml.python.org/ns"
1289 and attr.prefix is None
1290 and attr.value == "v"
1291 and elem.getAttributeNode("a") is None
1292 and elem.getAttributeNode("b") is None
1293 and elem.getAttributeNode("c").isSameNode(attr)
1294 and elem.getAttributeNodeNS(
1295 "http://xml.python.org/ns", "c").isSameNode(attr)
1296 and attrmap["c"].isSameNode(attr)
1297 and attrmap[("http://xml.python.org/ns", "c")].isSameNode(attr))
1298
1299 # Rename to have a namespace, with prefix
1300 attr = doc.renameNode(attr, "http://xml.python.org/ns2", "p:d")
1301 self.confirm(attr.name == "p:d"
1302 and attr.nodeName == "p:d"
1303 and attr.localName == "d"
1304 and attr.namespaceURI == "http://xml.python.org/ns2"
1305 and attr.prefix == "p"
1306 and attr.value == "v"
1307 and elem.getAttributeNode("a") is None
1308 and elem.getAttributeNode("b") is None
1309 and elem.getAttributeNode("c") is None
1310 and elem.getAttributeNodeNS(
1311 "http://xml.python.org/ns", "c") is None
1312 and elem.getAttributeNode("p:d").isSameNode(attr)
1313 and elem.getAttributeNodeNS(
1314 "http://xml.python.org/ns2", "d").isSameNode(attr)
1315 and attrmap["p:d"].isSameNode(attr)
1316 and attrmap[("http://xml.python.org/ns2", "d")].isSameNode(attr))
1317
1318 # Rename back to a simple non-NS node
1319 attr = doc.renameNode(attr, xml.dom.EMPTY_NAMESPACE, "e")
1320 self.confirm(attr.name == "e"
1321 and attr.nodeName == "e"
1322 and attr.localName is None
1323 and attr.namespaceURI == xml.dom.EMPTY_NAMESPACE
1324 and attr.prefix is None
1325 and attr.value == "v"
1326 and elem.getAttributeNode("a") is None
1327 and elem.getAttributeNode("b") is None
1328 and elem.getAttributeNode("c") is None
1329 and elem.getAttributeNode("p:d") is None
1330 and elem.getAttributeNodeNS(
1331 "http://xml.python.org/ns", "c") is None
1332 and elem.getAttributeNode("e").isSameNode(attr)
1333 and attrmap["e"].isSameNode(attr))
1334
1335 self.assertRaises(xml.dom.NamespaceErr, doc.renameNode, attr,
1336 "http://xml.python.org/ns", "xmlns")
1337 self.checkRenameNodeSharedConstraints(doc, attr)
1338 doc.unlink()
1339
1340 def testRenameElement(self):
1341 doc = parseString("<doc/>")
1342 elem = doc.documentElement
1343
1344 # Simple renaming
1345 elem = doc.renameNode(elem, xml.dom.EMPTY_NAMESPACE, "a")
1346 self.confirm(elem.tagName == "a"
1347 and elem.nodeName == "a"
1348 and elem.localName is None
1349 and elem.namespaceURI == xml.dom.EMPTY_NAMESPACE
1350 and elem.prefix is None
1351 and elem.ownerDocument.isSameNode(doc))
1352
1353 # Rename to have a namespace, no prefix
1354 elem = doc.renameNode(elem, "http://xml.python.org/ns", "b")
1355 self.confirm(elem.tagName == "b"
1356 and elem.nodeName == "b"
1357 and elem.localName == "b"
1358 and elem.namespaceURI == "http://xml.python.org/ns"
1359 and elem.prefix is None
1360 and elem.ownerDocument.isSameNode(doc))
1361
1362 # Rename to have a namespace, with prefix
1363 elem = doc.renameNode(elem, "http://xml.python.org/ns2", "p:c")
1364 self.confirm(elem.tagName == "p:c"
1365 and elem.nodeName == "p:c"
1366 and elem.localName == "c"
1367 and elem.namespaceURI == "http://xml.python.org/ns2"
1368 and elem.prefix == "p"
1369 and elem.ownerDocument.isSameNode(doc))
1370
1371 # Rename back to a simple non-NS node
1372 elem = doc.renameNode(elem, xml.dom.EMPTY_NAMESPACE, "d")
1373 self.confirm(elem.tagName == "d"
1374 and elem.nodeName == "d"
1375 and elem.localName is None
1376 and elem.namespaceURI == xml.dom.EMPTY_NAMESPACE
1377 and elem.prefix is None
1378 and elem.ownerDocument.isSameNode(doc))
1379
1380 self.checkRenameNodeSharedConstraints(doc, elem)
1381 doc.unlink()
1382
1383 def testRenameOther(self):
1384 # We have to create a comment node explicitly since not all DOM
1385 # builders used with minidom add comments to the DOM.
1386 doc = xml.dom.minidom.getDOMImplementation().createDocument(
1387 xml.dom.EMPTY_NAMESPACE, "e", None)
1388 node = doc.createComment("comment")
1389 self.assertRaises(xml.dom.NotSupportedErr, doc.renameNode, node,
1390 xml.dom.EMPTY_NAMESPACE, "foo")
1391 doc.unlink()
1392
1393 def testWholeText(self):
1394 doc = parseString("<doc>a</doc>")
1395 elem = doc.documentElement
1396 text = elem.childNodes[0]
1397 self.assertEqual(text.nodeType, Node.TEXT_NODE)
1398
1399 self.checkWholeText(text, "a")
1400 elem.appendChild(doc.createTextNode("b"))
1401 self.checkWholeText(text, "ab")
1402 elem.insertBefore(doc.createCDATASection("c"), text)
1403 self.checkWholeText(text, "cab")
1404
1405 # make sure we don't cross other nodes
1406 splitter = doc.createComment("comment")
1407 elem.appendChild(splitter)
1408 text2 = doc.createTextNode("d")
1409 elem.appendChild(text2)
1410 self.checkWholeText(text, "cab")
1411 self.checkWholeText(text2, "d")
1412
1413 x = doc.createElement("x")
1414 elem.replaceChild(x, splitter)
1415 splitter = x
1416 self.checkWholeText(text, "cab")
1417 self.checkWholeText(text2, "d")
1418
1419 x = doc.createProcessingInstruction("y", "z")
1420 elem.replaceChild(x, splitter)
1421 splitter = x
1422 self.checkWholeText(text, "cab")
1423 self.checkWholeText(text2, "d")
1424
1425 elem.removeChild(splitter)
1426 self.checkWholeText(text, "cabd")
1427 self.checkWholeText(text2, "cabd")
1428
1429 def testPatch1094164(self):
1430 doc = parseString("<doc><e/></doc>")
1431 elem = doc.documentElement
1432 e = elem.firstChild
1433 self.confirm(e.parentNode is elem, "Before replaceChild()")
1434 # Check that replacing a child with itself leaves the tree unchanged
1435 elem.replaceChild(e, e)
1436 self.confirm(e.parentNode is elem, "After replaceChild()")
1437
1438 def testReplaceWholeText(self):
1439 def setup():
1440 doc = parseString("<doc>a<e/>d</doc>")
1441 elem = doc.documentElement
1442 text1 = elem.firstChild
1443 text2 = elem.lastChild
1444 splitter = text1.nextSibling
1445 elem.insertBefore(doc.createTextNode("b"), splitter)
1446 elem.insertBefore(doc.createCDATASection("c"), text1)
1447 return doc, elem, text1, splitter, text2
1448
1449 doc, elem, text1, splitter, text2 = setup()
1450 text = text1.replaceWholeText("new content")
1451 self.checkWholeText(text, "new content")
1452 self.checkWholeText(text2, "d")
1453 self.confirm(len(elem.childNodes) == 3)
1454
1455 doc, elem, text1, splitter, text2 = setup()
1456 text = text2.replaceWholeText("new content")
1457 self.checkWholeText(text, "new content")
1458 self.checkWholeText(text1, "cab")
1459 self.confirm(len(elem.childNodes) == 5)
1460
1461 doc, elem, text1, splitter, text2 = setup()
1462 text = text1.replaceWholeText("")
1463 self.checkWholeText(text2, "d")
1464 self.confirm(text is None
1465 and len(elem.childNodes) == 2)
1466
1467 def testSchemaType(self):
1468 doc = parseString(
1469 "<!DOCTYPE doc [\n"
1470 " <!ENTITY e1 SYSTEM 'http://xml.python.org/e1'>\n"
1471 " <!ENTITY e2 SYSTEM 'http://xml.python.org/e2'>\n"
1472 " <!ATTLIST doc id ID #IMPLIED \n"
1473 " ref IDREF #IMPLIED \n"
1474 " refs IDREFS #IMPLIED \n"
1475 " enum (a|b) #IMPLIED \n"
1476 " ent ENTITY #IMPLIED \n"
1477 " ents ENTITIES #IMPLIED \n"
1478 " nm NMTOKEN #IMPLIED \n"
1479 " nms NMTOKENS #IMPLIED \n"
1480 " text CDATA #IMPLIED \n"
1481 " >\n"
1482 "]><doc id='name' notid='name' text='splat!' enum='b'"
1483 " ref='name' refs='name name' ent='e1' ents='e1 e2'"
1484 " nm='123' nms='123 abc' />")
1485 elem = doc.documentElement
1486 # We don't want to rely on any specific loader at this point, so
1487 # just make sure we can get to all the names, and that the
1488 # DTD-based namespace is right. The names can vary by loader
1489 # since each supports a different level of DTD information.
1490 t = elem.schemaType
1491 self.confirm(t.name is None
1492 and t.namespace == xml.dom.EMPTY_NAMESPACE)
1493 names = "id notid text enum ref refs ent ents nm nms".split()
1494 for name in names:
1495 a = elem.getAttributeNode(name)
1496 t = a.schemaType
1497 self.confirm(hasattr(t, "name")
1498 and t.namespace == xml.dom.EMPTY_NAMESPACE)
1499
1500 def testSetIdAttribute(self):
1501 doc = parseString("<doc a1='v' a2='w'/>")
1502 e = doc.documentElement
1503 a1 = e.getAttributeNode("a1")
1504 a2 = e.getAttributeNode("a2")
1505 self.confirm(doc.getElementById("v") is None
1506 and not a1.isId
1507 and not a2.isId)
1508 e.setIdAttribute("a1")
1509 self.confirm(e.isSameNode(doc.getElementById("v"))
1510 and a1.isId
1511 and not a2.isId)
1512 e.setIdAttribute("a2")
1513 self.confirm(e.isSameNode(doc.getElementById("v"))
1514 and e.isSameNode(doc.getElementById("w"))
1515 and a1.isId
1516 and a2.isId)
1517 # replace the a1 node; the new node should *not* be an ID
1518 a3 = doc.createAttribute("a1")
1519 a3.value = "v"
1520 e.setAttributeNode(a3)
1521 self.confirm(doc.getElementById("v") is None
1522 and e.isSameNode(doc.getElementById("w"))
1523 and not a1.isId
1524 and a2.isId
1525 and not a3.isId)
1526 # renaming an attribute should not affect its ID-ness:
1527 doc.renameNode(a2, xml.dom.EMPTY_NAMESPACE, "an")
1528 self.confirm(e.isSameNode(doc.getElementById("w"))
1529 and a2.isId)
1530
1531 def testSetIdAttributeNS(self):
1532 NS1 = "http://xml.python.org/ns1"
1533 NS2 = "http://xml.python.org/ns2"
1534 doc = parseString("<doc"
1535 " xmlns:ns1='" + NS1 + "'"
1536 " xmlns:ns2='" + NS2 + "'"
1537 " ns1:a1='v' ns2:a2='w'/>")
1538 e = doc.documentElement
1539 a1 = e.getAttributeNodeNS(NS1, "a1")
1540 a2 = e.getAttributeNodeNS(NS2, "a2")
1541 self.confirm(doc.getElementById("v") is None
1542 and not a1.isId
1543 and not a2.isId)
1544 e.setIdAttributeNS(NS1, "a1")
1545 self.confirm(e.isSameNode(doc.getElementById("v"))
1546 and a1.isId
1547 and not a2.isId)
1548 e.setIdAttributeNS(NS2, "a2")
1549 self.confirm(e.isSameNode(doc.getElementById("v"))
1550 and e.isSameNode(doc.getElementById("w"))
1551 and a1.isId
1552 and a2.isId)
1553 # replace the a1 node; the new node should *not* be an ID
1554 a3 = doc.createAttributeNS(NS1, "a1")
1555 a3.value = "v"
1556 e.setAttributeNode(a3)
1557 self.confirm(e.isSameNode(doc.getElementById("w")))
1558 self.confirm(not a1.isId)
1559 self.confirm(a2.isId)
1560 self.confirm(not a3.isId)
1561 self.confirm(doc.getElementById("v") is None)
1562 # renaming an attribute should not affect its ID-ness:
1563 doc.renameNode(a2, xml.dom.EMPTY_NAMESPACE, "an")
1564 self.confirm(e.isSameNode(doc.getElementById("w"))
1565 and a2.isId)
1566
1567 def testSetIdAttributeNode(self):
1568 NS1 = "http://xml.python.org/ns1"
1569 NS2 = "http://xml.python.org/ns2"
1570 doc = parseString("<doc"
1571 " xmlns:ns1='" + NS1 + "'"
1572 " xmlns:ns2='" + NS2 + "'"
1573 " ns1:a1='v' ns2:a2='w'/>")
1574 e = doc.documentElement
1575 a1 = e.getAttributeNodeNS(NS1, "a1")
1576 a2 = e.getAttributeNodeNS(NS2, "a2")
1577 self.confirm(doc.getElementById("v") is None
1578 and not a1.isId
1579 and not a2.isId)
1580 e.setIdAttributeNode(a1)
1581 self.confirm(e.isSameNode(doc.getElementById("v"))
1582 and a1.isId
1583 and not a2.isId)
1584 e.setIdAttributeNode(a2)
1585 self.confirm(e.isSameNode(doc.getElementById("v"))
1586 and e.isSameNode(doc.getElementById("w"))
1587 and a1.isId
1588 and a2.isId)
1589 # replace the a1 node; the new node should *not* be an ID
1590 a3 = doc.createAttributeNS(NS1, "a1")
1591 a3.value = "v"
1592 e.setAttributeNode(a3)
1593 self.confirm(e.isSameNode(doc.getElementById("w")))
1594 self.confirm(not a1.isId)
1595 self.confirm(a2.isId)
1596 self.confirm(not a3.isId)
1597 self.confirm(doc.getElementById("v") is None)
1598 # renaming an attribute should not affect its ID-ness:
1599 doc.renameNode(a2, xml.dom.EMPTY_NAMESPACE, "an")
1600 self.confirm(e.isSameNode(doc.getElementById("w"))
1601 and a2.isId)
1602
1603 def assert_recursive_equal(self, doc, doc2):
1604 stack = [(doc, doc2)]
1605 while stack:
1606 n1, n2 = stack.pop()
1607 self.assertEqual(n1.nodeType, n2.nodeType)
1608 self.assertEqual(len(n1.childNodes), len(n2.childNodes))
1609 self.assertEqual(n1.nodeName, n2.nodeName)
1610 self.assertFalse(n1.isSameNode(n2))
1611 self.assertFalse(n2.isSameNode(n1))
1612 if n1.nodeType == Node.DOCUMENT_TYPE_NODE:
1613 len(n1.entities)
1614 len(n2.entities)
1615 len(n1.notations)
1616 len(n2.notations)
1617 self.assertEqual(len(n1.entities), len(n2.entities))
1618 self.assertEqual(len(n1.notations), len(n2.notations))
1619 for i in range(len(n1.notations)):
1620 # XXX this loop body doesn't seem to be executed?
1621 no1 = n1.notations.item(i)
1622 no2 = n1.notations.item(i)
1623 self.assertEqual(no1.name, no2.name)
1624 self.assertEqual(no1.publicId, no2.publicId)
1625 self.assertEqual(no1.systemId, no2.systemId)
1626 stack.append((no1, no2))
1627 for i in range(len(n1.entities)):
1628 e1 = n1.entities.item(i)
1629 e2 = n2.entities.item(i)
1630 self.assertEqual(e1.notationName, e2.notationName)
1631 self.assertEqual(e1.publicId, e2.publicId)
1632 self.assertEqual(e1.systemId, e2.systemId)
1633 stack.append((e1, e2))
1634 if n1.nodeType != Node.DOCUMENT_NODE:
1635 self.assertTrue(n1.ownerDocument.isSameNode(doc))
1636 self.assertTrue(n2.ownerDocument.isSameNode(doc2))
1637 for i in range(len(n1.childNodes)):
1638 stack.append((n1.childNodes[i], n2.childNodes[i]))
1639
1640 def testPickledDocument(self):
1641 doc = parseString(sample)
1642 for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
1643 s = pickle.dumps(doc, proto)
1644 doc2 = pickle.loads(s)
1645 self.assert_recursive_equal(doc, doc2)
1646
1647 def testDeepcopiedDocument(self):
1648 doc = parseString(sample)
1649 doc2 = copy.deepcopy(doc)
1650 self.assert_recursive_equal(doc, doc2)
1651
1652 def testSerializeCommentNodeWithDoubleHyphen(self):
1653 doc = create_doc_without_doctype()
1654 doc.appendChild(doc.createComment("foo--bar"))
1655 self.assertRaises(ValueError, doc.toxml)
1656
1657
1658 def testEmptyXMLNSValue(self):
1659 doc = parseString("<element xmlns=''>\n"
1660 "<foo/>\n</element>")
1661 doc2 = parseString(doc.toxml())
1662 self.confirm(doc2.namespaceURI == xml.dom.EMPTY_NAMESPACE)
1663
1664 def testExceptionOnSpacesInXMLNSValue(self):
1665 if pyexpat.version_info >= (2, 4, 5):
1666 context = self.assertRaisesRegex(ExpatError, 'syntax error')
1667 else:
1668 context = self.assertRaisesRegex(ValueError, 'Unsupported syntax')
1669
1670 with context:
1671 parseString('<element xmlns:abc="http:abc.com/de f g/hi/j k"><abc:foo /></element>')
1672
1673 def testDocRemoveChild(self):
1674 doc = parse(tstfile)
1675 title_tag = doc.documentElement.getElementsByTagName("TITLE")[0]
1676 self.assertRaises( xml.dom.NotFoundErr, doc.removeChild, title_tag)
1677 num_children_before = len(doc.childNodes)
1678 doc.removeChild(doc.childNodes[0])
1679 num_children_after = len(doc.childNodes)
1680 self.assertTrue(num_children_after == num_children_before - 1)
1681
1682 def testProcessingInstructionNameError(self):
1683 # wrong variable in .nodeValue property will
1684 # lead to "NameError: name 'data' is not defined"
1685 doc = parse(tstfile)
1686 pi = doc.createProcessingInstruction("y", "z")
1687 pi.nodeValue = "crash"
1688
1689 def test_minidom_attribute_order(self):
1690 xml_str = '<?xml version="1.0" ?><curriculum status="public" company="example"/>'
1691 doc = parseString(xml_str)
1692 output = io.StringIO()
1693 doc.writexml(output)
1694 self.assertEqual(output.getvalue(), xml_str)
1695
1696 def test_toxml_with_attributes_ordered(self):
1697 xml_str = '<?xml version="1.0" ?><curriculum status="public" company="example"/>'
1698 doc = parseString(xml_str)
1699 self.assertEqual(doc.toxml(), xml_str)
1700
1701 def test_toprettyxml_with_attributes_ordered(self):
1702 xml_str = '<?xml version="1.0" ?><curriculum status="public" company="example"/>'
1703 doc = parseString(xml_str)
1704 self.assertEqual(doc.toprettyxml(),
1705 '<?xml version="1.0" ?>\n'
1706 '<curriculum status="public" company="example"/>\n')
1707
1708 def test_toprettyxml_with_cdata(self):
1709 xml_str = '<?xml version="1.0" ?><root><node><![CDATA[</data>]]></node></root>'
1710 doc = parseString(xml_str)
1711 self.assertEqual(doc.toprettyxml(),
1712 '<?xml version="1.0" ?>\n'
1713 '<root>\n'
1714 '\t<node><![CDATA[</data>]]></node>\n'
1715 '</root>\n')
1716
1717 def test_cdata_parsing(self):
1718 xml_str = '<?xml version="1.0" ?><root><node><![CDATA[</data>]]></node></root>'
1719 dom1 = parseString(xml_str)
1720 self.checkWholeText(dom1.getElementsByTagName('node')[0].firstChild, '</data>')
1721 dom2 = parseString(dom1.toprettyxml())
1722 self.checkWholeText(dom2.getElementsByTagName('node')[0].firstChild, '</data>')
1723
1724 if __name__ == "__main__":
1725 unittest.main()