Package Exports
- postcss-values-parser
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (postcss-values-parser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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:
// ES6
let parser = require('postcss-values-parser');
let ast = parser('#fff').parse();
let color = ast // the Root node
.first // the Value node
.first; // a Word node, containing the color value.
// ES5
var parser = require('postcss-values-parser');
var ast = parser('#fff').parse();
var color = ast // the Root node
.first // the Value node
.first; // a Word node, containing the color value.
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.