Skip to content

Commit

Permalink
Fix error unlinking old namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Dec 19, 2024
1 parent 0d01000 commit d5da27b
Showing 1 changed file with 46 additions and 27 deletions.
73 changes: 46 additions & 27 deletions Source/NSXMLNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -73,9 +77,11 @@
}
}

void
BOOL
ensure_oldNs(xmlNodePtr node)
{
BOOL newDoc = NO;

if (node->doc == NULL)
{
// Create a private document for this node
Expand All @@ -86,6 +92,7 @@
#else
xmlSetTreeDoc(node, tmp);
#endif
newDoc = YES;
}
if (node->doc->oldNs == NULL)
{
Expand All @@ -96,6 +103,7 @@
ns->prefix = xmlStrdup((const xmlChar *)"xml");
node->doc->oldNs = ns;
}
return newDoc;
}

static int
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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))
{
Expand All @@ -1835,15 +1847,19 @@ - (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.
//ns = xmlDOMWrapStoreNs(node->doc, NULL, prefix);
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;
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d5da27b

Please sign in to comment.