forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessibility.js
185 lines (167 loc) · 6.62 KB
/
accessibility.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
/**
* @license Copyright 2016 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
/* global window, document, getNodeDetails */
import FRGatherer from '../../fraggle-rock/gather/base-gatherer.js';
import {axeSource} from '../../lib/axe.js';
import {pageFunctions} from '../../lib/page-functions.js';
/**
* @return {Promise<LH.Artifacts.Accessibility>}
*/
/* c8 ignore start */
async function runA11yChecks() {
/** @type {import('axe-core/axe')} */
// @ts-expect-error - axe defined by axeLibSource
const axe = window.axe;
const application = `lighthouse-${Math.random()}`;
axe.configure({
branding: {
application,
},
noHtml: true,
});
const axeResults = await axe.run(document, {
elementRef: true,
runOnly: {
type: 'tag',
values: [
'wcag2a',
'wcag2aa',
],
},
// resultTypes doesn't limit the output of the axeResults object. Instead, if it's defined,
// some expensive element identification is done only for the respective types. https://github.com/dequelabs/axe-core/blob/f62f0cf18f7b69b247b0b6362cf1ae71ffbf3a1b/lib/core/reporters/helpers/process-aggregate.js#L61-L97
resultTypes: ['violations', 'inapplicable'],
rules: {
// Consider http://go/prcpg for expert review of the aXe rules.
'tabindex': {enabled: true},
'accesskeys': {enabled: true},
'heading-order': {enabled: true},
'meta-viewport': {enabled: true},
'duplicate-id': {enabled: false},
'table-fake-caption': {enabled: false},
'td-has-header': {enabled: false},
'marquee': {enabled: false},
'area-alt': {enabled: false},
'html-xml-lang-mismatch': {enabled: false},
'blink': {enabled: false},
'server-side-image-map': {enabled: false},
'identical-links-same-purpose': {enabled: false},
'no-autoplay-audio': {enabled: false},
'svg-img-alt': {enabled: false},
'audio-caption': {enabled: false},
'aria-treeitem-name': {enabled: true},
// https://github.com/dequelabs/axe-core/issues/2958
'nested-interactive': {enabled: false},
'frame-focusable-content': {enabled: false},
'aria-roledescription': {enabled: false},
'scrollable-region-focusable': {enabled: false},
// TODO(paulirish): create audits and enable these 3.
'input-button-name': {enabled: false},
'role-img-alt': {enabled: false},
'select-name': {enabled: false},
},
});
// axe just scrolled the page, scroll back to the top of the page so that element positions
// are relative to the top of the page
document.documentElement.scrollTop = 0;
return {
violations: axeResults.violations.map(createAxeRuleResultArtifact),
incomplete: axeResults.incomplete.map(createAxeRuleResultArtifact),
notApplicable: axeResults.inapplicable.map(result => ({id: result.id})), // FYI: inapplicable => notApplicable!
passes: axeResults.passes.map(result => ({id: result.id})),
version: axeResults.testEngine.version,
};
}
/**
* @param {import('axe-core/axe').Result} result
* @return {LH.Artifacts.AxeRuleResult}
*/
function createAxeRuleResultArtifact(result) {
// Simplify `nodes` and collect nodeDetails for each.
const nodes = result.nodes.map(node => {
const {target, failureSummary, element} = node;
// TODO: with `elementRef: true`, `element` _should_ always be defined, but need to verify.
// @ts-expect-error - getNodeDetails put into scope via stringification
const nodeDetails = getNodeDetails(/** @type {HTMLElement} */ (element));
/** @type {Set<HTMLElement>} */
const relatedNodeElements = new Set();
/** @param {import('axe-core/axe').ImpactValue} impact */
const impactToNumber =
(impact) => [null, 'minor', 'moderate', 'serious', 'critical'].indexOf(impact);
const checkResults = [...node.any, ...node.all, ...node.none]
// @ts-expect-error CheckResult.impact is a string, even though ImpactValue is a thing.
.sort((a, b) => impactToNumber(b.impact) - impactToNumber(a.impact));
for (const checkResult of checkResults) {
for (const relatedNode of checkResult.relatedNodes || []) {
/** @type {HTMLElement} */
// @ts-expect-error - should always exist, just being cautious.
const relatedElement = relatedNode.element;
// Prevent overloading the report with way too many nodes.
if (relatedNodeElements.size >= 3) break;
// Should always exist, just being cautious.
if (!relatedElement) continue;
if (element === relatedElement) continue;
relatedNodeElements.add(relatedElement);
}
}
// @ts-expect-error - getNodeDetails put into scope via stringification
const relatedNodeDetails = [...relatedNodeElements].map(getNodeDetails);
return {
target,
failureSummary,
node: nodeDetails,
relatedNodes: relatedNodeDetails,
};
});
// Ensure errors can be serialized over the protocol.
/** @type {Error | undefined} */
// @ts-expect-error - when rules throw an error, axe saves it here.
// see https://github.com/dequelabs/axe-core/blob/eeff122c2de11dd690fbad0e50ba2fdb244b50e8/lib/core/base/audit.js#L684-L693
const resultError = result.error;
let error;
if (resultError instanceof Error) {
error = {
name: resultError.name,
message: resultError.message,
};
}
return {
id: result.id,
impact: result.impact || undefined,
tags: result.tags,
nodes,
error,
};
}
/* c8 ignore stop */
class Accessibility extends FRGatherer {
/** @type {LH.Gatherer.GathererMeta} */
meta = {
supportedModes: ['snapshot', 'navigation'],
};
static pageFns = {
runA11yChecks,
createAxeRuleResultArtifact,
};
/**
* @param {LH.Gatherer.FRTransitionalContext} passContext
* @return {Promise<LH.Artifacts.Accessibility>}
*/
getArtifact(passContext) {
const driver = passContext.driver;
return driver.executionContext.evaluate(runA11yChecks, {
args: [],
useIsolation: true,
deps: [
axeSource,
pageFunctions.getNodeDetailsString,
createAxeRuleResultArtifact,
],
});
}
}
export default Accessibility;