Skip to content

Instantly share code, notes, and snippets.

@UpperCod
Created October 4, 2020 15:54
Show Gist options
  • Save UpperCod/31cd0f2611de3559713ea15b4fa29a06 to your computer and use it in GitHub Desktop.
Save UpperCod/31cd0f2611de3559713ea15b4fa29a06 to your computer and use it in GitHub Desktop.
capture the css blocks, great to use with sheet.insertRule
const add = (list: string[]) => (rule: string) => list.push(rule) && "";
function parse(css: string) {
const rules = [];
const atRules = [];
css
.replace(/\s+/g, " ")
.replace(/@([^{]+)\{(.*?}) *}/g, add(atRules))
.replace(/([^}{;]*){([^}{]+)}/g, add(rules));
return [...rules, ...atRules];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment