Merge pull request #5262 from Chic-Tweetz/hint-nested-shadows

Hint nested & closed shadows
This commit is contained in:
Oliver Blanthorn 2025-09-13 12:09:15 +00:00 committed by GitHub
commit c0b902fb8d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -336,11 +336,19 @@ export function getSelector(e: HTMLElement) {
/* Get all the elements that match the given selector inside shadow DOM */
function getShadowElementsBySelector(selector: string) {
let elems = []
document.querySelectorAll("*").forEach(elem => {
if (elem.shadowRoot) {
elems = elems.concat(...elem.shadowRoot.querySelectorAll(selector))
}
})
const roots: (Document | ShadowRoot)[] = [document]
while (roots.length) {
const root = roots.pop()
root.querySelectorAll("*").forEach(elem => {
if ((elem as any).openOrClosedShadowRoot) {
roots.push((elem as any).openOrClosedShadowRoot)
elems = elems.concat(
...roots[roots.length - 1].querySelectorAll(selector),
)
}
})
}
return elems
}