-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
selectors.js
431 lines (407 loc) · 14.2 KB
/
selectors.js
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import { createParser } from 'rst-selector-parser';
import values from 'object.values';
import flat from 'array.prototype.flat';
import is from 'object-is';
import has from 'has';
import {
treeFilter,
nodeHasId,
findParentNode,
nodeMatchesObjectProps,
childrenOfNode,
hasClassName,
} from './RSTTraversal';
import { nodeHasType, propsOfNode } from './Utils';
import getAdapter from './getAdapter';
// our CSS selector parser instance
const parser = createParser();
// Combinators that allow you to chance selectors
const CHILD = 'childCombinator';
const ADJACENT_SIBLING = 'adjacentSiblingCombinator';
const GENERAL_SIBLING = 'generalSiblingCombinator';
const DESCENDANT = 'descendantCombinator';
// Selectors for targeting elements
const SELECTOR = 'selector';
const TYPE_SELECTOR = 'typeSelector';
const CLASS_SELECTOR = 'classSelector';
const ID_SELECTOR = 'idSelector';
const ATTRIBUTE_PRESENCE = 'attributePresenceSelector';
const ATTRIBUTE_VALUE = 'attributeValueSelector';
// @TODO we dont support these, throw if they are used
const PSEUDO_CLASS = 'pseudoClassSelector';
const PSEUDO_ELEMENT = 'pseudoElementSelector';
const EXACT_ATTRIBUTE_OPERATOR = '=';
const WHITELIST_ATTRIBUTE_OPERATOR = '~=';
const HYPHENATED_ATTRIBUTE_OPERATOR = '|=';
const PREFIX_ATTRIBUTE_OPERATOR = '^=';
const SUFFIX_ATTRIBUTE_OPERATOR = '$=';
const SUBSTRING_ATTRIBUTE_OPERATOR = '*=';
function unique(arr) {
return [...new Set(arr)];
}
/**
* Calls reduce on a array of nodes with the passed
* function, returning only unique results.
* @param {Function} fn
* @param {Array<Node>} nodes
*/
function uniqueReduce(fn, nodes) {
return unique(nodes.reduce(fn, []));
}
/**
* Takes a CSS selector and returns a set of tokens parsed
* by scalpel.
* @param {String} selector
*/
function safelyGenerateTokens(selector) {
try {
return parser.parse(selector);
} catch (err) {
throw new Error(`Failed to parse selector: ${selector}`);
}
}
function matchAttributeSelector(node, token) {
const { operator, value, name } = token;
const nodeProps = propsOfNode(node);
const descriptor = Object.getOwnPropertyDescriptor(nodeProps, name);
if (descriptor && descriptor.get) {
return false;
}
const nodePropValue = nodeProps[name];
if (typeof nodePropValue === 'undefined') {
return false;
}
if (token.type === ATTRIBUTE_PRESENCE) {
return has(nodeProps, token.name);
}
// Only the exact value operator ("=") can match non-strings
if (typeof nodePropValue !== 'string' || typeof value !== 'string') {
if (operator !== EXACT_ATTRIBUTE_OPERATOR) {
return false;
}
}
switch (operator) {
/**
* Represents an element with the att attribute whose value is exactly "val".
* @example
* [attr="val"] matches attr="val"
*/
case EXACT_ATTRIBUTE_OPERATOR:
return is(nodePropValue, value);
/**
* Represents an element with the att attribute whose value is a whitespace-separated
* list of words, one of which is exactly
* @example
* [rel~="copyright"] matches rel="copyright other"
*/
case WHITELIST_ATTRIBUTE_OPERATOR:
return nodePropValue.split(' ').indexOf(value) !== -1;
/**
* Represents an element with the att attribute, its value either being exactly the
* value or beginning with the value immediately followed by "-"
* @example
* [hreflang|="en"] matches hreflang="en-US"
*/
case HYPHENATED_ATTRIBUTE_OPERATOR:
return nodePropValue === value || nodePropValue.startsWith(`${value}-`);
/**
* Represents an element with the att attribute whose value begins with the prefix value.
* If the value is the empty string then the selector does not represent anything.
* @example
* [type^="image"] matches type="imageobject"
*/
case PREFIX_ATTRIBUTE_OPERATOR:
return value === '' ? false : nodePropValue.slice(0, value.length) === value;
/**
* Represents an element with the att attribute whose value ends with the suffix value.
* If the value is the empty string then the selector does not represent anything.
* @example
* [type$="image"] matches type="imageobject"
*/
case SUFFIX_ATTRIBUTE_OPERATOR:
return value === '' ? false : nodePropValue.slice(-value.length) === value;
/**
* Represents an element with the att attribute whose value contains at least one
* instance of the value. If value is the empty string then the
* selector does not represent anything.
* @example
* [title*="hello"] matches title="well hello there"
*/
case SUBSTRING_ATTRIBUTE_OPERATOR:
return value === '' ? false : nodePropValue.indexOf(value) !== -1;
default:
throw new Error(`Enzyme::Selector: Unknown attribute selector operator "${operator}"`);
}
}
function matchPseudoSelector(node, token, root) {
const { name, parameters } = token;
if (name === 'not') {
// eslint-disable-next-line no-use-before-define
return parameters.every(selector => reduceTreeBySelector(selector, node).length === 0);
}
if (name === 'empty') {
return treeFilter(node, n => n !== node).length === 0;
}
if (name === 'first-child') {
const { rendered } = findParentNode(root, node);
const [firstChild] = rendered;
return firstChild === node;
}
if (name === 'last-child') {
const { rendered } = findParentNode(root, node);
return rendered[rendered.length - 1] === node;
}
throw new TypeError(`Enzyme::Selector does not support the "${token.name}" pseudo-element or pseudo-class selectors.`);
}
/**
* Takes a node and a token and determines if the node
* matches the predicate defined by the token.
* @param {Node} node
* @param {Token} token
*/
function nodeMatchesToken(node, token, root) {
if (node === null || typeof node === 'string') {
return false;
}
switch (token.type) {
/**
* Match against the className prop
* @example '.active' matches <div className='active' />
*/
case CLASS_SELECTOR:
return hasClassName(node, token.name);
/**
* Simple type matching
* @example 'div' matches <div />
*/
case TYPE_SELECTOR:
return nodeHasType(node, token.name);
/**
* Match against the `id` prop
* @example '#nav' matches <ul id="nav" />
*/
case ID_SELECTOR:
return nodeHasId(node, token.name);
/**
* Matches if an attribute is present, regardless
* of its value
* @example '[disabled]' matches <a disabled />
*/
case ATTRIBUTE_PRESENCE:
return matchAttributeSelector(node, token);
/**
* Matches if an attribute is present with the
* provided value
* @example '[data-foo=foo]' matches <div data-foo="foo" />
*/
case ATTRIBUTE_VALUE:
return matchAttributeSelector(node, token);
case PSEUDO_ELEMENT:
case PSEUDO_CLASS:
return matchPseudoSelector(node, token, root);
default:
throw new Error(`Unknown token type: ${token.type}`);
}
}
/**
* Returns a predicate function that checks if a
* node matches every token in the body of a selector
* token.
* @param {Token} token
*/
function buildPredicateFromToken(token, root) {
return node => token.body.every(bodyToken => nodeMatchesToken(node, bodyToken, root));
}
/**
* Returns whether a parsed selector is a complex selector, which
* is defined as a selector that contains combinators.
* @param {Array<Token>} tokens
*/
function isComplexSelector(tokens) {
return tokens.some(token => token.type !== SELECTOR);
}
/**
* Takes a component constructor, object, or string representing
* a simple selector and returns a predicate function that can
* be applied to a single node.
* @param {Function|Object|String} selector
*/
export function buildPredicate(selector) {
// If the selector is a string, parse it as a simple CSS selector
if (typeof selector === 'string') {
const tokens = safelyGenerateTokens(selector);
if (isComplexSelector(tokens)) {
throw new TypeError('This method does not support complex CSS selectors');
}
// Simple selectors only have a single selector token
return buildPredicateFromToken(tokens[0]);
}
// If the selector is an element type, check if the node's type matches
const adapter = getAdapter();
const isElementType = adapter.isValidElementType
? adapter.isValidElementType(selector)
: typeof selector === 'function';
if (isElementType) {
return node => node && node.type === selector;
}
// If the selector is an non-empty object, treat the keys/values as props
if (typeof selector === 'object') {
if (!Array.isArray(selector) && selector !== null && Object.keys(selector).length > 0) {
const hasUndefinedValues = values(selector).some(value => typeof value === 'undefined');
if (hasUndefinedValues) {
throw new TypeError('Enzyme::Props can’t have `undefined` values. Try using ‘findWhere()’ instead.');
}
return node => nodeMatchesObjectProps(node, selector);
}
throw new TypeError('Enzyme::Selector does not support an array, null, or empty object as a selector');
}
throw new TypeError('Enzyme::Selector expects a string, object, or valid element type (Component Constructor)');
}
/**
* Matches only nodes which are adjacent siblings (direct next sibling)
* against a predicate, returning those that match.
* @param {Array<Node>} nodes
* @param {Function} predicate
* @param {Node} root
*/
function matchAdjacentSiblings(nodes, predicate, root) {
return nodes.reduce((matches, node) => {
const parent = findParentNode(root, node);
// If there's no parent, there's no siblings
if (!parent) {
return matches;
}
const parentChildren = childrenOfNode(parent);
const nodeIndex = parentChildren.indexOf(node);
const adjacentSibling = parentChildren[nodeIndex + 1];
// No sibling
if (!adjacentSibling) {
return matches;
}
if (predicate(adjacentSibling)) {
matches.push(adjacentSibling);
}
return matches;
}, []);
}
/**
* Matches only nodes which are general siblings (any sibling *after*)
* against a predicate, returning those that match.
* @param {Array<Node>} nodes
* @param {Function} predicate
* @param {Node} root
*/
function matchGeneralSibling(nodes, predicate, root) {
return uniqueReduce((matches, node) => {
const parent = findParentNode(root, node);
if (!parent) {
return matches;
}
const parentChildren = childrenOfNode(parent);
const nodeIndex = parentChildren.indexOf(node);
const youngerSiblings = parentChildren.slice(nodeIndex + 1);
return matches.concat(youngerSiblings.filter(predicate));
}, nodes);
}
/**
* Matches only nodes which are direct children (not grandchildren, etc.)
* against a predicate, returning those that match.
* @param {Array<Node>} nodes
* @param {Function} predicate
*/
function matchDirectChild(nodes, predicate) {
return uniqueReduce(
(matches, node) => matches.concat(childrenOfNode(node).filter(predicate)),
nodes,
);
}
/**
* Matches all descendant nodes against a predicate,
* returning those that match.
* @param {Array<Node>} nodes
* @param {Function} predicate
*/
function matchDescendant(nodes, predicate) {
return uniqueReduce(
(matches, node) => matches.concat(treeFilter(node, predicate)),
flat(nodes.map(childrenOfNode)),
);
}
/**
* Takes an RST and reduces it to a set of nodes matching
* the selector. The selector can be a simple selector, which
* is handled by `buildPredicate`, or a complex CSS selector which
* reduceTreeBySelector parses and reduces the tree based on the combinators.
* @param {Function|Object|String} selector
* @param {RSTNode} root
*/
export function reduceTreeBySelector(selector, root) {
if (typeof selector === 'function' || typeof selector === 'object') {
return treeFilter(root, buildPredicate(selector));
}
let results = [];
if (typeof selector === 'string') {
const tokens = safelyGenerateTokens(selector);
let index = 0;
while (index < tokens.length) {
const token = tokens[index];
/**
* There are two types of tokens in a CSS selector:
*
* 1. Selector tokens. These target nodes directly, like
* type or attribute selectors. These are easy to apply
* because we can traverse the tree and return only
* the nodes that match the predicate.
*
* 2. Combinator tokens. These tokens chain together
* selector nodes. For example > for children, or +
* for adjacent siblings. These are harder to match
* as we have to track where in the tree we are
* to determine if a selector node applies or not.
*/
if (token.type === SELECTOR) {
const predicate = buildPredicateFromToken(token, root);
results = results.concat(treeFilter(root, predicate));
} else {
// We can assume there always all previously matched tokens since selectors
// cannot start with combinators.
const { type } = token;
// We assume the next token is a selector, so move the index
// forward and build the predicate.
index += 1;
const predicate = buildPredicateFromToken(tokens[index], root);
// We match against only the nodes which have already been matched,
// since a combinator is meant to refine a previous selector.
switch (type) {
// The + combinator
case ADJACENT_SIBLING:
results = matchAdjacentSiblings(results, predicate, root);
break;
// The ~ combinator
case GENERAL_SIBLING:
results = matchGeneralSibling(results, predicate, root);
break;
// The > combinator
case CHILD:
results = matchDirectChild(results, predicate);
break;
// The ' ' (whitespace) combinator
case DESCENDANT: {
results = matchDescendant(results, predicate);
break;
}
default:
throw new Error(`Unknown combinator selector: ${type}`);
}
}
index += 1;
}
} else {
throw new TypeError('Enzyme::Selector expects a string, object, or Component Constructor');
}
return results;
}
export function reduceTreesBySelector(selector, roots) {
const results = roots.map(n => reduceTreeBySelector(selector, n));
return unique(flat(results, 1));
}