Created
February 10, 2020 17:29
-
-
Save lahmatiy/d85baa3a9cd77dc4b2813531fd88262e to your computer and use it in GitHub Desktop.
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
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