[libxml2] Avoid calling xmlSetTreeDoc



commit 3e7b4f37aaaf920e6b81e20a3cd33c20420bfa15
Author: Nick Wellnhofer <wellnhofer aevum de>
Date:   Fri May 20 23:28:25 2022 +0200

    Avoid calling xmlSetTreeDoc
    
    Create text nodes with xmlNewDocText or set the document directly to
    avoid xmlSetTreeDoc being called when the node is inserted.

 SAX2.c               | 5 +++--
 doc/examples/io2.c   | 2 +-
 doc/examples/tree2.c | 6 +++---
 parser.c             | 5 +++--
 relaxng.c            | 2 +-
 tree.c               | 2 +-
 xinclude.c           | 4 ++--
 xmlsave.c            | 2 +-
 xmlschemas.c         | 8 +++++---
 9 files changed, 20 insertions(+), 16 deletions(-)
---
diff --git a/SAX2.c b/SAX2.c
index 6180f00b..ec89046b 100644
--- a/SAX2.c
+++ b/SAX2.c
@@ -2633,9 +2633,10 @@ xmlSAX2Text(xmlParserCtxtPtr ctxt, const xmlChar *ch, int len,
            }
        } else {
            /* Mixed content, first time */
-            if (type == XML_TEXT_NODE)
+            if (type == XML_TEXT_NODE) {
                 lastChild = xmlSAX2TextNode(ctxt, ch, len);
-            else
+                lastChild->doc = ctxt->myDoc;
+            } else
                 lastChild = xmlNewCDataBlock(ctxt->myDoc, ch, len);
            if (lastChild != NULL) {
                xmlAddChild(ctxt->node, lastChild);
diff --git a/doc/examples/io2.c b/doc/examples/io2.c
index 9607f742..98e1a503 100644
--- a/doc/examples/io2.c
+++ b/doc/examples/io2.c
@@ -25,7 +25,7 @@ main(void)
      * Create the document.
      */
     doc = xmlNewDoc(BAD_CAST "1.0");
-    n = xmlNewNode(NULL, BAD_CAST "root");
+    n = xmlNewDocNode(doc, NULL, BAD_CAST "root", NULL);
     xmlNodeSetContent(n, BAD_CAST "content");
     xmlDocSetRootElement(doc, n);
 
diff --git a/doc/examples/tree2.c b/doc/examples/tree2.c
index cb608535..83f29a0b 100644
--- a/doc/examples/tree2.c
+++ b/doc/examples/tree2.c
@@ -36,7 +36,7 @@ main(int argc, char **argv)
      * Creates a new document, a node and set it as a root node
      */
     doc = xmlNewDoc(BAD_CAST "1.0");
-    root_node = xmlNewNode(NULL, BAD_CAST "root");
+    root_node = xmlNewDocNode(doc, NULL, BAD_CAST "root", NULL);
     xmlDocSetRootElement(doc, root_node);
 
     /*
@@ -70,8 +70,8 @@ main(int argc, char **argv)
      * creates a node and a text node separately. They are "attached"
      * by xmlAddChild() 
      */
-    node = xmlNewNode(NULL, BAD_CAST "node4");
-    node1 = xmlNewText(BAD_CAST
+    node = xmlNewDocNode(doc, NULL, BAD_CAST "node4", NULL);
+    node1 = xmlNewDocText(doc, BAD_CAST
                    "other way to create content (which is also a node)");
     xmlAddChild(node, node1);
     xmlAddChild(root_node, node);
diff --git a/parser.c b/parser.c
index 1bea54eb..280a815d 100644
--- a/parser.c
+++ b/parser.c
@@ -7234,7 +7234,8 @@ xmlParseReference(xmlParserCtxtPtr ctxt) {
                     ent->owner = 1;
                     while (list != NULL) {
                         list->parent = (xmlNodePtr) ent;
-                        xmlSetTreeDoc(list, ent->doc);
+                        if (list->doc != ent->doc)
+                            xmlSetTreeDoc(list, ent->doc);
                         if (list->next == NULL)
                             ent->last = list;
                         list = list->next;
@@ -13583,7 +13584,7 @@ xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen,
     ctxt->input_id = 2;
     ctxt->instate = XML_PARSER_CONTENT;
 
-    fake = xmlNewComment(NULL);
+    fake = xmlNewDocComment(node->doc, NULL);
     if (fake == NULL) {
         xmlFreeParserCtxt(ctxt);
        return(XML_ERR_NO_MEMORY);
diff --git a/relaxng.c b/relaxng.c
index 80956b5c..7f0f2e74 100644
--- a/relaxng.c
+++ b/relaxng.c
@@ -7233,7 +7233,7 @@ xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
                                                 BAD_CAST "name", NULL);
                             if (node != NULL) {
                                 xmlAddPrevSibling(cur->children, node);
-                                text = xmlNewText(name);
+                                text = xmlNewDocText(node->doc, name);
                                 xmlAddChild(node, text);
                                 text = node;
                             }
diff --git a/tree.c b/tree.c
index 30f031d5..da34685b 100644
--- a/tree.c
+++ b/tree.c
@@ -5878,7 +5878,7 @@ xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
            xmlNodePtr last, newNode, tmp;
 
            last = cur->last;
-           newNode = xmlNewTextLen(content, len);
+           newNode = xmlNewDocTextLen(cur->doc, content, len);
            if (newNode != NULL) {
                tmp = xmlAddChild(cur, newNode);
                if (tmp != newNode)
diff --git a/xinclude.c b/xinclude.c
index a3aff3e6..0c6b3f29 100644
--- a/xinclude.c
+++ b/xinclude.c
@@ -1849,7 +1849,7 @@ xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
      */
     for (i = 0; i < ctxt->txtNr; i++) {
        if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
-            node = xmlNewText(ctxt->txtTab[i]);
+            node = xmlNewDocText(ctxt->doc, ctxt->txtTab[i]);
            goto loaded;
        }
     }
@@ -1898,7 +1898,7 @@ xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
     if (buf->encoder)
        xmlCharEncCloseFunc(buf->encoder);
     buf->encoder = xmlGetCharEncodingHandler(enc);
-    node = xmlNewText(NULL);
+    node = xmlNewDocText(ctxt->doc, NULL);
 
     /*
      * Scan all chars from the resource and add the to the node
diff --git a/xmlsave.c b/xmlsave.c
index 7b557494..9b42c66c 100644
--- a/xmlsave.c
+++ b/xmlsave.c
@@ -1379,7 +1379,7 @@ xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
                 (htmlIsBooleanAttr(cur->name))) {
            if (cur->children != NULL)
                xmlFreeNode(cur->children);
-           cur->children = xmlNewText(cur->name);
+           cur->children = xmlNewDocText(cur->doc, cur->name);
            if (cur->children != NULL)
                cur->children->parent = (xmlNodePtr) cur;
        }
diff --git a/xmlschemas.c b/xmlschemas.c
index 7b44bf60..c1c8f4ef 100644
--- a/xmlschemas.c
+++ b/xmlschemas.c
@@ -26467,13 +26467,15 @@ default_psvi:
            normValue = xmlSchemaNormalizeValue(inode->typeDef,
                inode->decl->value);
            if (normValue != NULL) {
-               textChild = xmlNewText(BAD_CAST normValue);
+               textChild = xmlNewDocText(inode->node->doc,
+                        BAD_CAST normValue);
                xmlFree(normValue);
            } else
-               textChild = xmlNewText(inode->decl->value);
+               textChild = xmlNewDocText(inode->node->doc,
+                        inode->decl->value);
            if (textChild == NULL) {
                VERROR_INT("xmlSchemaValidatorPopElem",
-                   "calling xmlNewText()");
+                   "calling xmlNewDocText()");
                goto internal_error;
            } else
                xmlAddChild(inode->node, textChild);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]