-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy patharia_attribute_value_valid.ts
More file actions
142 lines (139 loc) · 7.37 KB
/
aria_attribute_value_valid.ts
File metadata and controls
142 lines (139 loc) · 7.37 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
/******************************************************************************
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";
export const aria_attribute_value_valid: Rule = {
id: "aria_attribute_value_valid",
context: "dom:*",
dependencies: ["aria_attribute_allowed"],
refactor: {
"Rpt_Aria_ValidPropertyValue": {
"Pass_0": "Pass_0",
"Fail_1": "Fail_1"
}
},
help: {
"en-US": {
"group": "aria_attribute_value_valid.html",
"Pass_0": "aria_attribute_value_valid.html",
"Fail_1": "aria_attribute_value_valid.html"
}
},
messages: {
"en-US": {
"group": "ARIA property values must be valid",
"Pass_0": "Rule Passed",
"Fail_1": "The value \"{0}\" specified for attribute '{1}' on element <{2}> is not valid"
}
},
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;
let valueArr = new Array();
let attrNameArr = new Array();
let dataTypeArr = new Array();
let propertyDataTypes = ARIADefinitions.propertyDataTypes;
let contextAttributes = ruleContext.attributes;
let testedPropertyValues = 0;
if (contextAttributes) {
for (let i = 0, length = contextAttributes.length; i < length; i++) {
let attrName = contextAttributes[i].name;
if (AriaUtil.isDefinedAriaAttribute(ruleContext, attrName)) {
let dataTypes = propertyDataTypes[attrName];
let nodeValue = CommonUtil.normalizeSpacing(contextAttributes[i].nodeValue);
testedPropertyValues++;
if (dataTypes && dataTypes.values) {
if (dataTypes.values.indexOf(nodeValue) == -1) {
if (dataTypes.values.indexOf('undefined') != -1 && nodeValue.length == 0) {
//translate 'undefined' to mean ''
} else {
// aria-relevant is represented as a space delimited list of the following values:
// additions, removals, text; or a single catch-all value all.
if (dataTypes.type === "http://www.w3.org/2001/XMLSchema#nmtokens") {
let attrValues = contextAttributes[i].nodeValue.trim().split(" ");
// if the value all is specified, it cannot have any other value
if (attrValues.length > 1 && attrValues.includes("all")) {
valueArr.push(contextAttributes[i].nodeValue.split(" "));
attrNameArr.push(attrName);
dataTypeArr.push(dataTypes.values.toString());
} else {
let hash = {};
for (let j = 0; j < attrValues.length; j++) {
// if the individual value is not in the list of allowed values
if (attrValues[j] != "" && !dataTypes.values.includes(attrValues[j])) {
if (!hash.hasOwnProperty(attrName)) {
hash[attrName] = true;
attrNameArr.push(attrName);
}
valueArr.push(attrValues[j]);
dataTypeArr.push(dataTypes.values.toString());
}
}
}
} else {
valueArr.push(contextAttributes[i].nodeValue.split(" "));
attrNameArr.push(attrName);
dataTypeArr.push(dataTypes.values.toString());
}
}
}
} else if (dataTypes && dataTypes.type && dataTypes.type === "http://www.w3.org/2001/XMLSchema#int") {
let iVal = parseInt(nodeValue);
if (isNaN(iVal) || ("" + iVal !== nodeValue)) {
valueArr.push(nodeValue);
attrNameArr.push(attrName);
}
} else if (dataTypes && dataTypes.type && dataTypes.type == "http://www.w3.org/2001/XMLSchema#decimal") {
let fVal = parseFloat(nodeValue);
if (isNaN(fVal)) {
valueArr.push(nodeValue);
attrNameArr.push(attrName);
}
} else if (dataTypes && dataTypes.type && (dataTypes.type == "http://www.w3.org/2001/XMLSchema#boolean")) {
let tmpV = nodeValue.trim().toLowerCase();
if (tmpV !== "true" && tmpV !== "false") {
valueArr.push(nodeValue);
attrNameArr.push(attrName);
}
} else if (dataTypes && dataTypes.type && (dataTypes.type == "http://www.w3.org/2001/XMLSchema#string")) {
} else {
testedPropertyValues--;
}
}
}
}
let retMsg = new Array();
let passed = attrNameArr.length == 0;
retMsg.push(valueArr.join(", "));
retMsg.push(attrNameArr.join(", "));
retMsg.push(ruleContext.nodeName.toLowerCase());
// retMsg.push (dataTypeArr.join(", "));
//return new ValidationResult(passed, [ruleContext], attrNameArr, '', retMsg);
if (testedPropertyValues == 0) {
return null;
} else if (!passed) {
return RuleFail("Fail_1", retMsg);
} else {
return RulePass("Pass_0");
}
}
}