forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.js
170 lines (149 loc) · 4.55 KB
/
check.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
/*global CheckResult,DqElement */
function createExecutionContext(spec) {
/*eslint no-eval:0 */
'use strict';
if (typeof spec === 'string') {
return new Function('return ' + spec + ';')();
}
return spec;
}
function Check(spec) {
if (spec) {
this.id = spec.id;
this.configure(spec);
}
}
/**
* Unique ID for the check. Checks may be re-used, so there may be additional instances of checks
* with the same ID.
* @type {String}
*/
// Check.prototype.id;
/**
* Free-form options that are passed as the second parameter to the `evaluate`
* @type {Mixed}
*/
// Check.prototype.options;
/**
* The actual code, accepts 2 parameters: node (the node under test), options (see this.options).
* This function is run in the context of a checkHelper, which has the following methods
* - `async()` - if called, the check is considered to be asynchronous; returns a callback function
* - `data()` - free-form data object, associated to the `CheckResult` which is specific to each node
* @type {Function}
*/
// Check.prototype.evaluate;
/**
* Optional. Filter and/or modify checks for all nodes
* @type {Function}
*/
// Check.prototype.after;
/**
* enabled by default, if false, this check will not be included in the rule's evaluation
* @type {Boolean}
*/
Check.prototype.enabled = true;
/**
* Run the check's evaluate function (call `this.evaluate(node, options)`)
* @param {HTMLElement} node The node to test
* @param {Object} options The options that override the defaults and provide additional
* information for the check
* @param {Function} callback Function to fire when check is complete
*/
Check.prototype.run = function(node, options, context, resolve, reject) {
'use strict';
options = options || {};
var enabled = options.hasOwnProperty('enabled')
? options.enabled
: this.enabled,
checkOptions = options.options || this.options;
if (enabled) {
var checkResult = new CheckResult(this);
var checkHelper = axe.utils.checkHelper(
checkResult,
options,
resolve,
reject
);
var result;
try {
result = this.evaluate.call(
checkHelper,
node.actualNode,
checkOptions,
node,
context
);
} catch (e) {
// In the "Audit#run: should run all the rules" test, there is no `node` here. I do
// not know if this is intentional or not, so to be safe, we guard against the
// possible reference error.
if (node && node.actualNode) {
// Save a reference to the node we errored on for futher debugging.
e.errorNode = new DqElement(node.actualNode).toJSON();
}
reject(e);
return;
}
if (!checkHelper.isAsync) {
checkResult.result = result;
resolve(checkResult);
}
} else {
resolve(null);
}
};
/**
* Run the check's evaluate function (call `this.evaluate(node, options)`) synchronously
* @param {HTMLElement} node The node to test
* @param {Object} options The options that override the defaults and provide additional
* information for the check
*/
Check.prototype.runSync = function(node, options, context) {
options = options || {};
const { enabled = this.enabled } = options;
if (!enabled) {
return null;
}
const checkOptions = options.options || this.options;
const checkResult = new CheckResult(this);
const checkHelper = axe.utils.checkHelper(checkResult, options);
// throw error if a check is run that requires async behavior
checkHelper.async = function() {
throw new Error('Cannot run async check while in a synchronous run');
};
let result;
try {
result = this.evaluate.call(
checkHelper,
node.actualNode,
checkOptions,
node,
context
);
} catch (e) {
// In the "Audit#run: should run all the rules" test, there is no `node` here. I do
// not know if this is intentional or not, so to be safe, we guard against the
// possible reference error.
if (node && node.actualNode) {
// Save a reference to the node we errored on for futher debugging.
e.errorNode = new DqElement(node.actualNode).toJSON();
}
throw e;
}
checkResult.result = result;
return checkResult;
};
/**
* Override a check's settings after construction to allow for changing options
* without having to implement the entire check
*
* @param {Object} spec - the specification of the attributes to be changed
*/
Check.prototype.configure = function(spec) {
['options', 'enabled']
.filter(prop => spec.hasOwnProperty(prop))
.forEach(prop => (this[prop] = spec[prop]));
['evaluate', 'after']
.filter(prop => spec.hasOwnProperty(prop))
.forEach(prop => (this[prop] = createExecutionContext(spec[prop])));
};