postcss-selector-parser – CSS selector parser

postcss-selector-parser is a library for parsing and manipulating CSS selectors. It converts selector strings into an abstract syntax tree (AST), allowing developers to inspect, modify, and reconstruct selectors without manually processing their individual characters. The library is commonly used in stylesheet transformation tools, including applications built around the PostCSS ecosystem.

CSS selectors can contain complex combinations of element names, classes, IDs, attributes, pseudo-classes, and combinators. Working with these structures as plain text can be difficult, particularly when modifying selectors while preserving their original meaning. postcss-selector-parser provides an object-based interface that exposes these components individually, making it possible to perform targeted transformations and generate updated selector strings.

This is free and open source software.

Key Features

  • Parse CSS selector strings into an editable abstract syntax tree.
  • Traverse selector nodes using visitor methods for different selector types.
  • Inspect and modify class, ID, element, attribute, and pseudo-class selectors.
  • Support for combinators, nested selectors, and CSS nesting syntax.
  • Construct selector nodes programmatically and insert them into existing trees.
  • Remove, replace, and rearrange selector components.
  • Serialize modified selector trees back into CSS strings.
  • Preserve original whitespace and comments when processing selectors.
  • Optional whitespace normalization for producing compact selector strings.
  • Automatic escaping of characters that are invalid in CSS identifiers.
  • Synchronous and asynchronous selector-processing interfaces.
  • Configurable input-length and nesting-depth limits for processing untrusted selectors.
  • Integration with PostCSS without requiring PostCSS as a dependency.

Website: github.com/postcss/postcss-selector-parser
Support:
Developer: PostCSS contributors
License: MIT License

postcss-selector-parser is written in JavaScript. Learn JavaScript with our recommended free books and free tutorials.


Related Software

CSS Parsers
PostCSSTransform styles with JavaScript plugins
Lightning CSSParser, transformer, bundler, and minifier
ReworkPlugin framework for CSS preprocessing
PHP CSS ParserPHP library for reading CSS
MenschNon-validating CSS parser
CSSTreeTool set for CSS
StylisLightweight CSS preprocessor
tinycss2CSS parser and generator
ParseGo library offering lexers, parsers, and parsing utilities
LibCSSCSS parser and selection engine
Ruby CSS ParserLoad, parse and cascade CSS rule sets
ParserLibCSS3 SAX-inspired parser
css-parserFork of the cssutils project
ph-cssParse, represent, modify, and write CSS stylesheets
StylecowModern CSS to all browsers
Gonzales PECSS parser which plays nicely with preprocessors
CSSOMCSS parser written in pure JavaScript

Read our verdict in the software roundup.


Best Free and Open Source Software LinuxLinks helps you discover the best free and open source software for Linux. Explore our extensive collection of recommended applications, including alternatives to popular proprietary software.

We also publish software reviews, Linux tutorials, hardware reviews, free programming books and articles covering the wider open source community. Browse our Linux resources to discover more.

Know a useful free and open source Linux application we haven't covered? Suggest a program for inclusion.

Has your software been featured on LinuxLinks? Display one of our Featured on LinuxLinks badges on your website or GitHub README.
Subscribe

Please read our Comment Policy before commenting.

Notify of
guest
0 Comments
Oldest
Newest Most Voted