Skip to content

Commit

Permalink
Merge pull request #1453 from juandiegombr/master
Browse files Browse the repository at this point in the history
fix: [#1429] Element.insertBefore works when the node is already inserted
  • Loading branch information
capricorn86 authored Jun 20, 2024
2 parents 3504a93 + fbb71ce commit e6f9127
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 e6f9127

Please sign in to comment.