Skip to content

Instantly share code, notes, and snippets.

@jiayihu
Forked from davidgilbertson/copyStyles.js
Created December 24, 2017 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiayihu/a2622fbb59b9c6d0295c1060438e1b1b to your computer and use it in GitHub Desktop.
Save jiayihu/a2622fbb59b9c6d0295c1060438e1b1b to your computer and use it in GitHub Desktop.
function copyStyles(sourceDoc, targetDoc) {
Array.from(sourceDoc.styleSheets).forEach(styleSheet => {
if (styleSheet.cssRules) { // for <style> elements
const newStyleEl = sourceDoc.createElement('style');
Array.from(styleSheet.cssRules).forEach(cssRule => {
// write the text of each rule into the body of the style element
newStyleEl.appendChild(sourceDoc.createTextNode(cssRule.cssText));
});
targetDoc.head.appendChild(newStyleEl);
} else if (styleSheet.href) { // for <link> elements loading CSS from a URL
const newLinkEl = sourceDoc.createElement('link');
newLinkEl.rel = 'stylesheet';
newLinkEl.href = styleSheet.href;
targetDoc.head.appendChild(newLinkEl);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment