Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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