Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
261 lines (225 loc) · 6.42 KB
/
Copy pathindex.js
File metadata and controls
261 lines (225 loc) · 6.42 KB
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
/* global document, window, Promise */
var axeCore = require('axe-core');
var rIC = require('requestidlecallback');
var after = require('./after');
var requestIdleCallback = rIC.request;
var cancelIdleCallback = rIC.cancel;
var React = undefined;
var ReactDOM = undefined;
var boldCourier = 'font-weight:bold;font-family:Courier;';
var critical = 'color:red;font-weight:bold;';
var serious = 'color:red;font-weight:normal;';
var moderate = 'color:orange;font-weight:bold;';
var minor = 'color:orange;font-weight:normal;';
var defaultReset = 'font-color:black;font-weight:normal;';
var idleId;
var timeout;
var _createElement;
var components = {};
var nodes = [];
var cache = {};
function getPath(node) {
var path = [node];
do {
path.push(node.parentNode);
node = node.parentNode;
} while (node && node.nodeName.toLowerCase() !== 'html');
if (!node || !node.parentNode) {
return null;
}
return path.reverse();
}
function getCommonParent(nodes) {
var path, nextPath;
if (nodes.length === 1) {
return nodes.pop();
}
while (!path && nodes.length) {
path = getPath(nodes.pop());
}
while (nodes.length) {
nextPath = getPath(nodes.pop());
if (nextPath) {
path = path.filter(function(node, index) {
return nextPath.length > index && nextPath[index] === node;
});
}
}
return path ? path[path.length - 1] : document;
}
function logElement(node, logFn) {
var el = document.querySelector(node.target.toString());
if (!el) {
logFn('Selector: %c%s', boldCourier, node.target.toString());
} else {
logFn('Element: %o', el);
}
}
function logHtml(node) {
console.log('HTML: %c%s', boldCourier, node.html);
}
function logFailureMessage(node, key) {
var message = axeCore._audit.data.failureSummaries[key].failureMessage(
node[key].map(function(check) {
return check.message || '';
})
);
console.error(message);
}
function failureSummary(node, key) {
if (node[key].length > 0) {
logElement(node, console.groupCollapsed);
logHtml(node);
logFailureMessage(node, key);
var relatedNodes = [];
node[key].forEach(function(check) {
relatedNodes = relatedNodes.concat(check.relatedNodes);
});
if (relatedNodes.length > 0) {
console.groupCollapsed('Related nodes');
relatedNodes.forEach(function(relatedNode) {
logElement(relatedNode, console.log);
logHtml(relatedNode);
});
console.groupEnd();
}
console.groupEnd();
}
}
function createDeferred() {
var deferred = {};
deferred.promise = new Promise(function(resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});
return deferred;
}
function checkAndReport(node, timeout) {
if (idleId) {
cancelIdleCallback(idleId);
idleId = undefined;
}
var deferred = createDeferred();
nodes.push(node);
idleId = requestIdleCallback(
function() {
var n = getCommonParent(nodes);
if (n.nodeName.toLowerCase() === 'html') {
// if the only common parent is the body, then analyze the whole page
n = document;
}
axeCore.run(n, { reporter: 'v2' }, function(error, results) {
if (error) {
return deferred.reject(error);
}
results.violations = results.violations.filter(function(result) {
result.nodes = result.nodes.filter(function(node) {
var key = node.target.toString() + result.id;
var retVal = !cache[key];
cache[key] = key;
return retVal;
});
return !!result.nodes.length;
});
if (results.violations.length) {
console.group('%cNew aXe issues', serious);
results.violations.forEach(function(result) {
var fmt;
switch (result.impact) {
case 'critical':
fmt = critical;
break;
case 'serious':
fmt = serious;
break;
case 'moderate':
fmt = moderate;
break;
case 'minor':
fmt = minor;
break;
default:
fmt = minor;
break;
}
console.groupCollapsed(
'%c%s: %c%s %s',
fmt,
result.impact,
defaultReset,
result.help,
result.helpUrl
);
result.nodes.forEach(function(node) {
failureSummary(node, 'any');
failureSummary(node, 'none');
});
console.groupEnd();
});
console.groupEnd();
}
deferred.resolve();
});
},
{ timeout: timeout }
);
return deferred.promise;
}
function checkNode(component) {
var node = ReactDOM.findDOMNode(component);
if (node) {
checkAndReport(node, timeout);
}
}
function componentAfterRender(component) {
after(component, 'componentDidMount', checkNode);
after(component, 'componentDidUpdate', checkNode);
}
function addComponent(component) {
var reactInstance = component._reactInternalInstance || {};
var reactInstanceDebugID = reactInstance._debugID;
var reactFiberInstance = component._reactInternalFiber || {};
var reactFiberInstanceDebugID = reactFiberInstance._debugID;
if (reactInstanceDebugID && !components[reactInstanceDebugID]) {
components[reactInstanceDebugID] = component;
componentAfterRender(component);
} else if (
reactFiberInstanceDebugID &&
!components[reactFiberInstanceDebugID]
) {
components[reactFiberInstanceDebugID] = component;
componentAfterRender(component);
}
}
var reactAxe = function reactAxe(_React, _ReactDOM, _timeout, conf) {
React = _React;
ReactDOM = _ReactDOM;
timeout = _timeout;
if (conf) {
axeCore.configure(conf);
}
axeCore.configure({
checks: [
{
id: 'color-contrast',
options: {
noScroll: true
}
}
]
});
if (!_createElement) {
_createElement = React.createElement;
React.createElement = function() {
var reactEl = _createElement.apply(this, arguments);
if (reactEl._owner && reactEl._owner._instance) {
addComponent(reactEl._owner._instance);
} else if (reactEl._owner && reactEl._owner.stateNode) {
addComponent(reactEl._owner.stateNode);
}
return reactEl;
};
}
return checkAndReport(document.body, timeout);
};
module.exports = reactAxe;