@blockquote/frontend-utilities
Version:
Utilities for managing the DOM, handling events, and performing various common tasks in frontend development.
25 lines (24 loc) • 798 B
JavaScript
/**
* Returns true if the first node contains the second, even if the second node
* is in a shadow tree.
*
* The standard Node.contains() function does not account for Shadow DOM, and
* returns false if the supplied target node is sitting inside a shadow tree
* within the container.
*
* @param {Node} container - The container to search within.
* @param {Node} target - The node that may be inside the container.
* @returns {boolean} - True if the container contains the target node.
*/
export const deepContains = (container, target) => {
/** @type {any} */
let current = target;
while (current) {
const parent = current.assignedSlot || current.parentNode || current.host;
if (parent === container) {
return true;
}
current = parent;
}
return false;
};