This repository has been archived by the owner on Oct 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathindex.js
202 lines (175 loc) · 5.95 KB
/
index.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
export function scanForProblems(context, errorCallback, options) {
const errors = {}
function logError(err) {
const {name} = err
errors[name] = errors[name] || []
errors[name].push(err)
errorCallback(err)
}
for (const img of context.querySelectorAll('img')) {
if (!img.hasAttribute('alt')) {
logError(new ImageWithoutAltAttributeError(img))
}
}
for (const a of context.querySelectorAll('a')) {
if (a.hasAttribute('name') || elementIsHidden(a)) {
continue
}
if (a.getAttribute('href') == null && a.getAttribute('role') !== 'button') {
logError(new LinkWithoutLabelOrRoleError(a))
} else if (!accessibleText(a)) {
logError(new ElementWithoutLabelError(a))
}
}
for (const button of context.querySelectorAll('button')) {
if (!elementIsHidden(button) && !accessibleText(button)) {
logError(new ButtonWithoutLabelError(button))
}
}
for (const label of context.querySelectorAll('label')) {
// In case label.control isn't supported by browser, find the control manually (IE)
const control = label.control || document.getElementById(label.getAttribute('for')) || label.querySelector('input')
if (!control && !elementIsHidden(label)) {
logError(new LabelMissingControlError(label))
}
}
for (const input of context.querySelectorAll(
'input[type=text], input[type=url], input[type=search], input[type=number], textarea'
)) {
// In case input.labels isn't supported by browser, find the control manually (IE)
const inputLabel = input.labels
? input.labels[0]
: input.closest('label') || document.querySelector(`label[for="${input.id}"]`)
if (!inputLabel && !elementIsHidden(input) && !input.hasAttribute('aria-label')) {
logError(new InputMissingLabelError(input))
}
}
if (options && options['ariaPairs']) {
for (const selector in options['ariaPairs']) {
const ARIAAttrsRequired = options['ariaPairs'][selector]
for (const target of context.querySelectorAll(selector)) {
const missingAttrs = []
for (const attr of ARIAAttrsRequired) {
if (!target.hasAttribute(attr)) missingAttrs.push(attr)
}
if (missingAttrs.length > 0) {
logError(new ARIAAttributeMissingError(target, missingAttrs.join(', ')))
}
}
}
}
return errors
}
function errorSubclass(fn) {
fn.prototype = new Error()
fn.prototype.constructor = fn
}
function ImageWithoutAltAttributeError(element) {
this.name = 'ImageWithoutAltAttributeError'
this.stack = new Error().stack
this.element = element
this.message = `Missing alt attribute on ${inspect(element)}`
}
errorSubclass(ImageWithoutAltAttributeError)
function ElementWithoutLabelError(element) {
this.name = 'ElementWithoutLabelError'
this.stack = new Error().stack
this.element = element
this.message = `Missing text, title, or aria-label attribute on ${inspect(element)}`
}
errorSubclass(ElementWithoutLabelError)
function LinkWithoutLabelOrRoleError(element) {
this.name = 'LinkWithoutLabelOrRoleError'
this.stack = new Error().stack
this.element = element
this.message = `Missing href or role=button on ${inspect(element)}`
}
errorSubclass(LinkWithoutLabelOrRoleError)
function LabelMissingControlError(element) {
this.name = 'LabelMissingControlError'
this.stack = new Error().stack
this.element = element
this.message = `Label missing control on ${inspect(element)}`
}
errorSubclass(LabelMissingControlError)
function InputMissingLabelError(element) {
this.name = 'InputMissingLabelError'
this.stack = new Error().stack
this.element = element
this.message = `Missing label for or aria-label attribute on ${inspect(element)}`
}
errorSubclass(InputMissingLabelError)
function ButtonWithoutLabelError(element) {
this.name = 'ButtonWithoutLabelError'
this.stack = new Error().stack
this.element = element
this.message = `Missing text or aria-label attribute on ${inspect(element)}`
}
errorSubclass(ButtonWithoutLabelError)
function ARIAAttributeMissingError(element, attr) {
this.name = 'ARIAAttributeMissingError'
this.stack = new Error().stack
this.element = element
this.message = `Missing ${attr} attribute on ${inspect(element)}`
}
errorSubclass(ARIAAttributeMissingError)
function elementIsHidden(element) {
return element.closest('[aria-hidden="true"], [hidden], [style*="display: none"]') != null
}
function isText(value) {
return typeof value === 'string' && !!value.trim()
}
// Public: Check if an element has text visible by sight or screen reader.
//
// Examples
//
// <img alt="github" src="github.png">
// # => true
//
// <span aria-label="Open"></span>
// # => true
//
// <button></button>
// # => false
//
// Returns String text.
function accessibleText(node) {
switch (node.nodeType) {
case Node.ELEMENT_NODE:
if (
isText(node.getAttribute('alt')) ||
isText(node.getAttribute('aria-label')) ||
isText(node.getAttribute('title'))
)
return true
for (let i = 0; i < node.childNodes.length; i++) {
if (accessibleText(node.childNodes[i])) return true
}
break
case Node.TEXT_NODE:
return isText(node.data)
}
}
function inspect(element) {
let tagHTML = element.outerHTML
if (element.innerHTML) tagHTML = tagHTML.replace(element.innerHTML, '...')
const parents = []
while (element) {
if (element.nodeName === 'BODY') break
parents.push(selectors(element))
// Stop traversing when we hit an ID
if (element.id) break
element = element.parentNode
}
return `"${parents.reverse().join(' > ')}". \n\n${tagHTML}`
}
function selectors(element) {
const components = [element.nodeName.toLowerCase()]
if (element.id) components.push(`#${element.id}`)
if (typeof element.hasAttribute === 'function' && element.hasAttribute('class')) {
for (const name of element.getAttribute('class').split(/\s+/)) {
if (name.match(/^js-/)) components.push(`.${name}`)
}
}
return components.join('')
}