capricorn86 / happy-dom

A JavaScript implementation of a web browser without its graphical user interface
MIT License
3.09k stars 185 forks source link

insertBefore with a comment node causes duplication of node #1406

Closed mdafanasev closed 2 months ago

mdafanasev commented 2 months ago

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

  1. Create a DOM element and add it to the container
  2. Create a comment node and add it to the same container
  3. 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