bo-selector: CSS Selector Parser
The `bo-selector` package provides a CSS selector parser capable of generating an Abstract Syntax Tree (AST) from a CSS selector string. It is built using `jison`, a parser generator, which is a development dependency, meaning the runtime package has no external dependencies. The package is currently at version 0.0.10 and has not seen updates since its last publish in October 2013, indicating it is an abandoned project. Its primary utility is to parse CSS selectors like `'p:has(.foo), b'` into a structured JavaScript object that represents the selector's components and relationships. It exclusively uses the CommonJS module system. Given its age and lack of maintenance, it is highly unlikely to support modern CSS selector features introduced after 2013, nor does it provide an ECMAScript Module (ESM) entry point, making integration into contemporary JavaScript projects challenging.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use `import { parser } from 'bo-selector';` in an ESM module context.fixUse the CommonJS `require()` syntax: `const parser = require('bo-selector').parser;` -
parser.parse is not a function
cause Incorrectly trying to call `bo-selector()` directly or a non-existent method.fixEnsure you are accessing the `parser` property from the required module: `const { parser } = require('bo-selector');` or `const boselector = require('bo-selector'); const parser = boselector.parser;`
Warnings
- breaking The `bo-selector` package is abandoned, with its last publish in October 2013. It is not maintained and will not receive updates for new CSS features, bug fixes, or security patches.
- gotcha This package is strictly CommonJS-only. Attempting to use `import` statements will result in errors in an ESM context.
- gotcha Due to its age, `bo-selector` does not support modern CSS selector syntax (e.g., `:is()`, `:where()`, `:has()` with complex arguments, new pseudo-classes/elements) introduced after CSS Selectors Level 3 (roughly 2011-2013).
Install
-
npm install bo-selector -
yarn add bo-selector -
pnpm add bo-selector
Imports
- parser
import { parser } from 'bo-selector';const parser = require('bo-selector').parser;
Quickstart
const parser = require('bo-selector').parser;
const util = require('util');
// Example CSS selector string
const cssSelector = 'p:has(.foo), b.bar[data-attr="value"]';
// Parse the CSS selector into an Abstract Syntax Tree (AST)
const ast = parser.parse(cssSelector);
// Log the generated AST in a human-readable format
console.log('Parsed AST:');
console.log(util.inspect(ast, { depth: null, colors: true }));
// Demonstrate accessing parts of the AST (simplified)
if (ast.type === 'selector_list' && ast.selectors.length > 0) {
const firstSelectorElement = ast.selectors[0].element;
console.log('\nFirst selector element name:', firstSelectorElement.name);
if (firstSelectorElement.constraints && firstSelectorElement.constraints.length > 0) {
console.log('First selector element constraints:', firstSelectorElement.constraints.map(c => c.type));
}
}