Skip to content

Instantly share code, notes, and snippets.

@ka7eh
Created December 7, 2016 23:57
Show Gist options
  • Save ka7eh/f3e796f167eb8a4238c48651660e781b to your computer and use it in GitHub Desktop.
Save ka7eh/f3e796f167eb8a4238c48651660e781b to your computer and use it in GitHub Desktop.
Returns all styling rules of the passed doc as a string
function getStyles(doc) {
/** idea from https://github.com/NYTimes/svg-crowbar **/
var styles = '',
styleSheets = doc.styleSheets;
if (styleSheets) {
for (var i = 0; i < styleSheets.length; i++) {
processStyleSheet(styleSheets[i]);
}
}
function processStyleSheet(ss) {
if (ss.cssRules) {
for (var i = 0; i < ss.cssRules.length; i++) {
var rule = ss.cssRules[i];
if (rule.type === 3) { // Type 3 is CSSImportRule
processStyleSheet(rule.styleSheet);
} else {
// hack for illustrator crashing on descendant selectors
if (rule.selectorText) {
if (rule.selectorText.indexOf('>') === -1) {
styles += '\n' + rule.cssText;
}
}
}
}
}
}
return styles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment