-
Notifications
You must be signed in to change notification settings - Fork 56
/
validator.js
98 lines (91 loc) · 2.46 KB
/
validator.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
const SELECTOR = 'auto-complete'
const INPUT_RULE_ID = 'required-input-element-child'
// eslint-disable-next-line i18n-text/no-en
const INPUT_HELP_TEXT = 'This component requires an input field to be provided.'
const CLEAR_BUTTON_RULE_ID = 'optional-clear-must-be-button'
// eslint-disable-next-line i18n-text/no-en
const CLEAR_BUTTON_HELP_TEXT = 'If provided with clear button, it must be a button element.'
function checkForInput(autoCompleteElement) {
return autoCompleteElement.querySelectorAll('input').length === 1
}
function checkForOptionalClearButton(autoCompleteElement) {
const [input] = autoCompleteElement.querySelectorAll('input')
if (!input) {
return true
}
const clearButtonId = `${input.id || input.getAttribute('name')}-clear`
const clearButton = autoCompleteElement.ownerDocument.getElementById(clearButtonId)
if (!clearButton) {
return true
}
if (clearButton instanceof HTMLButtonElement) {
return true
}
return false
}
const rules = [
{
id: INPUT_RULE_ID,
excludeHidden: true,
selector: SELECTOR,
metadata: {
help: INPUT_HELP_TEXT,
helpUrl: '',
},
all: [`${INPUT_RULE_ID}_0`],
},
{
id: CLEAR_BUTTON_RULE_ID,
excludeHidden: true,
selector: SELECTOR,
metadata: {
help: CLEAR_BUTTON_HELP_TEXT,
helpUrl: '',
},
all: [`${CLEAR_BUTTON_RULE_ID}_0`],
},
]
const checks = [
{
id: `${INPUT_RULE_ID}_0`,
evaluate: checkForInput,
metadata: {impact: 'critical'},
},
{
id: `${CLEAR_BUTTON_RULE_ID}_0`,
evaluate: checkForOptionalClearButton,
metadata: {impact: 'critical'},
},
]
export function validator(domNode) {
const result = {
passes: [],
violations: [],
}
for (const element of domNode.getElementsByTagName(SELECTOR)) {
for (const rule of rules) {
for (const checkId of rule.all) {
const thisCheck = checks.find(check => check.id === checkId)
const checkResult = thisCheck.evaluate(element)
result[checkResult ? 'passes' : 'violations'].push({
id: rule.id,
help: rule.metadata.help,
helpUrl: rule.metadata.helpUrl,
nodes: [element],
})
}
}
}
return result
}
/**
*
* @param {import('axe-core').Spec} ruleset
* @returns {import('axe-core').Spec}
*/
export default function combineRules(ruleset = {}) {
return Object.assign({}, ruleset, {
checks: (ruleset.checks || []).concat(checks),
rules: (ruleset.rules || []).concat(rules),
})
}