css-selector-parser is a high-performance JavaScript/TypeScript library designed for parsing CSS selectors into a comprehensive Abstract Syntax Tree (AST). It provides an AST-based object model that enables programmatic manipulation, analysis, and transformation of selectors through its visitor pattern implementation. The library offers full compliance with a wide array of CSS selector specifications, including CSS1, CSS2, CSS3, and Selectors Level 4, with an additional "progressive" mode for handling unknown or future pseudo-classes and attributes. Currently at version 3.3.0, the package suggests an active development cycle, evidenced by its multiple migration guides for major versions. Key differentiators include its speed, memory efficiency, extensive TypeScript support for a well-documented API, and an interactive playground for real-time testing and visualization of AST outputs.
npm install css-selector-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates parsing a complex CSS selector into an AST and then traversing the AST to log specific node types like TagName and Attribute, showcasing basic library usage.
Review the 'Migrating from 1.x to 3.x' and 'Migrating from 2.x to 3.x' sections in the `CHANGELOG.md` file to adapt your codebase to the new API and AST structure.
Configure your project to use ES Modules by adding `"type": "module"` to your `package.json` or by using `.mjs` file extensions for ESM files. Use `import` statements as shown in the documentation.
For strict CSS compliance, explicitly specify a W3C standard (e.g., `css: 'selectors-4'`) when creating the parser: `createParser({ css: 'selectors-4' })`. Only use `progressive` if you intend to support experimental or custom syntax.Ensure your project is configured for ES Modules by adding `"type": "module"` to your `package.json`, or rename the file to `.mjs`. If you must use CommonJS, consider dynamic `import()` or transpile your code.
Ensure your project is configured for ES Modules and use `import { createParser } from 'css-selector-parser';`. If you need to use CommonJS, consider using dynamic `import()`: `const { createParser } = await import('css-selector-parser');` (requires Node.js 14+).Review the problematic selector for syntax errors against W3C specifications. If the selector uses modern or experimental features, try configuring the parser with a higher compliance level or the `progressive` option: `createParser({ css: 'selectors-4' })` or `createParser({ css: 'progressive' })`.No dependency data recorded yet.