-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
focus-method-delegatesFocus.html
232 lines (210 loc) · 7.69 KB
/
focus-method-delegatesFocus.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<title>HTML Test: focus() on shadow host with delegatesFocus</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/shadow-utils.js"></script>
<body>
<div id="host">
<div id="slottedToSecondSlot" slot="secondSlot">slottedToSecondSlot</div>
<div id="slottedToFirstSlot" slot="firstSlot">slottedToFirstSlot</div>
</div>
<div id="outside">outside</div>
</body>
<script>
const host = document.getElementById("host");
const slottedToSecondSlot = document.getElementById("slottedToSecondSlot");
const slottedToFirstSlot = document.getElementById("slottedToFirstSlot");
const outside = document.getElementById("outside");
const shadowRoot = host.attachShadow({ mode: "open", delegatesFocus: true });
const aboveSlots = document.createElement("div");
aboveSlots.innerText = "aboveSlots";
const firstSlot = document.createElement("slot");
firstSlot.name = "firstSlot";
const secondSlot = document.createElement("slot");
secondSlot.name = "secondSlot";
const belowSlots = document.createElement("div");
belowSlots.innerText = "belowSlots";
shadowRoot.appendChild(aboveSlots);
shadowRoot.appendChild(firstSlot);
shadowRoot.appendChild(secondSlot);
shadowRoot.appendChild(belowSlots);
const elementsInFlatTreeOrder = [host, aboveSlots, firstSlot,
slottedToFirstSlot, secondSlot, slottedToSecondSlot, belowSlots, outside];
// Final structure:
// <div #host> (delegatesFocus=true)
// #shadowRoot
// <div #aboveSlots>
// <slot #firstSlot>
// (slotted) <div #slottedToFirstSlot>
// <slot #secondSlot>
// (slotted) <div #slottedToSecondSlot>
// <div #belowSlots>
// <div #outside>
function setAllTabIndex(value) {
setTabIndex(elementsInFlatTreeOrder, value);
}
function removeAllTabIndex() {
removeTabIndex(elementsInFlatTreeOrder);
}
function resetTabIndexAndFocus() {
removeAllTabIndex();
resetFocus(document);
resetFocus(shadowRoot);
}
test(() => {
resetTabIndexAndFocus();
setAllTabIndex(0);
// Structure:
// <div #host> (delegatesFocus=true) tabindex=0
// #shadowRoot
// <div #aboveSlots> tabindex=0
// <slot #firstSlot> tabindex=0
// (slotted) <div #slottedToFirstSlot> tabindex=0
// <slot #secondSlot> tabindex=0
// (slotted) <div #slottedToSecondSlot> tabindex=0
// <div #belowSlots> tabindex=0
// <div #outside> tabindex=0
// First focusable = #aboveSlots
host.focus();
assert_equals(shadowRoot.activeElement, aboveSlots);
assert_equals(document.activeElement, host);
}, "focus() on host with delegatesFocus, all tabindex=0");
test(() => {
resetTabIndexAndFocus();
setAllTabIndex(0);
setTabIndex([host], -1);
// First focusable = #aboveSlots
host.focus();
assert_equals(shadowRoot.activeElement, aboveSlots);
assert_equals(document.activeElement, host);
}, "focus() on host with delegatesFocus & tabindex =-1, all other tabindex=0");
test(() => {
resetTabIndexAndFocus();
setTabIndex([aboveSlots, slottedToFirstSlot, slottedToSecondSlot, belowSlots], 0);
// First focusable = #aboveSlots
host.focus();
assert_equals(shadowRoot.activeElement, aboveSlots);
assert_equals(document.activeElement, host);
}, "focus() on host with delegatesFocus & no tabindex, all other tabindex=0");
test(() => {
resetTabIndexAndFocus();
setAllTabIndex(-1);
setTabIndex([host], 0);
// First focusable = #aboveSlots
host.focus();
assert_equals(shadowRoot.activeElement, aboveSlots);
assert_equals(document.activeElement, host);
}, "focus() on host with delegatesFocus & tabindex = 0, all other tabindex=-1");
test(() => {
resetTabIndexAndFocus();
removeAllTabIndex();
// No focusable element under #host in the flat tree.
host.focus();
assert_equals(shadowRoot.activeElement, null);
assert_equals(document.activeElement, null);
}, "focus() on host with delegatesFocus, all without tabindex");
test(() => {
resetTabIndexAndFocus();
// First focusable = #aboveSlots
setAllTabIndex(-1);
host.focus();
assert_equals(shadowRoot.activeElement, aboveSlots);
assert_equals(document.activeElement, host);
}, "focus() on host with delegatesFocus, all tabindex=-1");
test(() => {
resetTabIndexAndFocus();
removeAllTabIndex();
setTabIndex([host, belowSlots], 0);
// Structure:
// <div #host> (delegatesFocus=true) tabindex=0
// #shadowRoot
// <div #aboveSlots>
// <slot #firstSlot>
// (slotted) <div #slottedToFirstSlot>
// <slot #secondSlot>
// (slotted) <div #slottedToSecondSlot>
// <div #belowSlots> tabindex=0
// <div #outside>
// First focusable = #belowSlots
host.focus();
assert_equals(shadowRoot.activeElement, belowSlots);
assert_equals(document.activeElement, host);
}, "focus() on host with delegatesFocus & tabindex=0, #belowSlots with tabindex=0");
test(() => {
resetTabIndexAndFocus();
removeAllTabIndex();
setTabIndex([host, outside], 0);
// Structure:
// <div #host> (delegatesFocus=true) tabindex=0
// #shadowRoot
// <div #aboveSlots>
// <slot #firstSlot>
// (slotted) <div #slottedToFirstSlot>
// <slot #secondSlot>
// (slotted) <div #slottedToSecondSlot>
// <div #belowSlots>
// <div #outside> tabindex=0
// No focusable element under #host in the flat tree.
host.focus();
assert_equals(shadowRoot.activeElement, null);
assert_equals(document.activeElement, null);
}, "focus() on host with delegatesFocus & tabindex=0, #outside with tabindex=0");
test(() => {
resetTabIndexAndFocus();
setTabIndex([host, aboveSlots, belowSlots], 0);
// Structure:
// <div #host> (delegatesFocus=true) tabindex=0
// #shadowRoot
// <div #aboveSlots> tabindex=0
// <slot #firstSlot>
// (slotted) <div #slottedToFirstSlot>
// <slot #secondSlot>
// (slotted) <div #slottedToSecondSlot>
// <div #belowSlots> tabindex=0
// <div #outside>
// First focusable = #aboveSlots
host.focus();
assert_equals(shadowRoot.activeElement, aboveSlots);
assert_equals(document.activeElement, host);
}, "focus() on host with delegatesFocus & tabindex=0, #aboveSlots and #belowSlots with tabindex=0");
test(() => {
resetTabIndexAndFocus();
setTabIndex([host, aboveSlots], 0);
setTabIndex([belowSlots], 1);
// Structure:
// <div #host> (delegatesFocus=true) tabindex=0
// #shadowRoot
// <div #aboveSlots> tabindex=0
// <slot #firstSlot>
// (slotted) <div #slottedToFirstSlot>
// <slot #secondSlot>
// (slotted) <div #slottedToSecondSlot>
// <div #belowSlots> tabindex=1
// <div #outside>
// First focusable = #aboveSlots
host.focus();
assert_equals(shadowRoot.activeElement, aboveSlots);
assert_equals(document.activeElement, host);
}, "focus() on host with delegatesFocus & tabindex=0, #aboveSlots with tabindex=0 and #belowSlots with tabindex=1");
test(() => {
resetTabIndexAndFocus();
setTabIndex([host, slottedToFirstSlot, slottedToSecondSlot, belowSlots], 0);
// Structure:
// <div #host> (delegatesFocus=true) tabindex=0
// #shadowRoot
// <div #aboveSlots>
// <slot #firstSlot>
// (slotted) <div #slottedToFirstSlot> tabindex=0
// <slot #secondSlot>
// (slotted) <div #slottedToSecondSlot> tabindex=0
// <div #belowSlots> tabindex=0
// <div #outside>
// First focusable = #slottedToFirstSlot
host.focus();
assert_equals(shadowRoot.activeElement, null);
assert_equals(document.activeElement, slottedToFirstSlot);
}, "focus() on host with delegatesFocus & tabindex=0, #slottedToFirstSlot, #slottedToSecondSlot, #belowSlots with tabindex=0");
</script>