forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharia-required-children-evaluate.js
214 lines (187 loc) · 5.72 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
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
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 role = getExplicitRole(virtualNode, { dpub: true });
const required = requiredOwned(role);
if (required === null) {
return true;
}
const { ownedRoles, ownedElements } = getOwnedRoles(virtualNode, required);
const unallowed = ownedRoles.filter(({ role }) => !required.includes(role));
if (unallowed.length) {
this.relatedNodes(unallowed.map(({ ownedElement }) => ownedElement));
this.data({
messageKey: 'unallowed',
values: unallowed
.map(({ ownedElement, attr }) =>
getUnallowedSelector(ownedElement, attr)
)
.filter((selector, index, array) => array.indexOf(selector) === index)
.join(', ')
});
return false;
}
const missing = missingRequiredChildren(
virtualNode,
role,
required,
ownedRoles
);
if (!missing) {
return true;
}
this.data(missing);
// Only review empty nodes when a node is both empty and does not have an aria-owns relationship
if (reviewEmpty.includes(role) && !ownedElements.some(isContent)) {
return undefined;
}
return false;
}
/**
* Get all owned roles of an element
*/
function getOwnedRoles(virtualNode, required) {
// DAISY-AXE
const parentRole = getRole(virtualNode, { dpub: true });
const ownedRoles = [];
const ownedElements = getOwnedVirtual(virtualNode).filter(vNode => {
return vNode.props.nodeType !== 1 || isVisibleToScreenReaders(vNode);
});
for (let i = 0; i < ownedElements.length; i++) {
const ownedElement = ownedElements[i];
if (ownedElement.props.nodeType !== 1) {
continue;
}
// DAISY-AXE
const role = getRole(ownedElement, { noPresentational: true, dpub: true });
// const role = getRole(ownedElement, { noPresentational: true });
const globalAriaAttr = getGlobalAriaAttr(ownedElement);
const hasGlobalAriaOrFocusable =
!!globalAriaAttr || isFocusable(ownedElement);
// console.log(
// '>>>---=== 1',
// 'node name: ',
// virtualNode.actualNode.nodeName.toLowerCase(),
// 'node ID: ',
// virtualNode.actualNode.getAttribute('id'),
// 'role: ',
// role,
// 'required: ',
// JSON.stringify(required)
// );
// 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)) ||
// DAISY-AXE
(['list'].includes(role) &&
['doc-bibliography', 'doc-endnotes'].includes(parentRole))
) {
ownedElements.push(...ownedElement.children);
} else if (role || hasGlobalAriaOrFocusable) {
ownedRoles.push({
role,
attr: globalAriaAttr || 'tabindex',
ownedElement
});
}
}
return { ownedRoles, ownedElements };
}
/**
* Get missing children roles
*/
function missingRequiredChildren(virtualNode, role, required, ownedRoles) {
// console.log(
// '>>>---=== 2',
// 'node name: ',
// virtualNode.actualNode.nodeName.toLowerCase(),
// 'node ID: ',
// virtualNode.actualNode.getAttribute('id'),
// 'role: ',
// role,
// 'required: ',
// JSON.stringify(required),
// JSON.stringify(ownedRoles.reduce((prev, cur) => prev.concat(cur.role), []))
// );
for (let i = 0; i < ownedRoles.length; i++) {
const { role } = ownedRoles[i];
if (required.includes(role)) {
required = required.filter(requiredRole => requiredRole !== role);
return null;
}
}
if (required.length) {
return required;
}
return null;
}
/**
* 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
* @param {VirtualNode} vNode
* @returns {Boolean}
*/
function isContent(vNode) {
if (vNode.props.nodeType === 3) {
return vNode.props.nodeValue.trim().length > 0;
}
return hasContentVirtual(vNode, false, true);
}