diff --git a/test/common/test-functions.js b/test/common/test-functions.js index 3f944a7796c9..f55726fc90cf 100644 --- a/test/common/test-functions.js +++ b/test/common/test-functions.js @@ -4,15 +4,30 @@ * These are routines used by our testing code. */ +// Like querySelectorAll(), but traverses shadow DOM +window.querySelectorAllDeep = function(query, element) { + const result = Array.from( + element.shadowRoot + ? element.shadowRoot.childNodes + : element.nodeName === 'SLOT' ? element.assignedElements() : element.childNodes, + ) + .filter(element => element instanceof Element) + .map(element => window.querySelectorAllDeep(query, element)) + .flat(); + + if (element.matches?.(query)) + result.push(element); + return result; +}; + window.ph_select = function(sel) { if (sel.includes(":contains(")) { if (!window.Sizzle) { throw new Error("Using ':contains' when window.Sizzle is not available."); } return window.Sizzle(sel); - } else { - return Array.from(document.querySelectorAll(sel)); - } + } else + return window.querySelectorAllDeep(sel, document); }; window.ph_only = function(els, sel) {