CSSTree

一个 CSS 工具集,包括基于 W3C 规范和浏览器实现的快速详细的解析器、walker、生成器和词法分析程序。「A tool set for working with CSS including fast detailed parser, walker, generator and lexer based on W3C specs and browser implementations」

Github星跟蹤圖

CSSTree

NPM version
Build Status
Coverage Status
NPM Downloads
Twitter

CSSTree is a tool set for CSS: fast detailed parser (CSS → AST), walker (AST traversal), generator (AST → CSS) and lexer (validation and matching) based on specs 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 library isn't in final shape and needs further improvements (e.g. AST format and API are subjects to change in next major versions). However it's stable and used by projects like CSSO (CSS minifier) and SVGO (SVG optimizer) in production. Master branch contains changes for next major version, for stable published version see branch 1.0.

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 format for all possible node types). The parsing detail level can be changed through parser options, for example, you can disable parsing of selectors or declaration values for component parts.

  • Tolerant to errors by design

    Parser behaves as spec says: "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 parsers at the moment.

  • Syntax validation

    The build-in lexer can test CSS against syntaxes defined by W3C. CSSTree uses mdn/data 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.

Documentation

Tools

Usage

Install with npm:

> npm install css-tree

Basic usage:

var csstree = require('css-tree');

// parse CSS to AST
var ast = csstree.parse('.example { world: "!" }');

// traverse AST and modify it
csstree.walk(ast, function(node) {
    if (node.type === 'ClassSelector' && node.name === 'example') {
        node.name = 'hello';
    }
});

// generate CSS from AST
console.log(csstree.generate(ast));
// .hello{world:"!"}

Syntax matching:

// parse CSS to AST as a declaration value
var ast = csstree.parse('red 1px solid', { context: 'value' });

// match to syntax of `border` property
var matchResult = csstree.lexer.matchProperty('border', ast);

// check first value node is a <color>
console.log(matchResult.isType(ast.children.first(), 'color'));
// true

// get a type list matched to a node
console.log(matchResult.getTrace(ast.children.first()));
// [ { type: 'Property', name: 'border' },
//   { type: 'Type', name: 'color' },
//   { type: 'Type', name: 'named-color' },
//   { type: 'Keyword', name: 'red' } ]

Top level API

API map

License

MIT

主要指標

概覽
名稱與所有者csstree/csstree
主編程語言JavaScript
編程語言JavaScript (語言數: 3)
平台Linux, Mac, Windows
許可證MIT License
所有者活动
創建於2016-09-02 16:03:01
推送於2025-04-07 23:40:08
最后一次提交2025-04-08 08:40:08
發布數53
最新版本名稱v3.1.0 (發布於 )
第一版名稱v1.0.0-alpha6 (發布於 )
用户参与
星數2k
關注者數24
派生數95
提交數1.2k
已啟用問題?
問題數237
打開的問題數50
拉請求數41
打開的拉請求數15
關閉的拉請求數36
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?