UNPKG

@scania/tegel

Version:
27 lines (26 loc) 1.25 kB
/** * Recursively finds the first matching element or child based on a provided condition. * * @param {ParentNode} parentNode - The starting element or shadow root to search from. * @param {(el: HTMLElement) => boolean} searchPredicate - The condition to match the element, receives an HTMLElement and returns a boolean. * @param {boolean} [pierceShadow=false] - Whether to pierce through shadow DOM boundaries. * @returns {HTMLElement | null} - The first matching element or child, or null if none is found. */ function dfs(parentNode, searchPredicate, pierceShadow = false) { if (parentNode instanceof HTMLElement && searchPredicate(parentNode)) { return parentNode; } const childElements = parentNode instanceof HTMLSlotElement ? parentNode.assignedElements({ flatten: true }) : Array.from(parentNode.children); if (pierceShadow && parentNode instanceof HTMLElement && parentNode.shadowRoot) { childElements.push(...Array.from(parentNode.shadowRoot.children)); } let foundElement = null; childElements.some((child) => { foundElement = dfs(child, searchPredicate, pierceShadow); return foundElement !== null; }); return foundElement; } export default dfs;