Commit ca2b6592 authored by Fred Drake's avatar Fred Drake

Fix appendChild() and insertBefore() (and replaceChild() indirectly) when

the node being added is a fragment node.
This closes SF bug #487929.
parent 632aefe8
...@@ -19,6 +19,9 @@ Passed Test ...@@ -19,6 +19,9 @@ Passed Test
Passed Test Passed Test
Test Succeeded testAppendChild Test Succeeded testAppendChild
Passed assertion: len(Node.allnodes) == 0 Passed assertion: len(Node.allnodes) == 0
Passed appendChild(<fragment>)
Test Succeeded testAppendChildFragment
Passed assertion: len(Node.allnodes) == 0
Test Succeeded testAttrListItem Test Succeeded testAttrListItem
Passed assertion: len(Node.allnodes) == 0 Passed assertion: len(Node.allnodes) == 0
Test Succeeded testAttrListItemNS Test Succeeded testAttrListItemNS
...@@ -122,6 +125,10 @@ Passed testInsertBefore -- node properly placed in tree ...@@ -122,6 +125,10 @@ Passed testInsertBefore -- node properly placed in tree
Passed testInsertBefore -- node properly placed in tree Passed testInsertBefore -- node properly placed in tree
Test Succeeded testInsertBefore Test Succeeded testInsertBefore
Passed assertion: len(Node.allnodes) == 0 Passed assertion: len(Node.allnodes) == 0
Passed insertBefore(<fragment>, None)
Passed insertBefore(<fragment>, orig)
Test Succeeded testInsertBeforeFragment
Passed assertion: len(Node.allnodes) == 0
Test Succeeded testLegalChildren Test Succeeded testLegalChildren
Passed assertion: len(Node.allnodes) == 0 Passed assertion: len(Node.allnodes) == 0
Passed test NodeList.item() Passed test NodeList.item()
...@@ -172,6 +179,9 @@ Passed Test ...@@ -172,6 +179,9 @@ Passed Test
Passed Test Passed Test
Test Succeeded testRemoveAttributeNode Test Succeeded testRemoveAttributeNode
Passed assertion: len(Node.allnodes) == 0 Passed assertion: len(Node.allnodes) == 0
Passed replaceChild(<fragment>)
Test Succeeded testReplaceChildFragment
Passed assertion: len(Node.allnodes) == 0
Passed testSAX2DOM - siblings Passed testSAX2DOM - siblings
Passed testSAX2DOM - parents Passed testSAX2DOM - parents
Test Succeeded testSAX2DOM Test Succeeded testSAX2DOM
......
...@@ -79,6 +79,34 @@ def testInsertBefore(): ...@@ -79,6 +79,34 @@ def testInsertBefore():
, "testInsertBefore -- node properly placed in tree") , "testInsertBefore -- node properly placed in tree")
dom.unlink() dom.unlink()
def _create_fragment_test_nodes():
dom = parseString("<doc/>")
orig = dom.createTextNode("original")
c1 = dom.createTextNode("foo")
c2 = dom.createTextNode("bar")
c3 = dom.createTextNode("bat")
dom.documentElement.appendChild(orig)
frag = dom.createDocumentFragment()
frag.appendChild(c1)
frag.appendChild(c2)
frag.appendChild(c3)
return dom, orig, c1, c2, c3, frag
def testInsertBeforeFragment():
dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes()
dom.documentElement.insertBefore(frag, None)
confirm(tuple(dom.documentElement.childNodes) == (orig, c1, c2, c3),
"insertBefore(<fragment>, None)")
frag.unlink()
dom.unlink()
#
dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes()
dom.documentElement.insertBefore(frag, orig)
confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3, orig),
"insertBefore(<fragment>, orig)")
frag.unlink()
dom.unlink()
def testAppendChild(): def testAppendChild():
dom = parse(tstfile) dom = parse(tstfile)
dom.documentElement.appendChild(dom.createComment(u"Hello")) dom.documentElement.appendChild(dom.createComment(u"Hello"))
...@@ -86,6 +114,23 @@ def testAppendChild(): ...@@ -86,6 +114,23 @@ def testAppendChild():
confirm(dom.documentElement.childNodes[-1].data == "Hello") confirm(dom.documentElement.childNodes[-1].data == "Hello")
dom.unlink() dom.unlink()
def testAppendChildFragment():
dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes()
dom.documentElement.appendChild(frag)
confirm(tuple(dom.documentElement.childNodes) == (orig, c1, c2, c3),
"appendChild(<fragment>)")
frag.unlink()
dom.unlink()
def testReplaceChildFragment():
dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes()
dom.documentElement.replaceChild(frag, orig)
orig.unlink()
confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3),
"replaceChild(<fragment>)")
frag.unlink()
dom.unlink()
def testLegalChildren(): def testLegalChildren():
dom = Document() dom = Document()
elem = dom.createElement('element') elem = dom.createElement('element')
......
...@@ -132,7 +132,7 @@ class Node(xml.dom.Node): ...@@ -132,7 +132,7 @@ class Node(xml.dom.Node):
def insertBefore(self, newChild, refChild): def insertBefore(self, newChild, refChild):
if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE: if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE:
for c in newChild.childNodes: for c in tuple(newChild.childNodes):
self.insertBefore(c, refChild) self.insertBefore(c, refChild)
### The DOM does not clearly specify what to return in this case ### The DOM does not clearly specify what to return in this case
return newChild return newChild
...@@ -160,7 +160,7 @@ class Node(xml.dom.Node): ...@@ -160,7 +160,7 @@ class Node(xml.dom.Node):
def appendChild(self, node): def appendChild(self, node):
if node.nodeType == self.DOCUMENT_FRAGMENT_NODE: if node.nodeType == self.DOCUMENT_FRAGMENT_NODE:
for c in node.childNodes: for c in tuple(node.childNodes):
self.appendChild(c) self.appendChild(c)
### The DOM does not clearly specify what to return in this case ### The DOM does not clearly specify what to return in this case
return node return node
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment