-
Notifications
You must be signed in to change notification settings - Fork 784
/
matches.js
294 lines (269 loc) · 7.45 KB
/
matches.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
import cssParser from './css-parser';
/**
* matches implementation that operates on a VirtualNode
*
* @method matches
* @memberof axe.utils
* @param {VirtualNode} vNode VirtualNode to match
* @param {String} selector CSS selector string
* @return {Boolean}
*/
export default function matches(vNode, selector) {
const expressions = convertSelector(selector);
return expressions.some(expression => matchesExpression(vNode, expression));
}
function matchesTag(vNode, exp) {
return (
vNode.props.nodeType === 1 &&
(exp.tag === '*' || vNode.props.nodeName === exp.tag)
);
}
function matchesClasses(vNode, exp) {
return !exp.classes || exp.classes.every(cl => vNode.hasClass(cl.value));
}
function matchesAttributes(vNode, exp) {
return (
!exp.attributes ||
exp.attributes.every(att => {
const nodeAtt = vNode.attr(att.key);
return nodeAtt !== null && att.test(nodeAtt);
})
);
}
function matchesId(vNode, exp) {
return !exp.id || vNode.props.id === exp.id;
}
function matchesPseudos(target, exp) {
if (
!exp.pseudos ||
exp.pseudos.every(pseudo => {
if (pseudo.name === 'not') {
return !pseudo.expressions.some(expression => {
return matchesExpression(target, expression);
});
} else if (pseudo.name === 'is') {
return pseudo.expressions.some(expression => {
return matchesExpression(target, expression);
});
}
throw new Error(
'the pseudo selector ' + pseudo.name + ' has not yet been implemented'
);
})
) {
return true;
}
return false;
}
function matchExpression(vNode, expression) {
return (
matchesTag(vNode, expression) &&
matchesClasses(vNode, expression) &&
matchesAttributes(vNode, expression) &&
matchesId(vNode, expression) &&
matchesPseudos(vNode, expression)
);
}
const escapeRegExp = (() => {
/*! Credit: XRegExp 0.6.1 (c) 2007-2008 Steven Levithan <http://stevenlevithan.com/regex/xregexp/> MIT License */
const from = /(?=[\-\[\]{}()*+?.\\\^$|,#\s])/g;
const to = '\\';
return string => {
return string.replace(from, to);
};
})();
const reUnescape = /\\/g;
function convertAttributes(atts) {
/*! Credit Mootools Copyright Mootools, MIT License */
if (!atts) {
return;
}
return atts.map(att => {
const attributeKey = att.name.replace(reUnescape, '');
const attributeValue = (att.value || '').replace(reUnescape, '');
let test, regexp;
switch (att.operator) {
case '^=':
regexp = new RegExp('^' + escapeRegExp(attributeValue));
break;
case '$=':
regexp = new RegExp(escapeRegExp(attributeValue) + '$');
break;
case '~=':
regexp = new RegExp(
'(^|\\s)' + escapeRegExp(attributeValue) + '(\\s|$)'
);
break;
case '|=':
regexp = new RegExp('^' + escapeRegExp(attributeValue) + '(-|$)');
break;
case '=':
test = value => {
return attributeValue === value;
};
break;
case '*=':
test = value => {
return value && value.includes(attributeValue);
};
break;
case '!=':
test = value => {
return attributeValue !== value;
};
break;
// attribute existence
default:
test = value => {
return value !== null;
};
}
if (attributeValue === '' && /^[*$^]=$/.test(att.operator)) {
test = () => {
return false;
};
}
if (!test) {
test = value => {
return value && regexp.test(value);
};
}
return {
key: attributeKey,
value: attributeValue,
type: typeof att.value === 'undefined' ? 'attrExist' : 'attrValue',
test: test
};
});
}
function convertClasses(classes) {
if (!classes) {
return;
}
return classes.map(className => {
className = className.replace(reUnescape, '');
return {
value: className,
regexp: new RegExp('(^|\\s)' + escapeRegExp(className) + '(\\s|$)')
};
});
}
function convertPseudos(pseudos) {
if (!pseudos) {
return;
}
return pseudos.map(p => {
let expressions;
if (['is', 'not'].includes(p.name)) {
expressions = p.value;
expressions = expressions.selectors
? expressions.selectors
: [expressions];
expressions = convertExpressions(expressions);
}
return {
name: p.name,
expressions: expressions,
value: p.value
};
});
}
/**
* convert the css-selector-parser format into the Slick format
* @private
* @param Array {Object} expressions
* @return Array {Object}
*
*/
function convertExpressions(expressions) {
return expressions.map(exp => {
const newExp = [];
let rule = exp.rule;
while (rule) {
/* eslint no-restricted-syntax: 0 */
// `.tagName` is a property coming from the `CSSSelectorParser` library
newExp.push({
tag: rule.tagName ? rule.tagName.toLowerCase() : '*',
combinator: rule.nestingOperator ? rule.nestingOperator : ' ',
id: rule.id,
attributes: convertAttributes(rule.attrs),
classes: convertClasses(rule.classNames),
pseudos: convertPseudos(rule.pseudos)
});
rule = rule.rule;
}
return newExp;
});
}
/**
* Convert a CSS selector to the Slick format expression
*
* @private
* @param {String} selector CSS selector to convert
* @returns {Object[]} Array of Slick format expressions
*/
export function convertSelector(selector) {
let expressions = cssParser.parse(selector);
expressions = expressions.selectors ? expressions.selectors : [expressions];
return convertExpressions(expressions);
}
/**
* Determine if a virtual node matches a Slick format CSS expression.
*
* Note: this function is in the hot path, avoid memory allocation.
*
* @private
* @method optimizedMatchesExpression
* @memberof axe.utils
* @param {VirtualNode} vNode VirtualNode to match
* @param {Object|Object[]} expressions CSS selector expression or array of expressions
* @param {Number|NaN} index index of expression to match on if expressions is array
* @returns {Boolean}
*/
function optimizedMatchesExpression(vNode, expressions, index, matchAnyParent) {
if (!vNode) {
return false;
}
const isArray = Array.isArray(expressions);
const expression = isArray ? expressions[index] : expressions;
let machedExpression = matchExpression(vNode, expression);
while (!machedExpression && matchAnyParent && vNode.parent) {
vNode = vNode.parent;
machedExpression = matchExpression(vNode, expression);
}
if (index > 0) {
if ([' ', '>'].includes(expression.combinator) === false) {
throw new Error(
'axe.utils.matchesExpression does not support the combinator: ' +
expression.combinator
);
}
machedExpression =
machedExpression &&
optimizedMatchesExpression(
vNode.parent,
expressions,
index - 1,
expression.combinator === ' '
);
}
return machedExpression;
}
/**
* Determine if a virtual node matches a Slick format CSS expression
*
* @private
* @method matchesExpression
* @memberof axe.utils
* @param {VirtualNode} vNode VirtualNode to match
* @param {Object|Object[]} expressions CSS selector expression or array of expressions
* @returns {Boolean}
*/
export function matchesExpression(vNode, expressions, matchAnyParent) {
return optimizedMatchesExpression(
vNode,
expressions,
expressions.length - 1,
matchAnyParent
);
}