File: test_selection_at_beforeinput_insertReplacementText.html

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; 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 (129 lines) | stat: -rw-r--r-- 5,046 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
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Selection should select the target misspelled word before `beforeinput` event</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
<script>
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
  const { maybeOnSpellCheck } = SpecialPowers.ChromeUtils.importESModule(
    "resource://testing-common/AsyncSpellCheckTestHelper.sys.mjs"
  );

  function testTextEditor(aTextControl) {
    const description = aTextControl.localName;
    aTextControl.setAttribute("spellcheck", "true");
    aTextControl.value = "abc abx abc";
    aTextControl.focus();

    const textEditor = SpecialPowers.wrap(aTextControl).editor;
    const inlineSpellChecker = textEditor.getInlineSpellChecker(true);

    return new Promise(resolve => {
      maybeOnSpellCheck(aTextControl, () => {
        aTextControl.addEventListener("beforeinput", event => {
          is(
            `${event.type} (inputType=${event.inputType}), selectionStart=${
              aTextControl.selectionStart
            }, selectionEnd=${aTextControl.selectionEnd}`,
            "beforeinput (inputType=insertReplacementText), selectionStart=4, selectionEnd=7",
            `${description}: Selection should select the replacing word`
          );
          resolve();
        }, {once: true});
        aTextControl.selectionStart = aTextControl.selectionEnd = 5;
        const misspelledWord = inlineSpellChecker.getMisspelledWord(textEditor.rootElement.firstChild, 5);
        is(
          `${misspelledWord.startOffset}-${misspelledWord.endOffset}`,
          "4-7",
          `${description}: misspelled word range should be "abc [abx] abc"`
        );
        info(`${description}: Replacing "abx" with "aux"...`);
        inlineSpellChecker.replaceWord(textEditor.rootElement.firstChild, 5, "aux");
      });
    });
  }
  await testTextEditor(document.querySelector("input"));
  await testTextEditor(document.querySelector("textarea"));

  function testContentEditable(aEditingHost) {
    const description = "contenteditable";
    aEditingHost.setAttribute("spellcheck", "true");
    aEditingHost.textContent = "abc abx abc";
    aEditingHost.focus();

    const htmlEditor = SpecialPowers.wrap(window).docShell.editingSession.getEditorForWindow(window);
    const inlineSpellChecker = htmlEditor.getInlineSpellChecker(true);

    return new Promise(resolve => {
      maybeOnSpellCheck(aEditingHost, async () => {
        await new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)));
        function getRangeDescription(range) {
          function getNodeDescription(node) {
            if (!node) {
              return "null";
            }
            switch (node.nodeType) {
              case Node.TEXT_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})`;
        }
        aEditingHost.addEventListener("beforeinput", event => {
          is(
            `${event.type} (inputType=${event.inputType}), selection=${getRangeDescription(getSelection().getRangeAt(0))}`,
            `beforeinput (inputType=insertReplacementText), selection=${getRangeDescription({
              startContainer: aEditingHost.firstChild,
              startOffset: 4,
              endContainer: aEditingHost.firstChild,
              endOffset: 7,
            })}`,
            `${description}: Selection should select the replacing word`
          );
          resolve();
        }, {once: true});
        getSelection().collapse(aEditingHost.firstChild, 5);
        const misspelledWord = inlineSpellChecker.getMisspelledWord(aEditingHost.firstChild, 5);
        is(
          `${misspelledWord.startOffset}-${misspelledWord.endOffset}`,
          "4-7",
          `${description}: misspelled word range should be "abc [abx] abc"`
        );
        info(`${description}: Replacing "abx" with "aux"...`);
        inlineSpellChecker.replaceWord(aEditingHost.firstChild, 5, "aux");
      });
    });
  }
  await testContentEditable(document.querySelector("div[contenteditable]"));

  SimpleTest.finish();
});
</script>
</head>
<body>
<input>
<textarea></textarea>
<div contenteditable></div>
</body>
</html>