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.

70 lines
4.6 KiB

4 years ago
  1. # css-what [![Build Status](https://secure.travis-ci.org/fb55/css-what.svg?branch=master)](http://travis-ci.org/fb55/css-what)
  2. a CSS selector parser
  3. ## Example
  4. ```js
  5. const CSSwhat = require("css-what")
  6. CSSwhat.parse("foo[bar]:baz")
  7. ~> [
  8. [
  9. { type: "tag", name: "foo" },
  10. {
  11. type: "attribute",
  12. name: "bar",
  13. action: "exists",
  14. value: "",
  15. ignoreCase: false
  16. },
  17. { type: "pseudo", name: "baz", data: null }
  18. ]
  19. ]
  20. ```
  21. ## API
  22. **`CSSwhat.parse(str, options)` - Parses `str`, optionally with the passed `options`.**
  23. 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:
  24. | name | attributes | example | output |
  25. | ---------------- | --------------------------------------- | ------------- | ---------------------------------------------------------------------------------------- |
  26. | `tag` | `name` | `div` | `{ type: 'tag', name: 'div' }` |
  27. | `universal` | - | `*` | `{ type: 'universal' }` |
  28. | `pseudo` | `name`, `data` | `:name(data)` | `{ type: 'pseudo', name: 'name', data: 'data' }` |
  29. | `pseudo` | `name`, `data` | `:name` | `{ type: 'pseudo', name: 'name', data: null }` |
  30. | `pseudo-element` | `name` | `::name` | `{ type: 'pseudo-element', name: 'name' }` |
  31. | `attribute` | `name`, `action`, `value`, `ignoreCase` | `[attr]` | `{ type: 'attribute', name: 'attr', action: 'exists', value: '', ignoreCase: false }` |
  32. | `attribute` | `name`, `action`, `value`, `ignoreCase` | `[attr=val]` | `{ type: 'attribute', name: 'attr', action: 'equals', value: 'val', ignoreCase: false }` |
  33. | `attribute` | `name`, `action`, `value`, `ignoreCase` | `[attr^=val]` | `{ type: 'attribute', name: 'attr', action: 'start', value: 'val', ignoreCase: false }` |
  34. | `attribute` | `name`, `action`, `value`, `ignoreCase` | `[attr$=val]` | `{ type: 'attribute', name: 'attr', action: 'end', value: 'val', ignoreCase: false }` |
  35. | `child` | - | `>` | `{ type: 'child' }` |
  36. | `parent` | - | `<` | `{ type: 'parent' }` |
  37. | `sibling` | - | `~` | `{ type: 'sibling' }` |
  38. | `adjacent` | - | `+` | `{ type: 'adjacent' }` |
  39. | `descendant` | - | | `{ type: 'descendant' }` |
  40. **Options:**
  41. - `lowerCaseTags`: When false, tag names will not be lowercased. Defaults to `true`.
  42. - `lowerCaseAttributeNames`: When false, attribute names will not be lowercased. Defaults to `true`.
  43. - `xmlMode`: When `true`, `xmlMode` implies both `lowerCaseTags` and `lowerCaseAttributeNames` are set to `false`.
  44. **`CSSwhat.stringify(selector)` - Turns `selector` back into a string.**
  45. ---
  46. License: BSD-2-Clause
  47. ## Security contact information
  48. To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security).
  49. Tidelift will coordinate the fix and disclosure.
  50. ## `css-what` for enterprise
  51. Available as part of the Tidelift Subscription
  52. 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)