Skip to content

Commit

Permalink
fix: [1406] Fixes issue with insertBefore and comment reference node
Browse files Browse the repository at this point in the history
  • Loading branch information
mdafanasev committed Apr 13, 2024
1 parent c29f36c commit 676c634
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/happy-dom/src/nodes/element/ElementUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ export default class ElementUtility {
parentNodeChildren.splice(index, 1);
}
}
const parentChildNodes = (<Element>ancestorNode)[PropertySymbol.childNodes];
if (parentChildNodes) {
const index = parentChildNodes.indexOf(newNode);
if (index !== -1) {
parentChildNodes.splice(index, 1);
}
}
}

const ancestorNodeChildren = <HTMLCollection<HTMLElement>>(
Expand Down
12 changes: 12 additions & 0 deletions packages/happy-dom/test/nodes/element/Element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down

0 comments on commit 676c634

Please sign in to comment.