Bump to libxml2 2.9.7
[platform/upstream/libxml2.git] / tree.c
diff --git a/tree.c b/tree.c
index 307782c..86a8da7 100644 (file)
--- a/tree.c
+++ b/tree.c
  *
  */
 
+/* To avoid EBCDIC trouble when parsing on zOS */
+#if defined(__MVS__)
+#pragma convert("ISO8859-1")
+#endif
+
 #define IN_LIBXML
 #include "libxml.h"
 
 #include <string.h> /* for memset() only ! */
+#include <stddef.h>
 #include <limits.h>
 #ifdef HAVE_CTYPE_H
 #include <ctype.h>
@@ -254,10 +260,10 @@ xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
  *
  * [NS 7] LocalPart ::= NCName
  *
- * Returns NULL if not a QName, otherwise the local part, and prefix
- *   is updated to get the Prefix if any.
+ * Returns NULL if the name doesn't have a prefix. Otherwise, returns the
+ * local part, and prefix is updated to get the Prefix. Both the return value
+ * and the prefix must be freed by the caller.
  */
-
 xmlChar *
 xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
     int len = 0;
@@ -1044,7 +1050,7 @@ xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
  * DICT_FREE:
  * @str:  a string
  *
- * Free a string if it is not owned by the "dict" dictionnary in the
+ * Free a string if it is not owned by the "dict" dictionary in the
  * current scope
  */
 #define DICT_FREE(str)                                         \
@@ -1057,7 +1063,7 @@ xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
  * DICT_COPY:
  * @str:  a string
  *
- * Copy a string using a "dict" dictionnary in the current scope,
+ * Copy a string using a "dict" dictionary in the current scope,
  * if availabe.
  */
 #define DICT_COPY(str, cpy) \
@@ -1074,7 +1080,7 @@ xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
  * DICT_CONST_COPY:
  * @str:  a string
  *
- * Copy a string using a "dict" dictionnary in the current scope,
+ * Copy a string using a "dict" dictionary in the current scope,
  * if availabe.
  */
 #define DICT_CONST_COPY(str, cpy) \
@@ -1401,6 +1407,8 @@ xmlStringLenGetNodeList(const xmlDoc *doc, const xmlChar *value, int len) {
                        else if ((ent != NULL) && (ent->children == NULL)) {
                            xmlNodePtr temp;
 
+                            /* Set to non-NULL value to avoid recursion. */
+                           ent->children = (xmlNodePtr) -1;
                            ent->children = xmlStringGetNodeList(doc,
                                    (const xmlChar*)node->content);
                            ent->owner = 1;
@@ -1451,9 +1459,9 @@ xmlStringLenGetNodeList(const xmlDoc *doc, const xmlChar *value, int len) {
        node->content = xmlBufDetach(buf);
 
        if (last == NULL) {
-           last = ret = node;
+           ret = node;
        } else {
-           last = xmlAddNextSibling(last, node);
+           xmlAddNextSibling(last, node);
        }
     } else if (ret == NULL) {
         ret = xmlNewDocText(doc, BAD_CAST "");
@@ -1593,12 +1601,15 @@ xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) {
                        else if ((ent != NULL) && (ent->children == NULL)) {
                            xmlNodePtr temp;
 
+                            /* Set to non-NULL value to avoid recursion. */
+                           ent->children = (xmlNodePtr) -1;
                            ent->children = xmlStringGetNodeList(doc,
                                    (const xmlChar*)node->content);
                            ent->owner = 1;
                            temp = ent->children;
                            while (temp) {
                                temp->parent = (xmlNodePtr)ent;
+                               ent->last = temp;
                                temp = temp->next;
                            }
                        }
@@ -1639,9 +1650,9 @@ xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) {
        node->content = xmlBufDetach(buf);
 
        if (last == NULL) {
-           last = ret = node;
+           ret = node;
        } else {
-           last = xmlAddNextSibling(last, node);
+           xmlAddNextSibling(last, node);
        }
     }
 
@@ -2270,7 +2281,7 @@ xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
     cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
     if (cur == NULL) {
        xmlTreeErrMemory("building node");
-       /* we can't check here that name comes from the doc dictionnary */
+       /* we can't check here that name comes from the doc dictionary */
        return(NULL);
     }
     memset(cur, 0, sizeof(xmlNode));
@@ -2350,7 +2361,7 @@ xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
            UPDATE_LAST_CHILD_AND_PARENT(cur)
        }
     } else {
-        /* if name don't come from the doc dictionnary free it here */
+        /* if name don't come from the doc dictionary free it here */
         if ((name != NULL) && (doc != NULL) &&
            (!(xmlDictOwns(doc->dict, name))))
            xmlFree(name);
@@ -2799,8 +2810,27 @@ xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
        if(tree->type == XML_ELEMENT_NODE) {
            prop = tree->properties;
            while (prop != NULL) {
+                if (prop->atype == XML_ATTRIBUTE_ID) {
+                    xmlRemoveID(tree->doc, prop);
+                }
+
                prop->doc = doc;
                xmlSetListDoc(prop->children, doc);
+
+                /*
+                 * TODO: ID attributes should be also added to the new
+                 * document, but this breaks things like xmlReplaceNode.
+                 * The underlying problem is that xmlRemoveID is only called
+                 * if a node is destroyed, not if it's unlinked.
+                 */
+#if 0
+                if (xmlIsID(doc, tree, prop)) {
+                    xmlChar *idVal = xmlNodeListGetString(doc, prop->children,
+                                                          1);
+                    xmlAddID(NULL, doc, idVal, prop);
+                }
+#endif
+
                prop = prop->next;
            }
        }
@@ -3682,7 +3712,7 @@ xmlFreeNodeList(xmlNodePtr cur) {
             * When a node is a text node or a comment, it uses a global static
             * variable for the name of the node.
             * Otherwise the node name might come from the document's
-            * dictionnary
+            * dictionary
             */
            if ((cur->name != NULL) &&
                (cur->type != XML_TEXT_NODE) &&
@@ -3751,7 +3781,7 @@ xmlFreeNode(xmlNodePtr cur) {
     /*
      * When a node is a text node or a comment, it uses a global static
      * variable for the name of the node.
-     * Otherwise the node name might come from the document's dictionnary
+     * Otherwise the node name might come from the document's dictionary
      */
     if ((cur->name != NULL) &&
         (cur->type != XML_TEXT_NODE) &&
@@ -4576,7 +4606,7 @@ xmlGetLineNoInternal(const xmlNode *node, int depth)
        (node->type == XML_PI_NODE)) {
        if (node->line == 65535) {
            if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL))
-               result = (long) node->psvi;
+               result = (long) (ptrdiff_t) node->psvi;
            else if ((node->type == XML_ELEMENT_NODE) &&
                     (node->children != NULL))
                result = xmlGetLineNoInternal(node->children, depth + 1);
@@ -4735,8 +4765,8 @@ xmlGetNodePath(const xmlNode *node)
             if (occur == 0) {
                 tmp = cur->next;
                 while (tmp != NULL && occur == 0) {
-                 if (tmp->type == XML_COMMENT_NODE)
-                   occur++;
+                   if (tmp->type == XML_COMMENT_NODE)
+                       occur++;
                     tmp = tmp->next;
                 }
                 if (occur != 0)
@@ -8229,7 +8259,7 @@ xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
                        ns = ns->next;
                    } while (ns != NULL);
                }
-               /* No break on purpose. */
+                /* Falls through. */
            case XML_ATTRIBUTE_NODE:
                if (node->ns != NULL) {
                    /*
@@ -8820,7 +8850,7 @@ next_ns_decl:
                }
                if (! adoptns)
                    goto ns_end;
-               /* No break on purpose. */
+                /* Falls through. */
            case XML_ATTRIBUTE_NODE:
                /* No ns, no fun. */
                if (cur->ns == NULL)
@@ -9101,7 +9131,7 @@ xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
                            goto internal_error;
                    }
                }
-               /* No break on purpose. */
+                /* Falls through. */
            case XML_ATTRIBUTE_NODE:
                /* No namespace, no fun. */
                if (cur->ns == NULL)