Skip to content

Commit

Permalink
fix: [#1429] Element.insertBefore works when the node is already inse…
Browse files Browse the repository at this point in the history
…rted
  • Loading branch information
juandiegombr committed Jun 3, 2024
1 parent ef2dfea commit 221e22b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/happy-dom/src/nodes/element/ElementUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ export default class ElementUtility {
referenceNode: Node | null,
options?: { disableAncestorValidation?: boolean }
): Node {
if (newNode === referenceNode) {
return newNode;
}

// NodeUtility.insertBefore() will call appendChild() for the scenario where "referenceNode" is "null" or "undefined"
if (newNode[PropertySymbol.nodeType] === NodeTypeEnum.elementNode && referenceNode) {
if (
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 @@ -1247,6 +1247,18 @@ describe('Element', () => {
const elements = container.querySelectorAll('p');
expect(elements.length).toBe(1);
});

it('Inserts correctly with when adding a children that is already inserted', () => {
const container = document.createElement('div');
const child = document.createElement('p');
child.textContent = 'A';
container.appendChild(child);

container.insertBefore(child, child);

const elements = container.querySelectorAll('p');
expect(elements.length).toBe(1);
});
});

describe('get previousElementSibling()', () => {
Expand Down

0 comments on commit 221e22b

Please sign in to comment.