Last active
November 13, 2020 18:35
-
-
Save Robdel12/5cd25c39ccf58b192402c2c984146d81 to your computer and use it in GitHub Desktop.
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
// snagged from MDN | |
// The goal is to take all the CSS created in the CSS Object Model (CSSOM) | |
// and inject it into the DOM so Percy can render it safely in our browsers | |
let cssOmStyles = [].slice.call(document.styleSheets).reduce((prev: String, styleSheet: CSSStyleSheet) => { | |
// Make sure it has a rulesheet, does NOT have a href (no external stylesheets), and isn't already in the DOM. | |
let hasHref = styleSheet.href; | |
let hasStyleInDom = styleSheet.ownerNode.innerText.length > 0; | |
if (styleSheet.cssRules && !hasHref && !hasStyleInDom) { | |
return ( | |
prev + | |
[].slice.call(styleSheet.cssRules).reduce((prev: String, cssRule: CSSRule) => { | |
return prev + cssRule.cssText; | |
}, "") | |
); | |
} else { | |
return prev; | |
} | |
}, ""); | |
let $stylesheet = document.createElement('style'); | |
$stylesheet.type = 'text/css'; | |
let styles = document.createTextNode(cssOmStyles); | |
$stylesheet.appendChild(styles); | |
document.head.appendChild($stylesheet); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment