postcss-values-parser


A CSS property value parser for use with PostCSS,
following the same node, container, and traversal patterns as PostCSS.
As with PostCSS and postcss-selector-parser, this parser generates an
Abstract Syntax Tree,
(aka "AST") which allows for ease of traversal and granular inspection of each
part of a property's value.
postcss-values-parser vs. postcss-value-parser
Yeah, it's a tad confusing. The Lesshint
project needed a parser that would allow detailed inspection of property values
to the same degree that PostCSS and postcss-selector-parser
provided. This was especailly important for the Lesshint project, as it provides
for very granular rules for linting LESS.
postcss-value-parser
makes a lot of assumption about how values should be parsed and how the resulting
AST should be organized. It was also fairly out of sync with the tokenzing and
traversal patterns and convenience methods found in PostCSS and
postcss-selector-parser.
So we needed an alternative, and drew upon all three projects to put together a
value parser that met and exceeded our needs. The improvements include:
- Written using ES6
- Uses the same Gulp toolchain as PostCSS
- Doesn't strip characters; eg. parenthesis
- Full AST traversal
- AST traversal based on node type
- Simple methods to derive strings from the parsed result
- Follows PostCSS patterns for whitespace between Nodes
- Provides convenience properties for number units, colors, etc.
Usage
Please see the API Documentation for full usage information.
As with any NPM module, start with the install:
npm install postcss-values-parser
Using this parser is straightforward and doesn't require callbacks:
let parser = require('postcss-values-parser');
let ast = parser('#fff').parse();
let color = ast
.first
.first;
var parser = require('postcss-values-parser');
var ast = parser('#fff').parse();
var color = ast
.first
.first;
Acknowledgements
This project was heavily influenced by postcss-selector-parser
and utilized many patterns and logical constructs from the project.
Tests and some tokenizing techniques found in postcss-value-parser
and were used.