1 | # hast-util-parse-selector
|
---|
2 |
|
---|
3 | [![Build][build-badge]][build]
|
---|
4 | [![Coverage][coverage-badge]][coverage]
|
---|
5 | [![Downloads][downloads-badge]][downloads]
|
---|
6 | [![Size][size-badge]][size]
|
---|
7 | [![Sponsors][sponsors-badge]][collective]
|
---|
8 | [![Backers][backers-badge]][collective]
|
---|
9 | [![Chat][chat-badge]][chat]
|
---|
10 |
|
---|
11 | [**hast**][hast] utility to create an [*element*][element] from a simple CSS
|
---|
12 | selector.
|
---|
13 |
|
---|
14 | ## Install
|
---|
15 |
|
---|
16 | [npm][]:
|
---|
17 |
|
---|
18 | ```sh
|
---|
19 | npm install hast-util-parse-selector
|
---|
20 | ```
|
---|
21 |
|
---|
22 | ## Use
|
---|
23 |
|
---|
24 | ```js
|
---|
25 | var parseSelector = require('hast-util-parse-selector')
|
---|
26 |
|
---|
27 | console.log(parseSelector('.quux#bar.baz.qux'))
|
---|
28 | ```
|
---|
29 |
|
---|
30 | Yields:
|
---|
31 |
|
---|
32 | ```js
|
---|
33 | { type: 'element',
|
---|
34 | tagName: 'div',
|
---|
35 | properties: { id: 'bar', className: [ 'quux', 'baz', 'qux' ] },
|
---|
36 | children: [] }
|
---|
37 | ```
|
---|
38 |
|
---|
39 | ## API
|
---|
40 |
|
---|
41 | ### `parseSelector([selector][, defaultTagName])`
|
---|
42 |
|
---|
43 | Create an [*element*][element] [*node*][node] from a simple CSS selector.
|
---|
44 |
|
---|
45 | ###### `selector`
|
---|
46 |
|
---|
47 | `string`, optional — Can contain a tag-name (`foo`), classes (`.bar`),
|
---|
48 | and an ID (`#baz`).
|
---|
49 | Multiple classes are allowed.
|
---|
50 | Uses the last ID if multiple IDs are found.
|
---|
51 |
|
---|
52 | ###### `defaultTagName`
|
---|
53 |
|
---|
54 | `string`, optional, defaults to `div` — Tag name to use if `selector` does not
|
---|
55 | specify one.
|
---|
56 |
|
---|
57 | ###### Returns
|
---|
58 |
|
---|
59 | [`Element`][element].
|
---|
60 |
|
---|
61 | ## Security
|
---|
62 |
|
---|
63 | Improper use of the `selector` or `defaultTagName` can open you up to a
|
---|
64 | [cross-site scripting (XSS)][xss] attack as the value of `tagName`, when
|
---|
65 | resolving to `script`, injects a `script` element into the syntax tree.
|
---|
66 |
|
---|
67 | Do not use user input in `selector` or use [`hast-util-santize`][sanitize].
|
---|
68 |
|
---|
69 | ## Related
|
---|
70 |
|
---|
71 | * [`hast-util-from-selector`](https://github.com/syntax-tree/hast-util-from-selector)
|
---|
72 | — parse complex CSS selectors to nodes
|
---|
73 |
|
---|
74 | ## Contribute
|
---|
75 |
|
---|
76 | See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
|
---|
77 | started.
|
---|
78 | See [`support.md`][support] for ways to get help.
|
---|
79 |
|
---|
80 | This project has a [code of conduct][coc].
|
---|
81 | By interacting with this repository, organization, or community you agree to
|
---|
82 | abide by its terms.
|
---|
83 |
|
---|
84 | ## License
|
---|
85 |
|
---|
86 | [MIT][license] © [Titus Wormer][author]
|
---|
87 |
|
---|
88 | <!-- Definitions -->
|
---|
89 |
|
---|
90 | [build-badge]: https://img.shields.io/travis/syntax-tree/hast-util-parse-selector.svg
|
---|
91 |
|
---|
92 | [build]: https://travis-ci.org/syntax-tree/hast-util-parse-selector
|
---|
93 |
|
---|
94 | [coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-parse-selector.svg
|
---|
95 |
|
---|
96 | [coverage]: https://codecov.io/github/syntax-tree/hast-util-parse-selector
|
---|
97 |
|
---|
98 | [downloads-badge]: https://img.shields.io/npm/dm/hast-util-parse-selector.svg
|
---|
99 |
|
---|
100 | [downloads]: https://www.npmjs.com/package/hast-util-parse-selector
|
---|
101 |
|
---|
102 | [size-badge]: https://img.shields.io/bundlephobia/minzip/hast-util-parse-selector.svg
|
---|
103 |
|
---|
104 | [size]: https://bundlephobia.com/result?p=hast-util-parse-selector
|
---|
105 |
|
---|
106 | [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
|
---|
107 |
|
---|
108 | [backers-badge]: https://opencollective.com/unified/backers/badge.svg
|
---|
109 |
|
---|
110 | [collective]: https://opencollective.com/unified
|
---|
111 |
|
---|
112 | [chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
|
---|
113 |
|
---|
114 | [chat]: https://github.com/syntax-tree/unist/discussions
|
---|
115 |
|
---|
116 | [npm]: https://docs.npmjs.com/cli/install
|
---|
117 |
|
---|
118 | [license]: license
|
---|
119 |
|
---|
120 | [author]: https://wooorm.com
|
---|
121 |
|
---|
122 | [contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
|
---|
123 |
|
---|
124 | [support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
|
---|
125 |
|
---|
126 | [coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
|
---|
127 |
|
---|
128 | [hast]: https://github.com/syntax-tree/hast
|
---|
129 |
|
---|
130 | [node]: https://github.com/syntax-tree/hast#nodes
|
---|
131 |
|
---|
132 | [element]: https://github.com/syntax-tree/hast#element
|
---|
133 |
|
---|
134 | [xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
|
---|
135 |
|
---|
136 | [sanitize]: https://github.com/syntax-tree/hast-util-sanitize
|
---|