blob: 0c4a198c2f95735f733bfa43cc76d940fe87ee71 [file] [log] [blame] [view]
Nikolay Vitkov10371982025-02-03 17:08:071# css-what
Tim van der Lippe706ec962021-06-04 12:24:422
Nikolay Vitkov10371982025-02-03 17:08:073[![Build Status](https://img.shields.io/github/workflow/status/fb55/css-what/Node.js%20CI/master)](https://github.com/fb55/css-what/actions/workflows/nodejs-test.yml)
4[![Coverage](https://img.shields.io/coveralls/github/fb55/css-what/master)](https://coveralls.io/github/fb55/css-what?branch=master)
5
6A CSS selector parser.
Tim van der Lippe706ec962021-06-04 12:24:427
8## Example
9
10```js
Nikolay Vitkov10371982025-02-03 17:08:0711import * as CSSwhat from "css-what";
12
Tim van der Lippe706ec962021-06-04 12:24:4213CSSwhat.parse("foo[bar]:baz")
14
15~> [
16 [
17 { type: "tag", name: "foo" },
18 {
19 type: "attribute",
20 name: "bar",
21 action: "exists",
22 value: "",
Nikolay Vitkov10371982025-02-03 17:08:0723 ignoreCase: null
Tim van der Lippe706ec962021-06-04 12:24:4224 },
25 { type: "pseudo", name: "baz", data: null }
26 ]
27]
28```
29
30## API
31
Nikolay Vitkov10371982025-02-03 17:08:0732**`CSSwhat.parse(selector)` - Parses `selector`.**
Tim van der Lippe706ec962021-06-04 12:24:4233
34The 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:
35
Nikolay Vitkov10371982025-02-03 17:08:0736| name | properties | example | output |
37| ------------------- | --------------------------------------- | ------------- | ---------------------------------------------------------------------------------------- |
38| `tag` | `name` | `div` | `{ type: 'tag', name: 'div' }` |
39| `universal` | - | `*` | `{ type: 'universal' }` |
40| `pseudo` | `name`, `data` | `:name(data)` | `{ type: 'pseudo', name: 'name', data: 'data' }` |
41| `pseudo` | `name`, `data` | `:name` | `{ type: 'pseudo', name: 'name', data: null }` |
42| `pseudo-element` | `name` | `::name` | `{ type: 'pseudo-element', name: 'name' }` |
43| `attribute` | `name`, `action`, `value`, `ignoreCase` | `[attr]` | `{ type: 'attribute', name: 'attr', action: 'exists', value: '', ignoreCase: false }` |
44| `attribute` | `name`, `action`, `value`, `ignoreCase` | `[attr=val]` | `{ type: 'attribute', name: 'attr', action: 'equals', value: 'val', ignoreCase: false }` |
45| `attribute` | `name`, `action`, `value`, `ignoreCase` | `[attr^=val]` | `{ type: 'attribute', name: 'attr', action: 'start', value: 'val', ignoreCase: false }` |
46| `attribute` | `name`, `action`, `value`, `ignoreCase` | `[attr$=val]` | `{ type: 'attribute', name: 'attr', action: 'end', value: 'val', ignoreCase: false }` |
47| `child` | - | `>` | `{ type: 'child' }` |
48| `parent` | - | `<` | `{ type: 'parent' }` |
49| `sibling` | - | `~` | `{ type: 'sibling' }` |
50| `adjacent` | - | `+` | `{ type: 'adjacent' }` |
51| `descendant` | - | | `{ type: 'descendant' }` |
52| `column-combinator` | - | `\|\|` | `{ type: 'column-combinator' }` |
Tim van der Lippe706ec962021-06-04 12:24:4253
54**`CSSwhat.stringify(selector)` - Turns `selector` back into a string.**
55
56---
57
58License: BSD-2-Clause
59
60## Security contact information
61
62To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security).
63Tidelift will coordinate the fix and disclosure.
64
65## `css-what` for enterprise
66
67Available as part of the Tidelift Subscription
68
69The maintainers of `css-what` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-css-what?utm_source=npm-css-what&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)