@@ -2,56 +2,25 @@ import { is, selectAll, selectOne } from 'css-select';
2
2
import { createAdapter } from './svgo/css-select-adapter.js' ;
3
3
4
4
/**
5
- * @param {Map<import('./types.js').XastNode, import('./types.js').XastParent> } parents
5
+ * @param {import('./types.js').XastParent } relativeNode
6
+ * @param {Map<import('./types.js').XastNode, import('./types.js').XastParent>= } parents
6
7
* @returns {import('css-select').Options<import('./types.js').XastNode & { children?: any }, import('./types.js').XastElement> }
7
8
*/
8
- function createCssSelectOptions ( parents ) {
9
+ function createCssSelectOptions ( relativeNode , parents ) {
9
10
return {
10
11
xmlMode : true ,
11
- adapter : createAdapter ( parents ) ,
12
+ adapter : createAdapter ( relativeNode , parents ) ,
12
13
} ;
13
14
}
14
15
15
- /**
16
- * Maps all nodes to their parent node recursively.
17
- *
18
- * @param {import('./types.js').XastParent } node
19
- * @returns {Map<import('./types.js').XastNode, import('./types.js').XastParent> }
20
- */
21
- export function mapNodesToParents ( node ) {
22
- /** @type {Map<import('./types.js').XastNode, import('./types.js').XastParent> } */
23
- const parents = new Map ( ) ;
24
-
25
- for ( const child of node . children ) {
26
- parents . set ( child , node ) ;
27
- visit (
28
- child ,
29
- {
30
- element : {
31
- enter : ( child , parent ) => {
32
- parents . set ( child , parent ) ;
33
- } ,
34
- } ,
35
- } ,
36
- node ,
37
- ) ;
38
- }
39
-
40
- return parents ;
41
- }
42
-
43
16
/**
44
17
* @param {import('./types.js').XastParent } node Element to query the children of.
45
18
* @param {string } selector CSS selector string.
46
19
* @param {Map<import('./types.js').XastNode, import('./types.js').XastParent>= } parents
47
20
* @returns {import('./types.js').XastChild[] } All matching elements.
48
21
*/
49
- export const querySelectorAll = (
50
- node ,
51
- selector ,
52
- parents = mapNodesToParents ( node ) ,
53
- ) => {
54
- return selectAll ( selector , node , createCssSelectOptions ( parents ) ) ;
22
+ export const querySelectorAll = ( node , selector , parents ) => {
23
+ return selectAll ( selector , node , createCssSelectOptions ( node , parents ) ) ;
55
24
} ;
56
25
57
26
/**
@@ -60,12 +29,8 @@ export const querySelectorAll = (
60
29
* @param {Map<import('./types.js').XastNode, import('./types.js').XastParent>= } parents
61
30
* @returns {?import('./types.js').XastChild } First match, or null if there was no match.
62
31
*/
63
- export const querySelector = (
64
- node ,
65
- selector ,
66
- parents = mapNodesToParents ( node ) ,
67
- ) => {
68
- return selectOne ( selector , node , createCssSelectOptions ( parents ) ) ;
32
+ export const querySelector = ( node , selector , parents ) => {
33
+ return selectOne ( selector , node , createCssSelectOptions ( node , parents ) ) ;
69
34
} ;
70
35
71
36
/**
@@ -74,45 +39,8 @@ export const querySelector = (
74
39
* @param {Map<import('./types.js').XastNode, import('./types.js').XastParent>= } parents
75
40
* @returns {boolean }
76
41
*/
77
- export const matches = ( node , selector , parents = mapNodesToParents ( node ) ) => {
78
- return is ( node , selector , createCssSelectOptions ( parents ) ) ;
79
- } ;
80
-
81
- export const visitSkip = Symbol ( ) ;
82
-
83
- /**
84
- * @param {import('./types.js').XastNode } node
85
- * @param {import('./types.js').Visitor } visitor
86
- * @param {any= } parentNode
87
- */
88
- export const visit = ( node , visitor , parentNode ) => {
89
- const callbacks = visitor [ node . type ] ;
90
- if ( callbacks ?. enter ) {
91
- // @ts -expect-error hard to infer
92
- const symbol = callbacks . enter ( node , parentNode ) ;
93
- if ( symbol === visitSkip ) {
94
- return ;
95
- }
96
- }
97
- // visit root children
98
- if ( node . type === 'root' ) {
99
- // copy children array to not lose cursor when children is spliced
100
- for ( const child of node . children ) {
101
- visit ( child , visitor , node ) ;
102
- }
103
- }
104
- // visit element children if still attached to parent
105
- if ( node . type === 'element' ) {
106
- if ( parentNode . children . includes ( node ) ) {
107
- for ( const child of node . children ) {
108
- visit ( child , visitor , node ) ;
109
- }
110
- }
111
- }
112
- if ( callbacks ?. exit ) {
113
- // @ts -expect-error hard to infer
114
- callbacks . exit ( node , parentNode ) ;
115
- }
42
+ export const matches = ( node , selector , parents ) => {
43
+ return is ( node , selector , createCssSelectOptions ( node , parents ) ) ;
116
44
} ;
117
45
118
46
/**
0 commit comments