{"name":"css-selector-parser","version":"3.2.0","keywords":["css","css selector","css selector parser","pseudo-classes","pseudo-elements","css attributes","css tags","css classes"],"author":{"name":"Marat Dulin","email":"mdevils@yandex.ru"},"license":"MIT","_id":"css-selector-parser@3.2.0","maintainers":[{"name":"mdevils","email":"npmjs@maratd.com"}],"homepage":"https://github.com/mdevils/css-selector-parser#readme","bugs":{"url":"https://github.com/mdevils/css-selector-parser/issues"},"dist":{"shasum":"ca18669ea5e9e1ea4187df627c0f8b29632e7eb1","tarball":"http://registry-npm.ywwl.com/css-selector-parser/download/css-selector-parser-3.2.0.tgz","fileCount":37,"integrity":"sha512-L1bdkNKUP5WYxiW5dW6vA2hd3sL8BdRNLy2FCX0rLVise4eNw9nBdeBuJHxlELieSE2H1f6bYQFfwVUwWCV9rQ==","signatures":[{"sig":"MEUCIBxUbDcIxgVYb9JlMDE21hfNFW7IoKT5swAXB9EYJP4aAiEAxBZKoXYLB2igFdxq9U3M8QH3GbisyhxPHA7OR4KWTDk=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":182783,"size":22635,"noattachment":false,"key":"/css-selector-parser/-/css-selector-parser-3.2.0.tgz"},"main":"./dist/cjs/index.js","husky":{"hooks":{"commit-msg":"commitlint -E HUSKY_GIT_PARAMS","pre-commit":"npm run lint && npm run test"}},"types":"./dist/cjs/index","module":"./dist/mjs/index.js","exports":{".":{"import":"./dist/mjs/index.js","require":"./dist/cjs/index.js"}},"funding":[{"url":"https://github.com/sponsors/mdevils","type":"github"},{"url":"https://patreon.com/mdevils","type":"patreon"}],"gitHead":"f04297f6a9dada7590052df3d82ef475a97806f1","scripts":{"lint":"eslint {src,test,benchmark}/**.ts","test":"jest test","build":"rm -Rf dist && tsc -p tsconfig.json && tsc -p tsconfig.mjs.json && echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json && echo '{\"type\": \"module\"}' > dist/mjs/package.json","release":"standard-version","lint:fix":"eslint --fix {src,test,benchmark}/**.ts","benchmark":"ts-node benchmark/benchmark.ts","test:dist":"TEST_DIST=cjs npm run test","build:docs":"rm -Rf docs && typedoc --excludeInternal --excludeExternals --disableSources --plugin typedoc-plugin-markdown --entryDocument ../README.md --out docs src/index.ts && ts-node tools/cleanup-docs.ts","test:watch":"jest --watch test","prepublishOnly":"npm run build"},"typings":"./dist/cjs/index","_npmUser":{"name":"mdevils","email":"npmjs@maratd.com"},"commitlint":{"rules":{"subject-case":[2,"never",["start-case","pascal-case"]]},"extends":["@commitlint/config-conventional"]},"repository":{"url":"git+https://github.com/mdevils/css-selector-parser.git","type":"git"},"_npmVersion":"11.6.2","description":"Powerful and compliant CSS selector parser.","directories":{},"sideEffects":false,"_nodeVersion":"25.1.0","_hasShrinkwrap":false,"devDependencies":{"jest":"^29.5.0","husky":"^4.3.8","eslint":"^8.43.0","ts-jest":"^29.1.0","ts-node":"^10.9.1","typedoc":"^0.24.8","prettier":"^3.2.5","typescript":"^4.9.0","@types/jest":"^29.5.2","@commitlint/cli":"^17.6.6","standard-version":"^9.5.0","eslint-plugin-import":"^2.27.5","eslint-config-prettier":"^8.8.0","eslint-plugin-prettier":"^5.1.3","typedoc-plugin-markdown":"^3.15.3","@typescript-eslint/parser":"^7.1.0","jest-ts-webcompat-resolver":"^1.0.0","@commitlint/config-conventional":"^17.6.6","@typescript-eslint/eslint-plugin":"^7.1.0"},"_npmOperationalInternal":{"tmp":"tmp/css-selector-parser_3.2.0_1763402235447_0.9790513920959021","host":"s3://npm-registry-packages-npm-production"},"readme":"# css-selector-parser\n\n[![npm](https://img.shields.io/npm/v/css-selector-parser)](https://www.npmjs.com/package/css-selector-parser)\n[![npm bundle size](https://img.shields.io/bundlephobia/minzip/css-selector-parser)](https://bundlephobia.com/package/css-selector-parser)\n[![NPM License](https://img.shields.io/npm/l/css-selector-parser)](https://github.com/mdevils/css-selector-parser/blob/master/LICENSE)\n[![GitHub stars](https://img.shields.io/github/stars/mdevils/css-selector-parser)](https://github.com/mdevils/css-selector-parser/stargazers)\n\nA high-performance CSS selector parser with advanced features for modern web development.\n\n## Features\n\n- 🚀 **Fast and memory-efficient** parsing for all CSS selectors\n- 🌳 **AST-based** object model for programmatic manipulation\n- 🚶 **AST traversal** with visitor pattern for analyzing and transforming selectors\n- 📊 **Full compliance** with all CSS selector specifications\n- 🧪 **Comprehensive test coverage**\n- 📚 **Well-documented API** with TypeScript support\n- 🔄 **Two-way conversion** between CSS selectors and AST\n- 🧩 **Modular support** for various CSS specifications\n- 🎮 **[Interactive Playground](https://mdevils.github.io/css-selector-parser/)** - Try it in your browser!\n\n## Playground\n\n**[🎮 Launch Interactive Playground](https://mdevils.github.io/css-selector-parser/)**\n\nTest CSS selectors in your browser with syntax highlighting, real-time AST visualization, and configurable parser options.\n\n## Supported CSS Selector Standards\n\n- `css1`: [W3C CSS1 Specification](https://www.w3.org/TR/CSS1/)\n- `css2`: [W3C CSS2 Specification](https://www.w3.org/TR/CSS2/)\n- `css3`/`selectors-3`: [W3C Selectors Level 3](https://www.w3.org/TR/selectors-3/)\n- `selectors-4`: [W3C Selectors Level 4](https://www.w3.org/TR/selectors-4/)\n- `latest`: refers to `selectors-4`\n- `progressive`: `latest` + accepts unknown pseudo-classes, pseudo-elements and attribute case sensitivity modifiers\n\n## Migration Guides\n\n- [Migrating from 1.x to 3.x](CHANGELOG.md#migrating-from-1x-to-3x)\n- [Migrating from 2.x to 3.x](CHANGELOG.md#migrating-from-2x-to-3x)\n- [Migrating from 1.x to 2.x](CHANGELOG.md#220)\n\nSee [Changelog](CHANGELOG.md) for release details.\n\n## Installation\n\n```bash\nnpm install css-selector-parser\n# or\nyarn add css-selector-parser\n# or\npnpm add css-selector-parser\n```\n\n## Usage\n\n### Parsing Selectors\n\n```javascript\nimport { createParser } from 'css-selector-parser';\n\nconst parse = createParser();\nconst selector = parse('a[href^=\"/\"], .container:has(nav) > a[href]:nth-child(2)::before');\n\nconsole.log(selector);\n```\n\nThis produces an AST (Abstract Syntax Tree) output:\n\n```javascript\n({\n    type: 'Selector',\n    rules: [\n        {\n            type: 'Rule',\n            items: [\n                { type: 'TagName', name: 'a' },\n                {\n                    type: 'Attribute',\n                    name: 'href',\n                    operator: '^=',\n                    value: { type: 'String', value: '/' }\n                }\n            ]\n        },\n        {\n            type: 'Rule',\n            items: [\n                { type: 'ClassName', name: 'container' },\n                {\n                    type: 'PseudoClass',\n                    name: 'has',\n                    argument: {\n                        type: 'Selector',\n                        rules: [\n                            {\n                                type: 'Rule',\n                                items: [ { type: 'TagName', name: 'nav' } ]\n                            }\n                        ]\n                    }\n                }\n            ],\n            nestedRule: {\n                type: 'Rule',\n                items: [\n                    { type: 'TagName', name: 'a' },\n                    { type: 'Attribute', name: 'href' },\n                    {\n                        type: 'PseudoClass',\n                        name: 'nth-child',\n                        argument: { type: 'Formula', a: 0, b: 2 }\n                    },\n                    {\n                        type: 'PseudoElement',\n                        name: 'before'\n                    }\n                ],\n                combinator: '>'\n            }\n        }\n    ]\n})\n```\n\n### Building and Rendering Selectors\n\n```javascript\nimport { ast, render } from 'css-selector-parser';\n\nconst selector = ast.selector({\n    rules: [\n        ast.rule({\n            items: [\n                ast.tagName({name: 'a'}),\n                ast.attribute({name: 'href', operator: '^=', value: ast.string({value: '/'})})\n            ]\n        }),\n        ast.rule({\n            items: [\n                ast.className({name: 'container'}),\n                ast.pseudoClass({\n                    name: 'has',\n                    argument: ast.selector({\n                        rules: [ast.rule({items: [ast.tagName({name: 'nav'})]})]\n                    })\n                })\n            ],\n            nestedRule: ast.rule({\n                combinator: '>',\n                items: [\n                    ast.tagName({name: 'a'}),\n                    ast.attribute({name: 'href'}),\n                    ast.pseudoClass({\n                        name: 'nth-child',\n                        argument: ast.formula({a: 0, b: 2})\n                    }),\n                    ast.pseudoElement({name: 'before'})\n                ]\n            })\n        })\n    ]\n});\n\nconsole.log(render(selector)); // a[href^=\"/\"], .container:has(nav) > a[href]:nth-child(2)::before\n```\n\n### Traversing the AST\n\nThe `traverse` function allows you to walk through the AST and visit each node, making it easy to analyze or transform selectors.\n\n```javascript\nimport { createParser, traverse } from 'css-selector-parser';\n\nconst parse = createParser();\nconst selector = parse('div.foo > span#bar:hover::before');\n\n// Simple visitor function - called for each node\ntraverse(selector, (node, context) => {\n    console.log(node.type, context.parents.length);\n});\n\n// Visitor with enter/exit hooks\ntraverse(selector, {\n    enter(node, context) {\n        console.log('Entering:', node.type);\n        if (node.type === 'ClassName') {\n            console.log('Found class:', node.name);\n        }\n    },\n    exit(node, context) {\n        console.log('Leaving:', node.type);\n    }\n});\n\n// Skip visiting children of specific nodes\ntraverse(selector, (node) => {\n    if (node.type === 'PseudoClass') {\n        // Don't visit children of pseudo-classes\n        return false;\n    }\n});\n\n// Practical example: collect all class names\nconst classNames = [];\ntraverse(selector, (node) => {\n    if (node.type === 'ClassName') {\n        classNames.push(node.name);\n    }\n});\nconsole.log(classNames); // ['foo']\n\n// Access parent information\ntraverse(selector, (node, context) => {\n    console.log({\n        type: node.type,\n        parent: context.parent?.type,\n        depth: context.parents.length,\n        key: context.key,\n        index: context.index\n    });\n});\n```\n\nThe traversal context provides:\n- `node`: The current AST node being visited\n- `parent`: The parent node (undefined for root)\n- `parents`: Array of all ancestor nodes from root to current\n- `key`: Property name in parent that references this node\n- `index`: Array index if this node is in an array\n\n## CSS Modules Support\n\nCSS Modules are specifications that add new selectors or modify existing ones. This parser supports various CSS modules that can be included in your syntax definition:\n\n```javascript\nimport { createParser } from 'css-selector-parser';\n\n// Create a parser with specific CSS modules enabled\nconst parse = createParser({\n    syntax: 'selectors-4',\n    modules: ['css-position-3', 'css-scoping-1']\n});\n```\n\n### Supported CSS Modules\n\n| Module | Description |\n|--------|-------------|\n| `css-position-1/2/3/4` | Position-related pseudo-classes |\n| `css-scoping-1` | Shadow DOM selectors (`:host`, `:host-context()`, `::slotted()`) |\n| `css-pseudo-4` | Modern pseudo-elements (`::selection`, `::backdrop`, etc.) |\n| `css-shadow-parts-1` | `::part()` for styling shadow DOM components |\n| `css-nesting-1` | CSS Nesting selector (`&`) |\n\nThe `latest` syntax automatically includes all modules marked as current specifications.\n\n## API Documentation\n\n- [Complete API Documentation](docs/modules.md)\n- [Parsing CSS Selectors](docs/modules.md#createParser)\n- [Constructing CSS AST](docs/modules.md#ast)\n- [Rendering CSS AST](docs/modules.md#render)\n- [Traversing CSS AST](docs/modules.md#traverse)\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nMIT\n\n## Security Contact\n\nTo report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.\n\n## Sponsorship\n\nIf you find this project useful, please consider [sponsoring the developer](https://github.com/sponsors/mdevils) or [supporting on Patreon](https://patreon.com/mdevils).\n","publish_time":1763402235650,"_cnpm_publish_time":1763402235650,"dist-tags":{"latest":"3.3.0"}}