Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
css-selector-parser
Advanced tools
The css-selector-parser npm package is a utility for parsing CSS selectors into an abstract syntax tree (AST). This can be useful for various applications such as CSS preprocessing, validation, and transformation.
Parsing CSS Selectors
This feature allows you to parse a CSS selector string into an abstract syntax tree (AST). The example code demonstrates how to parse a complex CSS selector and output the resulting AST.
const { CssSelectorParser } = require('css-selector-parser');
const parser = new CssSelectorParser();
parser.registerNestingOperators('>', '+', '~');
parser.registerAttrEqualityMods('^', '$', '*', '~');
const ast = parser.parse('div.class[attr^="value"] > p');
console.log(JSON.stringify(ast, null, 2));
Customizing Parser Behavior
This feature allows you to customize the behavior of the parser by registering different nesting operators and attribute equality modifiers. The example code shows how to enable substitutes and register custom operators.
const { CssSelectorParser } = require('css-selector-parser');
const parser = new CssSelectorParser();
parser.registerNestingOperators('>', '+', '~');
parser.registerAttrEqualityMods('^', '$', '*', '~');
parser.enableSubstitutes();
const ast = parser.parse('div.class[attr^="value"] > p');
console.log(JSON.stringify(ast, null, 2));
Serializing AST to CSS Selector
This feature allows you to serialize an abstract syntax tree (AST) back into a CSS selector string. The example code demonstrates how to parse a CSS selector into an AST and then render it back to a string.
const { CssSelectorParser } = require('css-selector-parser');
const parser = new CssSelectorParser();
parser.registerNestingOperators('>', '+', '~');
parser.registerAttrEqualityMods('^', '$', '*', '~');
const ast = parser.parse('div.class[attr^="value"] > p');
const selector = parser.render(ast);
console.log(selector);
PostCSS Selector Parser is a tool for parsing, transforming, and stringifying CSS selectors. It is more feature-rich and integrates well with the PostCSS ecosystem, making it suitable for more complex CSS processing tasks.
css-what is a CSS selector parser that is part of the Cheerio library. It is designed to be fast and efficient, making it a good choice for applications that require high performance.
CSSTree is a tool for parsing, walking, and generating CSS. It provides a comprehensive set of features for working with CSS, including a detailed AST and various utilities for manipulating CSS. It is more comprehensive compared to css-selector-parser.
css1
: https://www.w3.org/TR/CSS1/css2
: https://www.w3.org/TR/CSS2/css3
/selectors-3
: https://www.w3.org/TR/selectors-3/selectors-4
: https://www.w3.org/TR/selectors-4/latest
: refers to selectors-4
progressive
: latest
+ accepts unknown psudo-classes, psudo-elements and attribute case sensitivity modifiersImportant: Migrating from 1.x.
npm install css-selector-parser
import {createParser} from 'css-selector-parser';
const parse = createParser();
const selector = parse('a[href^="/"], .container:has(nav) > a[href]:nth-child(2)');
console.log(selector);
Produces:
({
type: 'Selector',
rules: [
{
type: 'Rule',
tag: { type: 'TagName', name: 'a' },
attributes: [
{
type: 'Attribute',
name: 'href',
operator: '^=',
value: { type: 'String', value: '/' }
}
]
},
{
type: 'Rule',
classNames: [ 'container' ],
pseudoClasses: [
{
type: 'PseudoClass',
name: 'has',
argument: {
type: 'Selector',
rules: [ { type: 'Rule', tag: { type: 'TagName', name: 'nav' } } ]
}
}
],
nestedRule: {
type: 'Rule',
combinator: '>',
tag: { type: 'TagName', name: 'a' },
attributes: [ { type: 'Attribute', name: 'href' } ],
pseudoClasses: [
{
type: 'PseudoClass',
name: 'nth-child',
argument: { type: 'Formula', a: 0, b: 2 }
}
]
}
}
]
})
import {ast, render} from 'css-selector-parser';
const selector = ast.selector({
rules: [
ast.rule({
tag: ast.tagName({name: 'a'}),
attributes: [
ast.attribute({name: 'href', operator: '^=', value: ast.string({value: '/'})})
]
}),
ast.rule({
classNames: ['container'],
pseudoClasses: [
ast.pseudoClass({
name: 'has',
argument: ast.selector({
rules: [
ast.rule({tag: ast.tagName({name: 'nav'})})
]
})
})
],
nestedRule: ast.rule({
combinator: '>',
tag: ast.tagName({name: 'a'}),
attributes: [ast.attribute({name: 'href'})],
pseudoClasses: [
ast.pseudoClass({
name: 'nth-child',
argument: ast.formula({a: 0, b: 2})
})
],
pseudoElement: 'before'
})
})
]
});
console.log(render(selector)); // a[href^="/"], .container:has(nav) > a[href]:nth-child(2)::before
MIT
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
FAQs
Powerful and compliant CSS selector parser.
The npm package css-selector-parser receives a total of 848,107 weekly downloads. As such, css-selector-parser popularity was classified as popular.
We found that css-selector-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.