From: Andrew M. Kuchling Date: Tue, 22 Nov 2005 19:04:36 +0000 (+0000) Subject: [Patch #1094164] replaceChild(x,x) ends up removing x from the tree. Add fix from... X-Git-Tag: v2.4.3c1~209 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4af716958a93298b84606406936c20605e95a09f;p=thirdparty%2FPython%2Fcpython.git [Patch #1094164] replaceChild(x,x) ends up removing x from the tree. Add fix from Felix Rabe and a test case --- diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index 8b4c71523c15..68aac0f44a2f 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -1127,6 +1127,17 @@ def testWholeText(): checkWholeText(text, "cabd") checkWholeText(text2, "cabd") +def testPatch1094164 (): + doc = parseString("") + elem = doc.documentElement + e = elem.firstChild + confirm(e.parentNode is elem, "Before replaceChild()") + # Check that replacing a child with itself leaves the tree unchanged + elem.replaceChild(e, e) + confirm(e.parentNode is elem, "After replaceChild()") + + + def testReplaceWholeText(): def setup(): doc = parseString("ad") diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index 4bb4ef49ff1d..84be99b67a18 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -135,10 +135,10 @@ class Node(xml.dom.Node, GetattrMagic): if newChild.nodeType not in self._child_node_types: raise xml.dom.HierarchyRequestErr( "%s cannot be child of %s" % (repr(newChild), repr(self))) - if newChild.parentNode is not None: - newChild.parentNode.removeChild(newChild) if newChild is oldChild: return + if newChild.parentNode is not None: + newChild.parentNode.removeChild(newChild) try: index = self.childNodes.index(oldChild) except ValueError: