Rich-Harris / stacking-order

Determine which of two elements is in front of the other

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Potential cycle

bvaughn opened this issue · comments

This code has a potential cycle in it:

stacking-order/index.js

Lines 94 to 110 in 4ac3037

/** @param {HTMLElement} node */
function get_ancestors(node) {
const ancestors = [];
while (node) {
ancestors.push(node);
node = get_parent(node);
}
return ancestors; // [ node, ... <body>, <html>, document ]
}
/** @param {HTMLElement} node */
function get_parent(node) {
// @ts-ignore
return node.parentNode?.host || node.parentNode;
}

Context:
toeverything/blocksuite#6471 (comment)

I think this change fixes the cycle:

-  // @ts-ignore
-  return node.parentNode?.host || node.parentNode; 
+  const { parentNode } = node;
+  if (parentNode && parentNode instanceof ShadowRoot) {
+    return parentNode.host
+  }
+  return parentNode;