HAST Utility to Create Nodes from CSS Selectors

3.0.1 · active · verified Sun Apr 19

hast-util-from-selector is a utility package within the unified ecosystem designed to parse CSS selectors and generate corresponding HAST (Hypertext Abstract Syntax Tree) element nodes. It is currently at version 3.0.1, supporting Node.js 16 and later, and adheres to an ESM-only distribution model. The project follows a release cadence tied to Node.js LTS versions, with major versions introducing breaking changes aligned with Node.js support lifecycles or significant internal architectural shifts, such as the move to ESM in v2 and Node.js 16 requirement in v3. This package distinguishes itself from `hast-util-parse-selector` by offering more powerful and complex selector parsing capabilities, akin to `hastscript` but specifically focused on creating HAST elements from selector strings rather than a full hyperscript API. It's an active project, receiving regular updates for dependencies and type refinements.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing a complex CSS selector string into a HAST element tree.

import { fromSelector } from 'hast-util-from-selector';

const selectorString = 'p svg[viewbox="0 0 10 10"] circle[cx=10][cy=10][r=10]';
const hastNode = fromSelector(selectorString);

console.log(JSON.stringify(hastNode, null, 2));
/*
Yields:
{
  "type": "element",
  "tagName": "p",
  "properties": {},
  "children": [
    {
      "type": "element",
      "tagName": "svg",
      "properties": {
        "viewBox": "0 0 10 10"
      },
      "children": [
        {
          "type": "element",
          "tagName": "circle",
          "properties": {
            "cx": "10",
            "cy": "10",
            "r": "10"
          },
          "children": []
        }
      ]
    }
  ]
}
*/

view raw JSON →