-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
axe-audit.js
88 lines (77 loc) · 3.06 KB
/
axe-audit.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
/**
* @license Copyright 2017 Google Inc. 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';
/**
* @fileoverview Base class for all aXe audits. Provides a consistent way to
* generate audit results using aXe rule names.
*/
const Audit = require('../audit.js');
const i18n = require('../../lib/i18n/i18n.js');
const UIStrings = {
/** Label of a table column that identifies HTML elements that have failed an audit. */
failingElementsHeader: 'Failing Elements',
};
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
class AxeAudit extends Audit {
/**
* @param {LH.Artifacts} artifacts Accessibility gatherer artifacts. Note that AxeAudit
* expects the meta name for the class to match the rule id from aXe.
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
// Indicate if a test is not applicable.
// This means aXe did not find any nodes which matched these checks.
// Note in Lighthouse we use the phrasing "Not Applicable" (aXe uses "inapplicable", which sounds weird).
const notApplicables = artifacts.Accessibility.notApplicable || [];
const isNotApplicable = notApplicables.find(result => result.id === this.meta.id);
if (isNotApplicable) {
return {
score: 1,
notApplicable: true,
};
}
const violations = artifacts.Accessibility.violations || [];
const rule = violations.find(result => result.id === this.meta.id);
const impact = rule && rule.impact;
const tags = rule && rule.tags;
/** @type {LH.Audit.Details.Table['items']}>} */
let items = [];
if (rule && rule.nodes) {
items = rule.nodes.map(node => ({
node: /** @type {LH.Audit.Details.NodeValue} */ ({
type: 'node',
selector: Array.isArray(node.target) ? node.target.join(' ') : '',
path: node.path,
snippet: node.html || node.snippet,
explanation: node.failureSummary,
nodeLabel: node.nodeLabel,
}),
}));
}
/** @type {LH.Audit.Details.Table['headings']} */
const headings = [
{key: 'node', itemType: 'node', text: str_(UIStrings.failingElementsHeader)},
];
/** @type {LH.Audit.Details.DebugData|undefined} */
let debugData;
if (impact || tags) {
debugData = {
type: 'debugdata',
impact,
tags,
};
}
return {
score: Number(rule === undefined),
extendedInfo: {
value: rule,
},
details: {...Audit.makeTableDetails(headings, items), debugData},
};
}
}
module.exports = AxeAudit;
module.exports.UIStrings = UIStrings;