-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathaccessibility-test.js
202 lines (167 loc) · 6.19 KB
/
accessibility-test.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
//= require axe-core/axe.js
(function (window, document, axe) {
window.GOVUK = window.GOVUK || {}
function AccessibilityTest (selector, callback) {
if (typeof callback !== 'function') {
return
}
var element = document.querySelector(selector)
if (!element) {
return callback()
}
var dataDisabledRules = element.getAttribute('data-excluded-rules')
var disabledRules = dataDisabledRules ? JSON.parse(dataDisabledRules) : []
var axeRules = {}
disabledRules.forEach(function (rule) {
axeRules[rule] = { enabled: false }
})
var axeOptions = {
rules: axeRules
}
axe.run(selector, axeOptions, function (err, results) {
if (err) {
return callback(new Error('aXe Error: ' + err))
}
if (typeof results === 'undefined') {
return callback(new Error('aXe Error: Expected results but none returned'))
}
var consoleErrorText = _consoleErrorText(results.violations, results.url)
var bodyClass = results.violations.length === 0 ? 'js-test-a11y-success' : 'js-test-a11y-failed'
document.body.classList.add(bodyClass)
document.body.classList.add('js-test-a11y-finished')
callback(undefined, consoleErrorText, _processAxeResultsForPage(results))
})
}
var _consoleErrorText = function (violations, url) {
if (violations.length !== 0) {
return (
'\n' + 'Accessibility issues at ' +
url + '\n\n' +
violations.map(function (violation) {
var help = 'Problem: ' + violation.help + ' (' + violation.id + ')'
var helpUrl = 'Try fixing it with this help: ' + violation.helpUrl
var htmlAndTarget = violation.nodes.map(_renderNode).join('\n\n')
return [
help,
htmlAndTarget,
helpUrl
].join('\n\n\n')
}).join('\n\n- - -\n\n')
)
} else {
return false
}
}
var _processAxeResultsForPage = function (results) {
return {
violations: _mapSummaryAndCause(results.violations),
incompleteWarnings: _mapSummaryAndCause(results.incomplete)
}
}
var _mapSummaryAndCause = function (resultsArray) {
return resultsArray.map(function (result) {
var cssSelector = result.nodes.map(function (node) {
return {
selector: node.target,
reasons: node.any.map(function (item) {
return item.message
})
}
})
return {
id: result.id,
summary: result.help,
selectors: cssSelector,
url: result.helpUrl
}
})
}
var _renderNode = function (node) {
return (
' Check the HTML:\n' +
' `' + node.html + '`\n' +
' found with the selector:\n' +
' "' + node.target.join(', ') + '"'
)
}
var _findParent = function (element, selector) {
while (element.tagName !== 'HTML') {
if (element.matches(selector)) {
return element
}
element = element.parentNode
}
}
var _renderAxeResultsInGuide = function (resultsGroup, resultContainerSelector) {
resultsGroup.forEach(function (result) {
result.selectors.forEach(function (selectorObj) {
var activeEl = document.querySelector(selectorObj.selector)
var activeElParent = _findParent(activeEl, '[data-module="test-a11y"]')
var wrapper = activeElParent.querySelector(resultContainerSelector)
if (!wrapper) {
return
}
// Section to announce the overall problem.
var headerNodeLink = document.createElement('a')
headerNodeLink.href = result.url
headerNodeLink.textContent = '(see guidance)'
var headerNode = document.createElement('h3')
headerNode.textContent = result.summary + ' (' + result.id + ') '
headerNode.appendChild(headerNodeLink)
// Section to explain the reasons
var reasonsListNode = document.createElement('ul')
selectorObj.reasons.forEach(function (reason) {
var listItemNode = document.createElement('li')
listItemNode.textContent = reason
reasonsListNode.appendChild(listItemNode)
})
var reasonsHeaderNode = document.createElement('h4')
reasonsHeaderNode.textContent = 'Possible reasons why:'
var reasonsNode = document.createElement('div')
reasonsNode.appendChild(reasonsHeaderNode)
reasonsNode.appendChild(reasonsListNode)
// Section to help find the element
var findHeader = document.createElement('h4')
findHeader.textContent = 'Element can be found using the selector:'
var findSelectorNode = document.createElement('span')
findSelectorNode.className = 'selector'
findSelectorNode.textContent = selectorObj.selector
var findNode = document.createElement('p')
findNode.appendChild(findHeader)
findNode.appendChild(findSelectorNode)
// Put all the sections together
var resultHTML = document.createElement('div')
resultHTML.appendChild(headerNode)
resultHTML.appendChild(reasonsNode)
resultHTML.appendChild(findNode)
wrapper.appendChild(resultHTML)
})
})
}
var _throwUncaughtError = function (error) {
// aXe swallows throw errors so we pass it through a setTimeout
// so that it's not in aXe's context
setTimeout(function () {
throw new Error(error)
}, 0)
}
window.GOVUK.AccessibilityTest = AccessibilityTest
// Cut the mustard at IE9+ since aXe only works with those browsers.
// http://responsivenews.co.uk/post/18948466399/cutting-the-mustard
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', function () {
AccessibilityTest('[data-module="test-a11y"]', function (axeError, consoleErrorText, pageResults) {
if (axeError) {
_throwUncaughtError(axeError)
}
if (pageResults) {
_renderAxeResultsInGuide(pageResults.incompleteWarnings, '[data-module="test-a11y-warning"]')
_renderAxeResultsInGuide(pageResults.violations, '[data-module="test-a11y-violation"]')
}
if (consoleErrorText) {
_throwUncaughtError(consoleErrorText)
}
})
})
}
})(window, document, window.axe)