Skip to content

Commit 8b57b44

Browse files
committed
Add docs to types
1 parent f7eec7b commit 8b57b44

File tree

7 files changed

+107
-50
lines changed

7 files changed

+107
-50
lines changed

index.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
/**
22
* @typedef {import('./lib/types.js').Element} Element
3-
* @typedef {import('./lib/types.js').HastNode} HastNode
3+
* @typedef {import('./lib/types.js').Node} Node
44
* @typedef {import('./lib/types.js').Space} Space
55
*/
66

77
import {any} from './lib/any.js'
88
import {parse} from './lib/parse.js'
99

1010
/**
11+
* Check that the given `node` matches `selector`.
12+
*
13+
* This only checks the element itself, not the surrounding tree.
14+
* Thus, nesting in selectors is not supported (`p b`, `p > b`), neither are
15+
* selectors like `:first-child`, etc.
16+
* This only checks that the given element matches the selector.
17+
*
1118
* @param {string} selector
12-
* @param {HastNode} [node]
13-
* @param {Space} [space]
19+
* CSS selector, such as (`h1`, `a, b`).
20+
* @param {Node|undefined} [node]
21+
* Node that might match `selector`, should be an element.
22+
* @param {Space|undefined} [space='html']
23+
* Name of namespace (`'svg'` or `'html'`).
1424
* @returns {boolean}
25+
* Whether `node` matches `selector`.
1526
*/
1627
export function matches(selector, node, space) {
1728
return Boolean(
@@ -20,21 +31,38 @@ export function matches(selector, node, space) {
2031
}
2132

2233
/**
34+
* Select the first element that matches `selector` in the given `tree`.
35+
* Searches the tree in *preorder*.
36+
*
2337
* @param {string} selector
24-
* @param {HastNode} [node]
25-
* @param {Space} [space]
38+
* CSS selector, such as (`h1`, `a, b`).
39+
* @param {Node|undefined} [tree]
40+
* Tree to search.
41+
* @param {Space|undefined} [space='html']
42+
* Name of namespace (`'svg'` or `'html'`).
2643
* @returns {Element|null}
44+
* First element in `tree` that matches `selector` or `null` if nothing is
45+
* found.
46+
* This could be `tree` itself.
2747
*/
28-
export function select(selector, node, space) {
29-
return any(parse(selector), node, {space, one: true})[0] || null
48+
export function select(selector, tree, space) {
49+
return any(parse(selector), tree, {space, one: true})[0] || null
3050
}
3151

3252
/**
53+
* Select all elements that match `selector` in the given `tree`.
54+
* Searches the tree in *preorder*.
55+
*
3356
* @param {string} selector
34-
* @param {HastNode} [node]
35-
* @param {Space} [space]
57+
* CSS selector, such as (`h1`, `a, b`).
58+
* @param {Node|undefined} [tree]
59+
* Tree to search.
60+
* @param {Space|undefined} [space='html']
61+
* Name of namespace (`'svg'` or `'html'`).
3662
* @returns {Array<Element>}
63+
* Elements in `tree` that match `selector`.
64+
* This could include `tree` itself.
3765
*/
38-
export function selectAll(selector, node, space) {
39-
return any(parse(selector), node, {space})
66+
export function selectAll(selector, tree, space) {
67+
return any(parse(selector), tree, {space})
4068
}

lib/any.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @typedef {import('./types.js').Selectors} Selectors
44
* @typedef {import('./types.js').Rule} Rule
55
* @typedef {import('./types.js').RuleSet} RuleSet
6-
* @typedef {import('./types.js').HastNode} HastNode
6+
* @typedef {import('./types.js').Node} Node
77
* @typedef {import('./types.js').SelectIterator} SelectIterator
88
* @typedef {import('./types.js').SelectState} SelectState
99
*/
@@ -15,7 +15,7 @@ import {nest} from './nest.js'
1515
import {pseudo} from './pseudo.js'
1616
import {test} from './test.js'
1717

18-
/** @type {(query: Selectors|RuleSet|Rule, element: HastNode, state: SelectState) => Array<Element>} */
18+
/** @type {(query: Selectors|RuleSet|Rule, element: Node, state: SelectState) => Array<Element>} */
1919
const type = zwitch('type', {
2020
unknown: unknownType,
2121
invalid: invalidType,
@@ -24,7 +24,7 @@ const type = zwitch('type', {
2424

2525
/**
2626
* @param {Selectors|RuleSet|Rule} query
27-
* @param {HastNode|undefined} node
27+
* @param {Node|undefined} node
2828
* @param {SelectState} state
2929
* @returns {Array<Element>}
3030
*/
@@ -34,7 +34,7 @@ export function any(query, node, state) {
3434

3535
/**
3636
* @param {Selectors} query
37-
* @param {HastNode} node
37+
* @param {Node} node
3838
* @param {SelectState} state
3939
* @returns {Array<Element>}
4040
*/
@@ -51,7 +51,7 @@ function selectors(query, node, state) {
5151

5252
/**
5353
* @param {RuleSet} query
54-
* @param {HastNode} node
54+
* @param {Node} node
5555
* @param {SelectState} state
5656
* @returns {Array<Element>}
5757
*/
@@ -61,7 +61,7 @@ function ruleSet(query, node, state) {
6161

6262
/**
6363
* @param {Rule} query
64-
* @param {HastNode} tree
64+
* @param {Node} tree
6565
* @param {SelectState} state
6666
* @returns {Array<Element>}
6767
*/

lib/enter-state.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* @typedef {import('./types.js').SelectState} SelectState
3-
* @typedef {import('./types.js').HastNode} HastNode
3+
* @typedef {import('./types.js').Node} Node
44
* @typedef {import('./types.js').ElementChild} ElementChild
55
* @typedef {import('./types.js').Direction} Direction
6-
* @typedef {import('unist-util-visit/complex-types').Visitor<ElementChild>} Visitor
6+
* @typedef {import('unist-util-visit/complex-types.js').Visitor<ElementChild>} Visitor
77
*/
88

99
import {direction} from 'direction'
@@ -15,7 +15,7 @@ import {element} from './util.js'
1515

1616
/**
1717
* @param {SelectState} state
18-
* @param {HastNode} node
18+
* @param {Node} node
1919
* @returns {() => void}
2020
*/
2121
// eslint-disable-next-line complexity

lib/nest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @typedef {import('./types.js').Parent} Parent
55
* @typedef {import('./types.js').SelectState} SelectState
66
* @typedef {import('./types.js').SelectIterator} SelectIterator
7-
* @typedef {import('./types.js').HastNode} HastNode
7+
* @typedef {import('./types.js').Node} Node
88
* @typedef {import('./types.js').Handler} Handler
99
*/
1010

@@ -14,7 +14,7 @@ import {parent, element} from './util.js'
1414

1515
const own = {}.hasOwnProperty
1616

17-
/** @type {(query: Rule, node: HastNode, index: number|null, parent: Parent|null, state: SelectState) => void} */
17+
/** @type {(query: Rule, node: Node, index: number|null, parent: Parent|null, state: SelectState) => void} */
1818
const handle = zwitch('nestingOperator', {
1919
unknown: unknownNesting,
2020
// @ts-expect-error: hush.

lib/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @typedef {import('./types.js').Rule} Rule
3-
* @typedef {import('./types.js').HastNode} HastNode
3+
* @typedef {import('./types.js').Node} Node
44
* @typedef {import('./types.js').Parent} Parent
55
* @typedef {import('./types.js').SelectState} SelectState
66
*/
@@ -14,7 +14,7 @@ import {element} from './util.js'
1414

1515
/**
1616
* @param {Rule} query
17-
* @param {HastNode} node
17+
* @param {Node} node
1818
* @param {number|null} index
1919
* @param {Parent|null} parent
2020
* @param {SelectState} state

lib/types.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist').Parent} Parent
42
*
53
* @typedef {import('hast').Root} Root
4+
* @typedef {import('hast').Content} Content
65
* @typedef {import('hast').Element} Element
6+
* @typedef {import('hast').ElementContent} ElementChild
77
* @typedef {import('hast').Properties} Properties
8-
* @typedef {Element|Root} HastParent
9-
* @typedef {import('hast').Parent['children'][number]|Root} HastNode
10-
* @typedef {Element['children'][number]} ElementChild
11-
* @typedef {Properties[string]} PropertyValue
8+
* @typedef {Root|Content} Node
9+
* @typedef {Extract<Node, import('hast').Parent>} Parent
10+
* @typedef {Properties[keyof Properties]} PropertyValue
1211
*
1312
* @typedef {import('css-selector-parser').Selector} Selector
1413
* @typedef {import('css-selector-parser').Selectors} Selectors
@@ -18,8 +17,8 @@
1817
* @typedef {import('css-selector-parser').AttrValueType} AttrValueType
1918
* @typedef {Selector|Rule|RulePseudo} Query
2019
*
21-
* Fix for types.
2220
* @typedef RuleAttr
21+
* Fix for types from `css-selector-parser`.
2322
* @property {string} name
2423
* @property {string} [operator]
2524
* @property {AttrValueType} [valueType]
@@ -68,15 +67,15 @@
6867
/**
6968
* @callback SelectIterator
7069
* @param {Rule} query
71-
* @param {HastNode} node
70+
* @param {Node} node
7271
* @param {number} index
7372
* @param {Parent|null} parent
7473
* @param {SelectState} state
7574
*/
7675

7776
/**
7877
* @typedef {(
79-
* ((query: Rule, node: HastNode, index: number|null, parent: Parent|null, state: SelectState) => void)
78+
* ((query: Rule, node: Node, index: number|null, parent: Parent|null, state: SelectState) => void)
8079
* )} Handler
8180
*/
8281

readme.md

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,26 @@ There is no default export.
8383

8484
### `matches(selector, node[, space])`
8585

86-
Check that the given `node` ([`Node`][node], should be [element][]) matches the
87-
CSS selector `selector` (`string`, stuff like `a, b` is also supported).
88-
Also accepts a `space` (enum, `'svg'` or `'html'`, default: `'html'`)
89-
Returns whether the node matches or not (`boolean`).
86+
Check that the given `node` matches `selector`.
9087

9188
This only checks the element itself, not the surrounding tree.
9289
Thus, nesting in selectors is not supported (`p b`, `p > b`), neither are
9390
selectors like `:first-child`, etc.
9491
This only checks that the given element matches the selector.
9592

93+
###### Parameters
94+
95+
* `selector` (`string`)
96+
— CSS selector, such as (`h1`, `a, b`)
97+
* `node` ([`Node`][node], optional)
98+
— node that might match `selector`, should be an element
99+
* `space` (`'svg'` or `'html'`, default: `'html'`)
100+
— name of namespace
101+
102+
###### Returns
103+
104+
Whether `node` matches `selector` (`boolean`).
105+
96106
###### Example
97107

98108
```js
@@ -106,15 +116,27 @@ matches('.classy', h('a', {className: ['classy']})) // => true
106116
matches('#id', h('a', {id: 'id'})) // => true
107117
matches('[lang|=en]', h('a', {lang: 'en'})) // => true
108118
matches('[lang|=en]', h('a', {lang: 'en-GB'})) // => true
109-
// ...
119+
//
110120
```
111121

112122
### `select(selector, tree[, space])`
113123

114-
Select the first [element][] (or `null`) matching the CSS selector `selector` in
115-
the given `tree` ([`Node`][node]), which could be the tree itself.
116-
Searches the [tree][] in *[preorder][]*.
117-
Also accepts a `space` (enum, `'svg'` or `'html'`, default: `'html'`)
124+
Select the first element that matches `selector` in the given `tree`.
125+
Searches the tree in *[preorder][]*.
126+
127+
###### Parameters
128+
129+
* `selector` (`string`)
130+
— CSS selector, such as (`h1`, `a, b`)
131+
* `tree` ([`Node`][node], optional)
132+
— tree to search
133+
* `space` (`'svg'` or `'html'`, default: `'html'`)
134+
— name of namespace
135+
136+
###### Returns
137+
138+
First element in `tree` that matches `selector` or `null` if nothing is found.
139+
This could be `tree` itself.
118140

119141
###### Example
120142

@@ -147,10 +169,22 @@ Yields:
147169

148170
### `selectAll(selector, tree[, space])`
149171

150-
Select all [element][]s (`Array<Element>`) matching the CSS selector `selector`
151-
in the given `tree` ([`Node`][node]), which could include the tree itself.
152-
Searches the [tree][] in *[preorder][]*.
153-
Also accepts a `space` (enum, `'svg'` or `'html'`, default: `'html'`)
172+
Select all elements that match `selector` in the given `tree`.
173+
Searches the tree in *preorder*.
174+
175+
###### Parameters
176+
177+
* `selector` (`string`)
178+
— CSS selector, such as (`h1`, `a, b`)
179+
* `tree` ([`Node`][node], optional)
180+
— tree to search
181+
* `space` (`'svg'` or `'html'`, default: `'html'`)
182+
— name of namespace
183+
184+
###### Returns
185+
186+
Elements in `tree` that match `selector`.
187+
This could include `tree` itself.
154188

155189
###### Example
156190

@@ -385,16 +419,12 @@ abide by its terms.
385419

386420
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
387421

388-
[tree]: https://github.com/syntax-tree/unist#tree
389-
390422
[preorder]: https://github.com/syntax-tree/unist#preorder
391423

392424
[hast]: https://github.com/syntax-tree/hast
393425

394426
[node]: https://github.com/syntax-tree/hast#nodes
395427

396-
[element]: https://github.com/syntax-tree/hast#element
397-
398428
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
399429

400430
[unist-util-visit]: https://github.com/syntax-tree/unist-util-visit

0 commit comments

Comments
 (0)