forked from capricorn86/happy-dom
/
QuerySelector.ts
271 lines (238 loc) · 6.58 KB
/
QuerySelector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import Element from '../nodes/element/Element';
import IElement from '../nodes/element/IElement';
import INode from '../nodes/node/INode';
import Node from '../nodes/node/Node';
import INodeList from '../nodes/node/INodeList';
import SelectorItem from './SelectorItem';
import NodeListFactory from '../nodes/node/NodeListFactory';
const SELECTOR_PART_REGEXP = /(\[[^\]]+\]|[a-zA-Z0-9-_.#"*:()\]]+)|([ ,>]+)/g;
/**
* Utility for query selection in an HTML element.
*
* @class QuerySelector
*/
export default class QuerySelector {
/**
* Finds elements based on a query selector.
*
* @param node Node to search in.
* @param selector Selector.
* @returns HTML elements.
*/
public static querySelectorAll(node: INode, selector: string): INodeList<IElement> {
const matches = <INodeList<IElement>>NodeListFactory.create();
for (const parts of this.getSelectorParts(selector)) {
for (const element of this.findAll(node, [node], parts)) {
if (!matches.includes(element)) {
matches.push(element);
}
}
}
return matches;
}
/**
* Finds an element based on a query selector.
*
* @param node Node to search in.
* @param selector Selector.
* @returns HTML element.
*/
public static querySelector(node: INode, selector: string): IElement {
for (const parts of this.getSelectorParts(selector)) {
const match = this.findFirst(node, [node], parts);
if (match) {
return match;
}
}
return null;
}
/**
* Checks if a node matches a selector and returns priority weight.
*
* @param node Node to search in.
* @param selector Selector.
* @returns Result.
*/
public static match(node: INode, selector: string): { priorityWeight: number; matches: boolean } {
for (const parts of this.getSelectorParts(selector)) {
const result = this.matchesSelector(node, node, parts.reverse());
if (result.matches) {
return result;
}
}
return { priorityWeight: 0, matches: false };
}
/**
* Checks if a node matches a selector.
*
* @param targetNode Target node.
* @param currentNode Current node.
* @param selectorParts Selector parts.
* @param [priorityWeight] Priority weight.
* @returns Result.
*/
private static matchesSelector(
targetNode: INode,
currentNode: INode,
selectorParts: string[],
priorityWeight = 0
): {
priorityWeight: number;
matches: boolean;
} {
const isDirectChild = selectorParts[0] === '>';
if (isDirectChild) {
selectorParts = selectorParts.slice(1);
if (selectorParts.length === 0) {
return { priorityWeight: 0, matches: false };
}
}
if (selectorParts.length === 0) {
return { priorityWeight, matches: true };
}
const selector = new SelectorItem(selectorParts[0]);
const result = selector.match(<IElement>currentNode);
if (result.matches && selectorParts.length === 1) {
return {
priorityWeight: priorityWeight + result.priorityWeight,
matches: true
};
}
if (!currentNode.parentElement || (targetNode === currentNode && !result.matches)) {
return { priorityWeight: 0, matches: false };
}
return this.matchesSelector(
isDirectChild ? currentNode.parentElement : targetNode,
currentNode.parentElement,
result.matches ? selectorParts.slice(1) : selectorParts,
priorityWeight + result.priorityWeight
);
}
/**
* Finds elements based on a query selector for a part of a list of selectors separated with comma.
*
* @param rootNode Root node.
* @param nodes Nodes.
* @param selectorParts Selector parts.
* @param [selectorItem] Selector item.
* @returns HTML elements.
*/
private static findAll(
rootNode: INode,
nodes: INode[],
selectorParts: string[],
selectorItem?: SelectorItem
): IElement[] {
const isDirectChild = selectorParts[0] === '>';
if (isDirectChild) {
selectorParts = selectorParts.slice(1);
}
const selector = selectorItem || new SelectorItem(selectorParts[0]);
let matched = [];
for (const node of nodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
if (selector.match(<Element>node).matches) {
if (selectorParts.length === 1) {
if (rootNode !== node) {
matched.push(node);
}
} else {
matched = matched.concat(
this.findAll(rootNode, (<Element>node).children, selectorParts.slice(1), null)
);
}
}
}
if (!isDirectChild && node['children']) {
matched = matched.concat(this.findAll(rootNode, node['children'], selectorParts, selector));
}
}
return matched;
}
/**
* Finds an element based on a query selector for a part of a list of selectors separated with comma.
*
* @param rootNode
* @param nodes Nodes.
* @param selector Selector.
* @param selectorParts
* @param [selectorItem] Selector item.
* @returns HTML element.
*/
private static findFirst(
rootNode: INode,
nodes: INode[],
selectorParts: string[],
selectorItem?: SelectorItem
): IElement {
const isDirectChild = selectorParts[0] === '>';
if (isDirectChild) {
selectorParts = selectorParts.slice(1);
}
const selector = selectorItem || new SelectorItem(selectorParts[0]);
for (const node of nodes) {
if (node.nodeType === Node.ELEMENT_NODE && selector.match(<Element>node).matches) {
if (selectorParts.length === 1) {
if (rootNode !== node) {
return <Element>node;
}
} else {
const childSelector = this.findFirst(
rootNode,
(<Element>node).children,
selectorParts.slice(1),
null
);
if (childSelector) {
return childSelector;
}
}
}
if (!isDirectChild && node['children']) {
const childSelector = this.findFirst(rootNode, node['children'], selectorParts, selector);
if (childSelector) {
return childSelector;
}
}
}
return null;
}
/**
* Splits a selector string into groups and parts.
*
* @param selector Selector.
* @returns HTML element.
*/
private static getSelectorParts(selector: string): string[][] {
if (selector === '*' || (!selector.includes(',') && !selector.includes(' '))) {
return [[selector]];
}
const regexp = new RegExp(SELECTOR_PART_REGEXP);
const groups = [];
let currentSelector = '';
let parts = [];
let match;
while ((match = regexp.exec(selector))) {
if (match[2]) {
const trimmed = match[2].trim();
parts.push(currentSelector);
currentSelector = '';
if (trimmed === ',') {
groups.push(parts);
parts = [];
} else if (trimmed === '>') {
parts.push('>');
}
} else if (match[1]) {
currentSelector += match[1];
}
}
if (currentSelector !== '') {
parts.push(currentSelector);
}
if (parts.length > 0) {
groups.push(parts);
}
return groups;
}
}