Last active
April 16, 2017 11:30
-
-
Save scriptype/91734fe8b31fbd44475129543f0582dd to your computer and use it in GitHub Desktop.
Parses a given CSS rule
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(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