Skip to content

Commit

Permalink
Merge pull request #1407 from mdafanasev/1406-fix-issue-with-insertbe…
Browse files Browse the repository at this point in the history
…fore-and-comment-node

fix: [1406] Fixes issue with insertBefore and comment reference node
  • Loading branch information
capricorn86 committed May 6, 2024
2 parents 9095b2d + 84cc397 commit d159d4d
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 d159d4d

Please sign in to comment.