tiny-css-parser

A very small css parser


Keywords
CSS, parser, tokenizer, stream, tiny, small
License
MIT
Install
npm install tiny-css-parser@0.10.0

Documentation

Tiny Css Parser

Install size Dependencies NPM version

A very small css parser.

Consists of a lazy tokenizer, a lazy parser and an incremental tree builder. Both the tokenizer as well as the parser produce a stream of tokens.

The parser inserts properly balanced start and end tokens into the stream, effectively producing a traversal of the full parse tree. This stream can be fed to the tree builder to incrementally build an object tree from the stream.

Example

To parse a stylesheet into an object tree:

var css = require ('tiny-css-parser')
var tree = css.parseTree ('#menu { padding:0; margin:0; display:block }')
console.log (tree)

To lazily parse and traverse a stylesheet:

var stream = css.parse ('#menu { padding:0; margin:0; display:block }')

for (var token of stream)
  console.log (token)

It is possible to query the stream for state info and source position, as follows:

var stream = css.parse ('#menu { padding:0; margin:0; display:block }')

for (var token of stream)
  console.log (token, stream.state)

API

  • tokenize (string) (generator function)
  • parse (string) (generator function)
  • parseTree (string) (function)
  • tokens (object/ dictionary)