From 676c634b9ff6cc8b626254785abb6a111384e1ef Mon Sep 17 00:00:00 2001 From: Max Afanasev Date: Sun, 14 Apr 2024 01:12:01 +0200 Subject: [PATCH] fix: [1406] Fixes issue with insertBefore and comment reference node --- .../happy-dom/src/nodes/element/ElementUtility.ts | 7 +++++++ .../happy-dom/test/nodes/element/Element.test.ts | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/happy-dom/src/nodes/element/ElementUtility.ts b/packages/happy-dom/src/nodes/element/ElementUtility.ts index cc5d861e2..bbb3368b9 100644 --- a/packages/happy-dom/src/nodes/element/ElementUtility.ts +++ b/packages/happy-dom/src/nodes/element/ElementUtility.ts @@ -171,6 +171,13 @@ export default class ElementUtility { parentNodeChildren.splice(index, 1); } } + const parentChildNodes = (ancestorNode)[PropertySymbol.childNodes]; + if (parentChildNodes) { + const index = parentChildNodes.indexOf(newNode); + if (index !== -1) { + parentChildNodes.splice(index, 1); + } + } } const ancestorNodeChildren = >( diff --git a/packages/happy-dom/test/nodes/element/Element.test.ts b/packages/happy-dom/test/nodes/element/Element.test.ts index 9441f6055..302f93cb8 100644 --- a/packages/happy-dom/test/nodes/element/Element.test.ts +++ b/packages/happy-dom/test/nodes/element/Element.test.ts @@ -1235,6 +1235,18 @@ describe('Element', () => { expect(element.children['div'] === div).toBe(true); expect(element.children['span2'] === span2).toBe(true); }); + + it('Inserts correctly with comment reference node', () => { + const container = document.createElement('div'); + const child = document.createElement('p'); + child.textContent = 'A'; + container.appendChild(child); + const comment = document.createComment(''); + container.appendChild(comment); + container.insertBefore(child, comment); + const elements = container.querySelectorAll('p'); + expect(elements.length).toBe(1); + }); }); describe('get previousElementSibling()', () => {