[XML-SIG] DocumentFragment bug in minidom

Geert Jansen geert@boskant.nl
Sun, 8 Jul 2001 17:05:50 +0200


Hi!

While playing around with minidom and DocumentFragments, I ran across a
small bug in the handling of DocumentFragments. (I'm using vanilla Python
2.1)

When you're adding a DocumentFragment to a node with Node.appendNode(), this
is supposed to add all children of the DocumentFragment to the Node. I
noticed however that when the DocumentFragment has more than one node, its
_last_ node is skipped.

Looking through the sources, the problem seems to be caused in minidom.py,
lines 140-141:

   def appendChild(self, node):
        if node.nodeType == self.DOCUMENT_FRAGMENT_NODE:
            for c in node.childNodes:
                self.appendChild(c)
            ### The DOM does not clearly specify what to return in this case
            return node

The call "self.appendChild(c)" changes the list node.childNodes under our
feet, because it tries to remove the child from its parent. This apparently
works out in such a way that the iteration of node.childNodes skips the last
element.

With the patch below, appendChild() does work as expected with
DocumentFragment's.

--- minidom.py.old      Sat Jul  7 15:42:51 2001
+++ minidom.py  Sat Jul  7 15:48:59 2001
@@ -137,7 +137,9 @@

     def appendChild(self, node):
         if node.nodeType == self.DOCUMENT_FRAGMENT_NODE:
-            for c in node.childNodes:
+            # Make a copy of childNodes as appendChild() will change it.
+            children = [ c for c in node.childNodes ]
+            for c in children:
                 self.appendChild(c)
             ### The DOM does not clearly specify what to return in this
case
             return node


Can this patch be applied? Please CC me in replies, as I'm not subscribed to
the list.

Greetings,
Geert Jansen