summaryrefslogtreecommitdiffstats
path: root/remote/shared/test/xpcshell/test_DOM.js
blob: 19844659b95992c9a2a392e52a4f8402eda8ce7e (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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const { dom } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/DOM.sys.mjs"
);
const { NodeCache } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/webdriver/NodeCache.sys.mjs"
);

class MockElement {
  constructor(tagName, attrs = {}) {
    this.tagName = tagName;
    this.localName = tagName;

    this.isConnected = false;
    this.ownerGlobal = {
      document: {
        isActive() {
          return true;
        },
      },
    };

    for (let attr in attrs) {
      this[attr] = attrs[attr];
    }
  }

  get nodeType() {
    return 1;
  }

  get ELEMENT_NODE() {
    return 1;
  }

  // this is a severely limited CSS selector
  // that only supports lists of tag names
  matches(selector) {
    let tags = selector.split(",");
    return tags.includes(this.localName);
  }
}

class MockXULElement extends MockElement {
  constructor(tagName, attrs = {}) {
    super(tagName, attrs);
    this.namespaceURI = XUL_NS;

    if (typeof this.ownerDocument == "undefined") {
      this.ownerDocument = {};
    }
    if (typeof this.ownerDocument.documentElement == "undefined") {
      this.ownerDocument.documentElement = { namespaceURI: XUL_NS };
    }
  }
}

const xulEl = new MockXULElement("text");

const domElInPrivilegedDocument = new MockElement("input", {
  nodePrincipal: { isSystemPrincipal: true },
});
const xulElInPrivilegedDocument = new MockXULElement("text", {
  nodePrincipal: { isSystemPrincipal: true },
});

function setupTest() {
  const browser = Services.appShell.createWindowlessBrowser(false);

  browser.document.body.innerHTML = `
    <div id="foo" style="margin: 50px">
      <iframe></iframe>
      <video></video>
      <svg xmlns="http://www.w3.org/2000/svg"></svg>
      <textarea></textarea>
    </div>
  `;

  const divEl = browser.document.querySelector("div");
  const svgEl = browser.document.querySelector("svg");
  const textareaEl = browser.document.querySelector("textarea");
  const videoEl = browser.document.querySelector("video");

  const iframeEl = browser.document.querySelector("iframe");
  const childEl = iframeEl.contentDocument.createElement("div");
  iframeEl.contentDocument.body.appendChild(childEl);

  const shadowRoot = videoEl.openOrClosedShadowRoot;

  return {
    browser,
    nodeCache: new NodeCache(),
    childEl,
    divEl,
    iframeEl,
    shadowRoot,
    svgEl,
    textareaEl,
    videoEl,
  };
}

add_task(function test_findClosest() {
  const { divEl, videoEl } = setupTest();

  equal(dom.findClosest(divEl, "foo"), null);
  equal(dom.findClosest(videoEl, "div"), divEl);
});

add_task(function test_isSelected() {
  const { browser, divEl } = setupTest();

  const checkbox = browser.document.createElement("input");
  checkbox.setAttribute("type", "checkbox");

  ok(!dom.isSelected(checkbox));
  checkbox.checked = true;
  ok(dom.isSelected(checkbox));

  // selected is not a property of <input type=checkbox>
  checkbox.selected = true;
  checkbox.checked = false;
  ok(!dom.isSelected(checkbox));

  const option = browser.document.createElement("option");

  ok(!dom.isSelected(option));
  option.selected = true;
  ok(dom.isSelected(option));

  // checked is not a property of <option>
  option.checked = true;
  option.selected = false;
  ok(!dom.isSelected(option));

  // anything else should not be selected
  for (const type of [undefined, null, "foo", true, [], {}, divEl]) {
    ok(!dom.isSelected(type));
  }
});

add_task(function test_isElement() {
  const { divEl, iframeEl, shadowRoot, svgEl } = setupTest();

  ok(dom.isElement(divEl));
  ok(dom.isElement(svgEl));
  ok(dom.isElement(xulEl));
  ok(dom.isElement(domElInPrivilegedDocument));
  ok(dom.isElement(xulElInPrivilegedDocument));

  ok(!dom.isElement(shadowRoot));
  ok(!dom.isElement(divEl.ownerGlobal));
  ok(!dom.isElement(iframeEl.contentWindow));

  for (const type of [true, 42, {}, [], undefined, null]) {
    ok(!dom.isElement(type));
  }
});

add_task(function test_isDOMElement() {
  const { divEl, iframeEl, shadowRoot, svgEl } = setupTest();

  ok(dom.isDOMElement(divEl));
  ok(dom.isDOMElement(svgEl));
  ok(dom.isDOMElement(domElInPrivilegedDocument));

  ok(!dom.isDOMElement(shadowRoot));
  ok(!dom.isDOMElement(divEl.ownerGlobal));
  ok(!dom.isDOMElement(iframeEl.contentWindow));
  ok(!dom.isDOMElement(xulEl));
  ok(!dom.isDOMElement(xulElInPrivilegedDocument));

  for (const type of [true, 42, "foo", {}, [], undefined, null]) {
    ok(!dom.isDOMElement(type));
  }
});

add_task(function test_isXULElement() {
  const { divEl, iframeEl, shadowRoot, svgEl } = setupTest();

  ok(dom.isXULElement(xulEl));
  ok(dom.isXULElement(xulElInPrivilegedDocument));

  ok(!dom.isXULElement(divEl));
  ok(!dom.isXULElement(domElInPrivilegedDocument));
  ok(!dom.isXULElement(svgEl));
  ok(!dom.isXULElement(shadowRoot));
  ok(!dom.isXULElement(divEl.ownerGlobal));
  ok(!dom.isXULElement(iframeEl.contentWindow));

  for (const type of [true, 42, "foo", {}, [], undefined, null]) {
    ok(!dom.isXULElement(type));
  }
});

add_task(function test_isDOMWindow() {
  const { divEl, iframeEl, shadowRoot, svgEl } = setupTest();

  ok(dom.isDOMWindow(divEl.ownerGlobal));
  ok(dom.isDOMWindow(iframeEl.contentWindow));

  ok(!dom.isDOMWindow(divEl));
  ok(!dom.isDOMWindow(svgEl));
  ok(!dom.isDOMWindow(shadowRoot));
  ok(!dom.isDOMWindow(domElInPrivilegedDocument));
  ok(!dom.isDOMWindow(xulEl));
  ok(!dom.isDOMWindow(xulElInPrivilegedDocument));

  for (const type of [true, 42, {}, [], undefined, null]) {
    ok(!dom.isDOMWindow(type));
  }
});

add_task(function test_isShadowRoot() {
  const { browser, divEl, iframeEl, shadowRoot, svgEl } = setupTest();

  ok(dom.isShadowRoot(shadowRoot));

  ok(!dom.isShadowRoot(divEl));
  ok(!dom.isShadowRoot(svgEl));
  ok(!dom.isShadowRoot(divEl.ownerGlobal));
  ok(!dom.isShadowRoot(iframeEl.contentWindow));
  ok(!dom.isShadowRoot(xulEl));
  ok(!dom.isShadowRoot(domElInPrivilegedDocument));
  ok(!dom.isShadowRoot(xulElInPrivilegedDocument));

  for (const type of [true, 42, "foo", {}, [], undefined, null]) {
    ok(!dom.isShadowRoot(type));
  }

  const documentFragment = browser.document.createDocumentFragment();
  ok(!dom.isShadowRoot(documentFragment));
});

add_task(function test_isReadOnly() {
  const { browser, divEl, textareaEl } = setupTest();

  const input = browser.document.createElement("input");
  input.readOnly = true;
  ok(dom.isReadOnly(input));

  textareaEl.readOnly = true;
  ok(dom.isReadOnly(textareaEl));

  ok(!dom.isReadOnly(divEl));
  divEl.readOnly = true;
  ok(!dom.isReadOnly(divEl));

  ok(!dom.isReadOnly(null));
});

add_task(function test_isDisabled() {
  const { browser, divEl, svgEl } = setupTest();

  const select = browser.document.createElement("select");
  const option = browser.document.createElement("option");
  select.appendChild(option);
  select.disabled = true;
  ok(dom.isDisabled(option));

  const optgroup = browser.document.createElement("optgroup");
  option.parentNode = optgroup;
  ok(dom.isDisabled(option));

  optgroup.parentNode = select;
  ok(dom.isDisabled(option));

  select.disabled = false;
  ok(!dom.isDisabled(option));

  for (const type of ["button", "input", "select", "textarea"]) {
    const elem = browser.document.createElement(type);
    ok(!dom.isDisabled(elem));
    elem.disabled = true;
    ok(dom.isDisabled(elem));
  }

  ok(!dom.isDisabled(divEl));

  svgEl.disabled = true;
  ok(!dom.isDisabled(svgEl));

  ok(!dom.isDisabled(new MockXULElement("browser", { disabled: true })));
});

add_task(function test_isEditingHost() {
  const { browser, divEl, svgEl } = setupTest();

  ok(!dom.isEditingHost(null));

  ok(!dom.isEditingHost(divEl));
  divEl.contentEditable = true;
  ok(dom.isEditingHost(divEl));

  ok(!dom.isEditingHost(svgEl));
  browser.document.designMode = "on";
  ok(dom.isEditingHost(svgEl));
});

add_task(function test_isEditable() {
  const { browser, divEl, svgEl, textareaEl } = setupTest();

  ok(!dom.isEditable(null));

  for (let type of [
    "checkbox",
    "radio",
    "hidden",
    "submit",
    "button",
    "image",
  ]) {
    const input = browser.document.createElement("input");
    input.setAttribute("type", type);

    ok(!dom.isEditable(input));
  }

  const input = browser.document.createElement("input");
  ok(dom.isEditable(input));
  input.setAttribute("type", "text");
  ok(dom.isEditable(input));

  ok(dom.isEditable(textareaEl));

  const textareaDisabled = browser.document.createElement("textarea");
  textareaDisabled.disabled = true;
  ok(!dom.isEditable(textareaDisabled));

  const textareaReadOnly = browser.document.createElement("textarea");
  textareaReadOnly.readOnly = true;
  ok(!dom.isEditable(textareaReadOnly));

  ok(!dom.isEditable(divEl));
  divEl.contentEditable = true;
  ok(dom.isEditable(divEl));

  ok(!dom.isEditable(svgEl));
  browser.document.designMode = "on";
  ok(dom.isEditable(svgEl));
});

add_task(function test_isMutableFormControlElement() {
  const { browser, divEl, textareaEl } = setupTest();

  ok(!dom.isMutableFormControl(null));

  ok(dom.isMutableFormControl(textareaEl));

  const textareaDisabled = browser.document.createElement("textarea");
  textareaDisabled.disabled = true;
  ok(!dom.isMutableFormControl(textareaDisabled));

  const textareaReadOnly = browser.document.createElement("textarea");
  textareaReadOnly.readOnly = true;
  ok(!dom.isMutableFormControl(textareaReadOnly));

  const mutableStates = new Set([
    "color",
    "date",
    "datetime-local",
    "email",
    "file",
    "month",
    "number",
    "password",
    "range",
    "search",
    "tel",
    "text",
    "url",
    "week",
  ]);
  for (const type of mutableStates) {
    const input = browser.document.createElement("input");
    input.setAttribute("type", type);
    ok(dom.isMutableFormControl(input));
  }

  const inputHidden = browser.document.createElement("input");
  inputHidden.setAttribute("type", "hidden");
  ok(!dom.isMutableFormControl(inputHidden));

  ok(!dom.isMutableFormControl(divEl));
  divEl.contentEditable = true;
  ok(!dom.isMutableFormControl(divEl));
  browser.document.designMode = "on";
  ok(!dom.isMutableFormControl(divEl));
});

add_task(function test_coordinates() {
  const { divEl } = setupTest();

  let coords = dom.coordinates(divEl);
  ok(coords.hasOwnProperty("x"));
  ok(coords.hasOwnProperty("y"));
  equal(typeof coords.x, "number");
  equal(typeof coords.y, "number");

  deepEqual(dom.coordinates(divEl), { x: 0, y: 0 });
  deepEqual(dom.coordinates(divEl, 10, 10), { x: 10, y: 10 });
  deepEqual(dom.coordinates(divEl, -5, -5), { x: -5, y: -5 });

  Assert.throws(() => dom.coordinates(null), /node is null/);

  Assert.throws(
    () => dom.coordinates(divEl, "string", undefined),
    /Offset must be a number/
  );
  Assert.throws(
    () => dom.coordinates(divEl, undefined, "string"),
    /Offset must be a number/
  );
  Assert.throws(
    () => dom.coordinates(divEl, "string", "string"),
    /Offset must be a number/
  );
  Assert.throws(
    () => dom.coordinates(divEl, {}, undefined),
    /Offset must be a number/
  );
  Assert.throws(
    () => dom.coordinates(divEl, undefined, {}),
    /Offset must be a number/
  );
  Assert.throws(
    () => dom.coordinates(divEl, {}, {}),
    /Offset must be a number/
  );
  Assert.throws(
    () => dom.coordinates(divEl, [], undefined),
    /Offset must be a number/
  );
  Assert.throws(
    () => dom.coordinates(divEl, undefined, []),
    /Offset must be a number/
  );
  Assert.throws(
    () => dom.coordinates(divEl, [], []),
    /Offset must be a number/
  );
});

add_task(function test_isDetached() {
  const { childEl, iframeEl } = setupTest();

  let detachedShadowRoot = childEl.attachShadow({ mode: "open" });
  detachedShadowRoot.innerHTML = "<input></input>";

  // Connected to the DOM
  ok(!dom.isDetached(detachedShadowRoot));

  // Node document (ownerDocument) is not the active document
  iframeEl.remove();
  ok(dom.isDetached(detachedShadowRoot));

  // host element is stale (eg. not connected)
  detachedShadowRoot.host.remove();
  equal(childEl.isConnected, false);
  ok(dom.isDetached(detachedShadowRoot));
});

add_task(function test_isStale() {
  const { childEl, iframeEl } = setupTest();

  // Connected to the DOM
  ok(!dom.isStale(childEl));

  // Not part of the active document
  iframeEl.remove();
  ok(dom.isStale(childEl));

  // Not connected to the DOM
  childEl.remove();
  ok(dom.isStale(childEl));
});