You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

postcss-selector-parser

Package Overview
Dependencies
Maintainers
4
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-selector-parser

> Selector parser with built in methods for working with selector strings.


Version published
Weekly downloads
36M
decreased by-1.11%
Maintainers
4
Install size
204 kB
Created
Weekly downloads
 

Package description

What is postcss-selector-parser?

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.

What are postcss-selector-parser's main functionalities?

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');

Other packages similar to postcss-selector-parser

Readme

Source

postcss-selector-parser test

Selector parser with built in methods for working with selector strings.

Install

With npm do:

npm install postcss-selector-parser

Quick Start

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.

API

Please see API.md.

Credits

  • Huge thanks to Andrey Sitnik (@ai) for work on PostCSS which helped accelerate this module's development.

License

MIT

FAQs

Package last updated on 11 Jul 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc