From de3f70146dc531a1f2c0976dc1c2bff84529f161 Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer <wellnhofer@aevum.de>
Date: Tue, 28 Nov 2023 13:01:38 +0100
Subject: [PATCH] tree: Fix regression when copying DTDs

This reverts commit d39f78069dff496ec865c73aa44d7110e429bce9.

Fixes #634.

Reference: https://github.com/GNOME/libxml2/commit/de3f70146dc531a1f2c0976dc1c2bff84529f161
Conflict: NA

---
 tree.c | 31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)

diff --git a/tree.c b/tree.c
index a6264e8b..5a9c24d1 100644
--- a/tree.c
+++ b/tree.c
@@ -4301,28 +4301,29 @@ xmlNodePtr
 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
     xmlNodePtr ret = NULL;
     xmlNodePtr p = NULL,q;
-    xmlDtdPtr newSubset = NULL;
 
     while (node != NULL) {
-	if (node->type == XML_DTD_NODE ) {
 #ifdef LIBXML_TREE_ENABLED
-	    if ((doc == NULL) || (doc->intSubset != NULL)) {
+	if (node->type == XML_DTD_NODE ) {
+	    if (doc == NULL) {
 		node = node->next;
 		continue;
 	    }
-            q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
-            if (q == NULL) goto error;
-            q->doc = doc;
-            q->parent = parent;
-            newSubset = (xmlDtdPtr) q;
-#else
-            node = node->next;
-            continue;
+	    if (doc->intSubset == NULL) {
+		q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
+		if (q == NULL) goto error;
+		q->doc = doc;
+		q->parent = parent;
+		doc->intSubset = (xmlDtdPtr) q;
+		xmlAddChild(parent, q);
+	    } else {
+		q = (xmlNodePtr) doc->intSubset;
+		xmlAddChild(parent, q);
+	    }
+	} else
 #endif /* LIBXML_TREE_ENABLED */
-	} else {
 	    q = xmlStaticCopyNode(node, doc, parent, 1);
-	    if (q == NULL) goto error;
-        }
+	if (q == NULL) goto error;
 	if (ret == NULL) {
 	    q->prev = NULL;
 	    ret = p = q;
@@ -4334,8 +4335,6 @@ xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
 	}
 	node = node->next;
     }
-    if (newSubset != NULL)
-        doc->intSubset = newSubset;
     return(ret);
 error:
     xmlFreeNodeList(ret);
-- 
2.33.0

