All Projects β†’ csstree β†’ Csstree

csstree / Csstree

Licence: mit
A tool set for CSS including fast detailed parser, walker, generator and lexer based on W3C specs and browser implementations

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Csstree

Php Parser
🌿 NodeJS PHP Parser - extract AST or tokens (PHP5 and PHP7)
Stars: ✭ 400 (-64.32%)
Mutual labels:  ast, lexer, parser
Charly Vm
Fibers, Closures, C-Module System | NaN-boxing, bytecode-VM written in C++
Stars: ✭ 66 (-94.11%)
Mutual labels:  ast, lexer, parser
Grmtools
Rust grammar tool libraries and binaries
Stars: ✭ 153 (-86.35%)
Mutual labels:  lexer, parser, generator
Tiny Compiler
A tiny compiler for a language featuring LL(2) with Lexer, Parser, ASM-like codegen and VM. Complex enough to give you a flavour of how the "real" thing works whilst not being a mere toy example
Stars: ✭ 425 (-62.09%)
Mutual labels:  ast, lexer, parser
Participle
A parser library for Go
Stars: ✭ 2,302 (+105.35%)
Mutual labels:  ast, lexer, parser
Graphql Go Tools
Tools to write high performance GraphQL applications using Go/Golang.
Stars: ✭ 96 (-91.44%)
Mutual labels:  ast, lexer, parser
Tolerant Php Parser
An early-stage PHP parser designed for IDE usage scenarios.
Stars: ✭ 717 (-36.04%)
Mutual labels:  ast, parser, fast
Cub
The Cub Programming Language
Stars: ✭ 198 (-82.34%)
Mutual labels:  ast, lexer, parser
Query Translator
Query Translator is a search query translator with AST representation
Stars: ✭ 165 (-85.28%)
Mutual labels:  ast, parser, generator
Lioness
The Lioness Programming Language
Stars: ✭ 155 (-86.17%)
Mutual labels:  ast, lexer, parser
Snapdragon
snapdragon is an extremely pluggable, powerful and easy-to-use parser-renderer factory.
Stars: ✭ 180 (-83.94%)
Mutual labels:  ast, lexer, parser
Exprtk
C++ Mathematical Expression Parsing And Evaluation Library
Stars: ✭ 301 (-73.15%)
Mutual labels:  ast, lexer, parser
Minigo
minigoπŸ₯is a small Go compiler made from scratch. It can compile itself.
Stars: ✭ 456 (-59.32%)
Mutual labels:  lexer, parser
Badgen
Fast handcraft svg badge generator.
Stars: ✭ 464 (-58.61%)
Mutual labels:  generator, fast
Vim Doge
(Do)cumentation (Ge)nerator 10+ languages πŸ“š Generate proper code documentation skeletons with a single keypress. ⚑️πŸ”₯
Stars: ✭ 533 (-52.45%)
Mutual labels:  generator, fast
Tenko
An 100% spec compliant ES2021 JavaScript parser written in JS
Stars: ✭ 490 (-56.29%)
Mutual labels:  ast, parser
Webassemblyjs
Toolchain for WebAssembly
Stars: ✭ 566 (-49.51%)
Mutual labels:  ast, parser
Postcss
Transforming styles with JS plugins
Stars: ✭ 25,612 (+2184.75%)
Mutual labels:  ast, parser
Astexplorer
A web tool to explore the ASTs generated by various parsers.
Stars: ✭ 4,330 (+286.26%)
Mutual labels:  ast, parser
Meriyah
A 100% compliant, self-hosted javascript parser - https://meriyah.github.io/meriyah
Stars: ✭ 690 (-38.45%)
Mutual labels:  ast, parser

CSSTree logo

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

Related projects

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].