Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
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.
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 }));
PostCSS is a tool for transforming CSS with JavaScript plugins. It can do similar tasks as css-tree, such as parsing, walking the AST, and generating CSS. PostCSS is plugin-based, which makes it more extensible and allows for a wide range of transformations.
Sass is a preprocessor scripting language that is interpreted or compiled into CSS. It offers more syntactic features compared to css-tree, such as variables, nesting, and mixins, but it is not primarily focused on parsing and manipulating existing CSS.
Less is another CSS pre-processor, similar to Sass, that extends the capabilities of CSS with dynamic behavior such as variables, mixins, operations, and functions. Less and css-tree serve different purposes, with Less focusing on writing CSS in a more functional way and css-tree on parsing and manipulation.
clean-css is a fast and efficient CSS optimizer for Node.js and the browser. It focuses on minification, which is one of the features of css-tree, but does not provide a general-purpose CSS parsing and manipulation API.
Fast detailed CSS parser
Work in progress. Project in alpha stage since AST format is subject to change.
Docs and tools:
Related projects:
> npm install css-tree
var csstree = require('css-tree');
var ast = csstree.parse('.example { world: "!" }');
csstree.walk(ast, function(node) {
if (node.type === 'Class' && node.name === 'example') {
node.name = 'hello';
}
});
console.log(csstree.translate(ast));
// .hello{world:"!"}
MIT
Syntax matching use mdn/data by Mozilla Contributors
1.0.0-alpha15 (February 8, 2017)
atruleExpression
contextkeyword()
and property()
property()
to not lowercase custom property namesvariable
boolean flag in property()
resultscanner
into tokenizer
syntax
into lexer
docs/*.html
files to csstree/docs repoelement()
function for Value
context (-moz-element()
supported as well)Universal
node type into Type
Id
-> IdSelector
Class
-> ClassSelector
Type
-> TypeSelector
Attribute
-> AttributeSelector
PseudoClass
-> PseudoClassSelector
PseudoElement
-> PseudoElementSelector
Hash
-> HexColor
Space
-> WhiteSpace
An+B
-> AnPlusB
Progid
node typeMediaQuery
consumer to not validate syntax on parse and to include whitespaces in children sequence as isWhiteSpace.value
property to store whitespace sequencefalse
some part of CSS represents as balanced Raw
):
parseAtruleExpression
– to parse at-rule expressions (true
by default)parseSelector
– to parse rule's selector (true
by default)parseValue
- to parse declaration's value (true
by default)parseCustomProperty
– to parse value and fallback of custom property (false
by default)FAQs
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 npm package css-tree receives a total of 25,228,175 weekly downloads. As such, css-tree popularity was classified as popular.
We found that css-tree demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.