File: commandfor.html

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (81 lines) | stat: -rw-r--r-- 3,745 bytes parent folder | download | duplicates (3)
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
<!DOCTYPE HTML>
<html>

<head>
  <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/testdriver-actions.js"></script>
  <script src="/wai-aria/scripts/aria-utils.js"></script>
</head>

<body>
  <button id="toggle-button-1" command="toggle-popover" commandfor="x-popover-1">Toggle the popover</button>
  <x-popover-1 id="x-popover-1" popover>
    <template shadowrootmode="open" shadowrootreferencetarget="popover">
      <div id="popover" popover>Popover content inside shadow root</div>
    </template>
  </x-popover-1>

  <button id="toggle-button-2" command="toggle-popover" commandfor="x-popover-2">Toggle the popover</button>
  <x-popover-2 id="x-popover-2" popover></x-popover-2>
  <script>
    const customPopover2 = document.querySelector('x-popover-2');
    customPopover2.attachShadow({ mode: 'open', referenceTarget: 'popover' });
    customPopover2.shadowRoot.innerHTML = '<div id="popover" popover>Popover content inside shadow root</div>';
  </script>

  <button id="toggle-button-3" command="toggle-popover">Toggle the popover</button>
  <x-popover-3 id="x-popover-3" popover>
    <template shadowrootmode="open" shadowrootreferencetarget="popover">
      <div id="popover" popover>Popover content inside shadow root</div>
    </template>
  </x-popover-3>

  <script>
    function testCommandFor(popoverId, buttonId, name) {
      test(function () {
        const xPopover = document.getElementById(popoverId);
        const popover = xPopover.shadowRoot.getElementById("popover");

        let xPopoverShowCount = 0;
        xPopover.addEventListener('beforetoggle', (e) => {
          if (e.newState === "open")
            ++xPopoverShowCount;
        });
        let popoverShowCount = 0;
        popover.addEventListener('beforetoggle', (e) => {
          if (e.newState === "open")
            ++popoverShowCount;
        });

        const toggleButton = document.getElementById(buttonId);
        toggleButton.click();

        assert_equals(xPopoverShowCount, 0, "host should not be toggled if valid reference target is set");
        assert_equals(popoverShowCount, 1, "inner element should be target of toggle if reference target is set");
        popover.hidePopover();

        // Setting referenceTarget to null means the host element is the target of the idref.
        xPopover.shadowRoot.referenceTarget = null;
        toggleButton.click();
        assert_equals(xPopoverShowCount, 1, "host should be toggled if reference target is null");
        assert_equals(popoverShowCount, 1, "inner element should not be toggled if reference target is null");
        popover.hidePopover();

        // Setting referenceTarget to empty string means the idref is treated as invalid.
        xPopover.shadowRoot.referenceTarget = "";
        toggleButton.click();
        assert_equals(xPopoverShowCount, 1, "no additional toggle on host if reference target is empty string");
        assert_equals(popoverShowCount, 1, "no additional toggle on inner element if reference target is empty string");
      }, name);
    }
    testCommandFor('x-popover-1', 'toggle-button-1', "Shadow root reference target works with commandfor attribute.");
    testCommandFor('x-popover-2', 'toggle-button-2', "Shadow root reference target works with commandfor attribute via options.");
    document.getElementById('toggle-button-3').commandForElement = document.getElementById('x-popover-3');
    testCommandFor('x-popover-3', 'toggle-button-3', "Shadow root reference target works with .commandForElement property.");
  </script>
</body>

</html>