diff --git a/polygerrit-ui/app/scripts/util.js b/polygerrit-ui/app/scripts/util.js index 672c43fa67..e26d6d99b8 100644 --- a/polygerrit-ui/app/scripts/util.js +++ b/polygerrit-ui/app/scripts/util.js @@ -128,5 +128,41 @@ return element; }; + /** + * Query selector all dom elements matching with certain selector. + * + * This is shadow DOM compatible, but only works when selector is within + * one shadow host, won't work if your selector is crossing + * multiple shadow hosts. + * + * Note: this can be very expensive, only use when have to. + */ + util.querySelectorAll = (el, selector) => { + let nodes = [el]; + const results = new Set(); + while (nodes.length) { + const node = nodes.pop(); + + if (!node || !node.querySelectorAll) continue; + + // Try find all from regular children + [...node.querySelectorAll(selector)] + .forEach(el => results.add(el)); + + // Add all nodes with shadowRoot and loop through + const allShadowNodes = [...node.querySelectorAll('*')] + .filter(child => !!child.shadowRoot) + .map(child => child.shadowRoot); + nodes = nodes.concat(allShadowNodes); + + // Add shadowRoot of current node if has one + // as its not included in node.querySelectorAll('*') + if (node.shadowRoot) { + nodes.push(node.shadowRoot); + } + } + return [...results]; + }; + window.util = util; })(window);