Skip to content

Instantly share code, notes, and snippets.

@lahmatiy
Created February 10, 2020 17:29
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 lahmatiy/d85baa3a9cd77dc4b2813531fd88262e to your computer and use it in GitHub Desktop.
Save lahmatiy/d85baa3a9cd77dc4b2813531fd88262e to your computer and use it in GitHub Desktop.
const csstree = require("css-tree");
const cssDefinitions = ["color"];
const ast = csstree.parse(`
.some-selector {
color: black;
border: 1px solid #fff;
}
`, { parseValue: false });
csstree.walk(ast, {
visit: 'Declaration',
enter: function(decl) {
const ast = csstree.parse(decl.value.value, {
context: "value"
});
const fragments = csstree.lexer.findValueFragments(decl.property, ast, 'Type', cssDefinitions[0]);
if (fragments.length) {
console.log(`${decl.property}: ${decl.value.value}`);
fragments.forEach(f => {
const color = f.nodes.first(); // color node
console.log(csstree.generate(color))
});
console.log('');
}
}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment