summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tree.c27
1 files changed, 26 insertions, 1 deletions
diff --git a/tree.c b/tree.c
index 689a6b66..99eef30e 100644
--- a/tree.c
+++ b/tree.c
@@ -2812,6 +2812,20 @@ xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
return(cur);
}
+static const xmlChar *_copyStringForNewDictIfNeeded(xmlDictPtr oldDict, xmlDictPtr newDict, const xmlChar *oldValue) {
+ const xmlChar *newValue = oldValue;
+ if (oldValue) {
+ int oldDictOwnsOldValue = oldDict && (xmlDictOwns(oldDict, oldValue) == 1);
+ if (oldDictOwnsOldValue) {
+ if (newDict)
+ newValue = xmlDictLookup(newDict, oldValue, -1);
+ else
+ newValue = xmlStrdup(oldValue);
+ }
+ }
+ return newValue;
+}
+
/**
* xmlSetTreeDoc:
* @tree: the top element
@@ -2826,6 +2840,9 @@ xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
return;
if (tree->doc != doc) {
+ xmlDictPtr oldTreeDict = tree->doc ? tree->doc->dict : NULL;
+ xmlDictPtr newDict = doc ? doc->dict : NULL;
+
if(tree->type == XML_ELEMENT_NODE) {
prop = tree->properties;
while (prop != NULL) {
@@ -2833,7 +2850,11 @@ xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
xmlRemoveID(tree->doc, prop);
}
- prop->doc = doc;
+ if (prop->doc != doc) {
+ xmlDictPtr oldPropDict = prop->doc ? prop->doc->dict : NULL;
+ prop->name = _copyStringForNewDictIfNeeded(oldPropDict, newDict, prop->name);
+ prop->doc = doc;
+ }
xmlSetListDoc(prop->children, doc);
/*
@@ -2862,6 +2883,10 @@ xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
} else if (tree->children != NULL) {
xmlSetListDoc(tree->children, doc);
}
+
+ tree->name = _copyStringForNewDictIfNeeded(oldTreeDict, newDict, tree->name);
+ tree->content = (xmlChar *)_copyStringForNewDictIfNeeded(oldTreeDict, NULL, tree->content);
+ /* FIXME: tree->ns should be updated as in xmlStaticCopyNode(). */
tree->doc = doc;
}
}