forked from davidtodd/landmarks
-
Notifications
You must be signed in to change notification settings - Fork 7
/
content.finder.js
304 lines (241 loc) · 6.78 KB
/
content.finder.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
'use strict'
/* exported LandmarksFinder */
function LandmarksFinder(win, doc) {
//
// Constants
//
// List of landmarks to navigate
const regionTypes = Object.freeze([
'banner',
'complementary',
'contentinfo',
'form', // should label
'main',
'navigation',
'region', // must label
'search'
])
// Mapping of HTML5 elements to implicit roles
const implicitRoles = Object.freeze({
ASIDE: 'complementary',
FOOTER: 'contentinfo', // depending on its ancestor elements
FORM: 'form',
HEADER: 'banner', // depending on its ancestor elements
MAIN: 'main',
NAV: 'navigation',
SECTION: 'region'
})
// Sectioning content elements
const sectioningContentElements = Object.freeze([
'ARTICLE',
'ASIDE',
'NAV',
'SECTION'
])
// Non-<body> sectioning root elements
const nonBodySectioningRootElements = Object.freeze([
'BLOCKQUOTE',
'DETAILS',
'FIELDSET',
'FIGURE',
'TD'
])
// non-<body> sectioning elements and <main>
const nonBodySectioningElementsAndMain = Object.freeze(
sectioningContentElements.concat(nonBodySectioningRootElements, 'MAIN')
)
//
// Found landmarks
//
let landmarks = []
// Each member of this array is an object of the form:
// depth: (int) -- indicates nesting of landmarks
// role: (string) -- the ARIA role
// label: (string or null) -- author-supplied label
// element: (HTML*Element) -- in-memory element
let haveSearchedForLandmarks = false
//
// Keeping track of landmark navigation
//
let currentlySelectedIndex
// If we find a <main> or role="main" element...
let mainElementIndex
// Keep a reference to the currently-selected element in case the page
// changes and the landmarks are updated.
let selectedElement
function updateSelectedIndexAndReturnElement(index) {
if (landmarks.length === 0) return
currentlySelectedIndex = index
selectedElement = landmarks[index].element
return selectedElement
}
//
// Functions that refer to document or window
//
// Recursive function for building list of landmarks from a root element
function getLandmarks(element, depth, parentLandmark) {
if (element === undefined) return
element.childNodes.forEach(function(elementChild) {
if (elementChild.nodeType === win.Node.ELEMENT_NODE) {
if (isHidden(elementChild)) return
// Support HTML5 elements' native roles
let role = getRoleFromTagNameAndContainment(elementChild)
// Elements with explicitly-set rolees
if (elementChild.getAttribute) {
const tempRole = elementChild.getAttribute('role')
if (tempRole) {
role = tempRole
}
}
// The element may or may not have a label
const label = getARIAProvidedLabel(elementChild)
// Add the element if it should be considered a landmark
if (role && isLandmark(role, label)) {
if (parentLandmark && isDescendant(parentLandmark, elementChild)) {
depth = depth + 1
}
landmarks.push({
'depth': depth,
'role': role,
'label': label,
'element': elementChild
})
// Was this element selected before we were called (i.e.
// before the page was dynamically updated)?
if (selectedElement === elementChild) {
currentlySelectedIndex = landmarks.length - 1
}
// If this is the first main landmark (there should only
// be one), store a reference to it.
if (mainElementIndex < 0 && role === 'main') {
mainElementIndex = landmarks.length - 1
}
parentLandmark = elementChild
}
}
// Recursively traverse the tree structure of the child node
getLandmarks(elementChild, depth, parentLandmark)
})
}
function getARIAProvidedLabel(element) {
let label = null
const labelID = element.getAttribute('aria-labelledby')
if (labelID !== null) {
const labelElement = doc.getElementById(labelID)
label = getInnerText(labelElement)
}
if (label === null) {
label = element.getAttribute('aria-label')
}
return label
}
//
// Functions that do not refer to document or window
//
function isLandmark(role, label) {
// Region and form are landmarks only when they have labels
if (role === 'region' || role === 'form') {
return label !== null
}
// Is the role (which may have been explicitly set) a valid landmark type?
return regionTypes.includes(role)
}
function getInnerText(element) {
let text = null
if (element) {
text = element.innerText
if (text === undefined) {
text = element.textContent
}
}
return text
}
function isDescendant(ancestor, child) {
let node = child.parentNode
while (node !== null) {
if (node === ancestor) {
return true
}
node = node.parentNode
}
return false
}
function getRoleFromTagNameAndContainment(element) {
const name = element.tagName
let role = null
if (name) {
if (implicitRoles.hasOwnProperty(name)) {
role = implicitRoles[name]
}
// <header> and <footer> elements have some containment-
// related constraints on whether they're counted as landmarks
if (name === 'HEADER' || name === 'FOOTER') {
if (!isChildOfTopLevelSection(element)) {
role = null
}
}
}
return role
}
function isChildOfTopLevelSection(element) {
let ancestor = element.parentNode
while (ancestor !== null) {
if (nonBodySectioningElementsAndMain.includes(ancestor.tagName)) {
return false
}
ancestor = ancestor.parentNode
}
return true
}
function isHidden(element) {
const style = win.getComputedStyle(element)
if (element.hasAttribute('hidden')
|| style.visibility === 'hidden'
|| style.display === 'none' ) {
return true
}
return false
}
//
// Public API
//
this.find = function() {
landmarks = []
mainElementIndex = -1
currentlySelectedIndex = -1
getLandmarks(doc.body.parentNode, 0) // supports role on <body>
haveSearchedForLandmarks = true
}
this.haveSearchedForLandmarks = function() {
return haveSearchedForLandmarks
}
this.reset = function() {
haveSearchedForLandmarks = false
}
this.filter = function() {
return landmarks.map(landmark => ({
depth: landmark.depth,
role: landmark.role,
label: landmark.label
}))
}
this.numberOfLandmarks = function() {
return haveSearchedForLandmarks ? landmarks.length : -1
}
this.nextLandmarkElement = function() {
return updateSelectedIndexAndReturnElement(
(currentlySelectedIndex + 1) % landmarks.length
)
}
this.previousLandmarkElement = function() {
return updateSelectedIndexAndReturnElement(
(currentlySelectedIndex <= 0) ? landmarks.length - 1 : currentlySelectedIndex - 1
)
}
this.landmarkElement = function(index) {
return updateSelectedIndexAndReturnElement(index)
}
this.selectMainElement = function() {
return mainElementIndex < 0 ? null : updateSelectedIndexAndReturnElement(mainElementIndex)
}
}