-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy patharia_attribute_exists.ts
More file actions
123 lines (116 loc) · 5.66 KB
/
aria_attribute_exists.ts
File metadata and controls
123 lines (116 loc) · 5.66 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
/******************************************************************************
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 { AriaUtil } from "../util/AriaUtil";
import { CommonUtil } from "../util/CommonUtil";
import { ARIADefinitions } from "../../v2/aria/ARIADefinitions";
import { VisUtil } from "../util/VisUtil";
export const aria_attribute_exists: Rule = {
id: "aria_attribute_exists",
context: "dom:*[role]",
dependencies: ["aria_role_allowed"],
refactor: {
"Rpt_Aria_EmptyPropertyValue": {
"pass": "pass",
"fail_empty_attribute": "fail_empty_attribute"
}
},
help: {
"en-US": {
"pass": "aria_attribute_exists.html",
"fail_empty_attribute": "aria_attribute_exists.html",
"group": "aria_attribute_exists.html"
}
},
messages: {
"en-US": {
"pass": "Rule Passed",
"fail_empty_attribute": "The element attribute(s): '{0}' value is empty",
"group": "When specifying a required ARIA attribute, the value must not be empty"
}
},
rulesets: [{
"id": ["IBM_Accessibility", "IBM_Accessibility_next", "WCAG_2_1", "WCAG_2_0", "WCAG_2_2"],
"num": ["4.1.2"],
"level": eRulePolicy.VIOLATION,
"toolkitLevel": eToolkitLevel.LEVEL_ONE
}],
act: ["6a7281"],
run: (context: RuleContext, options?: {}, contextHierarchies?: RuleContextHierarchy): RuleResult | RuleResult[] => {
const ruleContext = context["dom"].node as Element;
//skip the check if the element is hidden
if (VisUtil.isNodeHiddenFromAT(ruleContext))
return;
let attrNameArr = new Array();
let designPatterns = ARIADefinitions.designPatterns;
let hasAttribute = CommonUtil.hasAttribute;
let testedProperties = 0;
let role = AriaUtil.getResolvedRole(ruleContext);
if (!role) return;
if (designPatterns[role] && AriaUtil.getRoleRequiredProperties(role, ruleContext) != null) {
let requiredRoleProps = AriaUtil.getRoleRequiredProperties(role, ruleContext);
for (let i = 0, length = requiredRoleProps.length; i < length; i++) {
let attribute = requiredRoleProps[i];
if (hasAttribute(ruleContext, attribute)) {
testedProperties++;
let nodeValue = CommonUtil.normalizeSpacing(ruleContext.getAttribute(requiredRoleProps[i]));
if (nodeValue.length == 0) attrNameArr.push(requiredRoleProps[i]);
} else if (requiredRoleProps[i] == "aria-labelledby") {
if ((role == "radiogroup") && (hasAttribute(ruleContext, "aria-label"))) {
testedProperties++;
let nodeValue = CommonUtil.normalizeSpacing(ruleContext.getAttribute("aria-label"));
if (nodeValue.length == 0) attrNameArr.push("aria-label");
}
} else if (requiredRoleProps[i] == "aria-valuenow") {
if ((role == "progressbar") && (hasAttribute(ruleContext, "aria-valuetext"))) {
testedProperties++;
let nodeValue = CommonUtil.normalizeSpacing(ruleContext.getAttribute("aria-valuetext"));
if (nodeValue.length == 0) attrNameArr.push("aria-valuetext");
}
}
}
}
if (designPatterns[role]) {
let tagProperty = AriaUtil.getElementAriaProperty(ruleContext);
let permittedRoles = [];
permittedRoles.push(role);
let allowedAttributes = AriaUtil.getAllowedAriaAttributes(ruleContext, permittedRoles, tagProperty);
for (let i = 0, length = allowedAttributes.length; i < length; i++) {
let attribute = allowedAttributes[i];
if (attribute == "aria-checked" || attribute == "aria-selected" ||
attribute == "aria-expanded" || attribute == "aria-orientation" ||
attribute == "aria-level") {
if (hasAttribute(ruleContext, attribute)) {
testedProperties++;
let nodeValue = CommonUtil.normalizeSpacing(ruleContext.getAttribute(attribute));
if (nodeValue.length == 0 && !attrNameArr.includes(attribute)) {
attrNameArr.push(attribute);
}
}
}
}
}
let retMsg = new Array();
let passed = attrNameArr.length == 0;
retMsg.push(attrNameArr.join(", "));
//return new ValidationResult(passed, [ruleContext], attrNameArr, '', retMsg);
if (testedProperties == 0) {
return null;
} else if (!passed) {
return RuleFail("fail_empty_attribute", retMsg);
} else {
return RulePass("pass");
}
}
}