From 26c5eb08c5ffc52b8926d35916c52027b47fc06a Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Mon, 30 May 2016 15:00:48 -0400 Subject: [PATCH] fix over-eager namespace pruning when reparenting this fix is @ccutrer's from #1333 fixes #1332 --- ext/nokogiri/xml_node.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/ext/nokogiri/xml_node.c b/ext/nokogiri/xml_node.c index 623fa78e491..caaa49e3ffb 100644 --- a/ext/nokogiri/xml_node.c +++ b/ext/nokogiri/xml_node.c @@ -35,13 +35,13 @@ static void relink_namespace(xmlNodePtr reparented) xmlNsPtr ns; if (reparented->type != XML_ATTRIBUTE_NODE && - reparented->type != XML_ELEMENT_NODE) return; + reparented->type != XML_ELEMENT_NODE) { return; } if (reparented->ns == NULL || reparented->ns->prefix == NULL) { name = xmlSplitQName2(reparented->name, &prefix); if(reparented->type == XML_ATTRIBUTE_NODE) { - if (prefix == NULL || strcmp((char*)prefix, XMLNS_PREFIX) == 0) return; + if (prefix == NULL || strcmp((char*)prefix, XMLNS_PREFIX) == 0) { return; } } ns = xmlSearchNs(reparented->doc, reparented, prefix); @@ -57,18 +57,19 @@ static void relink_namespace(xmlNodePtr reparented) } /* Avoid segv when relinking against unlinked nodes. */ - if (reparented->type != XML_ELEMENT_NODE || !reparented->parent) return; + if (reparented->type != XML_ELEMENT_NODE || !reparented->parent) { return; } /* Make sure that our reparented node has the correct namespaces */ - if(!reparented->ns && reparented->doc != (xmlDocPtr)reparented->parent) + if (!reparented->ns && reparented->doc != (xmlDocPtr)reparented->parent) { xmlSetNs(reparented, reparented->parent->ns); + } /* Search our parents for an existing definition */ - if(reparented->nsDef) { + if (reparented->nsDef) { xmlNsPtr curr = reparented->nsDef; xmlNsPtr prev = NULL; - while(curr) { + while (curr) { xmlNsPtr ns = xmlSearchNsByHref( reparented->doc, reparented->parent, @@ -76,7 +77,7 @@ static void relink_namespace(xmlNodePtr reparented) ); /* If we find the namespace is already declared, remove it from this * definition list. */ - if(ns && ns != curr) { + if (ns && ns != curr && xmlStrEqual(ns->prefix, curr->prefix)) { if (prev) { prev->next = curr->next; } else { @@ -92,12 +93,12 @@ static void relink_namespace(xmlNodePtr reparented) /* Only walk all children if there actually is a namespace we need to */ /* reparent. */ - if(NULL == reparented->ns) return; + if (NULL == reparented->ns) { return; } /* When a node gets reparented, walk it's children to make sure that */ /* their namespaces are reparented as well. */ child = reparented->children; - while(NULL != child) { + while (NULL != child) { relink_namespace(child); child = child->next; }