Socket
Socket
Sign inDemoInstall

css-tree

Package Overview
Dependencies
2
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    css-tree

Fast detailed CSS parser


Version published
Weekly downloads
28M
decreased by-0.95%
Maintainers
1
Install size
1.78 MB
Created
Weekly downloads
 

Package description

What is css-tree?

The css-tree npm package is a tool for parsing and manipulating CSS. It allows users to parse CSS strings into an abstract syntax tree (AST), walk over nodes in the tree, generate CSS strings, and more. It is useful for tasks such as CSS minification, linting, and transformation.

What are css-tree's main functionalities?

Parsing CSS to AST

This feature allows you to parse a CSS string and convert it into an abstract syntax tree (AST) for further manipulation or analysis.

const csstree = require('css-tree');
const ast = csstree.parse('.example { color: red; }');

Walking the AST

This feature enables you to traverse the AST and apply functions or extract information from specific nodes.

csstree.walk(ast, function(node) {
  if (node.type === 'ClassSelector') {
    console.log(node.name);
  }
});

Generating CSS from AST

After manipulating the AST, you can generate a CSS string from the modified AST, which can be used in stylesheets or injected into web pages.

const modifiedAST = csstree.parse('.example { color: blue; }');
const css = csstree.generate(modifiedAST);

Minifying CSS

css-tree can be used to minify CSS by parsing it with compression options and then translating the AST back to a CSS string.

const compressedCSS = csstree.translate(csstree.parse('.example { color: red; }', { compress: true }));

Other packages similar to css-tree

Changelog

Source

1.0.0-alpha22 (September 8, 2017)

  • Parser
    • Fixed exception on parsing of unclosed {}-block in tolerant mode
    • Added tolerant mode support for DeclarationList
    • Added standalone entry point, i.e. default parser can be used via require('css-tree/lib/parser') (#47)
  • Generator
    • Changed generator to produce +n when AnPlusB.a is +1 to be "round-trip" with parser
    • Added standalone entry point, i.e. default generators can be used via require('css-tree/lib/generator')
  • Walker
    • Added standalone entry point, i.e. default walkers can be used via require('css-tree/lib/walker') (#47)
  • Lexer
    • Added default keyword to the list of invalid values for <custom-ident> (since it reversed per spec)
  • Convertors (toPlainObject() and fromPlainObject()) moved to lib/convertor (entry point is require('css-tree/lib/convertor'))

Readme

Source

CSSTree logo

CSSTree

NPM version Build Status Coverage Status Join the CSSTree chat at https://gitter.im/csstree/csstree Twitter

The set of tools for working with CSS, including fast detailed parser (string->AST), walkers, generators (AST->string) and even lexer (validation and matching) based on knowledge of spec and browser implementations (see schema for details). The main goal to be efficient and W3C spec complient, with focus on analyzing and source-to-source processing.

Work in progress. The project in alpha stage since some parts need further experiments, AST format and API are subjects to change. However it's stable enough and used by packages like CSSO (CSS minifier) in production.

Docs and tools:

Related projects:

Install

> npm install css-tree

Usage

var csstree = require('css-tree');
var ast = csstree.parse('.example { world: "!" }');

csstree.walk(ast, function(node) {
    if (node.type === 'ClassSelector' && node.name === 'example') {
        node.name = 'hello';
    }
});

console.log(csstree.translate(ast));
// .hello{world:"!"}

Top level API

API map

License

MIT

Syntax matching uses mdn/data by Mozilla Contributors

Keywords

FAQs

Last updated on 08 Sep 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc