Skip to content

Commit b0d92b1

Browse files
committed
Fix to return results in tree order
Related-to: syntax-tree/unist-util-select#14.
1 parent cb1abde commit b0d92b1

13 files changed

+732
-860
lines changed

index.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import {html, svg} from 'property-information'
9-
import {any} from './lib/any.js'
9+
import {queryToSelectors, walk} from './lib/walk.js'
1010
import {parse} from './lib/parse.js'
1111

1212
/**
@@ -27,10 +27,10 @@ import {parse} from './lib/parse.js'
2727
* Whether `node` matches `selector`.
2828
*/
2929
export function matches(selector, node, space) {
30-
const state = createState(node, space)
30+
const state = createState(selector, node, space)
3131
state.one = true
3232
state.shallow = true
33-
any(parse(selector), node || undefined, state)
33+
walk(state, node || undefined)
3434
return state.results.length > 0
3535
}
3636

@@ -50,9 +50,9 @@ export function matches(selector, node, space) {
5050
* This could be `tree` itself.
5151
*/
5252
export function select(selector, tree, space) {
53-
const state = createState(tree, space)
53+
const state = createState(selector, tree, space)
5454
state.one = true
55-
any(parse(selector), tree || undefined, state)
55+
walk(state, tree || undefined)
5656
// To do in major: return `undefined` instead.
5757
return state.results[0] || null
5858
}
@@ -72,28 +72,31 @@ export function select(selector, tree, space) {
7272
* This could include `tree` itself.
7373
*/
7474
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)
7777
return state.results
7878
}
7979

8080
/**
81+
* @param {string} selector
82+
* Tree to search.
8183
* @param {Node | null | undefined} [tree]
8284
* Tree to search.
8385
* @param {Space | null | undefined} [space='html']
8486
* Name of namespace (`'svg'` or `'html'`).
8587
* @returns {SelectState} SelectState
8688
*/
87-
export function createState(tree, space) {
89+
export function createState(selector, tree, space) {
8890
return {
91+
// State of the query.
92+
rootQuery: queryToSelectors(parse(selector)),
8993
results: [],
9094
// @ts-expect-error assume elements.
9195
scopeElements: tree ? (tree.type === 'root' ? tree.children : [tree]) : [],
92-
iterator: undefined,
9396
one: false,
9497
shallow: false,
95-
index: false,
9698
found: false,
99+
// State in the tree.
97100
schema: space === 'svg' ? svg : html,
98101
language: undefined,
99102
direction: 'ltr',

lib/any.js

-154
This file was deleted.

lib/class-name.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @returns {boolean}
1212
*/
1313
export function className(query, element) {
14-
/** @type {Array<string>} */
14+
/** @type {readonly string[]} */
1515
// @ts-expect-error Assume array.
1616
const value = element.properties.className || []
1717
let index = -1

0 commit comments

Comments
 (0)