summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/interactive-elements/the-dialog-element/dialog-autofocus.html
blob: 149a53eacff584882692b7812b54b89b1e6db1ab (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
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./resources/common.js"></script>
<script>
promise_test(() => {
  return waitUntilLoadedAndAutofocused().then(() => {
        assert_equals(document.activeElement, document.getElementById("outer-button"));

        var dialog = document.getElementById('dialog');
        dialog.showModal();

        autofocusButton = document.getElementById('autofocus-button');
        assert_equals(document.activeElement, autofocusButton);

        anotherButton = document.getElementById('another-button');
        anotherButton.focus();
        assert_equals(document.activeElement, anotherButton);

        // Test that recreating layout does not give focus back to a previously autofocused element.
        autofocusButton.style.display = 'none';
        document.body.offsetHeight;
        autofocusButton.style.display = 'block';
        document.body.offsetHeight;
        assert_equals(document.activeElement, anotherButton);

        // Test that reinserting does not give focus back to a previously autofocused element.
        var parentNode = autofocusButton.parentNode;
        parentNode.removeChild(autofocusButton);
        document.body.offsetHeight;
        parentNode.appendChild(autofocusButton);
        document.body.offsetHeight;
        assert_equals(document.activeElement, anotherButton);

        dialog.close();
        // Test that dialog focusing steps run when a dialog is reopened.
        dialog.showModal();
        assert_equals(document.activeElement, autofocusButton);
        dialog.close();
  });
}, "autofocus when a modal dialog is opened");
</script>
</head>
<body>
<button id="outer-button" autofocus></button>
<dialog id="dialog">
    <button></button>
    <!-- Unfocusable elements with [autofocus] should be ignored. -->
    <input autofocus disabled>
    <textarea autofocus hidden></textarea>
    <dialog>
        <button autofocus></button>
    </dialog>
    <div>
        <span>
            <button id="autofocus-button" autofocus></button>
        </span>
    </div>
    <button id="another-button" autofocus></button>
</dialog>

</body>
</html>