githubEdit

css-tree

CSSTree logo

CSSTree

NPM versionarrow-up-right Build Statusarrow-up-right Coverage Statusarrow-up-right NPM Downloadsarrow-up-right Twitterarrow-up-right

CSSTree is a tool set to work with CSS, including fastarrow-up-right detailed parser (string->AST), walker (AST traversal), generator (AST->string) and lexer (validation and matching) based on knowledge of spec and browser implementations. The main goal is to be efficient and W3C spec compliant, with focus on CSS analyzing and source-to-source transforming tasks.

NOTE: The project is in alpha stage since some parts need further improvements, AST format and API are subjects to change. However it's stable enough and used by packages like CSSOarrow-up-right (CSS minifier) and SVGOarrow-up-right (SVG optimizer) in production.

Features

  • Detailed parsing with an adjustable level of detail

    By default CSSTree parses CSS as detailed as possible, i.e. each single logical part is representing with its own AST node (see AST formatarrow-up-right for all possible node types). The parsing detail level can be changed through parser optionsarrow-up-right, for example, you can disable parsing of selectors or declaration values for component parts.

  • Tolerant to errors by design

    Parser behaves as spec saysarrow-up-right: "When errors occur in CSS, the parser attempts to recover gracefully, throwing away only the minimum amount of content before returning to parsing as normal". The only thing the parser departs from the specification is that it doesn't throw away bad content, but wraps it in a special node type (Raw) that allows processing it later.

  • Fast and efficient

    CSSTree is created with focus on performance and effective memory consumption. Therefore it's one of the fastest CSS parsersarrow-up-right at the moment.

  • Syntax validation

    The build-in lexer can test CSS against syntaxes defined by W3C. CSSTree uses mdn/dataarrow-up-right as a basis for lexer's dictionaries and extends it with vendor specific and legacy syntaxes. Lexer can only check the declaration values currently, but this feature will be extended to other parts of the CSS in the future.

Docs

Tools

Usage

Install with npm:

Basic usage:

Syntax matching:

Top level API

API map

License

MIT

Last updated

Was this helpful?