+++ /dev/null
-# test for xml.dom.minidom\r
-\r
-import pickle\r
-from StringIO import StringIO\r
-from test.test_support import verbose, run_unittest, findfile\r
-import unittest\r
-\r
-import xml.dom\r
-import xml.dom.minidom\r
-import xml.parsers.expat\r
-\r
-from xml.dom.minidom import parse, Node, Document, parseString\r
-from xml.dom.minidom import getDOMImplementation\r
-\r
-\r
-tstfile = findfile("test.xml", subdir="xmltestdata")\r
-\r
-\r
-# The tests of DocumentType importing use these helpers to construct\r
-# the documents to work with, since not all DOM builders actually\r
-# create the DocumentType nodes.\r
-def create_doc_without_doctype(doctype=None):\r
- return getDOMImplementation().createDocument(None, "doc", doctype)\r
-\r
-def create_nonempty_doctype():\r
- doctype = getDOMImplementation().createDocumentType("doc", None, None)\r
- doctype.entities._seq = []\r
- doctype.notations._seq = []\r
- notation = xml.dom.minidom.Notation("my-notation", None,\r
- "http://xml.python.org/notations/my")\r
- doctype.notations._seq.append(notation)\r
- entity = xml.dom.minidom.Entity("my-entity", None,\r
- "http://xml.python.org/entities/my",\r
- "my-notation")\r
- entity.version = "1.0"\r
- entity.encoding = "utf-8"\r
- entity.actualEncoding = "us-ascii"\r
- doctype.entities._seq.append(entity)\r
- return doctype\r
-\r
-def create_doc_with_doctype():\r
- doctype = create_nonempty_doctype()\r
- doc = create_doc_without_doctype(doctype)\r
- doctype.entities.item(0).ownerDocument = doc\r
- doctype.notations.item(0).ownerDocument = doc\r
- return doc\r
-\r
-class MinidomTest(unittest.TestCase):\r
- def confirm(self, test, testname = "Test"):\r
- self.assertTrue(test, testname)\r
-\r
- def checkWholeText(self, node, s):\r
- t = node.wholeText\r
- self.confirm(t == s, "looking for %s, found %s" % (repr(s), repr(t)))\r
-\r
- def testParseFromFile(self):\r
- dom = parse(StringIO(open(tstfile).read()))\r
- dom.unlink()\r
- self.confirm(isinstance(dom,Document))\r
-\r
- def testGetElementsByTagName(self):\r
- dom = parse(tstfile)\r
- self.confirm(dom.getElementsByTagName("LI") == \\r
- dom.documentElement.getElementsByTagName("LI"))\r
- dom.unlink()\r
-\r
- def testInsertBefore(self):\r
- dom = parseString("<doc><foo/></doc>")\r
- root = dom.documentElement\r
- elem = root.childNodes[0]\r
- nelem = dom.createElement("element")\r
- root.insertBefore(nelem, elem)\r
- self.confirm(len(root.childNodes) == 2\r
- and root.childNodes.length == 2\r
- and root.childNodes[0] is nelem\r
- and root.childNodes.item(0) is nelem\r
- and root.childNodes[1] is elem\r
- and root.childNodes.item(1) is elem\r
- and root.firstChild is nelem\r
- and root.lastChild is elem\r
- and root.toxml() == "<doc><element/><foo/></doc>"\r
- , "testInsertBefore -- node properly placed in tree")\r
- nelem = dom.createElement("element")\r
- root.insertBefore(nelem, None)\r
- self.confirm(len(root.childNodes) == 3\r
- and root.childNodes.length == 3\r
- and root.childNodes[1] is elem\r
- and root.childNodes.item(1) is elem\r
- and root.childNodes[2] is nelem\r
- and root.childNodes.item(2) is nelem\r
- and root.lastChild is nelem\r
- and nelem.previousSibling is elem\r
- and root.toxml() == "<doc><element/><foo/><element/></doc>"\r
- , "testInsertBefore -- node properly placed in tree")\r
- nelem2 = dom.createElement("bar")\r
- root.insertBefore(nelem2, nelem)\r
- self.confirm(len(root.childNodes) == 4\r
- and root.childNodes.length == 4\r
- and root.childNodes[2] is nelem2\r
- and root.childNodes.item(2) is nelem2\r
- and root.childNodes[3] is nelem\r
- and root.childNodes.item(3) is nelem\r
- and nelem2.nextSibling is nelem\r
- and nelem.previousSibling is nelem2\r
- and root.toxml() ==\r
- "<doc><element/><foo/><bar/><element/></doc>"\r
- , "testInsertBefore -- node properly placed in tree")\r
- dom.unlink()\r
-\r
- def _create_fragment_test_nodes(self):\r
- dom = parseString("<doc/>")\r
- orig = dom.createTextNode("original")\r
- c1 = dom.createTextNode("foo")\r
- c2 = dom.createTextNode("bar")\r
- c3 = dom.createTextNode("bat")\r
- dom.documentElement.appendChild(orig)\r
- frag = dom.createDocumentFragment()\r
- frag.appendChild(c1)\r
- frag.appendChild(c2)\r
- frag.appendChild(c3)\r
- return dom, orig, c1, c2, c3, frag\r
-\r
- def testInsertBeforeFragment(self):\r
- dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()\r
- dom.documentElement.insertBefore(frag, None)\r
- self.confirm(tuple(dom.documentElement.childNodes) ==\r
- (orig, c1, c2, c3),\r
- "insertBefore(<fragment>, None)")\r
- frag.unlink()\r
- dom.unlink()\r
-\r
- dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()\r
- dom.documentElement.insertBefore(frag, orig)\r
- self.confirm(tuple(dom.documentElement.childNodes) ==\r
- (c1, c2, c3, orig),\r
- "insertBefore(<fragment>, orig)")\r
- frag.unlink()\r
- dom.unlink()\r
-\r
- def testAppendChild(self):\r
- dom = parse(tstfile)\r
- dom.documentElement.appendChild(dom.createComment(u"Hello"))\r
- self.confirm(dom.documentElement.childNodes[-1].nodeName == "#comment")\r
- self.confirm(dom.documentElement.childNodes[-1].data == "Hello")\r
- dom.unlink()\r
-\r
- def testAppendChildFragment(self):\r
- dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()\r
- dom.documentElement.appendChild(frag)\r
- self.confirm(tuple(dom.documentElement.childNodes) ==\r
- (orig, c1, c2, c3),\r
- "appendChild(<fragment>)")\r
- frag.unlink()\r
- dom.unlink()\r
-\r
- def testReplaceChildFragment(self):\r
- dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()\r
- dom.documentElement.replaceChild(frag, orig)\r
- orig.unlink()\r
- self.confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3),\r
- "replaceChild(<fragment>)")\r
- frag.unlink()\r
- dom.unlink()\r
-\r
- def testLegalChildren(self):\r
- dom = Document()\r
- elem = dom.createElement('element')\r
- text = dom.createTextNode('text')\r
- self.assertRaises(xml.dom.HierarchyRequestErr, dom.appendChild, text)\r
-\r
- dom.appendChild(elem)\r
- self.assertRaises(xml.dom.HierarchyRequestErr, dom.insertBefore, text,\r
- elem)\r
- self.assertRaises(xml.dom.HierarchyRequestErr, dom.replaceChild, text,\r
- elem)\r
-\r
- nodemap = elem.attributes\r
- self.assertRaises(xml.dom.HierarchyRequestErr, nodemap.setNamedItem,\r
- text)\r
- self.assertRaises(xml.dom.HierarchyRequestErr, nodemap.setNamedItemNS,\r
- text)\r
-\r
- elem.appendChild(text)\r
- dom.unlink()\r
-\r
- def testNamedNodeMapSetItem(self):\r
- dom = Document()\r
- elem = dom.createElement('element')\r
- attrs = elem.attributes\r
- attrs["foo"] = "bar"\r
- a = attrs.item(0)\r
- self.confirm(a.ownerDocument is dom,\r
- "NamedNodeMap.__setitem__() sets ownerDocument")\r
- self.confirm(a.ownerElement is elem,\r
- "NamedNodeMap.__setitem__() sets ownerElement")\r
- self.confirm(a.value == "bar",\r
- "NamedNodeMap.__setitem__() sets value")\r
- self.confirm(a.nodeValue == "bar",\r
- "NamedNodeMap.__setitem__() sets nodeValue")\r
- elem.unlink()\r
- dom.unlink()\r
-\r
- def testNonZero(self):\r
- dom = parse(tstfile)\r
- self.confirm(dom)# should not be zero\r
- dom.appendChild(dom.createComment("foo"))\r
- self.confirm(not dom.childNodes[-1].childNodes)\r
- dom.unlink()\r
-\r
- def testUnlink(self):\r
- dom = parse(tstfile)\r
- dom.unlink()\r
-\r
- def testElement(self):\r
- dom = Document()\r
- dom.appendChild(dom.createElement("abc"))\r
- self.confirm(dom.documentElement)\r
- dom.unlink()\r
-\r
- def testAAA(self):\r
- dom = parseString("<abc/>")\r
- el = dom.documentElement\r
- el.setAttribute("spam", "jam2")\r
- self.confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA")\r
- a = el.getAttributeNode("spam")\r
- self.confirm(a.ownerDocument is dom,\r
- "setAttribute() sets ownerDocument")\r
- self.confirm(a.ownerElement is dom.documentElement,\r
- "setAttribute() sets ownerElement")\r
- dom.unlink()\r
-\r
- def testAAB(self):\r
- dom = parseString("<abc/>")\r
- el = dom.documentElement\r
- el.setAttribute("spam", "jam")\r
- el.setAttribute("spam", "jam2")\r
- self.confirm(el.toxml() == '<abc spam="jam2"/>', "testAAB")\r
- dom.unlink()\r
-\r
- def testAddAttr(self):\r
- dom = Document()\r
- child = dom.appendChild(dom.createElement("abc"))\r
-\r
- child.setAttribute("def", "ghi")\r
- self.confirm(child.getAttribute("def") == "ghi")\r
- self.confirm(child.attributes["def"].value == "ghi")\r
-\r
- child.setAttribute("jkl", "mno")\r
- self.confirm(child.getAttribute("jkl") == "mno")\r
- self.confirm(child.attributes["jkl"].value == "mno")\r
-\r
- self.confirm(len(child.attributes) == 2)\r
-\r
- child.setAttribute("def", "newval")\r
- self.confirm(child.getAttribute("def") == "newval")\r
- self.confirm(child.attributes["def"].value == "newval")\r
-\r
- self.confirm(len(child.attributes) == 2)\r
- dom.unlink()\r
-\r
- def testDeleteAttr(self):\r
- dom = Document()\r
- child = dom.appendChild(dom.createElement("abc"))\r
-\r
- self.confirm(len(child.attributes) == 0)\r
- child.setAttribute("def", "ghi")\r
- self.confirm(len(child.attributes) == 1)\r
- del child.attributes["def"]\r
- self.confirm(len(child.attributes) == 0)\r
- dom.unlink()\r
-\r
- def testRemoveAttr(self):\r
- dom = Document()\r
- child = dom.appendChild(dom.createElement("abc"))\r
-\r
- child.setAttribute("def", "ghi")\r
- self.confirm(len(child.attributes) == 1)\r
- child.removeAttribute("def")\r
- self.confirm(len(child.attributes) == 0)\r
- dom.unlink()\r
-\r
- def testRemoveAttrNS(self):\r
- dom = Document()\r
- child = dom.appendChild(\r
- dom.createElementNS("http://www.python.org", "python:abc"))\r
- child.setAttributeNS("http://www.w3.org", "xmlns:python",\r
- "http://www.python.org")\r
- child.setAttributeNS("http://www.python.org", "python:abcattr", "foo")\r
- self.confirm(len(child.attributes) == 2)\r
- child.removeAttributeNS("http://www.python.org", "abcattr")\r
- self.confirm(len(child.attributes) == 1)\r
- dom.unlink()\r
-\r
- def testRemoveAttributeNode(self):\r
- dom = Document()\r
- child = dom.appendChild(dom.createElement("foo"))\r
- child.setAttribute("spam", "jam")\r
- self.confirm(len(child.attributes) == 1)\r
- node = child.getAttributeNode("spam")\r
- child.removeAttributeNode(node)\r
- self.confirm(len(child.attributes) == 0\r
- and child.getAttributeNode("spam") is None)\r
- dom.unlink()\r
-\r
- def testChangeAttr(self):\r
- dom = parseString("<abc/>")\r
- el = dom.documentElement\r
- el.setAttribute("spam", "jam")\r
- self.confirm(len(el.attributes) == 1)\r
- el.setAttribute("spam", "bam")\r
- # Set this attribute to be an ID and make sure that doesn't change\r
- # when changing the value:\r
- el.setIdAttribute("spam")\r
- self.confirm(len(el.attributes) == 1\r
- and el.attributes["spam"].value == "bam"\r
- and el.attributes["spam"].nodeValue == "bam"\r
- and el.getAttribute("spam") == "bam"\r
- and el.getAttributeNode("spam").isId)\r
- el.attributes["spam"] = "ham"\r
- self.confirm(len(el.attributes) == 1\r
- and el.attributes["spam"].value == "ham"\r
- and el.attributes["spam"].nodeValue == "ham"\r
- and el.getAttribute("spam") == "ham"\r
- and el.attributes["spam"].isId)\r
- el.setAttribute("spam2", "bam")\r
- self.confirm(len(el.attributes) == 2\r
- and el.attributes["spam"].value == "ham"\r
- and el.attributes["spam"].nodeValue == "ham"\r
- and el.getAttribute("spam") == "ham"\r
- and el.attributes["spam2"].value == "bam"\r
- and el.attributes["spam2"].nodeValue == "bam"\r
- and el.getAttribute("spam2") == "bam")\r
- el.attributes["spam2"] = "bam2"\r
- self.confirm(len(el.attributes) == 2\r
- and el.attributes["spam"].value == "ham"\r
- and el.attributes["spam"].nodeValue == "ham"\r
- and el.getAttribute("spam") == "ham"\r
- and el.attributes["spam2"].value == "bam2"\r
- and el.attributes["spam2"].nodeValue == "bam2"\r
- and el.getAttribute("spam2") == "bam2")\r
- dom.unlink()\r
-\r
- def testGetAttrList(self):\r
- pass\r
-\r
- def testGetAttrValues(self): pass\r
-\r
- def testGetAttrLength(self): pass\r
-\r
- def testGetAttribute(self): pass\r
-\r
- def testGetAttributeNS(self): pass\r
-\r
- def testGetAttributeNode(self): pass\r
-\r
- def testGetElementsByTagNameNS(self):\r
- d="""<foo xmlns:minidom='http://pyxml.sf.net/minidom'>\r
- <minidom:myelem/>\r
- </foo>"""\r
- dom = parseString(d)\r
- elems = dom.getElementsByTagNameNS("http://pyxml.sf.net/minidom",\r
- "myelem")\r
- self.confirm(len(elems) == 1\r
- and elems[0].namespaceURI == "http://pyxml.sf.net/minidom"\r
- and elems[0].localName == "myelem"\r
- and elems[0].prefix == "minidom"\r
- and elems[0].tagName == "minidom:myelem"\r
- and elems[0].nodeName == "minidom:myelem")\r
- dom.unlink()\r
-\r
- def get_empty_nodelist_from_elements_by_tagName_ns_helper(self, doc, nsuri,\r
- lname):\r
- nodelist = doc.getElementsByTagNameNS(nsuri, lname)\r
- self.confirm(len(nodelist) == 0)\r
-\r
- def testGetEmptyNodeListFromElementsByTagNameNS(self):\r
- doc = parseString('<doc/>')\r
- self.get_empty_nodelist_from_elements_by_tagName_ns_helper(\r
- doc, 'http://xml.python.org/namespaces/a', 'localname')\r
- self.get_empty_nodelist_from_elements_by_tagName_ns_helper(\r
- doc, '*', 'splat')\r
- self.get_empty_nodelist_from_elements_by_tagName_ns_helper(\r
- doc, 'http://xml.python.org/namespaces/a', '*')\r
-\r
- doc = parseString('<doc xmlns="http://xml.python.org/splat"><e/></doc>')\r
- self.get_empty_nodelist_from_elements_by_tagName_ns_helper(\r
- doc, "http://xml.python.org/splat", "not-there")\r
- self.get_empty_nodelist_from_elements_by_tagName_ns_helper(\r
- doc, "*", "not-there")\r
- self.get_empty_nodelist_from_elements_by_tagName_ns_helper(\r
- doc, "http://somewhere.else.net/not-there", "e")\r
-\r
- def testElementReprAndStr(self):\r
- dom = Document()\r
- el = dom.appendChild(dom.createElement("abc"))\r
- string1 = repr(el)\r
- string2 = str(el)\r
- self.confirm(string1 == string2)\r
- dom.unlink()\r
-\r
- def testElementReprAndStrUnicode(self):\r
- dom = Document()\r
- el = dom.appendChild(dom.createElement(u"abc"))\r
- string1 = repr(el)\r
- string2 = str(el)\r
- self.confirm(string1 == string2)\r
- dom.unlink()\r
-\r
- def testElementReprAndStrUnicodeNS(self):\r
- dom = Document()\r
- el = dom.appendChild(\r
- dom.createElementNS(u"http://www.slashdot.org", u"slash:abc"))\r
- string1 = repr(el)\r
- string2 = str(el)\r
- self.confirm(string1 == string2)\r
- self.confirm("slash:abc" in string1)\r
- dom.unlink()\r
-\r
- def testAttributeRepr(self):\r
- dom = Document()\r
- el = dom.appendChild(dom.createElement(u"abc"))\r
- node = el.setAttribute("abc", "def")\r
- self.confirm(str(node) == repr(node))\r
- dom.unlink()\r
-\r
- def testTextNodeRepr(self): pass\r
-\r
- def testWriteXML(self):\r
- str = '<?xml version="1.0" ?><a b="c"/>'\r
- dom = parseString(str)\r
- domstr = dom.toxml()\r
- dom.unlink()\r
- self.confirm(str == domstr)\r
-\r
- def testAltNewline(self):\r
- str = '<?xml version="1.0" ?>\n<a b="c"/>\n'\r
- dom = parseString(str)\r
- domstr = dom.toprettyxml(newl="\r\n")\r
- dom.unlink()\r
- self.confirm(domstr == str.replace("\n", "\r\n"))\r
-\r
- def testProcessingInstruction(self):\r
- dom = parseString('<e><?mypi \t\n data \t\n ?></e>')\r
- pi = dom.documentElement.firstChild\r
- self.confirm(pi.target == "mypi"\r
- and pi.data == "data \t\n "\r
- and pi.nodeName == "mypi"\r
- and pi.nodeType == Node.PROCESSING_INSTRUCTION_NODE\r
- and pi.attributes is None\r
- and not pi.hasChildNodes()\r
- and len(pi.childNodes) == 0\r
- and pi.firstChild is None\r
- and pi.lastChild is None\r
- and pi.localName is None\r
- and pi.namespaceURI == xml.dom.EMPTY_NAMESPACE)\r
-\r
- def testProcessingInstructionRepr(self): pass\r
-\r
- def testTextRepr(self): pass\r
-\r
- def testWriteText(self): pass\r
-\r
- def testDocumentElement(self): pass\r
-\r
- def testTooManyDocumentElements(self):\r
- doc = parseString("<doc/>")\r
- elem = doc.createElement("extra")\r
- # Should raise an exception when adding an extra document element.\r
- self.assertRaises(xml.dom.HierarchyRequestErr, doc.appendChild, elem)\r
- elem.unlink()\r
- doc.unlink()\r
-\r
- def testCreateElementNS(self): pass\r
-\r
- def testCreateAttributeNS(self): pass\r
-\r
- def testParse(self): pass\r
-\r
- def testParseString(self): pass\r
-\r
- def testComment(self): pass\r
-\r
- def testAttrListItem(self): pass\r
-\r
- def testAttrListItems(self): pass\r
-\r
- def testAttrListItemNS(self): pass\r
-\r
- def testAttrListKeys(self): pass\r
-\r
- def testAttrListKeysNS(self): pass\r
-\r
- def testRemoveNamedItem(self):\r
- doc = parseString("<doc a=''/>")\r
- e = doc.documentElement\r
- attrs = e.attributes\r
- a1 = e.getAttributeNode("a")\r
- a2 = attrs.removeNamedItem("a")\r
- self.confirm(a1.isSameNode(a2))\r
- self.assertRaises(xml.dom.NotFoundErr, attrs.removeNamedItem, "a")\r
-\r
- def testRemoveNamedItemNS(self):\r
- doc = parseString("<doc xmlns:a='http://xml.python.org/' a:b=''/>")\r
- e = doc.documentElement\r
- attrs = e.attributes\r
- a1 = e.getAttributeNodeNS("http://xml.python.org/", "b")\r
- a2 = attrs.removeNamedItemNS("http://xml.python.org/", "b")\r
- self.confirm(a1.isSameNode(a2))\r
- self.assertRaises(xml.dom.NotFoundErr, attrs.removeNamedItemNS,\r
- "http://xml.python.org/", "b")\r
-\r
- def testAttrListValues(self): pass\r
-\r
- def testAttrListLength(self): pass\r
-\r
- def testAttrList__getitem__(self): pass\r
-\r
- def testAttrList__setitem__(self): pass\r
-\r
- def testSetAttrValueandNodeValue(self): pass\r
-\r
- def testParseElement(self): pass\r
-\r
- def testParseAttributes(self): pass\r
-\r
- def testParseElementNamespaces(self): pass\r
-\r
- def testParseAttributeNamespaces(self): pass\r
-\r
- def testParseProcessingInstructions(self): pass\r
-\r
- def testChildNodes(self): pass\r
-\r
- def testFirstChild(self): pass\r
-\r
- def testHasChildNodes(self): pass\r
-\r
- def _testCloneElementCopiesAttributes(self, e1, e2, test):\r
- attrs1 = e1.attributes\r
- attrs2 = e2.attributes\r
- keys1 = attrs1.keys()\r
- keys2 = attrs2.keys()\r
- keys1.sort()\r
- keys2.sort()\r
- self.confirm(keys1 == keys2, "clone of element has same attribute keys")\r
- for i in range(len(keys1)):\r
- a1 = attrs1.item(i)\r
- a2 = attrs2.item(i)\r
- self.confirm(a1 is not a2\r
- and a1.value == a2.value\r
- and a1.nodeValue == a2.nodeValue\r
- and a1.namespaceURI == a2.namespaceURI\r
- and a1.localName == a2.localName\r
- , "clone of attribute node has proper attribute values")\r
- self.confirm(a2.ownerElement is e2,\r
- "clone of attribute node correctly owned")\r
-\r
- def _setupCloneElement(self, deep):\r
- dom = parseString("<doc attr='value'><foo/></doc>")\r
- root = dom.documentElement\r
- clone = root.cloneNode(deep)\r
- self._testCloneElementCopiesAttributes(\r
- root, clone, "testCloneElement" + (deep and "Deep" or "Shallow"))\r
- # mutilate the original so shared data is detected\r
- root.tagName = root.nodeName = "MODIFIED"\r
- root.setAttribute("attr", "NEW VALUE")\r
- root.setAttribute("added", "VALUE")\r
- return dom, clone\r
-\r
- def testCloneElementShallow(self):\r
- dom, clone = self._setupCloneElement(0)\r
- self.confirm(len(clone.childNodes) == 0\r
- and clone.childNodes.length == 0\r
- and clone.parentNode is None\r
- and clone.toxml() == '<doc attr="value"/>'\r
- , "testCloneElementShallow")\r
- dom.unlink()\r
-\r
- def testCloneElementDeep(self):\r
- dom, clone = self._setupCloneElement(1)\r
- self.confirm(len(clone.childNodes) == 1\r
- and clone.childNodes.length == 1\r
- and clone.parentNode is None\r
- and clone.toxml() == '<doc attr="value"><foo/></doc>'\r
- , "testCloneElementDeep")\r
- dom.unlink()\r
-\r
- def testCloneDocumentShallow(self):\r
- doc = parseString("<?xml version='1.0'?>\n"\r
- "<!-- comment -->"\r
- "<!DOCTYPE doc [\n"\r
- "<!NOTATION notation SYSTEM 'http://xml.python.org/'>\n"\r
- "]>\n"\r
- "<doc attr='value'/>")\r
- doc2 = doc.cloneNode(0)\r
- self.confirm(doc2 is None,\r
- "testCloneDocumentShallow:"\r
- " shallow cloning of documents makes no sense!")\r
-\r
- def testCloneDocumentDeep(self):\r
- doc = parseString("<?xml version='1.0'?>\n"\r
- "<!-- comment -->"\r
- "<!DOCTYPE doc [\n"\r
- "<!NOTATION notation SYSTEM 'http://xml.python.org/'>\n"\r
- "]>\n"\r
- "<doc attr='value'/>")\r
- doc2 = doc.cloneNode(1)\r
- self.confirm(not (doc.isSameNode(doc2) or doc2.isSameNode(doc)),\r
- "testCloneDocumentDeep: document objects not distinct")\r
- self.confirm(len(doc.childNodes) == len(doc2.childNodes),\r
- "testCloneDocumentDeep: wrong number of Document children")\r
- self.confirm(doc2.documentElement.nodeType == Node.ELEMENT_NODE,\r
- "testCloneDocumentDeep: documentElement not an ELEMENT_NODE")\r
- self.confirm(doc2.documentElement.ownerDocument.isSameNode(doc2),\r
- "testCloneDocumentDeep: documentElement owner is not new document")\r
- self.confirm(not doc.documentElement.isSameNode(doc2.documentElement),\r
- "testCloneDocumentDeep: documentElement should not be shared")\r
- if doc.doctype is not None:\r
- # check the doctype iff the original DOM maintained it\r
- self.confirm(doc2.doctype.nodeType == Node.DOCUMENT_TYPE_NODE,\r
- "testCloneDocumentDeep: doctype not a DOCUMENT_TYPE_NODE")\r
- self.confirm(doc2.doctype.ownerDocument.isSameNode(doc2))\r
- self.confirm(not doc.doctype.isSameNode(doc2.doctype))\r
-\r
- def testCloneDocumentTypeDeepOk(self):\r
- doctype = create_nonempty_doctype()\r
- clone = doctype.cloneNode(1)\r
- self.confirm(clone is not None\r
- and clone.nodeName == doctype.nodeName\r
- and clone.name == doctype.name\r
- and clone.publicId == doctype.publicId\r
- and clone.systemId == doctype.systemId\r
- and len(clone.entities) == len(doctype.entities)\r
- and clone.entities.item(len(clone.entities)) is None\r
- and len(clone.notations) == len(doctype.notations)\r
- and clone.notations.item(len(clone.notations)) is None\r
- and len(clone.childNodes) == 0)\r
- for i in range(len(doctype.entities)):\r
- se = doctype.entities.item(i)\r
- ce = clone.entities.item(i)\r
- self.confirm((not se.isSameNode(ce))\r
- and (not ce.isSameNode(se))\r
- and ce.nodeName == se.nodeName\r
- and ce.notationName == se.notationName\r
- and ce.publicId == se.publicId\r
- and ce.systemId == se.systemId\r
- and ce.encoding == se.encoding\r
- and ce.actualEncoding == se.actualEncoding\r
- and ce.version == se.version)\r
- for i in range(len(doctype.notations)):\r
- sn = doctype.notations.item(i)\r
- cn = clone.notations.item(i)\r
- self.confirm((not sn.isSameNode(cn))\r
- and (not cn.isSameNode(sn))\r
- and cn.nodeName == sn.nodeName\r
- and cn.publicId == sn.publicId\r
- and cn.systemId == sn.systemId)\r
-\r
- def testCloneDocumentTypeDeepNotOk(self):\r
- doc = create_doc_with_doctype()\r
- clone = doc.doctype.cloneNode(1)\r
- self.confirm(clone is None, "testCloneDocumentTypeDeepNotOk")\r
-\r
- def testCloneDocumentTypeShallowOk(self):\r
- doctype = create_nonempty_doctype()\r
- clone = doctype.cloneNode(0)\r
- self.confirm(clone is not None\r
- and clone.nodeName == doctype.nodeName\r
- and clone.name == doctype.name\r
- and clone.publicId == doctype.publicId\r
- and clone.systemId == doctype.systemId\r
- and len(clone.entities) == 0\r
- and clone.entities.item(0) is None\r
- and len(clone.notations) == 0\r
- and clone.notations.item(0) is None\r
- and len(clone.childNodes) == 0)\r
-\r
- def testCloneDocumentTypeShallowNotOk(self):\r
- doc = create_doc_with_doctype()\r
- clone = doc.doctype.cloneNode(0)\r
- self.confirm(clone is None, "testCloneDocumentTypeShallowNotOk")\r
-\r
- def check_import_document(self, deep, testName):\r
- doc1 = parseString("<doc/>")\r
- doc2 = parseString("<doc/>")\r
- self.assertRaises(xml.dom.NotSupportedErr, doc1.importNode, doc2, deep)\r
-\r
- def testImportDocumentShallow(self):\r
- self.check_import_document(0, "testImportDocumentShallow")\r
-\r
- def testImportDocumentDeep(self):\r
- self.check_import_document(1, "testImportDocumentDeep")\r
-\r
- def testImportDocumentTypeShallow(self):\r
- src = create_doc_with_doctype()\r
- target = create_doc_without_doctype()\r
- self.assertRaises(xml.dom.NotSupportedErr, target.importNode,\r
- src.doctype, 0)\r
-\r
- def testImportDocumentTypeDeep(self):\r
- src = create_doc_with_doctype()\r
- target = create_doc_without_doctype()\r
- self.assertRaises(xml.dom.NotSupportedErr, target.importNode,\r
- src.doctype, 1)\r
-\r
- # Testing attribute clones uses a helper, and should always be deep,\r
- # even if the argument to cloneNode is false.\r
- def check_clone_attribute(self, deep, testName):\r
- doc = parseString("<doc attr='value'/>")\r
- attr = doc.documentElement.getAttributeNode("attr")\r
- self.assertNotEqual(attr, None)\r
- clone = attr.cloneNode(deep)\r
- self.confirm(not clone.isSameNode(attr))\r
- self.confirm(not attr.isSameNode(clone))\r
- self.confirm(clone.ownerElement is None,\r
- testName + ": ownerElement should be None")\r
- self.confirm(clone.ownerDocument.isSameNode(attr.ownerDocument),\r
- testName + ": ownerDocument does not match")\r
- self.confirm(clone.specified,\r
- testName + ": cloned attribute must have specified == True")\r
-\r
- def testCloneAttributeShallow(self):\r
- self.check_clone_attribute(0, "testCloneAttributeShallow")\r
-\r
- def testCloneAttributeDeep(self):\r
- self.check_clone_attribute(1, "testCloneAttributeDeep")\r
-\r
- def check_clone_pi(self, deep, testName):\r
- doc = parseString("<?target data?><doc/>")\r
- pi = doc.firstChild\r
- self.assertEqual(pi.nodeType, Node.PROCESSING_INSTRUCTION_NODE)\r
- clone = pi.cloneNode(deep)\r
- self.confirm(clone.target == pi.target\r
- and clone.data == pi.data)\r
-\r
- def testClonePIShallow(self):\r
- self.check_clone_pi(0, "testClonePIShallow")\r
-\r
- def testClonePIDeep(self):\r
- self.check_clone_pi(1, "testClonePIDeep")\r
-\r
- def testNormalize(self):\r
- doc = parseString("<doc/>")\r
- root = doc.documentElement\r
- root.appendChild(doc.createTextNode("first"))\r
- root.appendChild(doc.createTextNode("second"))\r
- self.confirm(len(root.childNodes) == 2\r
- and root.childNodes.length == 2,\r
- "testNormalize -- preparation")\r
- doc.normalize()\r
- self.confirm(len(root.childNodes) == 1\r
- and root.childNodes.length == 1\r
- and root.firstChild is root.lastChild\r
- and root.firstChild.data == "firstsecond"\r
- , "testNormalize -- result")\r
- doc.unlink()\r
-\r
- doc = parseString("<doc/>")\r
- root = doc.documentElement\r
- root.appendChild(doc.createTextNode(""))\r
- doc.normalize()\r
- self.confirm(len(root.childNodes) == 0\r
- and root.childNodes.length == 0,\r
- "testNormalize -- single empty node removed")\r
- doc.unlink()\r
-\r
- def testNormalizeCombineAndNextSibling(self):\r
- doc = parseString("<doc/>")\r
- root = doc.documentElement\r
- root.appendChild(doc.createTextNode("first"))\r
- root.appendChild(doc.createTextNode("second"))\r
- root.appendChild(doc.createElement("i"))\r
- self.confirm(len(root.childNodes) == 3\r
- and root.childNodes.length == 3,\r
- "testNormalizeCombineAndNextSibling -- preparation")\r
- doc.normalize()\r
- self.confirm(len(root.childNodes) == 2\r
- and root.childNodes.length == 2\r
- and root.firstChild.data == "firstsecond"\r
- and root.firstChild is not root.lastChild\r
- and root.firstChild.nextSibling is root.lastChild\r
- and root.firstChild.previousSibling is None\r
- and root.lastChild.previousSibling is root.firstChild\r
- and root.lastChild.nextSibling is None\r
- , "testNormalizeCombinedAndNextSibling -- result")\r
- doc.unlink()\r
-\r
- def testNormalizeDeleteWithPrevSibling(self):\r
- doc = parseString("<doc/>")\r
- root = doc.documentElement\r
- root.appendChild(doc.createTextNode("first"))\r
- root.appendChild(doc.createTextNode(""))\r
- self.confirm(len(root.childNodes) == 2\r
- and root.childNodes.length == 2,\r
- "testNormalizeDeleteWithPrevSibling -- preparation")\r
- doc.normalize()\r
- self.confirm(len(root.childNodes) == 1\r
- and root.childNodes.length == 1\r
- and root.firstChild.data == "first"\r
- and root.firstChild is root.lastChild\r
- and root.firstChild.nextSibling is None\r
- and root.firstChild.previousSibling is None\r
- , "testNormalizeDeleteWithPrevSibling -- result")\r
- doc.unlink()\r
-\r
- def testNormalizeDeleteWithNextSibling(self):\r
- doc = parseString("<doc/>")\r
- root = doc.documentElement\r
- root.appendChild(doc.createTextNode(""))\r
- root.appendChild(doc.createTextNode("second"))\r
- self.confirm(len(root.childNodes) == 2\r
- and root.childNodes.length == 2,\r
- "testNormalizeDeleteWithNextSibling -- preparation")\r
- doc.normalize()\r
- self.confirm(len(root.childNodes) == 1\r
- and root.childNodes.length == 1\r
- and root.firstChild.data == "second"\r
- and root.firstChild is root.lastChild\r
- and root.firstChild.nextSibling is None\r
- and root.firstChild.previousSibling is None\r
- , "testNormalizeDeleteWithNextSibling -- result")\r
- doc.unlink()\r
-\r
- def testNormalizeDeleteWithTwoNonTextSiblings(self):\r
- doc = parseString("<doc/>")\r
- root = doc.documentElement\r
- root.appendChild(doc.createElement("i"))\r
- root.appendChild(doc.createTextNode(""))\r
- root.appendChild(doc.createElement("i"))\r
- self.confirm(len(root.childNodes) == 3\r
- and root.childNodes.length == 3,\r
- "testNormalizeDeleteWithTwoSiblings -- preparation")\r
- doc.normalize()\r
- self.confirm(len(root.childNodes) == 2\r
- and root.childNodes.length == 2\r
- and root.firstChild is not root.lastChild\r
- and root.firstChild.nextSibling is root.lastChild\r
- and root.firstChild.previousSibling is None\r
- and root.lastChild.previousSibling is root.firstChild\r
- and root.lastChild.nextSibling is None\r
- , "testNormalizeDeleteWithTwoSiblings -- result")\r
- doc.unlink()\r
-\r
- def testNormalizeDeleteAndCombine(self):\r
- doc = parseString("<doc/>")\r
- root = doc.documentElement\r
- root.appendChild(doc.createTextNode(""))\r
- root.appendChild(doc.createTextNode("second"))\r
- root.appendChild(doc.createTextNode(""))\r
- root.appendChild(doc.createTextNode("fourth"))\r
- root.appendChild(doc.createTextNode(""))\r
- self.confirm(len(root.childNodes) == 5\r
- and root.childNodes.length == 5,\r
- "testNormalizeDeleteAndCombine -- preparation")\r
- doc.normalize()\r
- self.confirm(len(root.childNodes) == 1\r
- and root.childNodes.length == 1\r
- and root.firstChild is root.lastChild\r
- and root.firstChild.data == "secondfourth"\r
- and root.firstChild.previousSibling is None\r
- and root.firstChild.nextSibling is None\r
- , "testNormalizeDeleteAndCombine -- result")\r
- doc.unlink()\r
-\r
- def testNormalizeRecursion(self):\r
- doc = parseString("<doc>"\r
- "<o>"\r
- "<i/>"\r
- "t"\r
- #\r
- #x\r
- "</o>"\r
- "<o>"\r
- "<o>"\r
- "t2"\r
- #x2\r
- "</o>"\r
- "t3"\r
- #x3\r
- "</o>"\r
- #\r
- "</doc>")\r
- root = doc.documentElement\r
- root.childNodes[0].appendChild(doc.createTextNode(""))\r
- root.childNodes[0].appendChild(doc.createTextNode("x"))\r
- root.childNodes[1].childNodes[0].appendChild(doc.createTextNode("x2"))\r
- root.childNodes[1].appendChild(doc.createTextNode("x3"))\r
- root.appendChild(doc.createTextNode(""))\r
- self.confirm(len(root.childNodes) == 3\r
- and root.childNodes.length == 3\r
- and len(root.childNodes[0].childNodes) == 4\r
- and root.childNodes[0].childNodes.length == 4\r
- and len(root.childNodes[1].childNodes) == 3\r
- and root.childNodes[1].childNodes.length == 3\r
- and len(root.childNodes[1].childNodes[0].childNodes) == 2\r
- and root.childNodes[1].childNodes[0].childNodes.length == 2\r
- , "testNormalize2 -- preparation")\r
- doc.normalize()\r
- self.confirm(len(root.childNodes) == 2\r
- and root.childNodes.length == 2\r
- and len(root.childNodes[0].childNodes) == 2\r
- and root.childNodes[0].childNodes.length == 2\r
- and len(root.childNodes[1].childNodes) == 2\r
- and root.childNodes[1].childNodes.length == 2\r
- and len(root.childNodes[1].childNodes[0].childNodes) == 1\r
- and root.childNodes[1].childNodes[0].childNodes.length == 1\r
- , "testNormalize2 -- childNodes lengths")\r
- self.confirm(root.childNodes[0].childNodes[1].data == "tx"\r
- and root.childNodes[1].childNodes[0].childNodes[0].data == "t2x2"\r
- and root.childNodes[1].childNodes[1].data == "t3x3"\r
- , "testNormalize2 -- joined text fields")\r
- self.confirm(root.childNodes[0].childNodes[1].nextSibling is None\r
- and root.childNodes[0].childNodes[1].previousSibling\r
- is root.childNodes[0].childNodes[0]\r
- and root.childNodes[0].childNodes[0].previousSibling is None\r
- and root.childNodes[0].childNodes[0].nextSibling\r
- is root.childNodes[0].childNodes[1]\r
- and root.childNodes[1].childNodes[1].nextSibling is None\r
- and root.childNodes[1].childNodes[1].previousSibling\r
- is root.childNodes[1].childNodes[0]\r
- and root.childNodes[1].childNodes[0].previousSibling is None\r
- and root.childNodes[1].childNodes[0].nextSibling\r
- is root.childNodes[1].childNodes[1]\r
- , "testNormalize2 -- sibling pointers")\r
- doc.unlink()\r
-\r
-\r
- def testBug0777884(self):\r
- doc = parseString("<o>text</o>")\r
- text = doc.documentElement.childNodes[0]\r
- self.assertEqual(text.nodeType, Node.TEXT_NODE)\r
- # Should run quietly, doing nothing.\r
- text.normalize()\r
- doc.unlink()\r
-\r
- def testBug1433694(self):\r
- doc = parseString("<o><i/>t</o>")\r
- node = doc.documentElement\r
- node.childNodes[1].nodeValue = ""\r
- node.normalize()\r
- self.confirm(node.childNodes[-1].nextSibling is None,\r
- "Final child's .nextSibling should be None")\r
-\r
- def testSiblings(self):\r
- doc = parseString("<doc><?pi?>text?<elm/></doc>")\r
- root = doc.documentElement\r
- (pi, text, elm) = root.childNodes\r
-\r
- self.confirm(pi.nextSibling is text and\r
- pi.previousSibling is None and\r
- text.nextSibling is elm and\r
- text.previousSibling is pi and\r
- elm.nextSibling is None and\r
- elm.previousSibling is text, "testSiblings")\r
-\r
- doc.unlink()\r
-\r
- def testParents(self):\r
- doc = parseString(\r
- "<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>")\r
- root = doc.documentElement\r
- elm1 = root.childNodes[0]\r
- (elm2a, elm2b) = elm1.childNodes\r
- elm3 = elm2b.childNodes[0]\r
-\r
- self.confirm(root.parentNode is doc and\r
- elm1.parentNode is root and\r
- elm2a.parentNode is elm1 and\r
- elm2b.parentNode is elm1 and\r
- elm3.parentNode is elm2b, "testParents")\r
- doc.unlink()\r
-\r
- def testNodeListItem(self):\r
- doc = parseString("<doc><e/><e/></doc>")\r
- children = doc.childNodes\r
- docelem = children[0]\r
- self.confirm(children[0] is children.item(0)\r
- and children.item(1) is None\r
- and docelem.childNodes.item(0) is docelem.childNodes[0]\r
- and docelem.childNodes.item(1) is docelem.childNodes[1]\r
- and docelem.childNodes.item(0).childNodes.item(0) is None,\r
- "test NodeList.item()")\r
- doc.unlink()\r
-\r
- def testSAX2DOM(self):\r
- from xml.dom import pulldom\r
-\r
- sax2dom = pulldom.SAX2DOM()\r
- sax2dom.startDocument()\r
- sax2dom.startElement("doc", {})\r
- sax2dom.characters("text")\r
- sax2dom.startElement("subelm", {})\r
- sax2dom.characters("text")\r
- sax2dom.endElement("subelm")\r
- sax2dom.characters("text")\r
- sax2dom.endElement("doc")\r
- sax2dom.endDocument()\r
-\r
- doc = sax2dom.document\r
- root = doc.documentElement\r
- (text1, elm1, text2) = root.childNodes\r
- text3 = elm1.childNodes[0]\r
-\r
- self.confirm(text1.previousSibling is None and\r
- text1.nextSibling is elm1 and\r
- elm1.previousSibling is text1 and\r
- elm1.nextSibling is text2 and\r
- text2.previousSibling is elm1 and\r
- text2.nextSibling is None and\r
- text3.previousSibling is None and\r
- text3.nextSibling is None, "testSAX2DOM - siblings")\r
-\r
- self.confirm(root.parentNode is doc and\r
- text1.parentNode is root and\r
- elm1.parentNode is root and\r
- text2.parentNode is root and\r
- text3.parentNode is elm1, "testSAX2DOM - parents")\r
- doc.unlink()\r
-\r
- def testEncodings(self):\r
- doc = parseString('<foo>€</foo>')\r
- self.confirm(doc.toxml() == u'<?xml version="1.0" ?><foo>\u20ac</foo>'\r
- and doc.toxml('utf-8') ==\r
- '<?xml version="1.0" encoding="utf-8"?><foo>\xe2\x82\xac</foo>'\r
- and doc.toxml('iso-8859-15') ==\r
- '<?xml version="1.0" encoding="iso-8859-15"?><foo>\xa4</foo>',\r
- "testEncodings - encoding EURO SIGN")\r
-\r
- # Verify that character decoding errors throw exceptions instead\r
- # of crashing\r
- self.assertRaises(UnicodeDecodeError, parseString,\r
- '<fran\xe7ais>Comment \xe7a va ? Tr\xe8s bien ?</fran\xe7ais>')\r
-\r
- doc.unlink()\r
-\r
- class UserDataHandler:\r
- called = 0\r
- def handle(self, operation, key, data, src, dst):\r
- dst.setUserData(key, data + 1, self)\r
- src.setUserData(key, None, None)\r
- self.called = 1\r
-\r
- def testUserData(self):\r
- dom = Document()\r
- n = dom.createElement('e')\r
- self.confirm(n.getUserData("foo") is None)\r
- n.setUserData("foo", None, None)\r
- self.confirm(n.getUserData("foo") is None)\r
- n.setUserData("foo", 12, 12)\r
- n.setUserData("bar", 13, 13)\r
- self.confirm(n.getUserData("foo") == 12)\r
- self.confirm(n.getUserData("bar") == 13)\r
- n.setUserData("foo", None, None)\r
- self.confirm(n.getUserData("foo") is None)\r
- self.confirm(n.getUserData("bar") == 13)\r
-\r
- handler = self.UserDataHandler()\r
- n.setUserData("bar", 12, handler)\r
- c = n.cloneNode(1)\r
- self.confirm(handler.called\r
- and n.getUserData("bar") is None\r
- and c.getUserData("bar") == 13)\r
- n.unlink()\r
- c.unlink()\r
- dom.unlink()\r
-\r
- def checkRenameNodeSharedConstraints(self, doc, node):\r
- # Make sure illegal NS usage is detected:\r
- self.assertRaises(xml.dom.NamespaceErr, doc.renameNode, node,\r
- "http://xml.python.org/ns", "xmlns:foo")\r
- doc2 = parseString("<doc/>")\r
- self.assertRaises(xml.dom.WrongDocumentErr, doc2.renameNode, node,\r
- xml.dom.EMPTY_NAMESPACE, "foo")\r
-\r
- def testRenameAttribute(self):\r
- doc = parseString("<doc a='v'/>")\r
- elem = doc.documentElement\r
- attrmap = elem.attributes\r
- attr = elem.attributes['a']\r
-\r
- # Simple renaming\r
- attr = doc.renameNode(attr, xml.dom.EMPTY_NAMESPACE, "b")\r
- self.confirm(attr.name == "b"\r
- and attr.nodeName == "b"\r
- and attr.localName is None\r
- and attr.namespaceURI == xml.dom.EMPTY_NAMESPACE\r
- and attr.prefix is None\r
- and attr.value == "v"\r
- and elem.getAttributeNode("a") is None\r
- and elem.getAttributeNode("b").isSameNode(attr)\r
- and attrmap["b"].isSameNode(attr)\r
- and attr.ownerDocument.isSameNode(doc)\r
- and attr.ownerElement.isSameNode(elem))\r
-\r
- # Rename to have a namespace, no prefix\r
- attr = doc.renameNode(attr, "http://xml.python.org/ns", "c")\r
- self.confirm(attr.name == "c"\r
- and attr.nodeName == "c"\r
- and attr.localName == "c"\r
- and attr.namespaceURI == "http://xml.python.org/ns"\r
- and attr.prefix is None\r
- and attr.value == "v"\r
- and elem.getAttributeNode("a") is None\r
- and elem.getAttributeNode("b") is None\r
- and elem.getAttributeNode("c").isSameNode(attr)\r
- and elem.getAttributeNodeNS(\r
- "http://xml.python.org/ns", "c").isSameNode(attr)\r
- and attrmap["c"].isSameNode(attr)\r
- and attrmap[("http://xml.python.org/ns", "c")].isSameNode(attr))\r
-\r
- # Rename to have a namespace, with prefix\r
- attr = doc.renameNode(attr, "http://xml.python.org/ns2", "p:d")\r
- self.confirm(attr.name == "p:d"\r
- and attr.nodeName == "p:d"\r
- and attr.localName == "d"\r
- and attr.namespaceURI == "http://xml.python.org/ns2"\r
- and attr.prefix == "p"\r
- and attr.value == "v"\r
- and elem.getAttributeNode("a") is None\r
- and elem.getAttributeNode("b") is None\r
- and elem.getAttributeNode("c") is None\r
- and elem.getAttributeNodeNS(\r
- "http://xml.python.org/ns", "c") is None\r
- and elem.getAttributeNode("p:d").isSameNode(attr)\r
- and elem.getAttributeNodeNS(\r
- "http://xml.python.org/ns2", "d").isSameNode(attr)\r
- and attrmap["p:d"].isSameNode(attr)\r
- and attrmap[("http://xml.python.org/ns2", "d")].isSameNode(attr))\r
-\r
- # Rename back to a simple non-NS node\r
- attr = doc.renameNode(attr, xml.dom.EMPTY_NAMESPACE, "e")\r
- self.confirm(attr.name == "e"\r
- and attr.nodeName == "e"\r
- and attr.localName is None\r
- and attr.namespaceURI == xml.dom.EMPTY_NAMESPACE\r
- and attr.prefix is None\r
- and attr.value == "v"\r
- and elem.getAttributeNode("a") is None\r
- and elem.getAttributeNode("b") is None\r
- and elem.getAttributeNode("c") is None\r
- and elem.getAttributeNode("p:d") is None\r
- and elem.getAttributeNodeNS(\r
- "http://xml.python.org/ns", "c") is None\r
- and elem.getAttributeNode("e").isSameNode(attr)\r
- and attrmap["e"].isSameNode(attr))\r
-\r
- self.assertRaises(xml.dom.NamespaceErr, doc.renameNode, attr,\r
- "http://xml.python.org/ns", "xmlns")\r
- self.checkRenameNodeSharedConstraints(doc, attr)\r
- doc.unlink()\r
-\r
- def testRenameElement(self):\r
- doc = parseString("<doc/>")\r
- elem = doc.documentElement\r
-\r
- # Simple renaming\r
- elem = doc.renameNode(elem, xml.dom.EMPTY_NAMESPACE, "a")\r
- self.confirm(elem.tagName == "a"\r
- and elem.nodeName == "a"\r
- and elem.localName is None\r
- and elem.namespaceURI == xml.dom.EMPTY_NAMESPACE\r
- and elem.prefix is None\r
- and elem.ownerDocument.isSameNode(doc))\r
-\r
- # Rename to have a namespace, no prefix\r
- elem = doc.renameNode(elem, "http://xml.python.org/ns", "b")\r
- self.confirm(elem.tagName == "b"\r
- and elem.nodeName == "b"\r
- and elem.localName == "b"\r
- and elem.namespaceURI == "http://xml.python.org/ns"\r
- and elem.prefix is None\r
- and elem.ownerDocument.isSameNode(doc))\r
-\r
- # Rename to have a namespace, with prefix\r
- elem = doc.renameNode(elem, "http://xml.python.org/ns2", "p:c")\r
- self.confirm(elem.tagName == "p:c"\r
- and elem.nodeName == "p:c"\r
- and elem.localName == "c"\r
- and elem.namespaceURI == "http://xml.python.org/ns2"\r
- and elem.prefix == "p"\r
- and elem.ownerDocument.isSameNode(doc))\r
-\r
- # Rename back to a simple non-NS node\r
- elem = doc.renameNode(elem, xml.dom.EMPTY_NAMESPACE, "d")\r
- self.confirm(elem.tagName == "d"\r
- and elem.nodeName == "d"\r
- and elem.localName is None\r
- and elem.namespaceURI == xml.dom.EMPTY_NAMESPACE\r
- and elem.prefix is None\r
- and elem.ownerDocument.isSameNode(doc))\r
-\r
- self.checkRenameNodeSharedConstraints(doc, elem)\r
- doc.unlink()\r
-\r
- def testRenameOther(self):\r
- # We have to create a comment node explicitly since not all DOM\r
- # builders used with minidom add comments to the DOM.\r
- doc = xml.dom.minidom.getDOMImplementation().createDocument(\r
- xml.dom.EMPTY_NAMESPACE, "e", None)\r
- node = doc.createComment("comment")\r
- self.assertRaises(xml.dom.NotSupportedErr, doc.renameNode, node,\r
- xml.dom.EMPTY_NAMESPACE, "foo")\r
- doc.unlink()\r
-\r
- def testWholeText(self):\r
- doc = parseString("<doc>a</doc>")\r
- elem = doc.documentElement\r
- text = elem.childNodes[0]\r
- self.assertEqual(text.nodeType, Node.TEXT_NODE)\r
-\r
- self.checkWholeText(text, "a")\r
- elem.appendChild(doc.createTextNode("b"))\r
- self.checkWholeText(text, "ab")\r
- elem.insertBefore(doc.createCDATASection("c"), text)\r
- self.checkWholeText(text, "cab")\r
-\r
- # make sure we don't cross other nodes\r
- splitter = doc.createComment("comment")\r
- elem.appendChild(splitter)\r
- text2 = doc.createTextNode("d")\r
- elem.appendChild(text2)\r
- self.checkWholeText(text, "cab")\r
- self.checkWholeText(text2, "d")\r
-\r
- x = doc.createElement("x")\r
- elem.replaceChild(x, splitter)\r
- splitter = x\r
- self.checkWholeText(text, "cab")\r
- self.checkWholeText(text2, "d")\r
-\r
- x = doc.createProcessingInstruction("y", "z")\r
- elem.replaceChild(x, splitter)\r
- splitter = x\r
- self.checkWholeText(text, "cab")\r
- self.checkWholeText(text2, "d")\r
-\r
- elem.removeChild(splitter)\r
- self.checkWholeText(text, "cabd")\r
- self.checkWholeText(text2, "cabd")\r
-\r
- def testPatch1094164(self):\r
- doc = parseString("<doc><e/></doc>")\r
- elem = doc.documentElement\r
- e = elem.firstChild\r
- self.confirm(e.parentNode is elem, "Before replaceChild()")\r
- # Check that replacing a child with itself leaves the tree unchanged\r
- elem.replaceChild(e, e)\r
- self.confirm(e.parentNode is elem, "After replaceChild()")\r
-\r
- def testReplaceWholeText(self):\r
- def setup():\r
- doc = parseString("<doc>a<e/>d</doc>")\r
- elem = doc.documentElement\r
- text1 = elem.firstChild\r
- text2 = elem.lastChild\r
- splitter = text1.nextSibling\r
- elem.insertBefore(doc.createTextNode("b"), splitter)\r
- elem.insertBefore(doc.createCDATASection("c"), text1)\r
- return doc, elem, text1, splitter, text2\r
-\r
- doc, elem, text1, splitter, text2 = setup()\r
- text = text1.replaceWholeText("new content")\r
- self.checkWholeText(text, "new content")\r
- self.checkWholeText(text2, "d")\r
- self.confirm(len(elem.childNodes) == 3)\r
-\r
- doc, elem, text1, splitter, text2 = setup()\r
- text = text2.replaceWholeText("new content")\r
- self.checkWholeText(text, "new content")\r
- self.checkWholeText(text1, "cab")\r
- self.confirm(len(elem.childNodes) == 5)\r
-\r
- doc, elem, text1, splitter, text2 = setup()\r
- text = text1.replaceWholeText("")\r
- self.checkWholeText(text2, "d")\r
- self.confirm(text is None\r
- and len(elem.childNodes) == 2)\r
-\r
- def testSchemaType(self):\r
- doc = parseString(\r
- "<!DOCTYPE doc [\n"\r
- " <!ENTITY e1 SYSTEM 'http://xml.python.org/e1'>\n"\r
- " <!ENTITY e2 SYSTEM 'http://xml.python.org/e2'>\n"\r
- " <!ATTLIST doc id ID #IMPLIED \n"\r
- " ref IDREF #IMPLIED \n"\r
- " refs IDREFS #IMPLIED \n"\r
- " enum (a|b) #IMPLIED \n"\r
- " ent ENTITY #IMPLIED \n"\r
- " ents ENTITIES #IMPLIED \n"\r
- " nm NMTOKEN #IMPLIED \n"\r
- " nms NMTOKENS #IMPLIED \n"\r
- " text CDATA #IMPLIED \n"\r
- " >\n"\r
- "]><doc id='name' notid='name' text='splat!' enum='b'"\r
- " ref='name' refs='name name' ent='e1' ents='e1 e2'"\r
- " nm='123' nms='123 abc' />")\r
- elem = doc.documentElement\r
- # We don't want to rely on any specific loader at this point, so\r
- # just make sure we can get to all the names, and that the\r
- # DTD-based namespace is right. The names can vary by loader\r
- # since each supports a different level of DTD information.\r
- t = elem.schemaType\r
- self.confirm(t.name is None\r
- and t.namespace == xml.dom.EMPTY_NAMESPACE)\r
- names = "id notid text enum ref refs ent ents nm nms".split()\r
- for name in names:\r
- a = elem.getAttributeNode(name)\r
- t = a.schemaType\r
- self.confirm(hasattr(t, "name")\r
- and t.namespace == xml.dom.EMPTY_NAMESPACE)\r
-\r
- def testSetIdAttribute(self):\r
- doc = parseString("<doc a1='v' a2='w'/>")\r
- e = doc.documentElement\r
- a1 = e.getAttributeNode("a1")\r
- a2 = e.getAttributeNode("a2")\r
- self.confirm(doc.getElementById("v") is None\r
- and not a1.isId\r
- and not a2.isId)\r
- e.setIdAttribute("a1")\r
- self.confirm(e.isSameNode(doc.getElementById("v"))\r
- and a1.isId\r
- and not a2.isId)\r
- e.setIdAttribute("a2")\r
- self.confirm(e.isSameNode(doc.getElementById("v"))\r
- and e.isSameNode(doc.getElementById("w"))\r
- and a1.isId\r
- and a2.isId)\r
- # replace the a1 node; the new node should *not* be an ID\r
- a3 = doc.createAttribute("a1")\r
- a3.value = "v"\r
- e.setAttributeNode(a3)\r
- self.confirm(doc.getElementById("v") is None\r
- and e.isSameNode(doc.getElementById("w"))\r
- and not a1.isId\r
- and a2.isId\r
- and not a3.isId)\r
- # renaming an attribute should not affect its ID-ness:\r
- doc.renameNode(a2, xml.dom.EMPTY_NAMESPACE, "an")\r
- self.confirm(e.isSameNode(doc.getElementById("w"))\r
- and a2.isId)\r
-\r
- def testSetIdAttributeNS(self):\r
- NS1 = "http://xml.python.org/ns1"\r
- NS2 = "http://xml.python.org/ns2"\r
- doc = parseString("<doc"\r
- " xmlns:ns1='" + NS1 + "'"\r
- " xmlns:ns2='" + NS2 + "'"\r
- " ns1:a1='v' ns2:a2='w'/>")\r
- e = doc.documentElement\r
- a1 = e.getAttributeNodeNS(NS1, "a1")\r
- a2 = e.getAttributeNodeNS(NS2, "a2")\r
- self.confirm(doc.getElementById("v") is None\r
- and not a1.isId\r
- and not a2.isId)\r
- e.setIdAttributeNS(NS1, "a1")\r
- self.confirm(e.isSameNode(doc.getElementById("v"))\r
- and a1.isId\r
- and not a2.isId)\r
- e.setIdAttributeNS(NS2, "a2")\r
- self.confirm(e.isSameNode(doc.getElementById("v"))\r
- and e.isSameNode(doc.getElementById("w"))\r
- and a1.isId\r
- and a2.isId)\r
- # replace the a1 node; the new node should *not* be an ID\r
- a3 = doc.createAttributeNS(NS1, "a1")\r
- a3.value = "v"\r
- e.setAttributeNode(a3)\r
- self.confirm(e.isSameNode(doc.getElementById("w")))\r
- self.confirm(not a1.isId)\r
- self.confirm(a2.isId)\r
- self.confirm(not a3.isId)\r
- self.confirm(doc.getElementById("v") is None)\r
- # renaming an attribute should not affect its ID-ness:\r
- doc.renameNode(a2, xml.dom.EMPTY_NAMESPACE, "an")\r
- self.confirm(e.isSameNode(doc.getElementById("w"))\r
- and a2.isId)\r
-\r
- def testSetIdAttributeNode(self):\r
- NS1 = "http://xml.python.org/ns1"\r
- NS2 = "http://xml.python.org/ns2"\r
- doc = parseString("<doc"\r
- " xmlns:ns1='" + NS1 + "'"\r
- " xmlns:ns2='" + NS2 + "'"\r
- " ns1:a1='v' ns2:a2='w'/>")\r
- e = doc.documentElement\r
- a1 = e.getAttributeNodeNS(NS1, "a1")\r
- a2 = e.getAttributeNodeNS(NS2, "a2")\r
- self.confirm(doc.getElementById("v") is None\r
- and not a1.isId\r
- and not a2.isId)\r
- e.setIdAttributeNode(a1)\r
- self.confirm(e.isSameNode(doc.getElementById("v"))\r
- and a1.isId\r
- and not a2.isId)\r
- e.setIdAttributeNode(a2)\r
- self.confirm(e.isSameNode(doc.getElementById("v"))\r
- and e.isSameNode(doc.getElementById("w"))\r
- and a1.isId\r
- and a2.isId)\r
- # replace the a1 node; the new node should *not* be an ID\r
- a3 = doc.createAttributeNS(NS1, "a1")\r
- a3.value = "v"\r
- e.setAttributeNode(a3)\r
- self.confirm(e.isSameNode(doc.getElementById("w")))\r
- self.confirm(not a1.isId)\r
- self.confirm(a2.isId)\r
- self.confirm(not a3.isId)\r
- self.confirm(doc.getElementById("v") is None)\r
- # renaming an attribute should not affect its ID-ness:\r
- doc.renameNode(a2, xml.dom.EMPTY_NAMESPACE, "an")\r
- self.confirm(e.isSameNode(doc.getElementById("w"))\r
- and a2.isId)\r
-\r
- def testPickledDocument(self):\r
- doc = parseString("<?xml version='1.0' encoding='us-ascii'?>\n"\r
- "<!DOCTYPE doc PUBLIC 'http://xml.python.org/public'"\r
- " 'http://xml.python.org/system' [\n"\r
- " <!ELEMENT e EMPTY>\n"\r
- " <!ENTITY ent SYSTEM 'http://xml.python.org/entity'>\n"\r
- "]><doc attr='value'> text\n"\r
- "<?pi sample?> <!-- comment --> <e/> </doc>")\r
- s = pickle.dumps(doc)\r
- doc2 = pickle.loads(s)\r
- stack = [(doc, doc2)]\r
- while stack:\r
- n1, n2 = stack.pop()\r
- self.confirm(n1.nodeType == n2.nodeType\r
- and len(n1.childNodes) == len(n2.childNodes)\r
- and n1.nodeName == n2.nodeName\r
- and not n1.isSameNode(n2)\r
- and not n2.isSameNode(n1))\r
- if n1.nodeType == Node.DOCUMENT_TYPE_NODE:\r
- len(n1.entities)\r
- len(n2.entities)\r
- len(n1.notations)\r
- len(n2.notations)\r
- self.confirm(len(n1.entities) == len(n2.entities)\r
- and len(n1.notations) == len(n2.notations))\r
- for i in range(len(n1.notations)):\r
- # XXX this loop body doesn't seem to be executed?\r
- no1 = n1.notations.item(i)\r
- no2 = n1.notations.item(i)\r
- self.confirm(no1.name == no2.name\r
- and no1.publicId == no2.publicId\r
- and no1.systemId == no2.systemId)\r
- stack.append((no1, no2))\r
- for i in range(len(n1.entities)):\r
- e1 = n1.entities.item(i)\r
- e2 = n2.entities.item(i)\r
- self.confirm(e1.notationName == e2.notationName\r
- and e1.publicId == e2.publicId\r
- and e1.systemId == e2.systemId)\r
- stack.append((e1, e2))\r
- if n1.nodeType != Node.DOCUMENT_NODE:\r
- self.confirm(n1.ownerDocument.isSameNode(doc)\r
- and n2.ownerDocument.isSameNode(doc2))\r
- for i in range(len(n1.childNodes)):\r
- stack.append((n1.childNodes[i], n2.childNodes[i]))\r
-\r
- def testSerializeCommentNodeWithDoubleHyphen(self):\r
- doc = create_doc_without_doctype()\r
- doc.appendChild(doc.createComment("foo--bar"))\r
- self.assertRaises(ValueError, doc.toxml)\r
-\r
- def testEmptyXMLNSValue(self):\r
- doc = parseString("<element xmlns=''>\n"\r
- "<foo/>\n</element>")\r
- doc2 = parseString(doc.toxml())\r
- self.confirm(doc2.namespaceURI == xml.dom.EMPTY_NAMESPACE)\r
-\r
-\r
-def test_main():\r
- run_unittest(MinidomTest)\r
-\r
-if __name__ == "__main__":\r
- test_main()\r