|
1 | 1 | /** |
2 | | - * @typedef {import('./lib/types.js').Element} Element |
3 | | - * @typedef {import('./lib/types.js').Node} Node |
4 | 2 | * @typedef {import('./lib/types.js').Space} Space |
5 | | - * @typedef {import('./lib/types.js').SelectState} SelectState |
6 | 3 | */ |
7 | 4 |
|
8 | | -import {html, svg} from 'property-information' |
9 | | -import {queryToSelectors, walk} from './lib/walk.js' |
10 | | -import {parse} from './lib/parse.js' |
11 | | - |
12 | | -/** |
13 | | - * Check that the given `node` matches `selector`. |
14 | | - * |
15 | | - * This only checks the element itself, not the surrounding tree. |
16 | | - * Thus, nesting in selectors is not supported (`p b`, `p > b`), neither are |
17 | | - * selectors like `:first-child`, etc. |
18 | | - * This only checks that the given element matches the selector. |
19 | | - * |
20 | | - * @param {string} selector |
21 | | - * CSS selector, such as (`h1`, `a, b`). |
22 | | - * @param {Node | null | undefined} [node] |
23 | | - * Node that might match `selector`, should be an element. |
24 | | - * @param {Space | null | undefined} [space='html'] |
25 | | - * Name of namespace (`'svg'` or `'html'`). |
26 | | - * @returns {boolean} |
27 | | - * Whether `node` matches `selector`. |
28 | | - */ |
29 | | -export function matches(selector, node, space) { |
30 | | - const state = createState(selector, node, space) |
31 | | - state.one = true |
32 | | - state.shallow = true |
33 | | - walk(state, node || undefined) |
34 | | - return state.results.length > 0 |
35 | | -} |
36 | | - |
37 | | -/** |
38 | | - * Select the first element that matches `selector` in the given `tree`. |
39 | | - * Searches the tree in *preorder*. |
40 | | - * |
41 | | - * @param {string} selector |
42 | | - * CSS selector, such as (`h1`, `a, b`). |
43 | | - * @param {Node | null | undefined} [tree] |
44 | | - * Tree to search. |
45 | | - * @param {Space | null | undefined} [space='html'] |
46 | | - * Name of namespace (`'svg'` or `'html'`). |
47 | | - * @returns {Element | null} |
48 | | - * First element in `tree` that matches `selector` or `null` if nothing is |
49 | | - * found. |
50 | | - * This could be `tree` itself. |
51 | | - */ |
52 | | -export function select(selector, tree, space) { |
53 | | - const state = createState(selector, tree, space) |
54 | | - state.one = true |
55 | | - walk(state, tree || undefined) |
56 | | - // To do in major: return `undefined` instead. |
57 | | - return state.results[0] || null |
58 | | -} |
59 | | - |
60 | | -/** |
61 | | - * Select all elements that match `selector` in the given `tree`. |
62 | | - * Searches the tree in *preorder*. |
63 | | - * |
64 | | - * @param {string} selector |
65 | | - * CSS selector, such as (`h1`, `a, b`). |
66 | | - * @param {Node | null | undefined} [tree] |
67 | | - * Tree to search. |
68 | | - * @param {Space | null | undefined} [space='html'] |
69 | | - * Name of namespace (`'svg'` or `'html'`). |
70 | | - * @returns {Array<Element>} |
71 | | - * Elements in `tree` that match `selector`. |
72 | | - * This could include `tree` itself. |
73 | | - */ |
74 | | -export function selectAll(selector, tree, space) { |
75 | | - const state = createState(selector, tree, space) |
76 | | - walk(state, tree || undefined) |
77 | | - return state.results |
78 | | -} |
79 | | - |
80 | | -/** |
81 | | - * @param {string} selector |
82 | | - * Tree to search. |
83 | | - * @param {Node | null | undefined} [tree] |
84 | | - * Tree to search. |
85 | | - * @param {Space | null | undefined} [space='html'] |
86 | | - * Name of namespace (`'svg'` or `'html'`). |
87 | | - * @returns {SelectState} SelectState |
88 | | - */ |
89 | | -function createState(selector, tree, space) { |
90 | | - return { |
91 | | - // State of the query. |
92 | | - rootQuery: queryToSelectors(parse(selector)), |
93 | | - results: [], |
94 | | - // @ts-expect-error assume elements. |
95 | | - scopeElements: tree ? (tree.type === 'root' ? tree.children : [tree]) : [], |
96 | | - one: false, |
97 | | - shallow: false, |
98 | | - found: false, |
99 | | - // State in the tree. |
100 | | - schema: space === 'svg' ? svg : html, |
101 | | - language: undefined, |
102 | | - direction: 'ltr', |
103 | | - editableOrEditingHost: false, |
104 | | - typeIndex: undefined, |
105 | | - elementIndex: undefined, |
106 | | - typeCount: undefined, |
107 | | - elementCount: undefined |
108 | | - } |
109 | | -} |
| 5 | +export {matches, select, selectAll} from './lib/index.js' |
0 commit comments