[6a3a178] | 1 | # postcss-values-parser [![Build Status](https://travis-ci.org/shellscape/postcss-values-parser.svg?branch=master)](https://travis-ci.org/shellscape/postcss-values-parser)
|
---|
| 2 |
|
---|
| 3 | <img align="right" width="95" height="95"
|
---|
| 4 | title="Philosopher’s stone, logo of PostCSS"
|
---|
| 5 | src="http://postcss.github.io/postcss/logo.svg">
|
---|
| 6 |
|
---|
| 7 | A CSS property value parser for use with [PostCSS](https://github.com/postcss/postcss),
|
---|
| 8 | following the same node, container, and traversal patterns as PostCSS.
|
---|
| 9 |
|
---|
| 10 | ##
|
---|
| 11 | <p align="center">
|
---|
| 12 | <b>:rocket: Are you ready to tackle ES6 and hone your JavaScript Skills?</b> :rocket:<br/>
|
---|
| 13 | Check out these outstanding <a href="https://es6.io/">ES6 courses</a> by <a href="https://github.com/wesbos">@wesbos</a>
|
---|
| 14 | </p>
|
---|
| 15 |
|
---|
| 16 | ---
|
---|
| 17 |
|
---|
| 18 | As with PostCSS and postcss-selector-parser, this parser generates an
|
---|
| 19 | [Abstract Syntax Tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree),
|
---|
| 20 | (aka "AST") which allows for ease of traversal and granular inspection of each
|
---|
| 21 | part of a property's value.
|
---|
| 22 |
|
---|
| 23 | ## postcss-values-parser vs. postcss-value-parser
|
---|
| 24 |
|
---|
| 25 | Yeah, it's a tad confusing. The [Lesshint](https://github.com/lesshint/lesshint)
|
---|
| 26 | project needed a parser that would allow detailed inspection of property values
|
---|
| 27 | to the same degree that PostCSS and [postcss-selector-parser](https://github.com/postcss/postcss-selector-parser)
|
---|
| 28 | provided. This was especailly important for the Lesshint project, as it provides
|
---|
| 29 | for very granular rules for linting LESS.
|
---|
| 30 |
|
---|
| 31 | [postcss-value-parser](https://github.com/TrySound/postcss-value-parser)
|
---|
| 32 | makes a lot of assumption about how values should be parsed and how the resulting
|
---|
| 33 | AST should be organized. It was also fairly out of sync with the tokenzing and
|
---|
| 34 | traversal patterns and convenience methods found in PostCSS and
|
---|
| 35 | postcss-selector-parser.
|
---|
| 36 |
|
---|
| 37 | So we needed an alternative, and drew upon all three projects to put together a
|
---|
| 38 | value parser that met and exceeded our needs. The improvements include:
|
---|
| 39 |
|
---|
| 40 | - Written using ES6
|
---|
| 41 | - Uses the same Gulp toolchain as PostCSS
|
---|
| 42 | - Doesn't strip characters; eg. parenthesis
|
---|
| 43 | - Full AST traversal
|
---|
| 44 | - AST traversal based on node type
|
---|
| 45 | - Simple methods to derive strings from the parsed result
|
---|
| 46 | - Follows PostCSS patterns for whitespace between Nodes
|
---|
| 47 | - Provides convenience properties for number units, colors, etc.
|
---|
| 48 |
|
---|
| 49 | ## Usage
|
---|
| 50 |
|
---|
| 51 | Please see the [API Documentation](API.md) for full usage information.
|
---|
| 52 |
|
---|
| 53 | As with any NPM module, start with the install:
|
---|
| 54 |
|
---|
| 55 | ```
|
---|
| 56 | npm install postcss-values-parser
|
---|
| 57 | ```
|
---|
| 58 |
|
---|
| 59 | Using this parser is straightforward and doesn't require callbacks:
|
---|
| 60 |
|
---|
| 61 | ```js
|
---|
| 62 | const parser = require('postcss-values-parser');
|
---|
| 63 | const ast = parser('#fff').parse();
|
---|
| 64 |
|
---|
| 65 | let color = ast // the Root node
|
---|
| 66 | .first // the Value node
|
---|
| 67 | .first; // a Word node, containing the color value.
|
---|
| 68 | ```
|
---|
| 69 |
|
---|
| 70 | ## Loose Mode
|
---|
| 71 |
|
---|
| 72 | Loose mode was introduced to support adherence to the W3C CSS Specification as
|
---|
| 73 | well as the ability to parse noncompliant CSS for variants like LESS, SCSS, and
|
---|
| 74 | CSSNext. If you're working with a noncompliant or CSS-like variant, then loose
|
---|
| 75 | mode is for you.
|
---|
| 76 |
|
---|
| 77 | For example, the parser
|
---|
| 78 | will throw an error by default if `calc` parameters [don't adhere to the spec](https://www.w3.org/TR/css-values/#calc-syntax).
|
---|
| 79 | However, with loose mode enabled, the parse will ignore spec rules and succeed.
|
---|
| 80 |
|
---|
| 81 | In-draft features, or CSS features in modules not yet finalized, often cause parser
|
---|
| 82 | errors. eg. `url(var(--somevar))`. Loose mode supports parsing of these features.
|
---|
| 83 |
|
---|
| 84 | Loose Mode is enabled by passing an option of `loose: true` to the `parser` method.
|
---|
| 85 |
|
---|
| 86 | ```js
|
---|
| 87 | const less = 'calc(2+2)'; // not valid per spec, but valid in LESS
|
---|
| 88 | const cssnext = 'url(var(--somevar))'; // not valid per spec, but in spec draft
|
---|
| 89 |
|
---|
| 90 | const parser = require('postcss-values-parser');
|
---|
| 91 | const ast = parser(less, { loose: true }).parse();
|
---|
| 92 |
|
---|
| 93 | // parse will succeed
|
---|
| 94 | ```
|
---|
| 95 |
|
---|
| 96 | ## Acknowledgements
|
---|
| 97 |
|
---|
| 98 | This project was heavily influenced by [postcss-selector-parser](https://github.com/postcss/postcss-selector-parser)
|
---|
| 99 | and utilized many patterns and logical constructs from the project.
|
---|
| 100 |
|
---|
| 101 | Tests and some tokenizing techniques found in [postcss-value-parser](https://github.com/TrySound/postcss-value-parser)
|
---|
| 102 | were used.
|
---|
| 103 |
|
---|
| 104 | ## Contributing
|
---|
| 105 |
|
---|
| 106 | - `git fork/clone`
|
---|
| 107 | - `npm i`
|
---|
| 108 | - Before PR'ing, make sure `npm test` still pass. Add test if you're adding features.
|
---|
| 109 |
|
---|
| 110 | When you tweak [API.md](API.md), please run `npm run toc` before PR'ing.
|
---|