-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.js
40 lines (37 loc) · 1.27 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @typedef {import('css-selector-parser').AstRule} AstRule
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('./types.js').SelectState} SelectState
*/
import {attribute} from './attribute.js'
import {pseudo} from './pseudo.js'
/**
* @param {AstRule} query
* @param {Node} node
* @param {number | undefined} index
* @param {Parent | undefined} parent
* @param {SelectState} state
* @returns {boolean}
*/
export function test(query, node, index, parent, state) {
for (const item of query.items) {
// eslint-disable-next-line unicorn/prefer-switch
if (item.type === 'Attribute') {
if (!attribute(item, node)) return false
} else if (item.type === 'Id') {
throw new Error('Invalid selector: id')
} else if (item.type === 'ClassName') {
throw new Error('Invalid selector: class')
} else if (item.type === 'PseudoClass') {
if (!pseudo(item, node, index, parent, state)) return false
} else if (item.type === 'PseudoElement') {
throw new Error('Invalid selector: `::' + item.name + '`')
} else if (item.type === 'TagName') {
if (item.name !== node.type) return false
} else {
// Otherwise `item.type` is `WildcardTag`, which matches.
}
}
return true
}