forked from syntax-tree/unist-util-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
any.js
140 lines (114 loc) · 2.78 KB
/
any.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict'
module.exports = match
var zwitch = require('zwitch')
var test = require('./test')
var nest = require('./nest')
var type = zwitch('type', {
unknown: unknownType,
invalid: invalidType,
handlers: {
selectors: selectors,
ruleSet: ruleSet,
rule: rule
}
})
function match(query, node, state) {
return query && node ? type(query, node, state) : []
}
function selectors(query, node, state) {
var collect = collector(state.one)
var index = -1
while (++index < query.selectors.length) {
collect(ruleSet(query.selectors[index], node, state))
}
return collect.result
}
function ruleSet(query, node, state) {
return rule(query.rule, node, state)
}
function rule(query, tree, state) {
var collect = collector(state.one)
if (state.shallow && query.rule) {
throw new Error('Expected selector without nesting')
}
nest(
query,
tree,
0,
null,
configure(query, {
scopeNodes: tree.type === 'root' ? tree.children : [tree],
iterator: iterator,
one: state.one,
shallow: state.shallow,
any: state.any
})
)
return collect.result
function iterator(query, node, index, parent, state) {
if (test(query, node, index, parent, state)) {
if (query.rule) {
nest(query.rule, node, index, parent, configure(query.rule, state))
} else {
collect(node)
state.found = true
}
}
}
}
function configure(query, state) {
var needsIndex = [
'first-child',
'first-of-type',
'last-child',
'last-of-type',
'nth-child',
'nth-last-child',
'nth-of-type',
'nth-last-of-type',
'only-child',
'only-of-type'
]
var pseudos = query.pseudos || []
var index = -1
while (++index < pseudos.length) {
if (needsIndex.indexOf(pseudos[index].name) > -1) {
state.index = true
break
}
}
return state
}
/* istanbul ignore next - Shouldn’t be invoked, all data is handled. */
function unknownType(query) {
throw new Error('Unknown type `' + query.type + '`')
}
/* istanbul ignore next - Shouldn’t be invoked, parser gives correct data. */
function invalidType() {
throw new Error('Invalid type')
}
function collector(one) {
var result = []
var found
collect.result = result
return collect
/* Append nodes to array, filtering out duplicates. */
function collect(node) {
var index = -1
if ('length' in node) {
while (++index < node.length) {
collectOne(node[index])
}
} else {
collectOne(node)
}
}
function collectOne(node) {
if (one) {
/* istanbul ignore if - shouldn’t happen, safeguards performance problems. */
if (found) throw new Error('Cannot collect multiple nodes')
found = true
}
if (result.indexOf(node) < 0) result.push(node)
}
}