1
1
<!-- lint disable no-html-->
2
2
3
- # hast-util-select [ ![ Build ] [ build-badge ]] [ build ] [ ![ Coverage ] [ coverage-badge ]] [ coverage ] [ ![ Downloads ] [ downloads-badge ]] [ downloads ] [ ![ Chat ] [ chat-badge ]] [ chat ]
3
+ # hast-util-select
4
4
5
- ` querySelector ` , ` querySelectorAll ` , and ` matches ` for [ HAST] [ ] nodes.
5
+ [ ![ Build] [ build-badge ]] [ build ]
6
+ [ ![ Coverage] [ coverage-badge ]] [ coverage ]
7
+ [ ![ Downloads] [ downloads-badge ]] [ downloads ]
8
+ [ ![ Size] [ size-badge ]] [ size ]
9
+ [ ![ Sponsors] [ sponsors-badge ]] [ collective ]
10
+ [ ![ Backers] [ backers-badge ]] [ collective ]
11
+ [ ![ Chat] [ chat-badge ]] [ chat ]
6
12
7
- One notable difference between DOM and HAST is that DOM nodes have references
13
+ [ ** hast** ] [ hast ] utility with equivalents ` querySelector ` , ` querySelectorAll ` ,
14
+ and ` matches ` .
15
+
16
+ One notable difference between DOM and hast is that DOM nodes have references
8
17
to their parents, meaning that ` document.body.matches(':last-child') ` can
9
- be evaluated. This information is not stored in HAST, so selectors like
10
- that don’t work.
18
+ be evaluated.
19
+ This information is not stored in hast, so selectors like that don’t work.
11
20
12
21
[ View the list of supported selectors »] [ support ]
13
22
14
- ## Installation
23
+ ## Install
15
24
16
25
[ npm] [ ] :
17
26
18
- ``` bash
27
+ ``` sh
19
28
npm install hast-util-select
20
29
```
21
30
22
31
## API
23
32
24
33
### ` select.matches(selector, node[, space]) `
25
34
26
- This only checks the element itself, not the surrounding tree. Thus, nesting
27
- in selectors is not supported (` p b ` , ` p > b ` ), neither are selectors like
28
- ` :first-child ` , etc. This simply checks that the given element matches the
29
- selector.
35
+ Check that the given ` node ` matches ` selector ` .
36
+ Returns boolean, whether the node matches or not.
37
+
38
+ This only checks the element itself, not the surrounding tree.
39
+ Thus, nesting in selectors is not supported (` p b ` , ` p > b ` ), neither are
40
+ selectors like ` :first-child ` , etc.
41
+ This only checks that the given element matches the selector.
30
42
31
43
##### Usage
32
44
@@ -48,8 +60,8 @@ matches('[lang|=en]', h('a', {lang: 'en-GB'})) // => true
48
60
49
61
* ` selector ` (` string ` )
50
62
— CSS selectors (` , ` is also supported)
51
- * ` node ` (` Node ` )
52
- — Thing to check, could be anything, but should be an [ element] [ ]
63
+ * ` node ` ([ ` Node ` ] [ node ] )
64
+ — Thing to check, could be anything, but should be an [ * element* ] [ element ]
53
65
* ` space ` (enum, ` 'svg' ` or ` 'html' ` , default: ` 'html' ` )
54
66
— Which space the node exists in
55
67
@@ -59,6 +71,10 @@ matches('[lang|=en]', h('a', {lang: 'en-GB'})) // => true
59
71
60
72
### ` select.select(selector, tree[, space]) `
61
73
74
+ Select the first ` node ` matching ` selector ` in the given ` tree ` (could be the
75
+ tree itself).
76
+ Searches the [ * tree* ] [ tree ] in [ * preorder* ] [ preorder ] .
77
+
62
78
##### Usage
63
79
64
80
``` js
@@ -88,24 +104,22 @@ Yields:
88
104
children: [ { type: ' text' , value: ' Delta' } ] }
89
105
```
90
106
91
- Select the first node matching ` selector ` in the given ` tree ` (could be the
92
- tree itself).
93
-
94
107
##### Parameters
95
108
96
109
* ` selector ` (` string ` ) — CSS selectors (` , ` is also supported)
97
- * ` tree ` (` Node ` ) — Thing to search.
110
+ * ` tree ` ([ ` Node ` ] [ node ] ) — [ * Tree * ] [ tree ] to search
98
111
* ` space ` (enum, ` 'svg' ` or ` 'html' ` , default: ` 'html' ` )
99
- — Which v the tree exists in
112
+ — Which space the tree exists in
100
113
101
114
##### Returns
102
115
103
- ` Element? ` — The found element, if any.
116
+ [ ` Element? ` ] [ element ] — The found element, if any.
104
117
105
118
### ` select.selectAll(selector, tree[, space]) `
106
119
107
- Select all nodes matching ` selector ` in the given ` tree ` (could include the
108
- tree itself).
120
+ Select all nodes matching ` selector ` in the given ` tree ` (could include the tree
121
+ itself).
122
+ Searches the [ * tree* ] [ tree ] in [ * preorder* ] [ preorder ] .
109
123
110
124
##### Usage
111
125
@@ -145,13 +159,13 @@ Yields:
145
159
##### Parameters
146
160
147
161
* ` selector ` (` string ` ) — CSS selectors (` , ` is also supported)
148
- * ` tree ` (` Node ` ) — Thing to search.
162
+ * ` tree ` ([ ` Node ` ] [ node ] ) — [ * Tree * ] [ tree ] to search
149
163
* ` space ` (enum, ` 'svg' ` or ` 'html' ` , default: ` 'html' ` )
150
164
— Which space the tree exists in
151
165
152
166
##### Returns
153
167
154
- ` Array.<Element> ` — All found elements, if any.
168
+ [ ` Array.<Element> ` ] [ element ] — All found elements, if any.
155
169
156
170
## Support
157
171
@@ -255,19 +269,21 @@ Yields:
255
269
256
270
###### Notes
257
271
258
- * \* — Not supported in ` matches ` .
272
+ * \* — Not supported in ` matches `
259
273
* † — Needs a user, browser, interactivity, or scripting to make sense
260
274
* ‡ — Not supported by the underlying algorithm
261
275
* § — Not very interested in writing / including the code for this
262
276
* ‖ — Too new, the spec is still changing
263
277
264
278
## Contribute
265
279
266
- See [ ` contributing.md ` in ` syntax-tree/hast ` ] [ contributing ] for ways to get
280
+ See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
267
281
started.
282
+ See [ ` support.md ` ] [ help ] for ways to get help.
268
283
269
- This organisation has a [ Code of Conduct] [ coc ] . By interacting with this
270
- repository, organisation, or community you agree to abide by its terms.
284
+ This project has a [ Code of Conduct] [ coc ] .
285
+ By interacting with this repository, organisation, or community you agree to
286
+ abide by its terms.
271
287
272
288
## License
273
289
@@ -287,22 +303,40 @@ repository, organisation, or community you agree to abide by its terms.
287
303
288
304
[ downloads ] : https://www.npmjs.com/package/hast-util-select
289
305
306
+ [ size-badge ] : https://img.shields.io/bundlephobia/minzip/hast-util-select.svg
307
+
308
+ [ size ] : https://bundlephobia.com/result?p=hast-util-select
309
+
310
+ [ sponsors-badge ] : https://opencollective.com/unified/sponsors/badge.svg
311
+
312
+ [ backers-badge ] : https://opencollective.com/unified/backers/badge.svg
313
+
314
+ [ collective ] : https://opencollective.com/unified
315
+
290
316
[ chat-badge ] : https://img.shields.io/badge/join%20the%20community-on%20spectrum-7b16ff.svg
291
317
292
- [ chat ] : https://spectrum.chat/unified/rehype
318
+ [ chat ] : https://spectrum.chat/unified/syntax-tree
293
319
294
320
[ npm ] : https://docs.npmjs.com/cli/install
295
321
296
322
[ license ] : license
297
323
298
324
[ author ] : https://wooorm.com
299
325
326
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/master/contributing.md
327
+
328
+ [ help ] : https://github.com/syntax-tree/.github/blob/master/support.md
329
+
330
+ [ coc ] : https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md
331
+
332
+ [ tree ] : https://github.com/syntax-tree/unist#tree
333
+
334
+ [ preorder ] : https://github.com/syntax-tree/unist#preorder
335
+
300
336
[ hast ] : https://github.com/syntax-tree/hast
301
337
338
+ [ node ] : https://github.com/syntax-tree/hast#nodes
339
+
302
340
[ element ] : https://github.com/syntax-tree/hast#element
303
341
304
342
[ support ] : #support
305
-
306
- [ contributing ] : https://github.com/syntax-tree/hast/blob/master/contributing.md
307
-
308
- [ coc ] : https://github.com/syntax-tree/hast/blob/master/code-of-conduct.md
0 commit comments