tiny-css-parser

0.10.0 • Public • Published

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)

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i tiny-css-parser

      Weekly Downloads

      4

      Version

      0.10.0

      License

      MIT

      Unpacked Size

      34.2 kB

      Total Files

      22

      Last publish

      Collaborators

      • alwinb