I have ran into this exact same problem with my code as well. I was doing the following:
while (shadowObject.firstChild) {
parentObject.appendChild(shadowObject.firstChild);
shadowObject.removeChild(shadowObject.firstChild);
}
The way that I fixed it was to remove the removeChild line so it looks like this:
while (shadowObject.firstChild) {
parentObject.appendChild(shadowObject.firstChild);
}
which fixed the problem. Firefox does the same thing. I found the reason for it at the following links:
https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild
The critical point of this article is this:
The Node.appendChild() method adds a node to the end of the list of
children of a specified parent node. If the given child is a reference
to an existing node in the document, appendChild() moves it from its
current position to the new position (there is no requirement to
remove the node from its parent node before appending it to some other
node).
This means that a node can't be in two points of the document
simultaneously. So if the node already has a parent, the node is first
removed, then appended at the new position.
When you think about it, it makes sense. This behavior is documented on the w3schools site, but it's easy to overlook.