Skip to content

Commit

Permalink
fix: ensure child node is a child of parent before removing
Browse files Browse the repository at this point in the history
Fixes GH-1401

If `current` isn't a child of `parent` calling `parent.removeChild(current)` will result in the error: `Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node`. This commonly occurs when the DOM is externally modified by a browser, such as when translating a page. The `nextSibling` may not be a child node of `parent`.

`current.parentNode?.removeChild(current)` ensures we are only removing a child node of a parent node.
  • Loading branch information
shama committed Aug 25, 2023
1 parent 68d371b commit 98fa4b6
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/@glimmer/runtime/lib/bounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ export function clear(bounds: Bounds): Nullable<SimpleNode> {
while (true) {
let next = current.nextSibling;

parent.removeChild(current);
if (parent === current.parentNode) {
current.parentNode.removeChild(current);
} else if (import.meta.env.DEV) {
if (current.parentNode === null) {
throw new Error('Attempted to clear a child node that doesn\'t have a parent node.');
} else {
throw new Error('Attempted to clear a child node that has been moved.');
}
}

if (current === last) {
return next;
Expand Down

0 comments on commit 98fa4b6

Please sign in to comment.