`hast-util-parse-selector` is a focused utility within the `unified` ecosystem designed to create HAST (Hypertext Abstract Syntax Tree) element nodes from simple CSS selector strings. Currently at stable version 4.0.0, this package follows semantic versioning, with major releases typically aligning with dropping support for unmaintained Node.js versions. It processes basic selectors that can include a tag name, multiple class names, and a single ID, transforming them into a `hast` element object with corresponding `tagName` and `properties` (including `id` and `className` arrays). While powerful for its specific use case, it explicitly states its niche nature, recommending more comprehensive alternatives like `hastscript` or `hast-util-from-selector` for handling complex CSS selectors. Its primary differentiator is its simplicity and directness in parsing straightforward selectors into HAST nodes, making it suitable for scenarios where a full-blown selector engine is overkill.
npm install hast-util-parse-selectorVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to create `hast` element nodes using various simple CSS selectors and `defaultTagName` options, including class names, IDs, and custom tag fallbacks.
Upgrade your Node.js environment to version 16 or higher. Alternatively, use `hast-util-parse-selector@^3` if you must remain on an older Node.js version.
Update your TypeScript dependency to a newer, supported version (e.g., TS 4.2+). The package is fully typed and expects a modern TypeScript environment.
Always import directly from the package name (`'hast-util-parse-selector'`). Avoid referencing internal paths like `'hast-util-parse-selector/lib/some-module.js'`.
Migrate your project to use ES Modules (`import ... from '...'`) or ensure your build system correctly transpiles ESM for CommonJS environments. Avoid `require()` for this package.
Never pass unsanitized user input directly to `parseSelector`. If user input is necessary, thoroughly sanitize it using a library like `hast-util-sanitize` before processing.
Change `const { parseSelector } = require('hast-util-parse-selector');` to `import { parseSelector } from 'hast-util-parse-selector';`. Ensure your project's `package.json` has `"type": "module"` or that files are `.mjs`.Verify your import statement is `import { parseSelector } from 'hast-util-parse-selector';` and that your environment supports ESM. If using a bundler, ensure it's configured for ESM.Ensure your `tsconfig.json` includes `"moduleResolution": "bundler"` (for modern bundlers) or `"node"` and `"allowSyntheticDefaultImports": true`. Also, update your `typescript` and potentially `@types/hast` dependencies to match the package's requirements (TS 4.2+ for v4.0.0).
No dependency data recorded yet.