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