8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** hast** ] [ hast ] utility to check if a [ * node* ] [ node ] is a (certain)
12
- [ * element* ] [ element ] .
11
+ [ hast] [ ] utility to check if a node is a (certain) element.
13
12
14
- ## Install
13
+ ## Contents
14
+
15
+ * [ What is this?] ( #what-is-this )
16
+ * [ When should I use this?] ( #when-should-i-use-this )
17
+ * [ Install] ( #install )
18
+ * [ Use] ( #use )
19
+ * [ API] ( #api )
20
+ * [ ` isElement(node[, test[, index, parent[, context]]]) ` ] ( #iselementnode-test-index-parent-context )
21
+ * [ ` convertElement(test) ` ] ( #convertelementtest )
22
+ * [ Types] ( #types )
23
+ * [ Compatibility] ( #compatibility )
24
+ * [ Security] ( #security )
25
+ * [ Related] ( #related )
26
+ * [ Contribute] ( #contribute )
27
+ * [ License] ( #license )
28
+
29
+ ## What is this?
30
+
31
+ This package is a small utility that checks that a node is a certain element.
32
+
33
+ ## When should I use this?
34
+
35
+ Use this small utility if you find yourself repeating code for checking what
36
+ elements nodes are.
37
+
38
+ A similar package, [ ` unist-util-is ` ] [ unist-util-is ] , works on any unist node.
39
+
40
+ For more advanced tests, [ ` hast-util-select ` ] [ hast-util-select ] can be used
41
+ to match against CSS selectors.
15
42
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.
43
+ ## Install
18
44
19
- [ npm] [ ] :
45
+ This package is [ ESM only] [ esm ] .
46
+ In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [ npm] [ ] :
20
47
21
48
``` sh
22
49
npm install hast-util-is-element
23
50
```
24
51
52
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
53
+
54
+ ``` js
55
+ import {isElement } from ' https://esm.sh/hast-util-is-element@2'
56
+ ```
57
+
58
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
59
+
60
+ ``` html
61
+ <script type =" module" >
62
+ import {isElement } from ' https://esm.sh/hast-util-is-element@2?bundle'
63
+ </script >
64
+ ```
65
+
25
66
## Use
26
67
27
68
``` js
@@ -36,46 +77,45 @@ isElement({type: 'element', tagName: 'a'}, ['a', 'area']) // => true
36
77
37
78
## API
38
79
39
- This package exports the following identifiers: ` isElement ` , ` convertElement ` .
80
+ This package exports the identifiers ` isElement ` and ` convertElement ` .
40
81
There is no default export.
41
82
42
83
### ` isElement(node[, test[, index, parent[, context]]]) `
43
84
44
85
Check if the given value is a (certain) [ * element* ] [ element ] .
45
86
46
- * ` node ` ([ ` Node ` ] [ node ] ) — Node to check.
87
+ ###### Parameters
88
+
89
+ * ` node ` ([ ` Node ` ] [ node ] ) — node to check
47
90
* ` test ` ([ ` Function ` ] [ test ] , ` string ` , or ` Array<Test> ` , optional)
48
- — When ` array ` , checks if any one of the subtests pass.
91
+ — when ` array ` , checks if any one of the subtests pass.
49
92
When ` string ` , checks that the element has that tag name.
50
93
When ` function ` , see [ ` test ` ] [ test ]
51
- * ` index ` (` number ` , optional) — [ Index ] [ ] of ` node ` in ` parent `
52
- * ` parent ` ([ ` Node ` ] [ node ] , optional) — [ Parent ] [ ] of ` node `
53
- * ` context ` (` * ` , optional) — Context object to invoke ` test ` with
94
+ * ` index ` (` number ` , optional) — [ index ] [ ] of ` node ` in ` parent `
95
+ * ` parent ` ([ ` Node ` ] [ node ] , optional) — [ parent ] [ ] of ` node `
96
+ * ` context ` (` * ` , optional) — context object to call ` test ` with
54
97
55
98
###### Returns
56
99
57
- ` boolean ` — Whether ` test ` passed * and* ` node ` is an [ ` Element ` ] [ element ] .
100
+ Whether ` test ` passed * and* ` node ` is an [ ` Element ` ] [ element ] ( ` boolean ` )
58
101
59
102
###### Throws
60
103
61
- ` Error ` — When an incorrect ` test ` , ` index ` , or ` parent ` is given.
104
+ When an incorrect ` test ` , ` index ` , or ` parent ` is given.
62
105
A ` node ` that is not a node, or not an element, does not throw.
63
106
64
107
#### ` function test(element[, index, parent]) `
65
108
66
109
###### Parameters
67
110
68
- * ` element ` ([ ` Element ` ] [ element ] ) — Element to check
69
- * ` index ` (` number? ` ) — [ Index] [ ] of ` node ` in ` parent `
70
- * ` parent ` ([ ` Node? ` ] [ node ] ) — [ Parent] [ ] of ` node `
71
-
72
- ###### Context
73
-
74
- ` * ` — The to ` is ` given ` context ` .
111
+ * ` this ` — the to ` is ` given ` context ` (` * ` )
112
+ * ` element ` ([ ` Element ` ] [ element ] ) — element to check
113
+ * ` index ` (` number? ` ) — [ index] [ ] of ` node ` in ` parent `
114
+ * ` parent ` ([ ` Node? ` ] [ node ] ) — [ parent] [ ] of ` node `
75
115
76
116
###### Returns
77
117
78
- ` boolean? ` — Whether ` element ` matches.
118
+ Whether ` element ` matches ( ` boolean? ` ) .
79
119
80
120
### ` convertElement(test) `
81
121
@@ -87,6 +127,31 @@ where something else passes a compatible test.
87
127
The created function is slightly faster because it expects valid input only.
88
128
Therefore, passing invalid input, yields unexpected results.
89
129
130
+ ## Types
131
+
132
+ This package is fully typed with [ TypeScript] [ ] .
133
+ It exports the additional types:
134
+
135
+ * ` TestFunctionAnything `
136
+ — models any test function
137
+ * ` TestFunctionPredicate<T> ` (where ` T ` extends ` Element ` )
138
+ — models a test function for ` T `
139
+ * ` Test `
140
+ — models any arbitrary test that can be given
141
+ * ` PredicateTest<T> ` (where ` T ` extends ` Element ` )
142
+ — models a test for ` T `
143
+ * ` AssertAnything `
144
+ — models a check function as returned by ` convertElement `
145
+ * ` AssertPredicate<T> ` (where ` T ` extends ` Element ` )
146
+ — models a check function for ` T ` as returned by ` convertElement `
147
+
148
+ ## Compatibility
149
+
150
+ Projects maintained by the unified collective are compatible with all maintained
151
+ versions of Node.js.
152
+ As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
153
+ Our projects sometimes work with older versions, but this is not guaranteed.
154
+
90
155
## Security
91
156
92
157
` hast-util-is-element ` does not change the syntax tree so there are no openings
@@ -96,21 +161,21 @@ for [cross-site scripting (XSS)][xss] attacks.
96
161
97
162
* [ ` hast-util-has-property ` ] ( https://github.com/syntax-tree/hast-util-has-property )
98
163
— check if a node has a property
99
- * [ ` hast-util-is-body-ok-link ` ] ( https://github.com/rehypejs/rehype-minify/tree/HEAD /packages/hast-util-is-body-ok-link )
164
+ * [ ` hast-util-is-body-ok-link ` ] ( https://github.com/rehypejs/rehype-minify/tree/main /packages/hast-util-is-body-ok-link )
100
165
— check if a node is “Body OK” link element
101
- * [ ` hast-util-is-conditional-comment ` ] ( https://github.com/rehypejs/rehype-minify/tree/HEAD /packages/hast-util-is-conditional-comment )
166
+ * [ ` hast-util-is-conditional-comment ` ] ( https://github.com/rehypejs/rehype-minify/tree/main /packages/hast-util-is-conditional-comment )
102
167
— check if a node is a conditional comment
103
- * [ ` hast-util-is-css-link ` ] ( https://github.com/rehypejs/rehype-minify/tree/HEAD /packages/hast-util-is-css-link )
168
+ * [ ` hast-util-is-css-link ` ] ( https://github.com/rehypejs/rehype-minify/tree/main /packages/hast-util-is-css-link )
104
169
— check if a node is a CSS link element
105
- * [ ` hast-util-is-css-style ` ] ( https://github.com/rehypejs/rehype-minify/tree/HEAD /packages/hast-util-is-css-style )
170
+ * [ ` hast-util-is-css-style ` ] ( https://github.com/rehypejs/rehype-minify/tree/main /packages/hast-util-is-css-style )
106
171
— check if a node is a CSS style element
107
172
* [ ` hast-util-embedded ` ] ( https://github.com/syntax-tree/hast-util-embedded )
108
173
— check if a node is an embedded element
109
174
* [ ` hast-util-heading ` ] ( https://github.com/syntax-tree/hast-util-heading )
110
175
— check if a node is a heading element
111
176
* [ ` hast-util-interactive ` ] ( https://github.com/syntax-tree/hast-util-interactive )
112
177
— check if a node is interactive
113
- * [ ` hast-util-is-javascript ` ] ( https://github.com/rehypejs/rehype-minify/tree/HEAD /packages/hast-util-is-javascript )
178
+ * [ ` hast-util-is-javascript ` ] ( https://github.com/rehypejs/rehype-minify/tree/main /packages/hast-util-is-javascript )
114
179
— check if a node is a JavaScript script element
115
180
* [ ` hast-util-labelable ` ] ( https://github.com/syntax-tree/hast-util-labelable )
116
181
— check whether a node is labelable
@@ -127,8 +192,8 @@ for [cross-site scripting (XSS)][xss] attacks.
127
192
128
193
## Contribute
129
194
130
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
131
- started.
195
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
196
+ ways to get started.
132
197
See [ ` support.md ` ] [ support ] for ways to get help.
133
198
134
199
This project has a [ code of conduct] [ coc ] .
@@ -169,15 +234,23 @@ abide by its terms.
169
234
170
235
[ npm ] : https://docs.npmjs.com/cli/install
171
236
237
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
238
+
239
+ [ esmsh ] : https://esm.sh
240
+
241
+ [ typescript ] : https://www.typescriptlang.org
242
+
172
243
[ license ] : license
173
244
174
245
[ author ] : https://wooorm.com
175
246
176
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
247
+ [ health ] : https://github.com/syntax-tree/.github
177
248
178
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support .md
249
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
179
250
180
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
251
+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support.md
252
+
253
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
181
254
182
255
[ hast ] : https://github.com/syntax-tree/hast
183
256
@@ -192,3 +265,7 @@ abide by its terms.
192
265
[ test ] : #function-testelement-index-parent
193
266
194
267
[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
268
+
269
+ [ unist-util-is ] : https://github.com/syntax-tree/unist-util-is
270
+
271
+ [ hast-util-select ] : https://github.com/syntax-tree/hast-util-select
0 commit comments