
Security News
Django Joins curl in Pushing Back on AI Slop Security Reports
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
postcss-selector-parser
Advanced tools
> Selector parser with built in methods for working with selector strings.
The postcss-selector-parser npm package is a parser that helps in transforming CSS selectors. It provides a rich API to analyze and manipulate CSS selectors programmatically. This package is particularly useful for tasks related to CSS processing, such as linting, optimization, and custom transformations.
Parsing and transforming selectors
This feature allows for the parsing and transformation of CSS selectors. In the provided code sample, all 'h1' tags in a selector are changed to 'h2' tags.
const parser = require('postcss-selector-parser');
const transform = selectors => {
selectors.walkTags(tag => {
if (tag.value === 'h1') {
tag.value = 'h2';
}
});
};
const transformed = parser(transform).processSync('h1.class');
Extracting classes from selectors
This feature demonstrates how to extract class names from a selector. The code sample extracts 'class1' and 'class2' from the selector string '.class1.class2'.
const parser = require('postcss-selector-parser');
const extractClasses = selector => {
const classes = [];
parser(selectors => {
selectors.walkClasses(classNode => {
classes.push(classNode.value);
});
}).processSync(selector);
return classes;
};
const classes = extractClasses('.class1.class2');
Working with pseudo classes
This feature focuses on manipulating pseudo classes within selectors. In the example, ':hover' pseudo classes are replaced with ':focus'.
const parser = require('postcss-selector-parser');
const transformPseudo = selector => {
return parser(selectors => {
selectors.walkPseudos(pseudo => {
if (pseudo.value === ':hover') {
pseudo.value = ':focus';
}
});
}).processSync(selector);
};
const result = transformPseudo('a:hover');
css-what is a CSS selector parser that can parse selectors into an understandable format but does not offer the same level of manipulation and transformation capabilities as postcss-selector-parser.
css-selector-tokenizer can tokenize and parse CSS selectors. It provides a different API and approach compared to postcss-selector-parser, focusing more on the tokenization aspect rather than direct manipulation.
scss-parser is designed to parse SCSS syntax. While it can handle selectors within the SCSS syntax, its primary focus is broader than just selectors, making it less specialized compared to postcss-selector-parser.
Selector parser with built in methods for working with selector strings.
With npm do:
npm install postcss-selector-parser
const parser = require('postcss-selector-parser');
const transform = selectors => {
selectors.walk(selector => {
// do something with the selector
console.log(String(selector))
});
};
const transformed = parser(transform).processSync('h1, h2, h3');
To normalize selector whitespace:
const parser = require('postcss-selector-parser');
const normalized = parser().processSync('h1, h2, h3', {lossless: false});
// -> h1,h2,h3
Async support is provided through parser.process
and will resolve a Promise
with the resulting selector string.
Please see API.md.
MIT
5.0.0-rc.0
This release has BREAKING CHANGES that were required to fix regressions in 4.0.0 and to make the Combinator Node API consistent for all combinator types. Please read carefully.
.a .b
) is stored in the AST has changed..a /for/ .b
) are now properly parsed as a combinator./
was encountered have been fixed.v6.0.0
.In prior releases, the value of a descendant combinator with multiple spaces included all the spaces.
.a .b
: Extra spaces are now stored as space before.
combinator.value === " "
combinator.value === " " && combinator.spaces.before === " "
.a /*comment*/.b
: A comment at the end of the combinator causes extra space to become after space.
combinator.value === " "
combinator.raws.value === " /*comment/"
combinator.value === " "
combinator.spaces.after === " "
combinator.raws.spaces.after === " /*comment*/"
.a<newline>.b
: whitespace that doesn't start or end with a single space character is stored as a raw value.
combinator.value === "\n"
combinator.raws.value === undefined
combinator.value === " "
combinator.raws.value === "\n"
Although, nonstandard and unlikely to ever become a standard, combinators like /deep/
and /for/
are now properly supported.
Because they've been taken off the standardization track, there is no spec-official name for combinators of the form /<ident>/
. However, I talked to Tab Atkins and we agreed to call them "named combinators" so now they are called that.
Before this release such named combinators were parsed without intention and generated three nodes of type "tag"
where the first and last nodes had a value of "/"
.
.a /for/ .b
is parsed as a combinator.
root.nodes[0].nodes[1].type === "tag"
root.nodes[0].nodes[1].value === "/"
root.nodes[0].nodes[1].type === "combinator"
root.nodes[0].nodes[1].value === "/for/"
.a /F\6fR/ .b
escapes are handled and uppercase is normalized.
root.nodes[0].nodes[2].type === "tag"
root.nodes[0].nodes[2].value === "F\\6fR"
root.nodes[0].nodes[1].type === "combinator"
root.nodes[0].nodes[1].value === "/for/"
root.nodes[0].nodes[1].raws.value === "/F\\6fR/"
A new API was added to look up a node based on the source location.
const selectorParser = require("postcss-selector-parser");
// You can find the most specific node for any given character
let combinator = selectorParser.astSync(".a > .b").atPosition(1,4);
combinator.toString() === " > ";
// You can check if a node includes a specific character
// Whitespace surrounding the node that is owned by that node
// is included in the check.
[2,3,4,5,6].map(column => combinator.isAtPosition(1, column));
// => [false, true, true, true, false]
FAQs
> Selector parser with built in methods for working with selector strings.
The npm package postcss-selector-parser receives a total of 66,471,971 weekly downloads. As such, postcss-selector-parser popularity was classified as popular.
We found that postcss-selector-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 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
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.