-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
maintain--tab-focus.ts
3569 lines (3024 loc) · 99.6 KB
/
maintain--tab-focus.ts
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable */
// @ts-nocheck
// Copied from https://github.com/medialize/ally.js
// License: MIT
// Copyright (c) 2015 Rodney Rehm
//
// Entrypoint: ally.js/maintain/tab-focus
import _platform from 'platform'
import cssEscape from 'css.escape'
// input may be undefined, selector-tring, Node, NodeList, HTMLCollection, array of Nodes
// yes, to some extent this is a bad replica of jQuery's constructor function
function nodeArray(input) {
if (!input) {
return []
}
if (Array.isArray(input)) {
return input
}
// instanceof Node - does not work with iframes
if (input.nodeType !== undefined) {
return [input]
}
if (typeof input === 'string') {
input = document.querySelectorAll(input)
}
if (input.length !== undefined) {
return [].slice.call(input, 0)
}
throw new TypeError('unexpected input ' + String(input))
}
function contextToElement(_ref) {
var context = _ref.context,
_ref$label = _ref.label,
label = _ref$label === undefined ? 'context-to-element' : _ref$label,
resolveDocument = _ref.resolveDocument,
defaultToDocument = _ref.defaultToDocument
var element = nodeArray(context)[0]
if (resolveDocument && element && element.nodeType === Node.DOCUMENT_NODE) {
element = element.documentElement
}
if (!element && defaultToDocument) {
return document.documentElement
}
if (!element) {
throw new TypeError(label + ' requires valid options.context')
}
if (
element.nodeType !== Node.ELEMENT_NODE &&
element.nodeType !== Node.DOCUMENT_FRAGMENT_NODE
) {
throw new TypeError(label + ' requires options.context to be an Element')
}
return element
}
function getShadowHost() {
var _ref =
arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
context = _ref.context
var element = contextToElement({
label: 'get/shadow-host',
context: context,
})
// walk up to the root
var container = null
while (element) {
container = element
element = element.parentNode
}
// https://developer.mozilla.org/en-US/docs/Web/API/Node.nodeType
// NOTE: Firefox 34 does not expose ShadowRoot.host (but 37 does)
if (
container.nodeType === container.DOCUMENT_FRAGMENT_NODE &&
container.host
) {
// the root is attached to a fragment node that has a host
return container.host
}
return null
}
function getDocument(node) {
if (!node) {
return document
}
if (node.nodeType === Node.DOCUMENT_NODE) {
return node
}
return node.ownerDocument || document
}
function isActiveElement(context) {
var element = contextToElement({
label: 'is/active-element',
resolveDocument: true,
context: context,
})
var _document = getDocument(element)
if (_document.activeElement === element) {
return true
}
var shadowHost = getShadowHost({ context: element })
if (shadowHost && shadowHost.shadowRoot.activeElement === element) {
return true
}
return false
}
// [elem, elem.parent, elem.parent.parent, …, html]
// will not contain the shadowRoot (DOCUMENT_FRAGMENT_NODE) and shadowHost
function getParents() {
var _ref =
arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
context = _ref.context
var list = []
var element = contextToElement({
label: 'get/parents',
context: context,
})
while (element) {
list.push(element)
// IE does know support parentElement on SVGElement
element = element.parentNode
if (element && element.nodeType !== Node.ELEMENT_NODE) {
element = null
}
}
return list
}
// Element.prototype.matches may be available at a different name
// https://developer.mozilla.org/en/docs/Web/API/Element/matches
var names = [
'matches',
'webkitMatchesSelector',
'mozMatchesSelector',
'msMatchesSelector',
]
var name = null
function findMethodName(element) {
names.some(function (_name) {
if (!element[_name]) {
return false
}
name = _name
return true
})
}
function elementMatches(element, selector) {
if (!name) {
findMethodName(element)
}
return element[name](selector)
}
// deep clone of original platform
var platform = JSON.parse(JSON.stringify(_platform))
// operating system
var os = platform.os.family || ''
var ANDROID = os === 'Android'
var WINDOWS = os.slice(0, 7) === 'Windows'
var OSX = os === 'OS X'
var IOS = os === 'iOS'
// layout
var BLINK = platform.layout === 'Blink'
var GECKO = platform.layout === 'Gecko'
var TRIDENT = platform.layout === 'Trident'
var EDGE = platform.layout === 'EdgeHTML'
var WEBKIT = platform.layout === 'WebKit'
// browser version (not layout engine version!)
var version = parseFloat(platform.version)
var majorVersion = Math.floor(version)
platform.majorVersion = majorVersion
platform.is = {
// operating system
ANDROID: ANDROID,
WINDOWS: WINDOWS,
OSX: OSX,
IOS: IOS,
// layout
BLINK: BLINK, // "Chrome", "Chrome Mobile", "Opera"
GECKO: GECKO, // "Firefox"
TRIDENT: TRIDENT, // "Internet Explorer"
EDGE: EDGE, // "Microsoft Edge"
WEBKIT: WEBKIT, // "Safari"
// INTERNET EXPLORERS
IE9: TRIDENT && majorVersion === 9,
IE10: TRIDENT && majorVersion === 10,
IE11: TRIDENT && majorVersion === 11,
}
function before() {
var data = {
// remember what had focus to restore after test
activeElement: document.activeElement,
// remember scroll positions to restore after test
windowScrollTop: window.scrollTop,
windowScrollLeft: window.scrollLeft,
bodyScrollTop: document.body.scrollTop,
bodyScrollLeft: document.body.scrollLeft,
}
// wrap tests in an element hidden from screen readers to prevent them
// from announcing focus, which can be quite irritating to the user
var iframe = document.createElement('iframe')
iframe.setAttribute(
'style',
'position:absolute; position:fixed; top:0; left:-2px; width:1px; height:1px; overflow:hidden;'
)
iframe.setAttribute('aria-live', 'off')
iframe.setAttribute('aria-busy', 'true')
iframe.setAttribute('aria-hidden', 'true')
document.body.appendChild(iframe)
var _window = iframe.contentWindow
var _document = _window.document
_document.open()
_document.close()
var wrapper = _document.createElement('div')
_document.body.appendChild(wrapper)
data.iframe = iframe
data.wrapper = wrapper
data.window = _window
data.document = _document
return data
}
// options.element:
// {string} element name
// {function} callback(wrapper, document) to generate an element
// options.mutate: (optional)
// {function} callback(element, wrapper, document) to manipulate element prior to focus-test.
// Can return DOMElement to define focus target (default: element)
// options.validate: (optional)
// {function} callback(element, focusTarget, document) to manipulate test-result
function test(data, options) {
// make sure we operate on a clean slate
data.wrapper.innerHTML = ''
// create dummy element to test focusability of
var element =
typeof options.element === 'string'
? data.document.createElement(options.element)
: options.element(data.wrapper, data.document)
// allow callback to further specify dummy element
// and optionally define element to focus
var focus =
options.mutate && options.mutate(element, data.wrapper, data.document)
if (!focus && focus !== false) {
focus = element
}
// element needs to be part of the DOM to be focusable
!element.parentNode && data.wrapper.appendChild(element)
// test if the element with invalid tabindex can be focused
focus && focus.focus && focus.focus()
// validate test's result
return options.validate
? options.validate(element, focus, data.document)
: data.document.activeElement === focus
}
function after(data) {
// restore focus to what it was before test and cleanup
if (data.activeElement === document.body) {
document.activeElement &&
document.activeElement.blur &&
document.activeElement.blur()
if (platform.is.IE10) {
// IE10 does not redirect focus to <body> when the activeElement is removed
document.body.focus()
}
} else {
data.activeElement && data.activeElement.focus && data.activeElement.focus()
}
document.body.removeChild(data.iframe)
// restore scroll position
window.scrollTop = data.windowScrollTop
window.scrollLeft = data.windowScrollLeft
document.body.scrollTop = data.bodyScrollTop
document.body.scrollLeft = data.bodyScrollLeft
}
function detectFocus(tests) {
var data = before()
var results = {}
Object.keys(tests).map(function (key) {
results[key] = test(data, tests[key])
})
after(data)
return results
}
// this file is overwritten by `npm run build:pre`
var version$1 = '1.4.1'
/*
Facility to cache test results in localStorage.
USAGE:
cache.get('key');
cache.set('key', 'value');
*/
function readLocalStorage(key) {
// allow reading from storage to retrieve previous support results
// even while the document does not have focus
var data = void 0
try {
data = window.localStorage && window.localStorage.getItem(key)
data = data ? JSON.parse(data) : {}
} catch (e) {
data = {}
}
return data
}
function writeLocalStorage(key, value) {
if (!document.hasFocus()) {
// if the document does not have focus when tests are executed, focus() may
// not be handled properly and events may not be dispatched immediately.
// This can happen when a document is reloaded while Developer Tools have focus.
try {
window.localStorage && window.localStorage.removeItem(key)
} catch (e) {
// ignore
}
return
}
try {
window.localStorage &&
window.localStorage.setItem(key, JSON.stringify(value))
} catch (e) {
// ignore
}
}
var userAgent =
(typeof window !== 'undefined' && window.navigator.userAgent) || ''
var cacheKey = 'ally-supports-cache'
var cache = readLocalStorage(cacheKey)
// update the cache if ally or the user agent changed (newer version, etc)
if (cache.userAgent !== userAgent || cache.version !== version$1) {
cache = {}
}
cache.userAgent = userAgent
cache.version = version$1
var cache$1 = {
get: function get() {
return cache
},
set: function set(values) {
Object.keys(values).forEach(function (key) {
cache[key] = values[key]
})
cache.time = new Date().toISOString()
writeLocalStorage(cacheKey, cache)
},
}
function cssShadowPiercingDeepCombinator() {
var combinator = void 0
// see https://dev.w3.org/csswg/css-scoping-1/#deep-combinator
// https://bugzilla.mozilla.org/show_bug.cgi?id=1117572
// https://code.google.com/p/chromium/issues/detail?id=446051
try {
document.querySelector('html >>> :first-child')
combinator = '>>>'
} catch (noArrowArrowArrow) {
try {
// old syntax supported at least up to Chrome 41
// https://code.google.com/p/chromium/issues/detail?id=446051
document.querySelector('html /deep/ :first-child')
combinator = '/deep/'
} catch (noDeep) {
combinator = ''
}
}
return combinator
}
var gif =
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-usemap
var focusAreaImgTabindex = {
element: 'div',
mutate: function mutate(element) {
element.innerHTML =
'<map name="image-map-tabindex-test">' +
'<area shape="rect" coords="63,19,144,45"></map>' +
'<img usemap="#image-map-tabindex-test" tabindex="-1" alt="" src="' +
gif +
'">'
return element.querySelector('area')
},
}
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-usemap
var focusAreaTabindex = {
element: 'div',
mutate: function mutate(element) {
element.innerHTML =
'<map name="image-map-tabindex-test">' +
'<area href="#void" tabindex="-1" shape="rect" coords="63,19,144,45"></map>' +
'<img usemap="#image-map-tabindex-test" alt="" src="' +
gif +
'">'
return false
},
validate: function validate(element, focusTarget, _document) {
if (platform.is.GECKO) {
// fixes https://github.com/medialize/ally.js/issues/35
// Firefox loads the DataURI asynchronously, causing a false-negative
return true
}
var focus = element.querySelector('area')
focus.focus()
return _document.activeElement === focus
},
}
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-usemap
var focusAreaWithoutHref = {
element: 'div',
mutate: function mutate(element) {
element.innerHTML =
'<map name="image-map-area-href-test">' +
'<area shape="rect" coords="63,19,144,45"></map>' +
'<img usemap="#image-map-area-href-test" alt="" src="' +
gif +
'">'
return element.querySelector('area')
},
validate: function validate(element, focusTarget, _document) {
if (platform.is.GECKO) {
// fixes https://github.com/medialize/ally.js/issues/35
// Firefox loads the DataURI asynchronously, causing a false-negative
return true
}
return _document.activeElement === focusTarget
},
}
var focusAudioWithoutControls = {
name: 'can-focus-audio-without-controls',
element: 'audio',
mutate: function mutate(element) {
try {
// invalid media file can trigger warning in console, data-uri to prevent HTTP request
element.setAttribute('src', gif)
} catch (e) {
// IE9 may throw "Error: Not implemented"
}
},
}
var invalidGif =
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ'
// NOTE: https://github.com/medialize/ally.js/issues/35
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-usemap
var focusBrokenImageMap = {
element: 'div',
mutate: function mutate(element) {
element.innerHTML =
'<map name="broken-image-map-test"><area href="#void" shape="rect" coords="63,19,144,45"></map>' +
'<img usemap="#broken-image-map-test" alt="" src="' +
invalidGif +
'">'
return element.querySelector('area')
},
}
// Children of focusable elements with display:flex are focusable in IE10-11
var focusChildrenOfFocusableFlexbox = {
element: 'div',
mutate: function mutate(element) {
element.setAttribute('tabindex', '-1')
element.setAttribute(
'style',
'display: -webkit-flex; display: -ms-flexbox; display: flex;'
)
element.innerHTML = '<span style="display: block;">hello</span>'
return element.querySelector('span')
},
}
// fieldset[tabindex=0][disabled] should not be focusable, but Blink and WebKit disagree
// @specification https://www.w3.org/TR/html5/disabled-elements.html#concept-element-disabled
// @browser-issue Chromium https://crbug.com/453847
// @browser-issue WebKit https://bugs.webkit.org/show_bug.cgi?id=141086
var focusFieldsetDisabled = {
element: 'fieldset',
mutate: function mutate(element) {
element.setAttribute('tabindex', 0)
element.setAttribute('disabled', 'disabled')
},
}
var focusFieldset = {
element: 'fieldset',
mutate: function mutate(element) {
element.innerHTML = '<legend>legend</legend><p>content</p>'
},
}
// elements with display:flex are focusable in IE10-11
var focusFlexboxContainer = {
element: 'span',
mutate: function mutate(element) {
element.setAttribute(
'style',
'display: -webkit-flex; display: -ms-flexbox; display: flex;'
)
element.innerHTML = '<span style="display: block;">hello</span>'
},
}
// form[tabindex=0][disabled] should be focusable as the
// specification doesn't know the disabled attribute on the form element
// @specification https://www.w3.org/TR/html5/forms.html#the-form-element
var focusFormDisabled = {
element: 'form',
mutate: function mutate(element) {
element.setAttribute('tabindex', 0)
element.setAttribute('disabled', 'disabled')
},
}
// NOTE: https://github.com/medialize/ally.js/issues/35
// fixes https://github.com/medialize/ally.js/issues/20
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-ismap
var focusImgIsmap = {
element: 'a',
mutate: function mutate(element) {
element.href = '#void'
element.innerHTML = '<img ismap src="' + gif + '" alt="">'
return element.querySelector('img')
},
}
// NOTE: https://github.com/medialize/ally.js/issues/35
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-usemap
var focusImgUsemapTabindex = {
element: 'div',
mutate: function mutate(element) {
element.innerHTML =
'<map name="image-map-tabindex-test"><area href="#void" shape="rect" coords="63,19,144,45"></map>' +
'<img usemap="#image-map-tabindex-test" tabindex="-1" alt="" ' +
'src="' +
gif +
'">'
return element.querySelector('img')
},
}
var focusInHiddenIframe = {
element: function element(wrapper, _document) {
var iframe = _document.createElement('iframe')
// iframe must be part of the DOM before accessing the contentWindow is possible
wrapper.appendChild(iframe)
// create the iframe's default document (<html><head></head><body></body></html>)
var iframeDocument = iframe.contentWindow.document
iframeDocument.open()
iframeDocument.close()
return iframe
},
mutate: function mutate(iframe) {
iframe.style.visibility = 'hidden'
var iframeDocument = iframe.contentWindow.document
var input = iframeDocument.createElement('input')
iframeDocument.body.appendChild(input)
return input
},
validate: function validate(iframe) {
var iframeDocument = iframe.contentWindow.document
var focus = iframeDocument.querySelector('input')
return iframeDocument.activeElement === focus
},
}
var result = !platform.is.WEBKIT
function focusInZeroDimensionObject() {
return result
}
// Firefox allows *any* value and treats invalid values like tabindex="-1"
// @browser-issue Gecko https://bugzilla.mozilla.org/show_bug.cgi?id=1128054
var focusInvalidTabindex = {
element: 'div',
mutate: function mutate(element) {
element.setAttribute('tabindex', 'invalid-value')
},
}
var focusLabelTabindex = {
element: 'label',
mutate: function mutate(element) {
element.setAttribute('tabindex', '-1')
},
validate: function validate(element, focusTarget, _document) {
// force layout in Chrome 49, otherwise the element won't be focusable
/* eslint-disable no-unused-vars */
var variableToPreventDeadCodeElimination = element.offsetHeight
/* eslint-enable no-unused-vars */
element.focus()
return _document.activeElement === element
},
}
var svg =
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtb' +
'G5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiBpZD0ic3ZnIj48dGV4dCB4PSIxMCIgeT0iMjAiIGlkPSJ' +
'zdmctbGluay10ZXh0Ij50ZXh0PC90ZXh0Pjwvc3ZnPg=='
// Note: IE10 on BrowserStack does not like this test
var focusObjectSvgHidden = {
element: 'object',
mutate: function mutate(element) {
element.setAttribute('type', 'image/svg+xml')
element.setAttribute('data', svg)
element.setAttribute('width', '200')
element.setAttribute('height', '50')
element.style.visibility = 'hidden'
},
}
// Note: IE10 on BrowserStack does not like this test
var focusObjectSvg = {
name: 'can-focus-object-svg',
element: 'object',
mutate: function mutate(element) {
element.setAttribute('type', 'image/svg+xml')
element.setAttribute('data', svg)
element.setAttribute('width', '200')
element.setAttribute('height', '50')
},
validate: function validate(element, focusTarget, _document) {
if (platform.is.GECKO) {
// Firefox seems to be handling the object creation asynchronously and thereby produces a false negative test result.
// Because we know Firefox is able to focus object elements referencing SVGs, we simply cheat by sniffing the user agent string
return true
}
return _document.activeElement === element
},
}
// Every Environment except IE9 considers SWF objects focusable
var result$1 = !platform.is.IE9
function focusObjectSwf() {
return result$1
}
var focusRedirectImgUsemap = {
element: 'div',
mutate: function mutate(element) {
element.innerHTML =
'<map name="focus-redirect-img-usemap"><area href="#void" shape="rect" coords="63,19,144,45"></map>' +
'<img usemap="#focus-redirect-img-usemap" alt="" ' +
'src="' +
gif +
'">'
// focus the <img>, not the <div>
return element.querySelector('img')
},
validate: function validate(element, focusTarget, _document) {
var target = element.querySelector('area')
return _document.activeElement === target
},
}
// see https://jsbin.com/nenirisage/edit?html,js,console,output
var focusRedirectLegend = {
element: 'fieldset',
mutate: function mutate(element) {
element.innerHTML =
'<legend>legend</legend><input tabindex="-1"><input tabindex="0">'
// take care of focus in validate();
return false
},
validate: function validate(element, focusTarget, _document) {
var focusable = element.querySelector('input[tabindex="-1"]')
var tabbable = element.querySelector('input[tabindex="0"]')
// Firefox requires this test to focus the <fieldset> first, while this is not necessary in
// https://jsbin.com/nenirisage/edit?html,js,console,output
element.focus()
element.querySelector('legend').focus()
return (
(_document.activeElement === focusable && 'focusable') ||
(_document.activeElement === tabbable && 'tabbable') ||
''
)
},
}
// https://github.com/medialize/ally.js/issues/21
var focusScrollBody = {
element: 'div',
mutate: function mutate(element) {
element.setAttribute('style', 'width: 100px; height: 50px; overflow: auto;')
element.innerHTML =
'<div style="width: 500px; height: 40px;">scrollable content</div>'
return element.querySelector('div')
},
}
// https://github.com/medialize/ally.js/issues/21
var focusScrollContainerWithoutOverflow = {
element: 'div',
mutate: function mutate(element) {
element.setAttribute('style', 'width: 100px; height: 50px;')
element.innerHTML =
'<div style="width: 500px; height: 40px;">scrollable content</div>'
},
}
// https://github.com/medialize/ally.js/issues/21
var focusScrollContainer = {
element: 'div',
mutate: function mutate(element) {
element.setAttribute('style', 'width: 100px; height: 50px; overflow: auto;')
element.innerHTML =
'<div style="width: 500px; height: 40px;">scrollable content</div>'
},
}
var focusSummary = {
element: 'details',
mutate: function mutate(element) {
element.innerHTML = '<summary>foo</summary><p>content</p>'
return element.firstElementChild
},
}
function makeFocusableForeignObject() {
// Constructs <foreignObject width="30" height="30"><input type="text"/></foreignObject>
// without raising a Trusted Types violation
var foreignObject = document.createElementNS(
'http://www.w3.org/2000/svg',
'foreignObject'
)
foreignObject.width.baseVal.value = 30
foreignObject.height.baseVal.value = 30
foreignObject.appendChild(document.createElement('input'))
foreignObject.lastChild.type = 'text'
return foreignObject
}
function focusSvgForeignObjectHack(element) {
// Edge13, Edge14: foreignObject focus hack
// https://jsbin.com/kunehinugi/edit?html,js,output
// https://jsbin.com/fajagi/3/edit?html,js,output
var isSvgElement =
element.ownerSVGElement || element.nodeName.toLowerCase() === 'svg'
if (!isSvgElement) {
return false
}
// inject and focus an <input> element into the SVG element to receive focus
var foreignObject = makeFocusableForeignObject()
element.appendChild(foreignObject)
var input = foreignObject.querySelector('input')
input.focus()
// upon disabling the activeElement, IE and Edge
// will not shift focus to <body> like all the other
// browsers, but instead find the first focusable
// ancestor and shift focus to that
input.disabled = true
// clean up
element.removeChild(foreignObject)
return true
}
function generate(element) {
return (
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' +
element +
'</svg>'
)
}
function focus(element) {
if (element.focus) {
return
}
try {
HTMLElement.prototype.focus.call(element)
} catch (e) {
focusSvgForeignObjectHack(element)
}
}
function validate(element, focusTarget, _document) {
focus(focusTarget)
return _document.activeElement === focusTarget
}
var focusSvgFocusableAttribute = {
element: 'div',
mutate: function mutate(element) {
element.innerHTML = generate('<text focusable="true">a</text>')
return element.querySelector('text')
},
validate: validate,
}
var focusSvgTabindexAttribute = {
element: 'div',
mutate: function mutate(element) {
element.innerHTML = generate('<text tabindex="0">a</text>')
return element.querySelector('text')
},
validate: validate,
}
var focusSvgNegativeTabindexAttribute = {
element: 'div',
mutate: function mutate(element) {
element.innerHTML = generate('<text tabindex="-1">a</text>')
return element.querySelector('text')
},
validate: validate,
}
var focusSvgUseTabindex = {
element: 'div',
mutate: function mutate(element) {
element.innerHTML = generate(
[
'<g id="ally-test-target"><a xlink:href="#void"><text>link</text></a></g>',
'<use xlink:href="#ally-test-target" x="0" y="0" tabindex="-1" />',
].join('')
)
return element.querySelector('use')
},
validate: validate,
}
var focusSvgForeignobjectTabindex = {
element: 'div',
mutate: function mutate(element) {
element.innerHTML = generate(
'<foreignObject tabindex="-1"><input type="text" /></foreignObject>'
)
// Safari 8's quersSelector() can't identify foreignObject, but getElementyByTagName() can
return (
element.querySelector('foreignObject') ||
element.getElementsByTagName('foreignObject')[0]
)
},
validate: validate,
}
// Firefox seems to be handling the SVG-document-in-iframe creation asynchronously
// and thereby produces a false negative test result. Thus the test is pointless
// and we resort to UA sniffing once again.
// see http://jsbin.com/vunadohoko/1/edit?js,console,output
var result$2 = Boolean(
platform.is.GECKO &&
typeof SVGElement !== 'undefined' &&
SVGElement.prototype.focus
)
function focusSvgInIframe() {
return result$2
}
var focusSvg = {
element: 'div',
mutate: function mutate(element) {
element.innerHTML = generate('')
return element.firstChild
},
validate: validate,
}
// Firefox allows *any* value and treats invalid values like tabindex="-1"
// @browser-issue Gecko https://bugzilla.mozilla.org/show_bug.cgi?id=1128054
var focusTabindexTrailingCharacters = {
element: 'div',
mutate: function mutate(element) {
element.setAttribute('tabindex', '3x')
},
}
var focusTable = {
element: 'table',
mutate: function mutate(element, wrapper, _document) {
// IE9 has a problem replacing TBODY contents with innerHTML.
// https://stackoverflow.com/a/8097055/515124
// element.innerHTML = '<tr><td>cell</td></tr>';
var fragment = _document.createDocumentFragment()
fragment.innerHTML = '<tr><td>cell</td></tr>'
element.appendChild(fragment)
},
}
var focusVideoWithoutControls = {
element: 'video',
mutate: function mutate(element) {
try {
// invalid media file can trigger warning in console, data-uri to prevent HTTP request
element.setAttribute('src', gif)
} catch (e) {
// IE9 may throw "Error: Not implemented"
}
},
}
// https://jsbin.com/vafaba/3/edit?html,js,console,output
var result$3 = platform.is.GECKO || platform.is.TRIDENT || platform.is.EDGE
function tabsequenceAreaAtImgPosition() {
return result$3
}
var testCallbacks = {
cssShadowPiercingDeepCombinator: cssShadowPiercingDeepCombinator,
focusInZeroDimensionObject: focusInZeroDimensionObject,
focusObjectSwf: focusObjectSwf,
focusSvgInIframe: focusSvgInIframe,
tabsequenceAreaAtImgPosition: tabsequenceAreaAtImgPosition,
}