5
5
* @typedef {Record<string, unknown> & {type: string, position?: Position | undefined} } NodeLike
6
6
*/
7
7
8
- import { any } from './lib/any .js'
8
+ import { queryToSelectors , walk } from './lib/walk .js'
9
9
import { parse } from './lib/parse.js'
10
10
import { parent } from './lib/util.js'
11
11
@@ -25,10 +25,10 @@ import {parent} from './lib/util.js'
25
25
* Whether `node` matches `selector`.
26
26
*/
27
27
export function matches ( selector , node ) {
28
- const state = createState ( node )
28
+ const state = createState ( selector , node )
29
29
state . one = true
30
30
state . shallow = true
31
- any ( parse ( selector ) , node || undefined , state )
31
+ walk ( state , node || undefined )
32
32
return state . results . length > 0
33
33
}
34
34
@@ -48,9 +48,9 @@ export function matches(selector, node) {
48
48
* This could be `tree` itself.
49
49
*/
50
50
export function select ( selector , tree ) {
51
- const state = createState ( tree )
51
+ const state = createState ( selector , tree )
52
52
state . one = true
53
- any ( parse ( selector ) , tree || undefined , state )
53
+ walk ( state , tree || undefined )
54
54
// To do next major: return `undefined`.
55
55
return state . results [ 0 ] || null
56
56
}
@@ -70,20 +70,23 @@ export function select(selector, tree) {
70
70
* This could include `tree` itself.
71
71
*/
72
72
export function selectAll ( selector , tree ) {
73
- const state = createState ( tree )
74
- any ( parse ( selector ) , tree || undefined , state )
73
+ const state = createState ( selector , tree )
74
+ walk ( state , tree || undefined )
75
75
return state . results
76
76
}
77
77
78
78
/**
79
+ * @param {string } selector
80
+ * Selector to parse.
79
81
* @param {Node | null | undefined } tree
82
+ * Tree to search.
80
83
* @returns {SelectState }
81
84
*/
82
- function createState ( tree ) {
85
+ function createState ( selector , tree ) {
83
86
return {
87
+ // State of the query.
88
+ rootQuery : queryToSelectors ( parse ( selector ) ) ,
84
89
results : [ ] ,
85
- any,
86
- iterator : undefined ,
87
90
scopeNodes : tree
88
91
? parent ( tree ) &&
89
92
// Root in nlcst.
@@ -93,8 +96,8 @@ function createState(tree) {
93
96
: [ ] ,
94
97
one : false ,
95
98
shallow : false ,
96
- index : false ,
97
99
found : false ,
100
+ // State in the tree.
98
101
typeIndex : undefined ,
99
102
nodeIndex : undefined ,
100
103
typeCount : undefined ,
0 commit comments