Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
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
var parser = require('postcss-selector-parser');
var transform = function (selectors) {
selectors.eachInside(function (selector) {
// do something with the selector
console.log(String(selector))
});
};
var transformed = parser(transform).process('h1, h2, h3').result;
Please see API.md.
MIT
2.0.0
This release contains the following breaking changes:
eachInside
iterators to walk
. For example, eachTag
is now
walkTags
, and eachInside
is now walk
.Node#removeSelf()
to Node#remove()
.Container#remove()
to Container#removeChild()
.Node#raw
to Node#raws
(thanks to @davidtheclark).&
as the nesting selector, rather than a tag selector.#{foo}
) as an
id selector (thanks to @davidtheclark).and;
[data-attr="foo=bar"]
) (thanks to @montmanu).quoted
and raw.unquoted
properties to attribute nodes
(thanks to @davidtheclark).FAQs
> Selector parser with built in methods for working with selector strings.
The npm package postcss-selector-parser receives a total of 31,169,996 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
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.