Skip to content

Commit 77250bd

Browse files
committed
Refactor to move implementation to lib/
1 parent d890330 commit 77250bd

File tree

2 files changed

+110
-105
lines changed

2 files changed

+110
-105
lines changed

index.js

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,5 @@
11
/**
2-
* @typedef {import('./lib/types.js').Element} Element
3-
* @typedef {import('./lib/types.js').Node} Node
42
* @typedef {import('./lib/types.js').Space} Space
5-
* @typedef {import('./lib/types.js').SelectState} SelectState
63
*/
74

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'

lib/index.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @typedef {import('./types.js').Element} Element
3+
* @typedef {import('./types.js').Node} Node
4+
* @typedef {import('./types.js').Space} Space
5+
* @typedef {import('./types.js').SelectState} SelectState
6+
*/
7+
8+
import {html, svg} from 'property-information'
9+
import {queryToSelectors, walk} from './walk.js'
10+
import {parse} from './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+
}

0 commit comments

Comments
 (0)