forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.js
334 lines (285 loc) · 8.23 KB
/
rule.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
/*global RuleResult, createExecutionContext, SupportError */
function Rule(spec, parentAudit) {
/*jshint maxcomplexity:11 */
'use strict';
this._audit = parentAudit;
/**
* The code, or string ID of the rule
* @type {String}
*/
this.id = spec.id;
/**
* Selector that this rule applies to
* @type {String}
*/
this.selector = spec.selector || '*';
/**
* Whether to exclude hiddden elements form analysis. Defaults to true.
* @type {Boolean}
*/
this.excludeHidden = typeof spec.excludeHidden === 'boolean' ? spec.excludeHidden : true;
/**
* Flag to enable or disable rule
* @type {Boolean}
*/
this.enabled = typeof spec.enabled === 'boolean' ? spec.enabled : true;
/**
* Denotes if the rule should be run if Context is not an entire page AND whether
* the Rule should be satisified regardless of Node
* @type {Boolean}
*/
this.pageLevel = typeof spec.pageLevel === 'boolean' ? spec.pageLevel : false;
/**
* Checks that any may return true to satisfy rule
* @type {Array}
*/
this.any = spec.any || [];
/**
* Checks that must all return true to satisfy rule
* @type {Array}
*/
this.all = spec.all || [];
/**
* Checks that none may return true to satisfy rule
* @type {Array}
*/
this.none = spec.none || [];
/**
* Tags associated to this rule
* @type {Array}
*/
this.tags = spec.tags || [];
if (spec.matches) {
/**
* Optional function to test if rule should be run against a node, overrides Rule#matches
* @type {Function}
*/
this.matches = createExecutionContext(spec.matches);
}
}
/**
* Optionally test each node against a `matches` function to determine if the rule should run against
* a given node. Defaults to `true`.
* @return {Boolean} Whether the rule should run
*/
Rule.prototype.matches = function () {
'use strict';
return true;
};
/**
* Selects `HTMLElement`s based on configured selector
* @param {Context} context The resolved Context object
* @return {Array} All matching `HTMLElement`s
*/
Rule.prototype.gather = function (context) {
'use strict';
var elements = axe.utils.select(this.selector, context);
if (this.excludeHidden) {
return elements.filter(function (element) {
return !axe.utils.isHidden(element.actualNode);
});
}
return elements;
};
Rule.prototype.runChecks = function (type, node, options, resolve, reject) {
'use strict';
var self = this;
var checkQueue = axe.utils.queue();
this[type].forEach(function (c) {
var check = self._audit.checks[c.id || c];
var option = axe.utils.getCheckOption(check, self.id, options);
checkQueue.defer(function (res, rej) {
check.run(node, option, res, rej);
});
});
checkQueue.then(function (results) {
results = results.filter(function (check) {
return check;
});
resolve({ type: type, results: results });
}).catch(reject);
};
/**
* Runs the Rule's `evaluate` function
* @param {Context} context The resolved Context object
* @param {Mixed} options Options specific to this rule
* @param {Function} callback Function to call when evaluate is complete; receives a RuleResult instance
*/
Rule.prototype.run = function (context, options, resolve, reject) {
const q = axe.utils.queue();
const ruleResult = new RuleResult(this);
let nodes;
try {
// Matches throws an error when it lacks support for document methods
nodes = this.gather(context)
.filter(node => this.matches(node.actualNode, node));
} catch (error) {
// Exit the rule execution if matches fails
reject(new SupportError({cause: error, ruleId: this.id}));
return;
}
if (options.performanceTimer) {
axe.log('gather (', nodes.length, '):', axe.utils.performanceTimer.timeElapsed()+'ms');
}
nodes.forEach(node => {
q.defer((resolveNode, rejectNode) => {
var checkQueue = axe.utils.queue();
checkQueue.defer((res, rej) => {
this.runChecks('any', node, options, res, rej);
});
checkQueue.defer((res, rej) => {
this.runChecks('all', node, options, res, rej);
});
checkQueue.defer((res, rej) => {
this.runChecks('none', node, options, res, rej);
});
checkQueue.then(function (results) {
if (results.length) {
var hasResults = false, result = {};
results.forEach(function (r) {
var res = r.results.filter(function (result) {
return result;
});
result[r.type] = res;
if (res.length) {
hasResults = true;
}
});
if (hasResults) {
result.node = new axe.utils.DqElement(node.actualNode, options);
ruleResult.nodes.push(result);
}
}
resolveNode();
}).catch(err => rejectNode(err));
});
});
q.then(() => resolve(ruleResult))
.catch(error => reject(error));
};
/**
* Iterates the rule's Checks looking for ones that have an after function
* @private
* @param {Rule} rule The rule to check for after checks
* @return {Array} Checks that have an after function
*/
function findAfterChecks(rule) {
'use strict';
return axe.utils.getAllChecks(rule).map(function (c) {
var check = rule._audit.checks[c.id || c];
return (check && typeof check.after === 'function') ? check : null;
}).filter(Boolean);
}
/**
* Finds and collates all results for a given Check on a specific Rule
* @private
* @param {Array} nodes RuleResult#nodes; array of 'detail' objects
* @param {String} checkID The ID of the Check to find
* @return {Array} Matching CheckResults
*/
function findCheckResults(nodes, checkID) {
'use strict';
var checkResults = [];
nodes.forEach(function (nodeResult) {
var checks = axe.utils.getAllChecks(nodeResult);
checks.forEach(function (checkResult) {
if (checkResult.id === checkID) {
checkResults.push(checkResult);
}
});
});
return checkResults;
}
function filterChecks(checks) {
'use strict';
return checks.filter(function (check) {
return check.filtered !== true;
});
}
function sanitizeNodes(result) {
'use strict';
var checkTypes = ['any', 'all', 'none'];
var nodes = result.nodes.filter(function (detail) {
var length = 0;
checkTypes.forEach(function (type) {
detail[type] = filterChecks(detail[type]);
length += detail[type].length;
});
return length > 0;
});
if (result.pageLevel && nodes.length) {
nodes = [nodes.reduce(function (a, b) {
if (a) {
checkTypes.forEach(function (type) {
a[type].push.apply(a[type], b[type]);
});
return a;
}
})];
}
return nodes;
}
/**
* Runs all of the Rule's Check#after methods
* @param {RuleResult} result The "pre-after" RuleResult
* @param {Mixed} options Options specific to the rule
* @return {RuleResult} The RuleResult as filtered by after functions
*/
Rule.prototype.after = function (result, options) {
'use strict';
var afterChecks = findAfterChecks(this);
var ruleID = this.id;
afterChecks.forEach(function (check) {
var beforeResults = findCheckResults(result.nodes, check.id);
var option = axe.utils.getCheckOption(check, ruleID, options);
var afterResults = check.after(beforeResults, option);
beforeResults.forEach(function (item) {
if (afterResults.indexOf(item) === -1) {
item.filtered = true;
}
});
});
result.nodes = sanitizeNodes(result);
return result;
};
/**
* Reconfigure a rule after it has been added
* @param {Object} spec - the attributes to be reconfigured
*/
Rule.prototype.configure = function (spec) {
/*jshint maxcomplexity:14 */
/*jshint maxstatements:20 */
/*jshint evil:true */
'use strict';
if (spec.hasOwnProperty('selector')) {
this.selector = spec.selector;
}
if (spec.hasOwnProperty('excludeHidden')) {
this.excludeHidden = typeof spec.excludeHidden === 'boolean' ? spec.excludeHidden : true;
}
if (spec.hasOwnProperty('enabled')) {
this.enabled = typeof spec.enabled === 'boolean' ? spec.enabled : true;
}
if (spec.hasOwnProperty('pageLevel')) {
this.pageLevel = typeof spec.pageLevel === 'boolean' ? spec.pageLevel : false;
}
if (spec.hasOwnProperty('any')) {
this.any = spec.any;
}
if (spec.hasOwnProperty('all')) {
this.all = spec.all;
}
if (spec.hasOwnProperty('none')) {
this.none = spec.none;
}
if (spec.hasOwnProperty('tags')) {
this.tags = spec.tags;
}
if (spec.hasOwnProperty('matches')) {
if (typeof spec.matches === 'string') {
this.matches = new Function('return ' + spec.matches + ';')();
} else {
this.matches = spec.matches;
}
}
};