From cadf1b9b16a95f1d22d811532b53cba60cdfa5df Mon Sep 17 00:00:00 2001 From: awais-ur-rehman Date: Sat, 31 Oct 2026 15:49:05 +0500 Subject: [PATCH 1/2] fix: preserve element types when cloning --- src/node.spec.ts | 11 +++++++++++ src/node.ts | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/node.spec.ts b/src/node.spec.ts index f350655e..0a850a7c 100644 --- a/src/node.spec.ts +++ b/src/node.spec.ts @@ -51,6 +51,17 @@ describe("Nodes", () => { expect(result.cloneNode(true)).toStrictEqual(result); }); + it.each(["script", "style"])( + "should preserve XML %s element types when cloning", + (name) => { + const result = parse(`<${name}>text`, { xmlMode: true }); + const element = result.children[0]; + expect(element.type).toBe(ElementType.Tag); + expect(element.cloneNode().type).toBe(ElementType.Tag); + expect(result.cloneNode(true)).toStrictEqual(result); + }, + ); + it("should not clone recursively if not asked to", () => { const result = parse("
"); expect(result.cloneNode(true)).toEqual(result); diff --git a/src/node.ts b/src/node.ts index 57669d37..5490fdfe 100644 --- a/src/node.ts +++ b/src/node.ts @@ -429,7 +429,12 @@ export function cloneNode(node: T, shouldRecurse = false): T { result = new Comment(node.data); } else if (isTag(node)) { const children = shouldRecurse ? cloneChildren(node.children) : []; - const clone = new Element(node.name, { ...node.attribs }, children); + const clone = new Element( + node.name, + { ...node.attribs }, + children, + node.type, + ); for (const child of children) { child.parent = clone; } From 90a2365518fe23f314c84704e738f1b1b451fb78 Mon Sep 17 00:00:00 2001 From: Felix Boehm <188768+fb55@users.noreply.github.com> Date: Fri, 6 Nov 2026 00:55:52 +0000 Subject: [PATCH 2/2] test(clone): test one XML element type script and style take the same path through cloneNode(), which now passes the node's type to the Element constructor. Co-Authored-By: Claude Opus 5.5 --- src/node.spec.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/node.spec.ts b/src/node.spec.ts index 0a850a7c..a9d86a45 100644 --- a/src/node.spec.ts +++ b/src/node.spec.ts @@ -51,16 +51,13 @@ describe("Nodes", () => { expect(result.cloneNode(true)).toStrictEqual(result); }); - it.each(["script", "style"])( - "should preserve XML %s element types when cloning", - (name) => { - const result = parse(`<${name}>text`, { xmlMode: true }); - const element = result.children[0]; - expect(element.type).toBe(ElementType.Tag); - expect(element.cloneNode().type).toBe(ElementType.Tag); - expect(result.cloneNode(true)).toStrictEqual(result); - }, - ); + it("should preserve XML element types when cloning", () => { + const result = parse("", { xmlMode: true }); + const element = result.children[0]; + expect(element.type).toBe(ElementType.Tag); + expect(element.cloneNode().type).toBe(ElementType.Tag); + expect(result.cloneNode(true)).toStrictEqual(result); + }); it("should not clone recursively if not asked to", () => { const result = parse("
");