Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  7] [ 3]  / answers: 1 / hits: 20319  / 12 Years ago, sat, june 9, 2012, 12:00:00

Update 2018: This question was asked long before PostCSS existed, and I would have probably used that.




I'd like to parse a blob of CSS into an AST so I can add prefixes to certain CSS directives.



Is there a CSS parser for JavaScript or Node that will do this?



I've searched NPM. The only useful result I've found is parser-lib, but it's stream-based and it looks like I'll need to write my own emitter for every CSS node.



Update: I also found JSCSSP, but it has no documentation...


More From » css

 Answers
11

Update: I previously mentioned JSCSSP, which is buggy seems to be abandoned. Obviously enough, the css module on NPM is the best:



css = require 'css'

input = '''
body {
font-family: sans-serif;
}
#thing.foo p.bar {
font-weight: bold;
}
'''

obj = css.parse input
sheet = obj.stylesheet

for rule in sheet.rules
rule.selectors = ('#XXX ' + s for s in rule.selectors)

console.log css.stringify(obj)


Output:



#XXX body {
font-family: sans-serif;
}
#XXX #thing.foo p.bar {
font-weight: bold;
}

[#85019] Friday, June 8, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anabellejaynav

Total Points: 176
Total Questions: 105
Total Answers: 105

Location: Croatia
Member since Fri, Sep 11, 2020
4 Years ago
;