Created
December 7, 2016 23:57
-
-
Save ka7eh/f3e796f167eb8a4238c48651660e781b to your computer and use it in GitHub Desktop.
Returns all styling rules of the passed doc as a string
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 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