forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aria-required-children-evaluate.js
165 lines (146 loc) · 4.74 KB
/
aria-required-children-evaluate.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
import {
requiredOwned,
getRole,
getExplicitRole,
getOwnedVirtual
} from '../../commons/aria';
import { getGlobalAriaAttrs } from '../../commons/standards';
import {
hasContentVirtual,
isFocusable,
isVisibleToScreenReaders
} from '../../commons/dom';
/**
* Check that an element owns all required children for its explicit role.
*
* Required roles are taken from the `ariaRoles` standards object from the roles `requiredOwned` property.
*
* @memberof checks
* @param {Boolean} options.reviewEmpty List of ARIA roles that should be flagged as "Needs Review" rather than a violation if the element has no owned children.
* @data {String[]} List of all missing owned roles.
* @returns {Mixed} True if the element owns all required roles. Undefined if `options.reviewEmpty=true` and the element has no owned children. False otherwise.
*/
export default function ariaRequiredChildrenEvaluate(
node,
options,
virtualNode
) {
const reviewEmpty =
options && Array.isArray(options.reviewEmpty) ? options.reviewEmpty : [];
const explicitRole = getExplicitRole(virtualNode, { dpub: true });
const required = requiredOwned(explicitRole);
if (required === null) {
return true;
}
const ownedRoles = getOwnedRoles(virtualNode, required);
const unallowed = ownedRoles.filter(
({ role, vNode }) => vNode.props.nodeType === 1 && !required.includes(role)
);
if (unallowed.length) {
this.relatedNodes(unallowed.map(({ vNode }) => vNode));
this.data({
messageKey: 'unallowed',
values: unallowed
.map(({ vNode, attr }) => getUnallowedSelector(vNode, attr))
.filter((selector, index, array) => array.indexOf(selector) === index)
.join(', ')
});
return false;
}
if (hasRequiredChildren(required, ownedRoles)) {
return true;
}
if (virtualNode.attr('aria-busy') === 'true') {
this.data({ messageKey: 'aria-busy' });
return true;
}
this.data(required);
// Only review empty nodes when a node is both empty and does not have an aria-owns relationship
if (reviewEmpty.includes(explicitRole) && !ownedRoles.some(isContent)) {
return undefined;
}
return false;
}
/**
* Get all owned roles of an element
*/
function getOwnedRoles(virtualNode, required) {
let vNode;
const parentRole = getRole(virtualNode, { dpub: true });
const ownedRoles = [];
const ownedVirtual = getOwnedVirtual(virtualNode);
while ((vNode = ownedVirtual.shift())) {
if (vNode.props.nodeType === 3) {
ownedRoles.push({ vNode, role: null });
}
if (vNode.props.nodeType !== 1 || !isVisibleToScreenReaders(vNode)) {
continue;
}
const role = getRole(vNode, { noPresentational: true, dpub: true });
const globalAriaAttr = getGlobalAriaAttr(vNode);
const hasGlobalAriaOrFocusable = !!globalAriaAttr || isFocusable(vNode);
// if owned node has no role or is presentational, or if role
// allows group or rowgroup, we keep parsing the descendant tree.
// this means intermediate roles between a required parent and
// child will fail the check
if (
(!role && !hasGlobalAriaOrFocusable) ||
(['group', 'rowgroup'].includes(role) &&
required.some(requiredRole => requiredRole === role)) ||
(['list'].includes(role) &&
['doc-bibliography', 'doc-endnotes'].includes(parentRole))
) {
ownedVirtual.push(...vNode.children);
} else if (role || hasGlobalAriaOrFocusable) {
const attr = globalAriaAttr || 'tabindex';
ownedRoles.push({ role, attr, vNode });
}
}
return ownedRoles;
}
/**
* See if any required roles are in the ownedRoles array
*/
function hasRequiredChildren(required, ownedRoles) {
return ownedRoles.some(({ role }) => role && required.includes(role));
}
/**
* Get the first global ARIA attribute the element has.
* @param {VirtualNode} vNode
* @return {String|null}
*/
function getGlobalAriaAttr(vNode) {
return getGlobalAriaAttrs().find(attr => vNode.hasAttr(attr));
}
/**
* Return a simple selector for an unallowed element.
* @param {VirtualNode} vNode
* @param {String} [attr] - Optional attribute which made the element unallowed
* @return {String}
*/
function getUnallowedSelector(vNode, attr) {
const { nodeName, nodeType } = vNode.props;
if (nodeType === 3) {
return `#text`;
}
const role = getExplicitRole(vNode, { dpub: true });
if (role) {
return `[role=${role}]`;
}
if (attr) {
return nodeName + `[${attr}]`;
}
return nodeName;
}
/**
* Check if the node has content, or is itself content
* @Object {Object} OwnedRole
* @property {VirtualNode} vNode
* @returns {Boolean}
*/
function isContent({ vNode }) {
if (vNode.props.nodeType === 3) {
return vNode.props.nodeValue.trim().length > 0;
}
return hasContentVirtual(vNode, false, true);
}