Skip to content

Instantly share code, notes, and snippets.

@scriptype
Last active April 16, 2017 11:30
Show Gist options
  • Save scriptype/91734fe8b31fbd44475129543f0582dd to your computer and use it in GitHub Desktop.
Save scriptype/91734fe8b31fbd44475129543f0582dd to your computer and use it in GitHub Desktop.
Parses a given CSS rule
(function(input) {
class CSSRule {
constructor(cssText) {
this.raw = cssText
this.parsed = CSSRule.parse(cssText)
return this
}
static parse(cssText) {
var [, selectorsText, declarationsText] = cssText
.replace(/\n/g, '')
.match(/(.+)\{(.+)\}/)
var selectors = selectorsText
.split(/,/)
.map(e => e.trim())
var declarations = declarationsText
.split(/;/)
.map(e => e.trim())
.filter(Boolean)
.map(e => e.split(/:/).map(p => p.trim()))
return {
selectors,
declarations
}
}
}
return new CSSRule(input)
})(`
.my-element, body,
some-custom-element and-its-child {
color: red;
background: rgba(255, 25, 5, .4);
color: blue;
}
`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment