• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# css-what
2
3[![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.
7
8## Example
9
10```js
11import * as CSSwhat from "css-what";
12
13CSSwhat.parse("foo[bar]:baz")
14
15~> [
16    [
17        { type: "tag", name: "foo" },
18        {
19            type: "attribute",
20            name: "bar",
21            action: "exists",
22            value: "",
23            ignoreCase: null
24        },
25        { type: "pseudo", name: "baz", data: null }
26    ]
27]
28```
29
30## API
31
32**`CSSwhat.parse(selector)` - Parses `selector`.**
33
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
36| 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' }`                                                          |
53
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)
70