Skip to content

alwinb/tiny-css-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)

About

A small css parser

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published