🚀 DAY 2 OF LAUNCH WEEK: Unify Your Security Stack with Socket Basics.Learn more →
Socket
Book a DemoInstallSign in
Socket

postcss-values-parser

Package Overview
Dependencies
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-values-parser

A CSS property value parser for use with PostCSS

Source
npmnpm
Version
0.1.8
Version published
Weekly downloads
4.9M
5.29%
Maintainers
2
Weekly downloads
 
Created
Source

postcss-values-parser

Build Status

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.

Keywords

css

FAQs

Package last updated on 28 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts