From d5da27bcbc0b3495705ecd3f492425766f53f7ea Mon Sep 17 00:00:00 2001 From: rfm Date: Thu, 19 Dec 2024 11:37:59 +0000 Subject: [PATCH] Fix error unlinking old namespace --- Source/NSXMLNode.m | 73 +++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/Source/NSXMLNode.m b/Source/NSXMLNode.m index 1a1759348..5ebf4718b 100644 --- a/Source/NSXMLNode.m +++ b/Source/NSXMLNode.m @@ -39,8 +39,8 @@ if ((node == NULL) || (ns == NULL)) return; - if ((node->type == XML_ATTRIBUTE_NODE) || - (node->type == XML_ELEMENT_NODE)) + if ((node->type == XML_ATTRIBUTE_NODE) + || (node->type == XML_ELEMENT_NODE)) { xmlNsPtr ns1 = node->ns; @@ -50,19 +50,23 @@ } // Either both the same or one NULL and the other the same - if (ns1 != NULL && - (((ns1->href == NULL) && - (xmlStrcmp(ns1->prefix, ns->prefix) == 0)) || - /* - ((ns1->prefix == NULL) && - (xmlStrcmp(ns1->href, ns->href) == 0)) || - */ - ((xmlStrcmp(ns1->prefix, ns->prefix) == 0) && - (xmlStrcmp(ns1->href, ns->href) == 0)))) - { - //xmlFreeNs(ns1); - xmlSetNs(node, ns); - } + if (ns1 != NULL) + { + BOOL equalPrefix; + + equalPrefix = (xmlStrcmp(ns1->prefix, ns->prefix) == 0) ? YES : NO; + + if (equalPrefix + && ((ns1->href == NULL) + || (xmlStrcmp(ns1->href, ns->href) == 0))) + { + xmlSetNs(node, ns); + if (ns1->href != NULL) + { + //xmlFreeNs(ns1); + } + } + } cleanup_namespaces(node->children, ns); cleanup_namespaces(node->next, ns); @@ -73,9 +77,11 @@ } } -void +BOOL ensure_oldNs(xmlNodePtr node) { + BOOL newDoc = NO; + if (node->doc == NULL) { // Create a private document for this node @@ -86,6 +92,7 @@ #else xmlSetTreeDoc(node, tmp); #endif + newDoc = YES; } if (node->doc->oldNs == NULL) { @@ -96,6 +103,7 @@ ns->prefix = xmlStrdup((const xmlChar *)"xml"); node->doc->oldNs = ns; } + return newDoc; } static int @@ -551,11 +559,15 @@ - (void) _insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index if (tmp) { // Try to resolve half defined namespaces - xmlNsPtr ns = tmp->oldNs; + xmlNsPtr ns; xmlNsPtr last = NULL; - ensure_oldNs(parentNode); + if (ensure_oldNs(parentNode)) + { + detached = parentNode->doc; + } + ns = tmp->oldNs; while (ns != NULL) { BOOL resolved = NO; @@ -594,7 +606,7 @@ - (void) _insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index // Unlink in old if (last == NULL) { - tmp->oldNs = NULL; + tmp->oldNs = tmp->oldNs->next; } else { @@ -1801,9 +1813,9 @@ - (void) setName: (NSString *)name } else { - const xmlChar *xmlName = XMLSTRING(name); - xmlChar *prefix = NULL; - xmlChar *localName; + const xmlChar *xmlName = XMLSTRING(name); + xmlChar *prefix = NULL; + xmlChar *localName; if (NULL == xmlName) { @@ -1814,8 +1826,8 @@ - (void) setName: (NSString *)name localName = xmlSplitQName2(xmlName, &prefix); if (prefix != NULL) { - if ((theNode->type == XML_ATTRIBUTE_NODE) || - (theNode->type == XML_ELEMENT_NODE)) + if ((theNode->type == XML_ATTRIBUTE_NODE) + || (theNode->type == XML_ELEMENT_NODE)) { if ((theNode->ns != NULL && theNode->ns->prefix == NULL)) { @@ -1835,7 +1847,10 @@ - (void) setName: (NSString *)name { xmlNsPtr oldNs; - ensure_oldNs(theNode); + if (ensure_oldNs(theNode)) + { + detached = theNode->doc; + } // Fake the name space and fix it later // This function is private, so re reimplemt it. @@ -1843,7 +1858,8 @@ - (void) setName: (NSString *)name oldNs = theNode->doc->oldNs; while (oldNs) { - if (oldNs->prefix != NULL && xmlStrEqual(oldNs->prefix, prefix)) + if (oldNs->prefix != NULL + && xmlStrEqual(oldNs->prefix, prefix)) { ns = oldNs; break; @@ -1964,7 +1980,10 @@ - (void) setURI: (NSString*)URI { xmlNsPtr oldNs; - ensure_oldNs(theNode); + if (ensure_oldNs(theNode)) + { + detached = theNode->doc; + } // Fake the name space and fix it later // This function is private, so re reimplemt it.