-
Notifications
You must be signed in to change notification settings - Fork 882
Expand file tree
/
Copy pathinvalid-children-evaluate.js
More file actions
98 lines (87 loc) · 2.55 KB
/
invalid-children-evaluate.js
File metadata and controls
98 lines (87 loc) · 2.55 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
import { isVisibleToScreenReaders } from '../../commons/dom';
import { getExplicitRole } from '../../commons/aria';
export default function invalidChildrenEvaluate(
node,
options = {},
virtualNode
) {
const relatedNodes = [];
const issues = [];
if (!virtualNode.children) {
return undefined;
}
const vChildren = mapWithNested(virtualNode.children);
while (vChildren.length) {
const { vChild, nested } = vChildren.shift();
if (options.divGroups && !nested && isDivGroup(vChild)) {
if (!vChild.children) {
return undefined;
}
const vGrandChildren = mapWithNested(vChild.children, true);
vChildren.push(...vGrandChildren);
continue;
}
const issue = getInvalidSelector(vChild, nested, options);
if (!issue) {
continue;
}
if (!issues.includes(issue)) {
issues.push(issue);
}
if (vChild?.actualNode?.nodeType === 1) {
relatedNodes.push(vChild.actualNode);
}
}
if (issues.length === 0) {
return false;
}
this.data({ values: issues.join(', ') });
this.relatedNodes(relatedNodes);
return true;
}
function getInvalidSelector(
vChild,
nested,
{ validRoles = [], validNodeNames = [] }
) {
const { nodeName, nodeType, nodeValue } = vChild.props;
const selector = nested ? 'div > ' : '';
if (nodeType === 3 && nodeValue.trim() !== '') {
return selector + `#text`;
}
if (nodeType !== 1 || !isVisibleToScreenReaders(vChild)) {
return false;
}
const role = getExplicitRole(vChild);
if (role) {
return validRoles.includes(role) ? false : selector + `[role=${role}]`;
}
if (validNodeNames.includes(nodeName)) {
return false;
}
// Check if a shadow host's shadow DOM root element is a valid child.
// This handles web components that wrap a valid element in their shadow DOM
// (e.g. <my-list-item> whose shadow DOM renders <li><slot></slot></li>).
if (vChild.actualNode?.shadowRoot) {
const shadowValid = vChild.children.some(shadowChild => {
if (shadowChild.actualNode?.nodeType !== 1) {
return false;
}
const shadowRole = getExplicitRole(shadowChild);
if (shadowRole) {
return validRoles.includes(shadowRole);
}
return validNodeNames.includes(shadowChild.props.nodeName);
});
if (shadowValid) {
return false;
}
}
return selector + nodeName;
}
function isDivGroup(vNode) {
return vNode.props.nodeName === 'div' && getExplicitRole(vNode) === null;
}
function mapWithNested(vNodes, nested = false) {
return vNodes.map(vChild => ({ vChild, nested }));
}