diff --git a/SAX2.c b/SAX2.c index 287d3092..d2e00da0 100644 --- a/SAX2.c +++ b/SAX2.c @@ -812,10 +812,6 @@ xmlSAX2StartDocument(void *ctx) doc->dict = ctxt->dict; xmlDictReference(doc->dict); } - if (xmlTreeEnsureXMLDecl(doc) == NULL) { - xmlSAX2ErrMemory(ctxt); - return; - } } if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) && (ctxt->input != NULL) && (ctxt->input->filename != NULL)) { diff --git a/debugXML.c b/debugXML.c index e9bfd0a3..f3448686 100644 --- a/debugXML.c +++ b/debugXML.c @@ -101,6 +101,10 @@ xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns) (node->type != XML_XINCLUDE_START)) return(-2); + /* xml namespace */ + if (xmlStrEqual(ns->prefix, BAD_CAST "xml")) + return(1); + while ((node != NULL) && ((node->type == XML_ELEMENT_NODE) || (node->type == XML_ATTRIBUTE_NODE) || @@ -119,14 +123,7 @@ xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns) } node = node->parent; } - /* the xml namespace may be declared on the document node */ - if ((node != NULL) && - ((node->type == XML_DOCUMENT_NODE) || - (node->type == XML_HTML_DOCUMENT_NODE))) { - xmlNsPtr oldNs = ((xmlDocPtr) node)->oldNs; - if (oldNs == ns) - return(1); - } + return(-3); } diff --git a/include/private/tree.h b/include/private/tree.h index a88cb0eb..fb103b52 100644 --- a/include/private/tree.h +++ b/include/private/tree.h @@ -9,8 +9,9 @@ XML_HIDDEN extern int __xmlRegisterCallbacks; -XML_HIDDEN xmlNsPtr -xmlTreeEnsureXMLDecl(xmlDocPtr doc); +XML_HIDDEN extern xmlNsPtr +xmlXmlNamespace; + XML_HIDDEN xmlNodePtr xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, int extended); diff --git a/parser.c b/parser.c index b996c397..95f3ed1c 100644 --- a/parser.c +++ b/parser.c @@ -2980,13 +2980,6 @@ xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefixOut) { if (cur == NULL) return(NULL); -#ifndef XML_XML_NAMESPACE - /* xml: prefix is not really a namespace */ - if ((cur[0] == 'x') && (cur[1] == 'm') && - (cur[2] == 'l') && (cur[3] == ':')) - return(xmlStrdup(name)); -#endif - /* nasty but well=formed */ if (cur[0] == ':') return(xmlStrdup(name)); diff --git a/testparser.c b/testparser.c index cabc5f44..c48b0d50 100644 --- a/testparser.c +++ b/testparser.c @@ -68,14 +68,14 @@ testBalancedChunk(void) { int err = 0; ret = xmlParseBalancedChunkMemory(NULL, NULL, NULL, 0, - BAD_CAST "start abc end", &list); + BAD_CAST "start abc end", &list); if ((ret != XML_ERR_OK) || (list == NULL) || ((elem = list->next) == NULL) || (elem->type != XML_ELEMENT_NODE) || - (elem->nsDef == NULL) || - (!xmlStrEqual(elem->nsDef->href, XML_XML_NAMESPACE))) { + (elem->ns == NULL) || + (!xmlStrEqual(elem->ns->href, XML_XML_NAMESPACE))) { fprintf(stderr, "xmlParseBalancedChunkMemory failed\n"); err = 1; } diff --git a/tree.c b/tree.c index 00510a21..0011bd9c 100644 --- a/tree.c +++ b/tree.c @@ -75,6 +75,16 @@ const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; static int xmlCompressMode = 0; +static const xmlNs xmlXmlNamespaceStruct = { + NULL, + XML_NAMESPACE_DECL, + XML_XML_NAMESPACE, + BAD_CAST "xml", + NULL, + NULL +}; +xmlNsPtr xmlXmlNamespace = (xmlNsPtr) &xmlXmlNamespaceStruct; + #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ xmlNodePtr ulccur = (n)->children; \ if (ulccur == NULL) { \ @@ -218,13 +228,6 @@ xmlSplitQName2(const xmlChar *name, xmlChar **prefix) { *prefix = NULL; if (name == NULL) return(NULL); -#ifndef XML_XML_NAMESPACE - /* xml: prefix is not really a namespace */ - if ((name[0] == 'x') && (name[1] == 'm') && - (name[2] == 'l') && (name[3] == ':')) - return(NULL); -#endif - /* nasty but valid */ if (name[0] == ':') return(NULL); @@ -3937,14 +3940,14 @@ xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { if ((cur->ns != NULL) && (target != NULL)) { xmlNsPtr ns; - ns = xmlSearchNs(target->doc, target, cur->ns->prefix); + ns = xmlSearchNs(NULL, target, cur->ns->prefix); if (ns == NULL) { /* * Humm, we are copying an element whose namespace is defined * out of the new tree scope. Search it in the original tree * and add it at the top of the new tree */ - ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix); + ns = xmlSearchNs(NULL, cur->parent, cur->ns->prefix); if (ns != NULL) { xmlNodePtr root = target; xmlNodePtr pred = NULL; @@ -3975,7 +3978,7 @@ xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { * we are in trouble: we need a new reconciled namespace. * This is expensive */ - ret->ns = xmlNewReconciledNs(target->doc, target, cur->ns); + ret->ns = xmlNewReconciledNs(NULL, target, cur->ns); if (ret->ns == NULL) goto error; } @@ -4208,7 +4211,7 @@ xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, if ((node->type == XML_ELEMENT_NODE) && (node->ns != NULL)) { xmlNsPtr ns; - ns = xmlSearchNs(doc, ret, node->ns->prefix); + ns = xmlSearchNs(NULL, ret, node->ns->prefix); if (ns == NULL) { /* * Humm, we are copying an element whose namespace is defined @@ -4218,14 +4221,14 @@ xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, * TODO: Searching the original tree seems unnecessary. We * already have a namespace URI. */ - ns = xmlSearchNs(node->doc, node, node->ns->prefix); + ns = xmlSearchNs(NULL, node, node->ns->prefix); if (ns != NULL) { xmlNodePtr root = ret; while (root->parent != NULL) root = root->parent; ret->ns = xmlNewNs(root, ns->href, ns->prefix); } else { - ret->ns = xmlNewReconciledNs(doc, ret, node->ns); + ret->ns = xmlNewReconciledNs(NULL, ret, node->ns); } if (ret->ns == NULL) goto error; @@ -5070,9 +5073,7 @@ xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { case XML_ATTRIBUTE_NODE: break; } - ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); - if (ns == NULL) - return; + ns = xmlXmlNamespace; xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); } #endif /* LIBXML_TREE_ENABLED */ @@ -5141,9 +5142,7 @@ xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { case XML_ATTRIBUTE_NODE: break; } - ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); - if (ns == NULL) - return; + ns = xmlXmlNamespace; switch (val) { case 0: xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default"); @@ -5294,9 +5293,7 @@ xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) { } } - ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE); - if (ns == NULL) - return(-1); + ns = xmlXmlNamespace; fixed = xmlPathToURI(uri); if (fixed == NULL) return(-1); @@ -6056,43 +6053,6 @@ xmlGetNsList(const xmlDoc *doc, const xmlNode *node) } #endif /* LIBXML_TREE_ENABLED */ -/* -* xmlTreeEnsureXMLDecl: -* @doc: the doc -* -* Ensures that there is an XML namespace declaration on the doc. -* -* Returns the XML ns-struct or NULL if a memory allocation failed. -*/ -xmlNsPtr -xmlTreeEnsureXMLDecl(xmlDocPtr doc) -{ - if (doc == NULL) - return (NULL); - if (doc->oldNs != NULL) - return (doc->oldNs); - { - xmlNsPtr ns; - ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); - if (ns == NULL) - return(NULL); - memset(ns, 0, sizeof(xmlNs)); - ns->type = XML_LOCAL_NAMESPACE; - ns->href = xmlStrdup(XML_XML_NAMESPACE); - if (ns->href == NULL) { - xmlFreeNs(ns); - return(NULL); - } - ns->prefix = xmlStrdup((const xmlChar *)"xml"); - if (ns->prefix == NULL) { - xmlFreeNs(ns); - return(NULL); - } - doc->oldNs = ns; - return (ns); - } -} - /** * xmlSearchNs: * @doc: the document @@ -6107,50 +6067,20 @@ xmlTreeEnsureXMLDecl(xmlDocPtr doc) * the namespace within those you will be in troubles !!! A warning * is generated to cover this case. * - * Returns the namespace pointer or NULL if no namespace was found or - * a memory allocation failed. Allocations can only fail if the "xml" - * namespace is queried and xmlTreeEnsureXMLDecl wasn't called - * successfully or doc is NULL. + * Returns the namespace pointer or NULL if no namespace was found. */ xmlNsPtr -xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { - +xmlSearchNs(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, + const xmlChar *nameSpace) { xmlNsPtr cur; const xmlNode *orig = node; - if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL); - if ((nameSpace != NULL) && - (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { - if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { - /* - * The XML-1.0 namespace is normally held on the root - * element. In this case exceptionally create it on the - * node element. - */ - cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); - if (cur == NULL) - return(NULL); - memset(cur, 0, sizeof(xmlNs)); - cur->type = XML_LOCAL_NAMESPACE; - cur->href = xmlStrdup(XML_XML_NAMESPACE); - cur->prefix = xmlStrdup((const xmlChar *)"xml"); - cur->next = node->nsDef; - node->nsDef = cur; - return(cur); - } - if (doc == NULL) { - doc = node->doc; - if (doc == NULL) - return(NULL); - } - /* - * Return the XML namespace declaration held by the doc. - */ - if (doc->oldNs == NULL) - return(xmlTreeEnsureXMLDecl(doc)); - else - return(doc->oldNs); - } + if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) + return(NULL); + + if (xmlStrEqual(nameSpace, BAD_CAST "xml")) + return(xmlXmlNamespace); + while (node != NULL) { if ((node->type == XML_ENTITY_REF_NODE) || (node->type == XML_ENTITY_NODE) || @@ -6238,54 +6168,21 @@ xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, * Search a Ns aliasing a given URI. Recurse on the parents until it finds * the defined namespace or return NULL otherwise. * - * Returns the namespace pointer or NULL if no namespace was found or - * a memory allocation failed. Allocations can only fail if the "xml" - * namespace is queried and xmlTreeEnsureXMLDecl wasn't called - * successfully or doc is NULL. + * Returns the namespace pointer or NULL if no namespace was found. */ xmlNsPtr -xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) -{ +xmlSearchNsByHref(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, + const xmlChar * href) { xmlNsPtr cur; xmlNodePtr orig = node; int is_attr; if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL)) return (NULL); - if (xmlStrEqual(href, XML_XML_NAMESPACE)) { - /* - * Only the document can hold the XML spec namespace. - */ - if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { - /* - * The XML-1.0 namespace is normally held on the root - * element. In this case exceptionally create it on the - * node element. - */ - cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); - if (cur == NULL) - return (NULL); - memset(cur, 0, sizeof(xmlNs)); - cur->type = XML_LOCAL_NAMESPACE; - cur->href = xmlStrdup(XML_XML_NAMESPACE); - cur->prefix = xmlStrdup((const xmlChar *) "xml"); - cur->next = node->nsDef; - node->nsDef = cur; - return (cur); - } - if (doc == NULL) { - doc = node->doc; - if (doc == NULL) - return(NULL); - } - /* - * Return the XML namespace declaration held by the doc. - */ - if (doc->oldNs == NULL) - return(xmlTreeEnsureXMLDecl(doc)); - else - return(doc->oldNs); - } + + if (xmlStrEqual(href, XML_XML_NAMESPACE)) + return(xmlXmlNamespace); + is_attr = (node->type == XML_ATTRIBUTE_NODE); while (node != NULL) { if ((node->type == XML_ENTITY_REF_NODE) || @@ -6298,7 +6195,7 @@ xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) if ((cur->href != NULL) && (href != NULL) && (xmlStrEqual(cur->href, href))) { if (((!is_attr) || (cur->prefix != NULL)) && - (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) + (xmlNsInScope(NULL, orig, node, cur->prefix) == 1)) return (cur); } cur = cur->next; @@ -6309,7 +6206,7 @@ xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) if ((cur->href != NULL) && (href != NULL) && (xmlStrEqual(cur->href, href))) { if (((!is_attr) || (cur->prefix != NULL)) && - (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) + (xmlNsInScope(NULL, orig, node, cur->prefix) == 1)) return (cur); } } @@ -6334,7 +6231,8 @@ xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href) * Returns the (new) namespace definition or NULL in case of error */ static xmlNsPtr -xmlNewReconciledNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { +xmlNewReconciledNs(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr tree, + xmlNsPtr ns) { xmlNsPtr def; xmlChar prefix[50]; int counter = 1; @@ -6348,7 +6246,7 @@ xmlNewReconciledNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { /* * Search an existing namespace definition inherited. */ - def = xmlSearchNsByHref(doc, tree, ns->href); + def = xmlSearchNsByHref(NULL, tree, ns->href); if (def != NULL) return(def); @@ -6361,7 +6259,7 @@ xmlNewReconciledNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { else snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix); - def = xmlSearchNs(doc, tree, prefix); + def = xmlSearchNs(NULL, tree, prefix); while (def != NULL) { if (counter > 1000) return(NULL); if (ns->prefix == NULL) @@ -6369,7 +6267,7 @@ xmlNewReconciledNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { else snprintf((char *) prefix, sizeof(prefix), "%.20s%d", (char *)ns->prefix, counter++); - def = xmlSearchNs(doc, tree, prefix); + def = xmlSearchNs(NULL, tree, prefix); } /* @@ -6402,7 +6300,7 @@ typedef struct { * Returns 0 on success or -1 in case of error. */ int -xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { +xmlReconciliateNs(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr tree) { xmlNsCache *cache = NULL; int sizeCache = 0; int nbCache = 0; @@ -6412,8 +6310,9 @@ xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { xmlAttrPtr attr; int ret = 0, i; - if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1); - if (node->doc != doc) return(-1); + if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) + return(-1); + while (node != NULL) { /* * Reconciliate the node namespace @@ -6429,7 +6328,7 @@ xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { /* * OK we need to recreate a new namespace definition */ - n = xmlNewReconciledNs(doc, tree, node->ns); + n = xmlNewReconciledNs(NULL, tree, node->ns); if (n == NULL) { ret = -1; } else { @@ -6473,7 +6372,7 @@ xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { /* * OK we need to recreate a new namespace definition */ - n = xmlNewReconciledNs(doc, tree, attr->ns); + n = xmlNewReconciledNs(NULL, tree, attr->ns); if (n == NULL) { ret = -1; } else { @@ -6964,7 +6863,7 @@ xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { if (nqname != NULL) { xmlNsPtr ns; xmlChar *prefix = xmlStrndup(name, len); - ns = xmlSearchNs(node->doc, node, prefix); + ns = xmlSearchNs(NULL, node, prefix); if (prefix != NULL) xmlFree(prefix); if (ns != NULL) @@ -7989,28 +7888,29 @@ xmlDOMWrapStoreNs(xmlDocPtr doc, if (doc == NULL) return (NULL); - ns = xmlTreeEnsureXMLDecl(doc); - if (ns == NULL) - return (NULL); - if (ns->next != NULL) { - /* Reuse. */ - ns = ns->next; - while (ns != NULL) { - if (((ns->prefix == prefix) || - xmlStrEqual(ns->prefix, prefix)) && - xmlStrEqual(ns->href, nsName)) { - return (ns); - } - if (ns->next == NULL) - break; - ns = ns->next; - } + + /* Reuse. */ + ns = doc->oldNs; + while (ns != NULL) { + if (((ns->prefix == prefix) || + xmlStrEqual(ns->prefix, prefix)) && + xmlStrEqual(ns->href, nsName)) { + return(ns); + } + if (ns->next == NULL) + break; + ns = ns->next; } + /* Create. */ - if (ns != NULL) { + if (ns == NULL) { + doc->oldNs = xmlNewNs(NULL, nsName, prefix); + return(doc->oldNs); + } else { ns->next = xmlNewNs(NULL, nsName, prefix); - return (ns->next); + return(ns->next); } + return(NULL); } @@ -8372,9 +8272,7 @@ xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node, *retNs = NULL; if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { - *retNs = xmlTreeEnsureXMLDecl(doc); - if (*retNs == NULL) - return (-1); + *retNs = xmlXmlNamespace; return (1); } cur = node; @@ -8418,7 +8316,7 @@ xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node, if (out) { int ret; - ret = xmlNsInScope(doc, node, prev, ns->prefix); + ret = xmlNsInScope(NULL, node, prev, ns->prefix); if (ret < 0) return (-1); /* @@ -8474,9 +8372,7 @@ xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node, *retNs = NULL; if (IS_STR_XML(prefix)) { if (retNs) { - *retNs = xmlTreeEnsureXMLDecl(doc); - if (*retNs == NULL) - return (-1); + *retNs = xmlXmlNamespace; } return (1); } @@ -8628,9 +8524,7 @@ xmlDOMWrapNSNormAcquireNormalizedNs(xmlDocPtr doc, /* * Insert XML namespace mapping. */ - *retNs = xmlTreeEnsureXMLDecl(doc); - if (*retNs == NULL) - return (-1); + *retNs = xmlXmlNamespace; return (0); } /* @@ -9921,7 +9815,7 @@ xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt, } /* XML Namespace. */ if (IS_STR_XML(attr->ns->prefix)) { - ns = xmlTreeEnsureXMLDecl(destDoc); + ns = xmlXmlNamespace; } else if (destParent == NULL) { /* * Store in @destDoc->oldNs. diff --git a/xpath.c b/xpath.c index 3bff0d59..818ff7d4 100644 --- a/xpath.c +++ b/xpath.c @@ -49,6 +49,7 @@ #include "private/buf.h" #include "private/error.h" +#include "private/tree.h" #include "private/xpath.h" /* Disabled for now */ @@ -224,15 +225,6 @@ xmlXPathIsInf(double val) { * the test should just be name[0] = ' ' */ -static xmlNs xmlXPathXMLNamespaceStruct = { - NULL, - XML_NAMESPACE_DECL, - XML_XML_NAMESPACE, - BAD_CAST "xml", - NULL, - NULL -}; -static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct; #ifndef LIBXML_THREAD_ENABLED /* * Optimizer is disabled only when threaded apps are detected while @@ -4260,10 +4252,8 @@ xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) { if (prefix == NULL) return(NULL); -#ifdef XML_XML_NAMESPACE if (xmlStrEqual(prefix, (const xmlChar *) "xml")) return(XML_XML_NAMESPACE); -#endif if (ctxt->namespaces != NULL) { int i; @@ -7344,7 +7334,7 @@ xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { ctxt->context->tmpNsNr++; } } - return((xmlNodePtr) xmlXPathXMLNamespace); + return((xmlNodePtr) xmlXmlNamespace); } if (ctxt->context->tmpNsNr > 0) { return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr];