Last active
January 10, 2019 12:35
-
-
Save benjamingr/95e9c72436cd06ca12211ceb01e51476 to your computer and use it in GitHub Desktop.
This file contains 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 querySelectorAllDeep(root, selector) { | |
const [start, ...rest] = Array.isArray(selector) ? selector : selector.split(" /deep/ "); | |
if (rest.length === 0) return [...root.querySelectorAll(start)]; | |
return [...root.querySelectorAll(start + " *")].map(x => x.shadowRoot).filter(Boolean).map(x => querySelectorAllDeep(x, rest)).flat(); | |
} | |
function unshadowDOM(dom, tagnNameOverride) { | |
const root = document.createElement(tagnNameOverride || dom.tagName || "fragment"); | |
for(const {name, value} of (dom.attributes || [])) { | |
try { | |
root.setAttribute(name, value); | |
} catch (e) { /* called with hidden$ attribute */ } | |
} | |
root.textContent = dom.textContent; | |
if (dom.shadowRoot) { | |
root.appendChild(unshadowDOM(dom.shadowRoot, "shadow")); | |
} | |
for(const kid of dom.children) { | |
root.appendChild(unshadowDOM(kid)); | |
} | |
return root; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hackier :D