summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/interactive-elements/the-dialog-element/dialog-focus-shadow-double-nested.html
blob: 2cd63eb796b38f6a272aeb9eff13e5974705236b (plain)
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
<!DOCTYPE html>
<meta charset="utf-8">
<title>dialog focusing delegation: with two nested shadow trees</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>

<dialog>
  <template class="turn-into-shadow-tree delegates-focus">
    <button disabled>Non-focusable</button>
    <template class="turn-into-shadow-tree delegates-focus">
      <button tabindex="-1">Focusable</button>
      <button tabindex="-1" autofocus>Focusable</button>
      <button tabindex="-1">Focusable</button>
    </template>
    <button tabindex="-1">Focusable</button>
  </template>
  <button tabindex="-1">Focusable</button>
</dialog>

<script>
function turnIntoShadowTree(template) {
  for (const subTemplate of template.content.querySelectorAll(".turn-into-shadow-tree")) {
    turnIntoShadowTree(subTemplate);
  }

  const div = document.createElement("div");
  div.attachShadow({ mode: "open", delegatesFocus: template.classList.contains("delegates-focus") });
  div.shadowRoot.append(template.content);
  template.replaceWith(div);
}

for (const template of document.querySelectorAll(".turn-into-shadow-tree")) {
  turnIntoShadowTree(template);
}

for (const method of ["show", "showModal"]) {
  test(t => {
    const dialog = document.querySelector("dialog");
    dialog[method]();
    t.add_cleanup(() => dialog.close());

    const shadowHostOuter = dialog.querySelector("div");
    assert_equals(document.activeElement, shadowHostOuter, "document.activeElement");

    const shadowHostInner = shadowHostOuter.shadowRoot.querySelector("div");
    assert_equals(shadowHostOuter.shadowRoot.activeElement, shadowHostInner, "shadowHostOuter.shadowRoot.activeElement");

    const button = shadowHostInner.shadowRoot.querySelector("[autofocus]");
    assert_equals(shadowHostInner.shadowRoot.activeElement, button, "shadowHostInner.shadowRoot.activeElement");
  }, `${method}()`);
}
</script>