File: test_moz_label.html

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (282 lines) | stat: -rw-r--r-- 9,244 bytes parent folder | download | duplicates (12)
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
<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>MozLabel tests</title>
    <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    <link
      rel="stylesheet"
      href="chrome://mochikit/content/tests/SimpleTest/test.css"
    />
    <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
    <link rel="stylesheet" href="chrome://global/skin/in-content/common.css" />
    <script
      type="module"
      src="chrome://global/content/elements/moz-label.mjs"
    ></script>
  </head>
  <body>
    <p id="display"></p>
    <div id="content">
      <label is="moz-label" for="checkbox" accesskey="c"
        >For the checkbox:</label
      >
      <input type="checkbox" id="checkbox" />

      <label is="moz-label" accesskey="n">
        For the nested checkbox:
        <input type="checkbox" />
      </label>

      <label is="moz-label" for="radio" accesskey="r">For the radio:</label>
      <input type="radio" id="radio" />

      <label is="moz-label" accesskey="F">
        For the nested radio:
        <input type="radio" />
      </label>

      <label is="moz-label" for="button" accesskey="b">For the button:</label>
      <button id="button">Click me</button>

      <label is="moz-label" accesskey="u">
        For the nested button:
        <button>Click me too</button>
      </label>

      <label is="moz-label" shownaccesskey="k" class="test-shownaccesskey">
        Shownaccesskey highlights the key
        <input type="checkbox" />
      </label>

      <label is="moz-label" accesskey="t" class="test-changetext"
        ><span class="text">It has text</span></label
      >
      <label is="moz-label" shownaccesskey="t" class="test-changetext"
        ><span class="text">It has text</span></label
      >
    </div>
    <script class="testbody" type="application/javascript">
      let labels = document.querySelectorAll("label[is='moz-label']");
      let isMac = navigator.platform.includes("Mac");

      function performAccessKey(key) {
        synthesizeKey(
          key,
          navigator.platform.includes("Mac")
            ? { altKey: true, ctrlKey: true }
            : { altKey: true, shiftKey: true }
        );
      }

      // Accesskey underlining is disabled by default on Mac.
      // Reload the window and wait for load to ensure pref is applied.
      add_setup(async function setup() {
        if (isMac && !SpecialPowers.getIntPref("ui.key.menuAccessKey")) {
          await SpecialPowers.pushPrefEnv(
            { set: [["ui.key.menuAccessKey", 1]] },
            async () => {
              window.location.reload();
              await new Promise(resolve => {
                addEventListener("load", resolve, { once: true });
              });
            }
          );
        }
      });

      add_task(async function testAccesskeyUnderlined() {
        labels.forEach(label => {
          let accessKey =
            label.getAttribute("accesskey") ||
            label.getAttribute("shownaccesskey");
          let wrapper = label.querySelector(".accesskey");
          is(
            wrapper.textContent,
            accessKey,
            "The accesskey character is wrapped."
          );

          let textDecoration = getComputedStyle(wrapper)["text-decoration"];
          ok(
            textDecoration.includes("underline"),
            "The accesskey character is underlined."
          );
        });
      });

      add_task(async function testAccesskeyFocus() {
        labels.forEach(label => {
          let accessKey = label.getAttribute("accesskey");
          if (!accessKey || label.classList.contains("test-changetext")) {
            return;
          }
          // Find the labelled element via the "for" attr if there's an ID
          // association, or select the lastElementChild for nested elements
          let element =
            document.getElementById(label.getAttribute("for")) ||
            label.lastElementChild;

          isnot(
            document.activeElement,
            element,
            "Focus is not on the associated element."
          );

          performAccessKey(accessKey);

          is(
            document.activeElement,
            element,
            "Focus moved to the associated element."
          );
        });
      });

      add_task(async function testAccesskeyChange() {
        let label = labels[0];
        let nextAccesskey = "x";
        let originalAccesskey = label.getAttribute("accesskey");
        let getWrapper = () => label.querySelector(".accesskey");
        is(
          getWrapper().textContent,
          originalAccesskey,
          "Original accesskey character is wrapped."
        );

        label.setAttribute("accesskey", nextAccesskey);
        is(
          getWrapper().textContent,
          nextAccesskey,
          "New accesskey character is wrapped."
        );

        let elementId = label.getAttribute("for");
        let focusedEl = document.getElementById(elementId);

        performAccessKey(originalAccesskey);
        isnot(
          document.activeElement.id,
          focusedEl.id,
          "Focus has not moved to the associated element."
        );

        performAccessKey(nextAccesskey);
        is(
          document.activeElement.id,
          focusedEl.id,
          "Focus moved to the associated element."
        );
      });

      add_task(async function testAccesskeyAppended() {
        let label = labels[0];
        let originalText = label.textContent;
        let accesskey = "z"; // Letter not included in the label text.
        label.setAttribute("accesskey", accesskey);

        let expectedText = `${originalText} (Z):`;
        is(
          label.textContent,
          expectedText,
          "Access key is appended when not included in label text."
        );
      });

      add_task(async function testLabelClick() {
        let label = labels[0];
        let input = document.getElementById(label.getAttribute("for"));
        is(input.checked, false, "The associated input is not checked.");

        // Input state changes on label click.
        synthesizeMouseAtCenter(label, {});
        ok(input.checked, "The associated input is checked.");

        // Input state doesn't change on label click when input is disabled.
        input.disabled = true;
        synthesizeMouseAtCenter(label, {});
        ok(input.checked, "The associated input is still checked.");
      });

      add_task(async function testShownaccesskey() {
        let label = [...labels].find(l =>
          l.classList.contains("test-shownaccesskey")
        );
        let input = label.querySelector("input");
        is(input.checked, false, "The associated input is not checked.");

        // Input state changes on label click.
        performAccessKey("k");
        is(
          input.checked,
          false,
          "The associated input is not triggered by accesskey."
        );
      });

      add_task(async function testAccesskeyTextContentChange() {
        const ORIG_TEXT = "It has text";
        const NEW_TEXT = "New text";
        const ACCESSKEY = "t";
        let [accesskeyLabel, shownaccesskeyLabel] =
          document.querySelectorAll(".test-changetext");

        is(
          accesskeyLabel.textContent,
          ORIG_TEXT,
          "Original text is correct accesskey"
        );
        is(
          shownaccesskeyLabel.textContent,
          ORIG_TEXT,
          "Original text is correct shownaccesskey"
        );

        let getWrapper = label => label.querySelector(".accesskey");
        is(
          getWrapper(accesskeyLabel).textContent,
          ACCESSKEY,
          "Original accesskey character is wrapped accesskey"
        );
        is(
          getWrapper(shownaccesskeyLabel).textContent,
          ACCESSKEY,
          "Original accesskey character is wrapped accesskey"
        );

        // Set the textContent on the inner span so we don't hit `set textContent`
        accesskeyLabel.firstElementChild.textContent = NEW_TEXT;
        is(
          accesskeyLabel.textContent,
          NEW_TEXT,
          "Updated text is correct accesskey"
        );
        ok(!getWrapper(accesskeyLabel), "Accesskey span was removed accesskey");
        // formatAccessKey waits a tick so we don't get infinite observer looping
        await new Promise(r => queueMicrotask(r));
        is(
          getWrapper(accesskeyLabel).textContent,
          ACCESSKEY,
          "Updated accesskey character is wrapped accesskey"
        );
        shownaccesskeyLabel.firstElementChild.textContent = NEW_TEXT;
        is(
          shownaccesskeyLabel.textContent,
          NEW_TEXT,
          "Updated text is correct shownaccesskey"
        );
        ok(
          !getWrapper(shownaccesskeyLabel),
          "Accesskey span was removed shownaccesskey"
        );
        // formatAccessKey waits a tick so we don't get infinite observer looping
        await new Promise(r => queueMicrotask(r));
        is(
          getWrapper(shownaccesskeyLabel).textContent,
          ACCESSKEY,
          "Updated accesskey character is wrapped accesskey"
        );
      });
    </script>
  </body>
</html>