happy-dom
happy-dom copied to clipboard
insertBefore with a comment node causes duplication of node
Description
Using insertBefore
with a comment node results in copying the target node instead of moving it
// Create container
const container = document.createElement("div");
document.body.appendChild(container);
// Create child
const child = document.createElement("p");
child.textContent = "Hi";
container.appendChild(child);
// Create comment node
const comment = document.createComment("");
container.appendChild(comment);
// Query nodes, it returns "['Hi']", as expected
console.log(document.querySelectorAll("p").map(item => item.textContent));
// Try to move node
container.insertBefore(child, comment);
// Query nodes, "['Hi']" expected, but it returns "['Hi', 'Hi']"
console.log(document.querySelectorAll("p").map(item => item.textContent));
Steps to reproduce
- Create a DOM element and add it to the container
- Create a comment node and add it to the same container
- Attempt to move the element using insertBefore, using the comment node as the reference node
Expected result:
The target node should be moved or remain untouched
Actual result:
The target node is duplicated
Reproduction
I have created a repository with a minimal reproduction of the issue
https://github.com/mdafanasev/happy-dom-insert-before-comment-issue
Environment
@happy-dom/global-registrator: 14.7.1
NodeJS: 20.11.0