8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** hast** ] [ hast ] utility to create an [ * element* ] [ element ] from a simple CSS
12
- selector.
11
+ [ hast] [ ] utility to create an element from a simple CSS selector.
13
12
14
- ## Install
13
+ ## What is this?
14
+
15
+ This package is a tiny utility that helps create elements.
15
16
16
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
17
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
17
+ ## When should I use this?
18
18
19
- [ npm] [ ] :
19
+ This utility is super niche.
20
+ You probably want the more powerful [ ` hastscript ` ] [ hastscript ] or
21
+ [ ` hast-util-from-selector ` ] [ hast-util-from-selector ]
22
+
23
+ ## Install
24
+
25
+ This package is [ ESM only] [ esm ] .
26
+ In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [ npm] [ ] :
20
27
21
28
``` sh
22
29
npm install hast-util-parse-selector
23
30
```
24
31
32
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
33
+
34
+ ``` js
35
+ import {parseSelector } from ' https://esm.sh/hast-util-parse-selector@3'
36
+ ```
37
+
38
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
39
+
40
+ ``` html
41
+ <script type =" module" >
42
+ import {parseSelector } from ' https://esm.sh/hast-util-parse-selector@3?bundle'
43
+ </script >
44
+ ```
45
+
25
46
## Use
26
47
27
48
``` js
@@ -41,36 +62,47 @@ Yields:
41
62
42
63
## API
43
64
44
- This package exports the following identifiers: ` parseSelector ` .
65
+ This package exports the identifier ` parseSelector ` .
45
66
There is no default export.
46
67
47
68
### ` parseSelector([selector][, defaultTagName]) `
48
69
49
70
Create an [ * element* ] [ element ] [ * node* ] [ node ] from a simple CSS selector.
50
71
51
- ###### ` selector `
52
-
53
- ` string ` , optional — Can contain a tag name (` foo ` ), classes (` .bar ` ),
54
- and an ID (` #baz ` ).
55
- Multiple classes are allowed.
56
- Uses the last ID if multiple IDs are found.
72
+ ###### Parameters
57
73
58
- ###### ` defaultTagName `
59
-
60
- ` string ` , optional, defaults to ` div ` — Tag name to use if ` selector ` does not
61
- specify one.
74
+ * ` selector ` (` string ` , optional)
75
+ — can contain a tag name (` foo ` ), classes (` .bar ` ), and an ID (` #baz ` ),
76
+ multiple classes are allowed, and uses the last ID if multiple IDs are found
77
+ * ` defaultTagName ` (` string ` , default: ` 'div' ` )
78
+ — tag name to use if ` selector ` does not specify one
62
79
63
80
###### Returns
64
81
65
82
[ ` Element ` ] [ element ] .
66
83
84
+ ## Types
85
+
86
+ This package is fully typed with [ TypeScript] [ ] .
87
+ It exports no additional types.
88
+ In TypeScript 4.2+, the type system can infer the tag name of literal
89
+ ` selector ` s and knows that the return element has that name.
90
+
91
+ ## Compatibility
92
+
93
+ Projects maintained by the unified collective are compatible with all maintained
94
+ versions of Node.js.
95
+ As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
96
+ Our projects sometimes work with older versions, but this is not guaranteed.
97
+
67
98
## Security
68
99
69
100
Improper use of the ` selector ` or ` defaultTagName ` can open you up to a
70
101
[ cross-site scripting (XSS)] [ xss ] attack as the value of ` tagName ` , when
71
102
resolving to ` script ` , injects a ` script ` element into the syntax tree.
72
103
73
- Do not use user input in ` selector ` or use [ ` hast-util-santize ` ] [ sanitize ] .
104
+ Do not use user input in ` selector ` or use
105
+ [ ` hast-util-santize ` ] [ hast-util-sanitize ] .
74
106
75
107
## Related
76
108
@@ -79,8 +111,8 @@ Do not use user input in `selector` or use [`hast-util-santize`][sanitize].
79
111
80
112
## Contribute
81
113
82
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
83
- started.
114
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
115
+ ways to get started.
84
116
See [ ` support.md ` ] [ support ] for ways to get help.
85
117
86
118
This project has a [ code of conduct] [ coc ] .
@@ -121,22 +153,34 @@ abide by its terms.
121
153
122
154
[ npm ] : https://docs.npmjs.com/cli/install
123
155
156
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
157
+
158
+ [ esmsh ] : https://esm.sh
159
+
160
+ [ typescript ] : https://www.typescriptlang.org
161
+
124
162
[ license ] : license
125
163
126
164
[ author ] : https://wooorm.com
127
165
128
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
166
+ [ health ] : https://github.com/syntax-tree/.github
167
+
168
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing.md
129
169
130
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
170
+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
131
171
132
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD /code-of-conduct.md
172
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main /code-of-conduct.md
133
173
134
174
[ hast ] : https://github.com/syntax-tree/hast
135
175
176
+ [ hast-util-sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
177
+
178
+ [ hastscript ] : https://github.com/syntax-tree/hastscript
179
+
180
+ [ hast-util-from-selector ] : https://github.com/syntax-tree/hast-util-from-selector
181
+
136
182
[ node ] : https://github.com/syntax-tree/hast#nodes
137
183
138
184
[ element ] : https://github.com/syntax-tree/hast#element
139
185
140
186
[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
141
-
142
- [ sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
0 commit comments