Commits

ph...@9ae0c189-cd1f-4510-a509-f4891f5cf20d  committed fb39329

Modified Files:
dom.c
[fix] appendChild will now work correctly with empty nodes

  • Participants
  • Parent commits c78aae4

Comments (0)

Files changed (1)

 
        if ( cur->type == XML_DOCUMENT_FRAG_NODE ) {
            c1 = cur->children;
-	   while ( c1 ){
+           while ( c1 ){
                c1->parent = p;
                c1 = c1->next;
-	   }  
+           }  
            c1 = cur->children;
-	   c2 = cur->last;
+           c2 = cur->last;
            cur->last = cur->children = NULL;
        }
        else {
                leader->next = c1;
 	       c1->prev = leader;
            }
-	   else if ( p ) {
+           else if ( p ) {
                p->children = c1;
            }
 	   
            if ( followup ) {
                followup->prev = c2;
-	       c2->next = followup;
+               c2->next = followup;
            }
-	   else if ( p ) {
+           else if ( p ) {
                p->last = c2;
            }
        }
     return 1;
 }
 
+void
+domUnlinkNode( xmlNodePtr node ) {
+    if ( node == NULL ) {
+        return;
+    }
+
+    if ( node->prev != NULL ) {
+        node->prev->next = node->next;
+    }
+
+    if ( node->next != NULL ) {
+        node->next->prev = node->prev;
+    }
+
+    if ( node->parent != NULL ) {
+        if ( node == node->parent->last ) {
+            node->parent->last = node->prev;
+        }
+
+        if ( node == node->parent->children ) {
+            node->parent->children = node->next;
+        }
+    }
+
+    node->prev   = NULL;
+    node->next   = NULL;
+    node->parent = NULL;
+}
+
 xmlNodePtr
 domImportNode( xmlDocPtr doc, xmlNodePtr node, int move ) {
     xmlNodePtr return_node = node;
     if ( move ) {
         return_node = node;
         if ( node->type != XML_DTD_NODE ) {
-            xmlUnlinkNode( node );
+            domUnlinkNode( node );
         }
     }
     else {
     }
 
     if ( newChild->doc == self->doc ){
-        xmlUnlinkNode( newChild );
+        domUnlinkNode( newChild ); 
     }
     else {
         xs_warn("WRONG_DOCUMENT_ERR - non conform implementation\n"); 
     }
  
     if ( self->children != NULL ) {
+        warn("unlink node!\n");
         domAddNodeToList( newChild, self->last, NULL );
     }
     else if (newChild->type == XML_DOCUMENT_FRAG_NODE ) {
-	newChild->children->parent = self;
+        xmlNodePtr c1;
+        newChild->children->parent = self;
         self->children = newChild->children;
-        self->last     = newChild->last;
-        domAddNodeToList( newChild, self->last, NULL );
+        c1 = newChild->children;
+        while ( c1 ){
+            c1->parent = self;
+            c1 = c1->next;
+        }  
+        self->last = newChild->last;
+        newChild->last = newChild->children = NULL;
     }
     else {
         self->children = newChild;
 	 && old->type != XML_ATTRIBUTE_NODE
 	 && old->type != XML_NAMESPACE_DECL
          && (self == old->parent)) {
-        xmlUnlinkNode( old );
+        domUnlinkNode( old );
     }
     return old ;
 }
     }
     
     if ( new->doc == self->doc ) {
-        xmlUnlinkNode( new );
+        domUnlinkNode( new );
     }
     else {
         /* WRONG_DOCUMENT_ERR - non conform implementation */
     }
 
     if ( self->doc == newChild->doc ){
-        xmlUnlinkNode( newChild );
+        domUnlinkNode( newChild );
     }
     else {
         newChild = domImportNode( self->doc, newChild, 1 );
     }
 
     if ( self->doc == newChild->doc ){
-        xmlUnlinkNode( newChild );
+        domUnlinkNode( newChild );
     }
     else {
         newChild = domImportNode( self->doc, newChild, 1 );
     prev = oldNode->prev;
     next = oldNode->next;
 
-    xmlUnlinkNode( oldNode );
+    if ( oldNode->_private == NULL ) {
+        xmlUnlinkNode( oldNode );
+    }
+    else {
+        domUnlinkNode( oldNode );
+    }
 
     if( prev == NULL && next == NULL ) {
         /* oldNode was the only child */