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
|
<!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, document.body);
}, "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, document.body);
}, "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, belowSlots);
assert_equals(document.activeElement, host);
}, "focus() on host with delegatesFocus & tabindex=0, #slottedToFirstSlot, #slottedToSecondSlot, #belowSlots with tabindex=0");
test(() => {
resetTabIndexAndFocus();
setTabIndex([aboveSlots, belowSlots], 0);
belowSlots.focus();
host.focus();
assert_equals(shadowRoot.activeElement, belowSlots);
}, "focus() on host with delegatesFocus and already-focused non-first shadow descendant");
function createNestedHosts(innerDelegatesFocus) {
// Structure:
// <div> outerHost
// <input> outerLightChild
// #shadowRoot outerShadow delegatesFocus=true
// <span> innerHost
// #shadowRoot inneShadow delegatesFocus=true/false
// <input> innerShadowChild
// <input> outerShadowChild
const outerHost = document.createElement('div');
const outerLightChild = document.createElement('input');
outerHost.appendChild(outerLightChild);
const innerHost = document.createElement('span');
const outerShadow = outerHost.attachShadow({mode: 'closed', delegatesFocus:true});
outerShadow.appendChild(innerHost);
const outerShadowChild = document.createElement('input');
outerShadow.appendChild(outerShadowChild);
const innerShadow = innerHost.attachShadow({mode: 'closed', delegatesFocus:innerDelegatesFocus});
const innerShadowChild = document.createElement('input');
innerShadow.appendChild(innerShadowChild);
document.body.insertBefore(outerHost, document.body.firstChild);
return {outerHost: outerHost,
outerLightChild: outerLightChild,
outerShadow: outerShadow,
outerShadowChild: outerShadowChild,
innerHost: innerHost,
innerShadow: innerShadow,
innerShadowChild: innerShadowChild};
}
test(() => {
const dom = createNestedHosts(false);
dom.outerHost.focus();
assert_equals(document.activeElement, dom.outerHost);
assert_equals(dom.outerShadow.activeElement, dom.outerShadowChild);
}, 'focus() on host with delegatesFocus with another host with no delegatesFocus and a focusable child');
test(() => {
const dom = createNestedHosts(true);
dom.outerHost.focus();
assert_equals(document.activeElement, dom.outerHost);
assert_equals(dom.outerShadow.activeElement, dom.innerHost);
assert_equals(dom.innerShadow.activeElement, dom.innerShadowChild);
}, 'focus() on host with delegatesFocus with another host with delegatesFocus and a focusable child');
test(() => {
// Structure:
// <div> host
// #shadowRoot root delegatesFocus=true
// <slot>
// (slotted) <div>
// <input>
// <input #firstFocusable>
const host = document.createElement("div");
const slotted = document.createElement("div");
slotted.appendChild(document.createElement("input"));
host.appendChild(slotted);
const root = host.attachShadow({mode: "open", delegatesFocus: true});
const firstFocusable = document.createElement("input");
root.innerHTML = "<slot>";
root.appendChild(firstFocusable);
document.body.appendChild(host);
host.focus();
assert_equals(document.activeElement, host);
assert_equals(root.activeElement, firstFocusable);
}, "focus() on host with delegatesFocus and slotted focusable children");
</script>
|