-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathfieldset_label_valid.ts
More file actions
155 lines (147 loc) · 6.54 KB
/
fieldset_label_valid.ts
File metadata and controls
155 lines (147 loc) · 6.54 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
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
/******************************************************************************
Copyright:: 2022- IBM, Inc
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.
*****************************************************************************/
import { Rule, RuleResult, RuleFail, RuleContext, RulePass, RuleContextHierarchy } from "../api/IRule";
import { eRulePolicy, eToolkitLevel } from "../api/IRule";
import { FragmentUtil } from "../../v2/checker/accessibility/util/fragment";
import { CacheUtil } from "../util/CacheUtil";
import { CommonUtil } from "../util/CommonUtil";
import { VisUtil } from "../util/VisUtil";
import { AccNameUtil } from "../util/AccNameUtil";
export const fieldset_label_valid: Rule = {
id: "fieldset_label_valid",
context: "aria:group",
refactor: {
"group_withInputs_hasName": {
"Pass_1": "Pass_1",
"Fail_1": "Fail_1",
"Fail_2": "Fail_2"}
},
help: {
"en-US": {
"Pass_1": "fieldset_label_valid.html",
"Fail_1": "fieldset_label_valid.html",
"Fail_2": "fieldset_label_valid.html",
"group": "fieldset_label_valid.html"
}
},
messages: {
"en-US": {
"Pass_1": "Group/Fieldset \"{0}\" with an input has a unique name",
"Fail_1": "Group/Fieldset does not have an accessible name",
"Fail_2": "Group/Fieldset \"{0}\" has a duplicate name to another group",
"group": "Groups with nested inputs must have unique accessible name"
}
},
rulesets: [{
"id": ["IBM_Accessibility", "IBM_Accessibility_next", "WCAG_2_1", "WCAG_2_0", "WCAG_2_2"],
"num": ["1.3.1", "3.3.2"],
"level": eRulePolicy.VIOLATION,
"toolkitLevel": eToolkitLevel.LEVEL_THREE
}],
act: [],
run: (context: RuleContext, options?: {}, contextHierarchies?: RuleContextHierarchy): RuleResult | RuleResult[] => {
const ruleContext = context["dom"].node as Element;
//skip the check if the element is hidden or disabled
if (VisUtil.isNodeHiddenFromAT(ruleContext) || CommonUtil.isNodeDisabled(ruleContext))
return;
let ownerDocument = FragmentUtil.getOwnerFragment(ruleContext);
let formCache = CacheUtil.getCache(
ruleContext.ownerDocument,
"landmark_group_input",
null
);
if (!formCache) {
formCache = {
groupsWithInputs: [],
groupsWithInputsComputedLabels: [],
};
let allGroupsTemp = ownerDocument.querySelectorAll(
'fieldset,[role="group"]'
);
let allGroups = Array.from(allGroupsTemp);
let groupsWithInputs = [];
for (let i = 0; i < allGroups.length; i++) {
// Loop over all the group nodes
if (allGroups[i].querySelector("input")) {
groupsWithInputs.push(allGroups[i]);
}
}
let groupsWithInputsComputedLabels = [];
for (let i = 0; i < groupsWithInputs.length; i++) {
const pair = AccNameUtil.computeAccessibleName(groupsWithInputs[i]);
// Loop over all the landmark nodes
groupsWithInputsComputedLabels.push(
/**ARIAMapper.computeName(groupsWithInputs[i])*/
pair && pair.name && pair.name.trim().length > 0 ? pair.name.trim() : ""
);
}
formCache.groupsWithInputs = groupsWithInputs;
formCache.groupsWithInputsComputedLabels =
groupsWithInputsComputedLabels;
CacheUtil.setCache(ruleContext.ownerDocument, "landmark_group_input",formCache);
}
// formCache.groupsWithInputs.forEach(element => {
// console.log("formCache.groupsWithInputs: " +element.id)
// });
// console.log("formCache.groupsWithInputsComputedLabels: " +formCache.groupsWithInputsComputedLabels)
// console.log("formCache.groupsWithInputsComputedLabels: " +formCache.groupsWithInputsComputedLabels.length)
let ruleContextFoundIngroupsWithInputsFlag = false;
let computedName = "";
if (!formCache.groupsWithInputs) {
// We do not have any groups with inputs. Therefore we should skip this rule trigger.
return null;
}
for (let i = 0; i < formCache.groupsWithInputs.length; i++) {
if (ruleContext.isSameNode(formCache.groupsWithInputs[i])) {
// We have found our ruleContext in the cache
ruleContextFoundIngroupsWithInputsFlag = true;
if (
formCache.groupsWithInputsComputedLabels[i] === "" ||
formCache.groupsWithInputsComputedLabels[i] === null
) {
// console.log("Fail_1")
return RuleFail("Fail_1");
}
let foundSameNameFlag = false;
for (
let j = 0;
j < formCache.groupsWithInputsComputedLabels.length;
j++
) {
if (i == j) {
continue;
} // We do not want to compare against ourselfs
if (
formCache.groupsWithInputsComputedLabels[i] ===
formCache.groupsWithInputsComputedLabels[j]
) {
foundSameNameFlag = true;
}
}
if (foundSameNameFlag) {
// console.log("Fail_2")
return RuleFail("Fail_2", [
formCache.groupsWithInputsComputedLabels[i],
]);
}
computedName = formCache.groupsWithInputsComputedLabels[i];
}
}
if (!ruleContextFoundIngroupsWithInputsFlag) {
// console.log("null return")
return null;
}
// console.log("Pass_1")
return RulePass("Pass_1", [computedName]);
}
}