Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e50959a

Browse files
committed
Fix appendChild() and insertBefore() (and replaceChild() indirectly) when
the node being added is a fragment node. This closes SF bug #487929.
1 parent bf7c804 commit e50959a

3 files changed

Lines changed: 57 additions & 2 deletions

File tree

Lib/test/output/test_minidom

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Passed Test
1919
Passed Test
2020
Test Succeeded testAppendChild
2121
Passed assertion: len(Node.allnodes) == 0
22+
Passed appendChild(<fragment>)
23+
Test Succeeded testAppendChildFragment
24+
Passed assertion: len(Node.allnodes) == 0
2225
Test Succeeded testAttrListItem
2326
Passed assertion: len(Node.allnodes) == 0
2427
Test Succeeded testAttrListItemNS
@@ -122,6 +125,10 @@ Passed testInsertBefore -- node properly placed in tree
122125
Passed testInsertBefore -- node properly placed in tree
123126
Test Succeeded testInsertBefore
124127
Passed assertion: len(Node.allnodes) == 0
128+
Passed insertBefore(<fragment>, None)
129+
Passed insertBefore(<fragment>, orig)
130+
Test Succeeded testInsertBeforeFragment
131+
Passed assertion: len(Node.allnodes) == 0
125132
Test Succeeded testLegalChildren
126133
Passed assertion: len(Node.allnodes) == 0
127134
Passed test NodeList.item()
@@ -172,6 +179,9 @@ Passed Test
172179
Passed Test
173180
Test Succeeded testRemoveAttributeNode
174181
Passed assertion: len(Node.allnodes) == 0
182+
Passed replaceChild(<fragment>)
183+
Test Succeeded testReplaceChildFragment
184+
Passed assertion: len(Node.allnodes) == 0
175185
Passed testSAX2DOM - siblings
176186
Passed testSAX2DOM - parents
177187
Test Succeeded testSAX2DOM

Lib/test/test_minidom.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,58 @@ def testInsertBefore():
7979
, "testInsertBefore -- node properly placed in tree")
8080
dom.unlink()
8181

82+
def _create_fragment_test_nodes():
83+
dom = parseString("<doc/>")
84+
orig = dom.createTextNode("original")
85+
c1 = dom.createTextNode("foo")
86+
c2 = dom.createTextNode("bar")
87+
c3 = dom.createTextNode("bat")
88+
dom.documentElement.appendChild(orig)
89+
frag = dom.createDocumentFragment()
90+
frag.appendChild(c1)
91+
frag.appendChild(c2)
92+
frag.appendChild(c3)
93+
return dom, orig, c1, c2, c3, frag
94+
95+
def testInsertBeforeFragment():
96+
dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes()
97+
dom.documentElement.insertBefore(frag, None)
98+
confirm(tuple(dom.documentElement.childNodes) == (orig, c1, c2, c3),
99+
"insertBefore(<fragment>, None)")
100+
frag.unlink()
101+
dom.unlink()
102+
#
103+
dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes()
104+
dom.documentElement.insertBefore(frag, orig)
105+
confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3, orig),
106+
"insertBefore(<fragment>, orig)")
107+
frag.unlink()
108+
dom.unlink()
109+
82110
def testAppendChild():
83111
dom = parse(tstfile)
84112
dom.documentElement.appendChild(dom.createComment(u"Hello"))
85113
confirm(dom.documentElement.childNodes[-1].nodeName == "#comment")
86114
confirm(dom.documentElement.childNodes[-1].data == "Hello")
87115
dom.unlink()
88116

117+
def testAppendChildFragment():
118+
dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes()
119+
dom.documentElement.appendChild(frag)
120+
confirm(tuple(dom.documentElement.childNodes) == (orig, c1, c2, c3),
121+
"appendChild(<fragment>)")
122+
frag.unlink()
123+
dom.unlink()
124+
125+
def testReplaceChildFragment():
126+
dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes()
127+
dom.documentElement.replaceChild(frag, orig)
128+
orig.unlink()
129+
confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3),
130+
"replaceChild(<fragment>)")
131+
frag.unlink()
132+
dom.unlink()
133+
89134
def testLegalChildren():
90135
dom = Document()
91136
elem = dom.createElement('element')

Lib/xml/dom/minidom.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def __getattr__(self, key):
132132

133133
def insertBefore(self, newChild, refChild):
134134
if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE:
135-
for c in newChild.childNodes:
135+
for c in tuple(newChild.childNodes):
136136
self.insertBefore(c, refChild)
137137
### The DOM does not clearly specify what to return in this case
138138
return newChild
@@ -160,7 +160,7 @@ def insertBefore(self, newChild, refChild):
160160

161161
def appendChild(self, node):
162162
if node.nodeType == self.DOCUMENT_FRAGMENT_NODE:
163-
for c in node.childNodes:
163+
for c in tuple(node.childNodes):
164164
self.appendChild(c)
165165
### The DOM does not clearly specify what to return in this case
166166
return node

0 commit comments

Comments
 (0)