summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/input-events/input-events-get-target-ranges.js
blob: 004416ec2a02e0abc254f9b2b7d0a0c41b751a7d (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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"use strict";

// TODO: extend `EditorTestUtils` in editing/include/edit-test-utils.mjs

const kBackspaceKey = "\uE003";
const kDeleteKey = "\uE017";
const kArrowRight = "\uE014";
const kArrowLeft = "\uE012";
const kShift = "\uE008";
const kMeta = "\uE03d";
const kControl = "\uE009";
const kAlt = "\uE00A";
const kKeyA = "a";

const kImgSrc =
  "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEElEQVR42mNgaGD4D8YwBgAw9AX9Y9zBwwAAAABJRU5ErkJggg==";

let gSelection, gEditor, gBeforeinput, gInput;

function initializeTest(aInnerHTML) {
  function onBeforeinput(event) {
    // NOTE: Blink makes `getTargetRanges()` return empty range after
    //       propagation, but this test wants to check the result during
    //       propagation.  Therefore, we need to cache the result, but will
    //       assert if `getTargetRanges()` returns different ranges after
    //       checking the cached ranges.
    event.cachedRanges = event.getTargetRanges();
    gBeforeinput.push(event);
  }
  function onInput(event) {
    event.cachedRanges = event.getTargetRanges();
    gInput.push(event);
  }
  if (gEditor !== document.querySelector("div[contenteditable]")) {
    if (gEditor) {
      gEditor.isListeningToInputEvents = false;
      gEditor.removeEventListener("beforeinput", onBeforeinput);
      gEditor.removeEventListener("input", onInput);
    }
    gEditor = document.querySelector("div[contenteditable]");
  }
  gSelection = getSelection();
  gBeforeinput = [];
  gInput = [];
  if (!gEditor.isListeningToInputEvents) {
    gEditor.isListeningToInputEvents = true;
    gEditor.addEventListener("beforeinput", onBeforeinput);
    gEditor.addEventListener("input", onInput);
  }

  setupEditor(aInnerHTML);
  gBeforeinput = [];
  gInput = [];
}

function getArrayOfRangesDescription(arrayOfRanges) {
  if (arrayOfRanges === null) {
    return "null";
  }
  if (arrayOfRanges === undefined) {
    return "undefined";
  }
  if (!Array.isArray(arrayOfRanges)) {
    return "Unknown Object";
  }
  if (arrayOfRanges.length === 0) {
    return "[]";
  }
  let result = "[";
  for (let range of arrayOfRanges) {
    result += `{${getRangeDescription(range)}},`;
  }
  result += "]";
  return result;
}

function getRangeDescription(range) {
  function getNodeDescription(node) {
    if (!node) {
      return "null";
    }
    switch (node.nodeType) {
      case Node.TEXT_NODE:
      case Node.COMMENT_NODE:
      case Node.CDATA_SECTION_NODE:
        return `${node.nodeName} "${node.data}"`;
      case Node.ELEMENT_NODE:
        return `<${node.nodeName.toLowerCase()}>`;
      default:
        return `${node.nodeName}`;
    }
  }
  if (range === null) {
    return "null";
  }
  if (range === undefined) {
    return "undefined";
  }
  return range.startContainer == range.endContainer &&
    range.startOffset == range.endOffset
    ? `(${getNodeDescription(range.startContainer)}, ${range.startOffset})`
    : `(${getNodeDescription(range.startContainer)}, ${
        range.startOffset
      }) - (${getNodeDescription(range.endContainer)}, ${range.endOffset})`;
}

function sendDeleteKey(modifier) {
  if (!modifier) {
    return new test_driver.Actions()
      .keyDown(kDeleteKey)
      .keyUp(kDeleteKey)
      .send();
  }
  return new test_driver.Actions()
    .keyDown(modifier)
    .keyDown(kDeleteKey)
    .keyUp(kDeleteKey)
    .keyUp(modifier)
    .send();
}

function sendBackspaceKey(modifier) {
  if (!modifier) {
    return new test_driver.Actions()
      .keyDown(kBackspaceKey)
      .keyUp(kBackspaceKey)
      .send();
  }
  return new test_driver.Actions()
    .keyDown(modifier)
    .keyDown(kBackspaceKey)
    .keyUp(kBackspaceKey)
    .keyUp(modifier)
    .send();
}

function sendKeyA() {
  return new test_driver.Actions()
    .keyDown(kKeyA)
    .keyUp(kKeyA)
    .send();
}

function sendArrowLeftKey() {
  return new test_driver.Actions()
    .keyDown(kArrowLeft)
    .keyUp(kArrowLeft)
    .send();
}

function sendArrowRightKey() {
  return new test_driver.Actions()
    .keyDown(kArrowRight)
    .keyUp(kArrowRight)
    .send();
}

function checkGetTargetRangesOfBeforeinputOnDeleteSomething(expectedRanges) {
  assert_equals(
    gBeforeinput.length,
    1,
    "One beforeinput event should be fired if the key operation tries to delete something"
  );
  assert_true(
    Array.isArray(gBeforeinput[0].cachedRanges),
    "gBeforeinput[0].getTargetRanges() should return an array of StaticRange instances at least during propagation"
  );
  let arrayOfExpectedRanges = Array.isArray(expectedRanges)
    ? expectedRanges
    : [expectedRanges];
  // Before checking the length of array of ranges, we should check the given
  // range first because the ranges are more important than whether there are
  // redundant additional unexpected ranges.
  for (
    let i = 0;
    i <
    Math.max(arrayOfExpectedRanges.length, gBeforeinput[0].cachedRanges.length);
    i++
  ) {
    assert_equals(
      getRangeDescription(gBeforeinput[0].cachedRanges[i]),
      getRangeDescription(arrayOfExpectedRanges[i]),
      `gBeforeinput[0].getTargetRanges()[${i}] should return expected range (inputType is "${gBeforeinput[0].inputType}")`
    );
  }
  assert_equals(
    gBeforeinput[0].cachedRanges.length,
    arrayOfExpectedRanges.length,
    `getTargetRanges() of beforeinput event should return ${arrayOfExpectedRanges.length} ranges`
  );
}

function checkGetTargetRangesOfInputOnDeleteSomething() {
  assert_equals(
    gInput.length,
    1,
    "One input event should be fired if the key operation deletes something"
  );
  // https://github.com/w3c/input-events/issues/113
  assert_true(
    Array.isArray(gInput[0].cachedRanges),
    "gInput[0].getTargetRanges() should return an array of StaticRange instances at least during propagation"
  );
  assert_equals(
    gInput[0].cachedRanges.length,
    0,
    "gInput[0].getTargetRanges() should return empty array during propagation"
  );
}

function checkGetTargetRangesOfInputOnDoNothing() {
  assert_equals(
    gInput.length,
    0,
    "input event shouldn't be fired when the key operation does not cause modifying the DOM tree"
  );
}

function checkBeforeinputAndInputEventsOnNOOP() {
  assert_equals(
    gBeforeinput.length,
    0,
    "beforeinput event shouldn't be fired when the key operation does not cause modifying the DOM tree"
  );
  assert_equals(
    gInput.length,
    0,
    "input event shouldn't be fired when the key operation does not cause modifying the DOM tree"
  );
}

function checkEditorContentResultAsSubTest(
  expectedResult,
  description,
  options = {}
) {
  test(() => {
    if (Array.isArray(expectedResult)) {
      assert_in_array(
        options.ignoreWhiteSpaceDifference
          ? gEditor.innerHTML.replace(/&nbsp;/g, " ")
          : gEditor.innerHTML,
        expectedResult
      );
    } else {
      assert_equals(
        options.ignoreWhiteSpaceDifference
          ? gEditor.innerHTML.replace(/&nbsp;/g, " ")
          : gEditor.innerHTML,
        expectedResult
      );
    }
  }, `${description} - comparing innerHTML`);
}

// Similar to `setupDiv` in editing/include/tests.js, this method sets
// innerHTML value of gEditor, and sets multiple selection ranges specified
// with the markers.
// - `[` specifies start boundary in a text node
// - `{` specifies start boundary before a node
// - `]` specifies end boundary in a text node
// - `}` specifies end boundary after a node
function setupEditor(innerHTMLWithRangeMarkers) {
  const startBoundaries = innerHTMLWithRangeMarkers.match(/\{|\[/g) || [];
  const endBoundaries = innerHTMLWithRangeMarkers.match(/\}|\]/g) || [];
  if (startBoundaries.length !== endBoundaries.length) {
    throw "Should match number of open/close markers";
  }

  gEditor.innerHTML = innerHTMLWithRangeMarkers;
  gEditor.focus();

  if (startBoundaries.length === 0) {
    // Don't remove the range for now since some tests may assume that
    // setting innerHTML does not remove all selection ranges.
    return;
  }

  function getNextRangeAndDeleteMarker(startNode) {
    function getNextLeafNode(node) {
      function inclusiveDeepestFirstChildNode(container) {
        while (container.firstChild) {
          container = container.firstChild;
        }
        return container;
      }
      if (node.hasChildNodes()) {
        return inclusiveDeepestFirstChildNode(node);
      }
      if (node.nextSibling) {
        return inclusiveDeepestFirstChildNode(node.nextSibling);
      }
      let nextSibling = (function nextSiblingOfAncestorElement(child) {
        for (
          let parent = child.parentElement;
          parent && parent != gEditor;
          parent = parent.parentElement
        ) {
          if (parent.nextSibling) {
            return parent.nextSibling;
          }
        }
        return null;
      })(node);
      if (!nextSibling) {
        return null;
      }
      return inclusiveDeepestFirstChildNode(nextSibling);
    }
    function scanMarkerInTextNode(textNode, offset) {
      return /[\{\[\]\}]/.exec(textNode.data.substr(offset));
    }
    let startMarker = (function scanNextStartMaker(
      startContainer,
      startOffset
    ) {
      function scanStartMakerInTextNode(textNode, offset) {
        let scanResult = scanMarkerInTextNode(textNode, offset);
        if (scanResult === null) {
          return null;
        }
        if (scanResult[0] === "}" || scanResult[0] === "]") {
          throw "An end marker is found before a start marker";
        }
        return {
          marker: scanResult[0],
          container: textNode,
          offset: scanResult.index + offset
        };
      }
      if (startContainer.nodeType === Node.TEXT_NODE) {
        let scanResult = scanStartMakerInTextNode(startContainer, startOffset);
        if (scanResult !== null) {
          return scanResult;
        }
      }
      let nextNode = startContainer;
      while ((nextNode = getNextLeafNode(nextNode))) {
        if (nextNode.nodeType === Node.TEXT_NODE) {
          let scanResult = scanStartMakerInTextNode(nextNode, 0);
          if (scanResult !== null) {
            return scanResult;
          }
          continue;
        }
      }
      return null;
    })(startNode, 0);
    if (startMarker === null) {
      return null;
    }
    let endMarker = (function scanNextEndMarker(startContainer, startOffset) {
      function scanEndMarkerInTextNode(textNode, offset) {
        let scanResult = scanMarkerInTextNode(textNode, offset);
        if (scanResult === null) {
          return null;
        }
        if (scanResult[0] === "{" || scanResult[0] === "[") {
          throw "A start marker is found before an end marker";
        }
        return {
          marker: scanResult[0],
          container: textNode,
          offset: scanResult.index + offset
        };
      }
      if (startContainer.nodeType === Node.TEXT_NODE) {
        let scanResult = scanEndMarkerInTextNode(startContainer, startOffset);
        if (scanResult !== null) {
          return scanResult;
        }
      }
      let nextNode = startContainer;
      while ((nextNode = getNextLeafNode(nextNode))) {
        if (nextNode.nodeType === Node.TEXT_NODE) {
          let scanResult = scanEndMarkerInTextNode(nextNode, 0);
          if (scanResult !== null) {
            return scanResult;
          }
          continue;
        }
      }
      return null;
    })(startMarker.container, startMarker.offset + 1);
    if (endMarker === null) {
      throw "Found an open marker, but not found corresponding close marker";
    }
    function indexOfContainer(container, child) {
      let offset = 0;
      for (let node = container.firstChild; node; node = node.nextSibling) {
        if (node == child) {
          return offset;
        }
        offset++;
      }
      throw "child must be a child node of container";
    }
    (function deleteFoundMarkers() {
      function removeNode(node) {
        let container = node.parentElement;
        let offset = indexOfContainer(container, node);
        node.remove();
        return { container, offset };
      }
      if (startMarker.container == endMarker.container) {
        // If the text node becomes empty, remove it and set collapsed range
        // to the position where there is the text node.
        if (startMarker.container.length === 2) {
          if (!/[\[\{][\]\}]/.test(startMarker.container.data)) {
            throw `Unexpected text node (data: "${startMarker.container.data}")`;
          }
          let { container, offset } = removeNode(startMarker.container);
          startMarker.container = endMarker.container = container;
          startMarker.offset = endMarker.offset = offset;
          startMarker.marker = endMarker.marker = "";
          return;
        }
        startMarker.container.data = `${startMarker.container.data.substring(
          0,
          startMarker.offset
        )}${startMarker.container.data.substring(
          startMarker.offset + 1,
          endMarker.offset
        )}${startMarker.container.data.substring(endMarker.offset + 1)}`;
        if (startMarker.offset >= startMarker.container.length) {
          startMarker.offset = endMarker.offset = startMarker.container.length;
          return;
        }
        endMarker.offset--; // remove the start marker's length
        if (endMarker.offset > endMarker.container.length) {
          endMarker.offset = endMarker.container.length;
        }
        return;
      }
      if (startMarker.container.length === 1) {
        let { container, offset } = removeNode(startMarker.container);
        startMarker.container = container;
        startMarker.offset = offset;
        startMarker.marker = "";
      } else {
        startMarker.container.data = `${startMarker.container.data.substring(
          0,
          startMarker.offset
        )}${startMarker.container.data.substring(startMarker.offset + 1)}`;
      }
      if (endMarker.container.length === 1) {
        let { container, offset } = removeNode(endMarker.container);
        endMarker.container = container;
        endMarker.offset = offset;
        endMarker.marker = "";
      } else {
        endMarker.container.data = `${endMarker.container.data.substring(
          0,
          endMarker.offset
        )}${endMarker.container.data.substring(endMarker.offset + 1)}`;
      }
    })();
    (function handleNodeSelectMarker() {
      if (startMarker.marker === "{") {
        if (startMarker.offset === 0) {
          // The range start with the text node.
          let container = startMarker.container.parentElement;
          startMarker.offset = indexOfContainer(
            container,
            startMarker.container
          );
          startMarker.container = container;
        } else if (startMarker.offset === startMarker.container.data.length) {
          // The range start after the text node.
          let container = startMarker.container.parentElement;
          startMarker.offset =
            indexOfContainer(container, startMarker.container) + 1;
          startMarker.container = container;
        } else {
          throw 'Start marker "{" is allowed start or end of a text node';
        }
      }
      if (endMarker.marker === "}") {
        if (endMarker.offset === 0) {
          // The range ends before the text node.
          let container = endMarker.container.parentElement;
          endMarker.offset = indexOfContainer(container, endMarker.container);
          endMarker.container = container;
        } else if (endMarker.offset === endMarker.container.data.length) {
          // The range ends with the text node.
          let container = endMarker.container.parentElement;
          endMarker.offset =
            indexOfContainer(container, endMarker.container) + 1;
          endMarker.container = container;
        } else {
          throw 'End marker "}" is allowed start or end of a text node';
        }
      }
    })();
    let range = document.createRange();
    range.setStart(startMarker.container, startMarker.offset);
    range.setEnd(endMarker.container, endMarker.offset);
    return range;
  }

  let ranges = [];
  for (
    let range = getNextRangeAndDeleteMarker(gEditor.firstChild);
    range;
    range = getNextRangeAndDeleteMarker(range.endContainer)
  ) {
    ranges.push(range);
  }

  gSelection.removeAllRanges();
  for (let range of ranges) {
    gSelection.addRange(range);
  }

  if (gSelection.rangeCount != ranges.length) {
    throw `Failed to set selection to the given ranges whose length is ${ranges.length}, but only ${gSelection.rangeCount} ranges are added`;
  }
}