set doc on last child tree in xmlAddChildList for bug #546772. Fix problem

* tree.c: set doc on last child tree in xmlAddChildList for 
  bug #546772. Fix problem adding an attribute via with xmlAddChild 
  reported by Kris Breuker.

svn path=/trunk/; revision=3806
diff --git a/ChangeLog b/ChangeLog
index 7ba1702..971a65b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Wed Dec 31 23:11:37 CET 2008 Rob Richards <rrichards@cdatazone.org>
+
+	* tree.c: set doc on last child tree in xmlAddChildList for 
+	  bug #546772. Fix problem adding an attribute via with xmlAddChild 
+	  reported by Kris Breuker.
+
 Sun Dec 27 14:16:13 CET 2008 Rob Richards <rrichards@cdatazone.org>
 
 	* xmlwriter.c: fix indenting in xmlTextWriterFullEndElement for 
diff --git a/tree.c b/tree.c
index fe89dc4..8e393da 100644
--- a/tree.c
+++ b/tree.c
@@ -3216,7 +3216,10 @@
         cur = cur->next;
     }
     cur->parent = parent;
-    cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
+    /* the parent may not be linked to a doc ! */
+    if (cur->doc != parent->doc) {
+        xmlSetTreeDoc(cur, parent->doc);
+    }
     parent->last = cur;
 
     return(cur);
@@ -3309,9 +3312,7 @@
     if (cur->type == XML_ATTRIBUTE_NODE) {
 		if (parent->type != XML_ELEMENT_NODE)
 			return(NULL);
-	if (parent->properties == NULL) {
-	    parent->properties = (xmlAttrPtr) cur;
-	} else {
+	if (parent->properties != NULL) {
 	    /* check if an attribute with the same name exists */
 	    xmlAttrPtr lastattr;
 
@@ -3326,8 +3327,13 @@
 	    }
 		if (lastattr == (xmlAttrPtr) cur)
 			return(cur);
+
+	}
+	if (parent->properties == NULL) {
+	    parent->properties = (xmlAttrPtr) cur;
+	} else {
 	    /* find the end */
-	    lastattr = parent->properties;
+	    xmlAttrPtr lastattr = parent->properties;
 	    while (lastattr->next != NULL) {
 		lastattr = lastattr->next;
 	    }