6
6
*/
7
7
8
8
import { html , svg } from 'property-information'
9
- import { any } from './lib/any .js'
9
+ import { queryToSelectors , walk } from './lib/walk .js'
10
10
import { parse } from './lib/parse.js'
11
11
12
12
/**
@@ -27,10 +27,10 @@ import {parse} from './lib/parse.js'
27
27
* Whether `node` matches `selector`.
28
28
*/
29
29
export function matches ( selector , node , space ) {
30
- const state = createState ( node , space )
30
+ const state = createState ( selector , node , space )
31
31
state . one = true
32
32
state . shallow = true
33
- any ( parse ( selector ) , node || undefined , state )
33
+ walk ( state , node || undefined )
34
34
return state . results . length > 0
35
35
}
36
36
@@ -50,9 +50,9 @@ export function matches(selector, node, space) {
50
50
* This could be `tree` itself.
51
51
*/
52
52
export function select ( selector , tree , space ) {
53
- const state = createState ( tree , space )
53
+ const state = createState ( selector , tree , space )
54
54
state . one = true
55
- any ( parse ( selector ) , tree || undefined , state )
55
+ walk ( state , tree || undefined )
56
56
// To do in major: return `undefined` instead.
57
57
return state . results [ 0 ] || null
58
58
}
@@ -72,28 +72,31 @@ export function select(selector, tree, space) {
72
72
* This could include `tree` itself.
73
73
*/
74
74
export function selectAll ( selector , tree , space ) {
75
- const state = createState ( tree , space )
76
- any ( parse ( selector ) , tree || undefined , state )
75
+ const state = createState ( selector , tree , space )
76
+ walk ( state , tree || undefined )
77
77
return state . results
78
78
}
79
79
80
80
/**
81
+ * @param {string } selector
82
+ * Tree to search.
81
83
* @param {Node | null | undefined } [tree]
82
84
* Tree to search.
83
85
* @param {Space | null | undefined } [space='html']
84
86
* Name of namespace (`'svg'` or `'html'`).
85
87
* @returns {SelectState } SelectState
86
88
*/
87
- export function createState ( tree , space ) {
89
+ export function createState ( selector , tree , space ) {
88
90
return {
91
+ // State of the query.
92
+ rootQuery : queryToSelectors ( parse ( selector ) ) ,
89
93
results : [ ] ,
90
94
// @ts -expect-error assume elements.
91
95
scopeElements : tree ? ( tree . type === 'root' ? tree . children : [ tree ] ) : [ ] ,
92
- iterator : undefined ,
93
96
one : false ,
94
97
shallow : false ,
95
- index : false ,
96
98
found : false ,
99
+ // State in the tree.
97
100
schema : space === 'svg' ? svg : html ,
98
101
language : undefined ,
99
102
direction : 'ltr' ,
0 commit comments