Created
March 5, 2021 09:08
-
-
Save captainbrosset/73dde5423b590f8f32111f829725c7dc to your computer and use it in GitHub Desktop.
Code snippets for the "How we built the DevTools Tooltips" article
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function querySelectorShadow(selector: string): Element|null { | |
let found: Element|null = null; | |
const search = (root: Element|ShadowRoot) => { | |
const iter = document.createTreeWalker( | |
root, | |
NodeFilter.SHOW_ELEMENT, | |
); | |
do { | |
const currentNode = iter.currentNode as HTMLElement; | |
if (currentNode.shadowRoot) { | |
search(currentNode.shadowRoot); | |
} | |
if (currentNode instanceof ShadowRoot) { | |
continue; | |
} | |
if (!found && currentNode.matches(selector)) { | |
found = currentNode; | |
} | |
} while (!found && iter.nextNode()); | |
}; | |
search(document.documentElement); | |
return found; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment