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 glimmerjsGH-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`.
  • Loading branch information
shama committed Aug 25, 2023
1 parent 68d371b commit ff3396d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/@glimmer/runtime/lib/bounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,17 @@ 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) {
// eslint-disable-next-line no-console
console.warn("Attempted to clear a child node that doesn't have a parent node.");
} else {
// eslint-disable-next-line no-console
console.warn('Attempted to clear a child node that has been moved.');
}
}

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

0 comments on commit ff3396d

Please sign in to comment.