A CSS parser powered by the style crate from Servo.
I was unable to find a single Node.js library that could reliably parse shorthand CSS property values without using Web APIs.
My desired use case was expanding shorthand properties into their constituent longform properties. For example:
background: center / contain no-repeat url("../foo.svg"), #eee 35% url("../bar.png");
The libraries I tested all failed to address my use case, for the following reasons:
- Doesn't parse property values at all
- Doesn't support
background
property values at all - Merely tokenizes
background
property values, failing to parse the value into sufficiently useful information - Fails when parsing valid
background
values (such as the example above) - Relies on browser-specific APIs such as
CSSStyleDeclaration
After many failed attempts, I have come to the conclusion that the most accurate and reliable way to parse CSS is to simply use the parser from a production-grade web browser. The style crate from Servo is a good choice because it has been integrated into Firefox.