css-what

a CSS selector parser

BSD-2-Clause 29 个版本
安装
npm install css-what
yarn add css-what
pnpm add css-what
bun add css-what
README

css-what

Node.js CI Coverage

A CSS selector parser.

Example

import * as CSSwhat from "css-what";

CSSwhat.parse("foo[bar]:baz")

~> [
    [
        { type: "tag", name: "foo" },
        {
            type: "attribute",
            name: "bar",
            action: "exists",
            value: "",
            ignoreCase: null
        },
        { type: "pseudo", name: "baz", data: null }
    ]
]

API

CSSwhat.parse(selector) - Parses selector.

The function returns a two-dimensional array. The first array represents selectors separated by commas (eg. sub1, sub2), the second contains the relevant tokens for that selector. Possible token types are:

name properties example output
tag name div { type: 'tag', name: 'div' }
universal - * { type: 'universal' }
pseudo name, data :name(data) { type: 'pseudo', name: 'name', data: 'data' }
pseudo name, data :name { type: 'pseudo', name: 'name', data: null }
pseudo-element name ::name { type: 'pseudo-element', name: 'name' }
attribute name, action, value, ignoreCase [attr] { type: 'attribute', name: 'attr', action: 'exists', value: '', ignoreCase: false }
attribute name, action, value, ignoreCase [attr=val] { type: 'attribute', name: 'attr', action: 'equals', value: 'val', ignoreCase: false }
attribute name, action, value, ignoreCase [attr^=val] { type: 'attribute', name: 'attr', action: 'start', value: 'val', ignoreCase: false }
attribute name, action, value, ignoreCase [attr$=val] { type: 'attribute', name: 'attr', action: 'end', value: 'val', ignoreCase: false }
child - > { type: 'child' }
parent - < { type: 'parent' }
sibling - ~ { type: 'sibling' }
adjacent - + { type: 'adjacent' }
descendant - { type: 'descendant' }
column-combinator - || { type: 'column-combinator' }

CSSwhat.stringify(selector) - Turns selector back into a string.


License: BSD-2-Clause

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

版本列表
8.0.0 2026-03-17
7.0.0 2025-06-29
6.2.2 2025-06-28
6.2.1 2025-06-28
6.2.0 2025-06-28
6.1.0 2022-03-31
6.0.1 2021-12-27
6.0.0 2021-12-26
5.1.0 2021-10-10
5.0.1 2021-05-28
5.0.0 2021-03-09
4.0.0 2020-10-13
3.4.2 2020-10-09
3.4.1 2020-10-01
3.4.0 2020-10-01
3.3.0 2020-05-31
3.2.1 2019-11-09
3.2.0 2019-08-02
3.1.0 2019-07-23
3.0.2 2019-07-13
3.0.1 2019-07-13
3.0.0 2019-07-13
2.1.3 2019-02-14
2.1.2 2018-10-21
2.1.0 2015-11-22
2.0.2 2015-03-12
2.0.1 2015-03-05
2.0.0 2015-03-01
1.0.0 2015-02-03