Gathering detailed insights and metrics for css-tree
Gathering detailed insights and metrics for css-tree
Gathering detailed insights and metrics for css-tree
Gathering detailed insights and metrics for css-tree
A tool set for CSS including fast detailed parser, walker, generator and lexer based on W3C specs and browser implementations
npm install css-tree
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,910 Stars
1,216 Commits
88 Forks
25 Watching
10 Branches
23 Contributors
Updated on 28 Nov 2024
JavaScript (99.52%)
CSS (0.34%)
HTML (0.14%)
Cumulative downloads
Total Downloads
Last day
-4.6%
5,903,281
Compared to previous day
Last week
2.2%
33,269,358
Compared to previous week
Last month
8.2%
140,723,339
Compared to previous month
Last year
19.7%
1,486,237,924
Compared to previous year
2
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.
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 built-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 and at-rules currently, but this feature will be extended to other parts of the CSS in the future.
Install with npm:
npm install css-tree
Basic usage:
1import * as csstree from 'css-tree'; 2 3// parse CSS to AST 4const ast = csstree.parse('.example { world: "!" }'); 5 6// traverse AST and modify it 7csstree.walk(ast, (node) => { 8 if (node.type === 'ClassSelector' && node.name === 'example') { 9 node.name = 'hello'; 10 } 11}); 12 13// generate CSS from AST 14console.log(csstree.generate(ast)); 15// .hello{world:"!"}
Syntax matching:
1// parse CSS to AST as a declaration value 2const ast = csstree.parse('red 1px solid', { context: 'value' }); 3 4// match to syntax of `border` property 5const matchResult = csstree.lexer.matchProperty('border', ast); 6 7// check first value node is a <color> 8console.log(matchResult.isType(ast.children.first, 'color')); 9// true 10 11// get a type list matched to a node 12console.log(matchResult.getTrace(ast.children.first)); 13// [ { type: 'Property', name: 'border' }, 14// { type: 'Type', name: 'color' }, 15// { type: 'Type', name: 'named-color' }, 16// { type: 'Keyword', name: 'red' } ]
Is it possible to import just a needed part of library like a parser or a walker. That's might useful for loading time or bundle size optimisations.
1import * as tokenizer from 'css-tree/tokenizer'; 2import * as parser from 'css-tree/parser'; 3import * as walker from 'css-tree/walker'; 4import * as lexer from 'css-tree/lexer'; 5import * as definitionSyntax from 'css-tree/definition-syntax'; 6import * as data from 'css-tree/definition-syntax-data'; 7import * as dataPatch from 'css-tree/definition-syntax-data-patch'; 8import * as utils from 'css-tree/utils';
Bundles are available for use in a browser:
dist/csstree.js
– minified IIFE with csstree
as global1<script src="node_modules/css-tree/dist/csstree.js"></script> 2<script> 3 csstree.parse('.example { color: green }'); 4</script>
dist/csstree.esm.js
– minified ES module1<script type="module"> 2 import { parse } from 'node_modules/css-tree/dist/csstree.esm.js' 3 parse('.example { color: green }'); 4</script>
One of CDN services like unpkg
or jsDelivr
can be used. By default (for short path) a ESM version is exposing. For IIFE version a full path to a bundle should be specified:
1<!-- ESM --> 2<script type="module"> 3 import * as csstree from 'https://cdn.jsdelivr.net/npm/css-tree'; 4 import * as csstree from 'https://unpkg.com/css-tree'; 5</script> 6 7<!-- IIFE with an export to global --> 8<script src="https://cdn.jsdelivr.net/npm/css-tree/dist/csstree.js"></script> 9<script src="https://unpkg.com/css-tree/dist/csstree.js"></script>
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
30 commit(s) and 21 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 5
Details
Reason
Found 3/23 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More