css-what

a CSS selector parser

# Example

const CSSwhat = require("css-what")
CSSwhat.parse("foo[bar]:baz")
~> [
[
{ type: "tag", name: "foo" },
{
type: "attribute",
name: "bar",
action: "exists",
value: "",
ignoreCase: false
},
{ type: "pseudo", name: "baz", data: null }
]
]

# API

**`CSSwhat.parse(str, options)` - Parses `str`, optionally with the passed `options`.**

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 | attributes | example | output
--- | --- | --- | ---

`tag`

`name`

`div`

‘{ type: 'tag’, name: 'div' }`\ilinebr </td> </tr> <tr class="markdownTableRowEven"> <td class="markdownTableBodyNone">`universal`\ilinebr </td> <td class="markdownTableBodyNone"> - \ilinebr </td> <td class="markdownTableBodyNone">`*`\ilinebr </td> <td class="markdownTableBodyNone">`{ type: 'universal' }`\ilinebr </td> </tr> <tr class="markdownTableRowOdd"> <td class="markdownTableBodyNone">`pseudo`\ilinebr </td> <td class="markdownTableBodyNone">`name`,`data`\ilinebr </td> <td class="markdownTableBodyNone">`:name(data)`\ilinebr </td> <td class="markdownTableBodyNone">`{ type: 'pseudo', name: 'name', data: 'data' }`\ilinebr </td> </tr> <tr class="markdownTableRowEven"> <td class="markdownTableBodyNone">`pseudo`\ilinebr </td> <td class="markdownTableBodyNone">`name`,`data`\ilinebr </td> <td class="markdownTableBodyNone">`:name`\ilinebr </td> <td class="markdownTableBodyNone">`{ type: 'pseudo', name: 'name', data: null }`\ilinebr </td> </tr> <tr class="markdownTableRowOdd"> <td class="markdownTableBodyNone">`pseudo-element`\ilinebr </td> <td class="markdownTableBodyNone">`name`\ilinebr </td> <td class="markdownTableBodyNone">`::name<tt>\ilinebr </td> <td class="markdownTableBodyNone">{ type: 'pseudo-element', name: 'name' }`\ilinebr </td> </tr> <tr class="markdownTableRowEven"> <td class="markdownTableBodyNone">`attribute`\ilinebr </td> <td class="markdownTableBodyNone">`name`,`action`,`value`,`ignoreCase`\ilinebr </td> <td class="markdownTableBodyNone">`[attr]`\ilinebr </td> <td class="markdownTableBodyNone">`{ type: 'attribute', name: 'attr', action: 'exists', value: '', ignoreCase: false }`\ilinebr </td> </tr> <tr class="markdownTableRowOdd"> <td class="markdownTableBodyNone">`attribute`\ilinebr </td> <td class="markdownTableBodyNone">`name`,`action`,`value`,`ignoreCase`\ilinebr </td> <td class="markdownTableBodyNone">`[attr=val]`\ilinebr </td> <td class="markdownTableBodyNone">`{ type: 'attribute', name: 'attr', action: 'equals', value: 'val', ignoreCase: false }`\ilinebr </td> </tr> <tr class="markdownTableRowEven"> <td class="markdownTableBodyNone">`attribute`\ilinebr </td> <td class="markdownTableBodyNone">`name`,`action`,`value`,`ignoreCase`\ilinebr </td> <td class="markdownTableBodyNone">`[attr^=val]`\ilinebr </td> <td class="markdownTableBodyNone">`{ type: 'attribute', name: 'attr', action: 'start', value: 'val', ignoreCase: false }`\ilinebr </td> </tr> <tr class="markdownTableRowOdd"> <td class="markdownTableBodyNone">`attribute`\ilinebr </td> <td class="markdownTableBodyNone">`name`,`action`,`value`,`ignoreCase`\ilinebr </td> <td class="markdownTableBodyNone">`[attr$=val]`\ilinebr </td> <td class="markdownTableBodyNone">`{ type: 'attribute', name: 'attr', action: 'end', value: 'val', ignoreCase: false }`\ilinebr </td> </tr> <tr class="markdownTableRowEven"> <td class="markdownTableBodyNone">`child`\ilinebr </td> <td class="markdownTableBodyNone"> - \ilinebr </td> <td class="markdownTableBodyNone">`>`\ilinebr </td> <td class="markdownTableBodyNone">`{ type: 'child' }`\ilinebr </td> </tr> <tr class="markdownTableRowOdd"> <td class="markdownTableBodyNone">`parent`\ilinebr </td> <td class="markdownTableBodyNone"> - \ilinebr </td> <td class="markdownTableBodyNone">`<`\ilinebr </td> <td class="markdownTableBodyNone">`{ type: 'parent' }`\ilinebr </td> </tr> <tr class="markdownTableRowEven"> <td class="markdownTableBodyNone">`sibling`\ilinebr </td> <td class="markdownTableBodyNone"> - \ilinebr </td> <td class="markdownTableBodyNone">`~`\ilinebr </td> <td class="markdownTableBodyNone">`{ type: 'sibling' }`\ilinebr </td> </tr> <tr class="markdownTableRowOdd"> <td class="markdownTableBodyNone">`adjacent`\ilinebr </td> <td class="markdownTableBodyNone"> - \ilinebr </td> <td class="markdownTableBodyNone">`+`\ilinebr </td> <td class="markdownTableBodyNone">`{ type: 'adjacent' }`\ilinebr </td> </tr> <tr class="markdownTableRowEven"> <td class="markdownTableBodyNone">`descendant`\ilinebr </td> <td class="markdownTableBodyNone"> - \ilinebr </td> <td class="markdownTableBodyNone"> \ilinebr </td> <td class="markdownTableBodyNone">`{ type: 'descendant' }`

Options:

- `lowerCaseTags`: When false, tag names will not be lowercased. Defaults to `true`.
- `lowerCaseAttributeNames`: When false, attribute names will not be lowercased. Defaults to `true`.
- `xmlMode`: When `true`, `xmlMode` implies both `lowerCaseTags` and `lowerCaseAttributeNames` are set to `false`.

**`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.

# <tt>css-what</tt> for enterprise

Available as part of the Tidelift Subscription

The 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.