Skip to content

Instantly share code, notes, and snippets.

@tomhodgins
Last active January 19, 2019 21:41
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 tomhodgins/19150f82943ff0243cb91a53e8e10f47 to your computer and use it in GitHub Desktop.
Save tomhodgins/19150f82943ff0243cb91a53e8e10f47 to your computer and use it in GitHub Desktop.
Process CSSRule objects with a callback function when given a CSSStyleSheet object and function as inputs
export default function(stylesheet, callback) {
const readRules = list => Array.from(list.cssRules).forEach(rule => {
callback(rule)
if (rule.cssRules !== undefined) {
readRules(rule)
}
})
if (
[stylesheet, stylesheet.cssRules, callback].every(thing => thing !== undefined)
&& callback.constructor.name === 'Function'
) {
readRules(stylesheet)
return stylesheet
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment