You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
4.6 KiB

1 month ago
  1. # css-what
  2. [![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)
  3. [![Coverage](https://img.shields.io/coveralls/github/fb55/css-what/master)](https://coveralls.io/github/fb55/css-what?branch=master)
  4. A CSS selector parser.
  5. ## Example
  6. ```js
  7. import * as CSSwhat from "css-what";
  8. CSSwhat.parse("foo[bar]:baz")
  9. ~> [
  10. [
  11. { type: "tag", name: "foo" },
  12. {
  13. type: "attribute",
  14. name: "bar",
  15. action: "exists",
  16. value: "",
  17. ignoreCase: null
  18. },
  19. { type: "pseudo", name: "baz", data: null }
  20. ]
  21. ]
  22. ```
  23. ## API
  24. **`CSSwhat.parse(selector)` - Parses `selector`.**
  25. 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:
  26. | name | properties | example | output |
  27. | ------------------- | --------------------------------------- | ------------- | ---------------------------------------------------------------------------------------- |
  28. | `tag` | `name` | `div` | `{ type: 'tag', name: 'div' }` |
  29. | `universal` | - | `*` | `{ type: 'universal' }` |
  30. | `pseudo` | `name`, `data` | `:name(data)` | `{ type: 'pseudo', name: 'name', data: 'data' }` |
  31. | `pseudo` | `name`, `data` | `:name` | `{ type: 'pseudo', name: 'name', data: null }` |
  32. | `pseudo-element` | `name` | `::name` | `{ type: 'pseudo-element', name: 'name' }` |
  33. | `attribute` | `name`, `action`, `value`, `ignoreCase` | `[attr]` | `{ type: 'attribute', name: 'attr', action: 'exists', value: '', ignoreCase: false }` |
  34. | `attribute` | `name`, `action`, `value`, `ignoreCase` | `[attr=val]` | `{ type: 'attribute', name: 'attr', action: 'equals', value: 'val', ignoreCase: false }` |
  35. | `attribute` | `name`, `action`, `value`, `ignoreCase` | `[attr^=val]` | `{ type: 'attribute', name: 'attr', action: 'start', value: 'val', ignoreCase: false }` |
  36. | `attribute` | `name`, `action`, `value`, `ignoreCase` | `[attr$=val]` | `{ type: 'attribute', name: 'attr', action: 'end', value: 'val', ignoreCase: false }` |
  37. | `child` | - | `>` | `{ type: 'child' }` |
  38. | `parent` | - | `<` | `{ type: 'parent' }` |
  39. | `sibling` | - | `~` | `{ type: 'sibling' }` |
  40. | `adjacent` | - | `+` | `{ type: 'adjacent' }` |
  41. | `descendant` | - | | `{ type: 'descendant' }` |
  42. | `column-combinator` | - | `\|\|` | `{ type: 'column-combinator' }` |
  43. **`CSSwhat.stringify(selector)` - Turns `selector` back into a string.**
  44. ---
  45. License: BSD-2-Clause
  46. ## Security contact information
  47. To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security).
  48. Tidelift will coordinate the fix and disclosure.
  49. ## `css-what` for enterprise
  50. Available as part of the Tidelift Subscription
  51. 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.](https://tidelift.com/subscription/pkg/npm-css-what?utm_source=npm-css-what&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)