summaryrefslogtreecommitdiffstats
path: root/editor/spellchecker/tests/test_spellcheck_after_edit.html
blob: e4fa76d2e41e617946a1818d1edc5c22adb1c559 (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
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Spellcheck result after edit</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<script>
let { maybeOnSpellCheck } = SpecialPowers.ChromeUtils.import(
  "resource://testing-common/AsyncSpellCheckTestHelper.jsm"
);

function waitForTick() {
  return new Promise(resolve =>
    SimpleTest.executeSoon(
      () => requestAnimationFrame(
        () => requestAnimationFrame(resolve)
      )
    )
  );
}

async function waitForOnSpellCheck(
  aSpellCheckSelection,
  aEditingHost,
  aWaitForNumberOfMisspelledWords,
  aWhen
) {
  info(`Waiting for onSpellCheck (${aWhen})...`);
  for (let retry = 0; retry < 100; retry++) {
    await waitForTick();
    await new Promise(resolve => maybeOnSpellCheck(aEditingHost, resolve));
    if (aWaitForNumberOfMisspelledWords === 0) {
      if (aSpellCheckSelection.rangeCount === 0) {
        break;
      }
    } else if (aSpellCheckSelection.rangeCount >= aWaitForNumberOfMisspelledWords) {
      break;
    }
  }
}

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
  /**
   * test object should have:
   *   init function
   *     @param normalSel       The normal selection for the editing host
   *     @param editingHost     The editing host of the editor
   *     @return                Number of misspelled word in the editor
   *
   *  run function
   *     @param editingHost     The editing host of the editor
   *     @return                Expected number of misspelled word in the editor
   *
   *  check function
   *     @param spellCheckSel   The spellcheck selection for the editing host
   *     @param editingHost     The editing host of the editor
   */
  for (const test of [
      {
        init: (normalSel, editingHost) => {
          info("Staring to test spellcheck of misspelled word after joining paragraphs");
          // eslint-disable-next-line no-unsanitized/property
          editingHost.innerHTML = "<p>It is</p><p>what I want</p>";
          normalSel.collapse(editingHost.querySelector("p + p").firstChild, 0);
          return 0;
        },
        run: (editingHost) => {
          document.execCommand("delete");
          return 0;
        },
        check: (spellCheckSel, editingHost) => {
          is(
            spellCheckSel.rangeCount,
            0,
            "The joined misspelled word shouldn't be marked as misspelled word because caret is in the word"
          );
        },
      },
      {
        init: (normalSel, editingHost) => {
          info("Staring to test spellcheck of correct word after joining paragraphs");
          // eslint-disable-next-line no-unsanitized/property
          editingHost.innerHTML = "<p>It's beco</p><p>ming nicer</p>";
          normalSel.collapse(editingHost.querySelector("p + p").firstChild, 0);
          return 2;
        },
        run: (editingHost) => {
          document.execCommand("delete");
          return 0;
        },
        check: (spellCheckSel, editingHost) => {
          is(
            spellCheckSel.rangeCount,
            0,
            "There shouldn't be misspelled word after joining separated word anyway"
          );
        },
      },
      {
        init: (normalSel, editingHost) => {
          info("Staring to test spellcheck of correct words after splitting a paragraph");
          // eslint-disable-next-line no-unsanitized/property
          editingHost.innerHTML = "<p>It iswhat I want</p>";
          normalSel.collapse(editingHost.querySelector("p").firstChild, "It is".length);
          return 1;
        },
        run: (editingHost) => {
          document.execCommand("insertParagraph");
          return 0;
        },
        check: (spellCheckSel, editingHost) => {
          is(
            spellCheckSel.rangeCount,
            0,
            "No word should be marked as misspelled after split"
          );
        },
      },
      {
        init: (normalSel, editingHost) => {
          info("Staring to test spellcheck of misspelled words after splitting a paragraph");
          // eslint-disable-next-line no-unsanitized/property
          editingHost.innerHTML = "<p>It's becoming nicer</p>";
          normalSel.collapse(editingHost.querySelector("p").firstChild, "It's beco".length);
          return 0;
        },
        run: (editingHost) => {
          document.execCommand("insertParagraph");
          return 1;
        },
        check: (spellCheckSel, editingHost) => {
          is(
            spellCheckSel.rangeCount,
            1,
            "The split word in the first paragraph should be marked as misspelled, but the second paragraph's should be so because of caret is in it"
          );
          if (!spellCheckSel.rangeCount) {
            return;
          }
          is(
            SpecialPowers.unwrap(spellCheckSel.getRangeAt(0).startContainer),
            editingHost.querySelector("p").firstChild,
            "First misspelled word should start in the first child of the first <p>"
          );
          is(
            SpecialPowers.unwrap(spellCheckSel.getRangeAt(0).endContainer),
            editingHost.querySelector("p").firstChild,
            "First misspelled word should end in the first child of the first <p>"
          );
          is(
            spellCheckSel.getRangeAt(0).startOffset,
            "It's ".length,
            "First misspelled word should start after 'It '"
          );
          is(
            spellCheckSel.getRangeAt(0).endOffset,
            "It's beco".length,
            "First misspelled word should end by after 'bec'"
          );
        },
      },
    ]) {
    const editingHost = document.createElement("div");
    editingHost.setAttribute("contenteditable", "");
    editingHost.setAttribute("spellcheck", "true");
    document.body.appendChild(editingHost);
    editingHost.focus();
    const editor =
      SpecialPowers.wrap(window).docShell.editingSession.getEditorForWindow(window);
    const nsISelectionController = SpecialPowers.Ci.nsISelectionController;
    const normalSel = editor.selectionController.getSelection(
        nsISelectionController.SELECTION_NORMAL
    );
    const spellCheckSel = editor.selectionController.getSelection(
      nsISelectionController.SELECTION_SPELLCHECK
    );
    const initialMisspelledWords = test.init(normalSel, editingHost);
    await waitForOnSpellCheck(
      spellCheckSel, editingHost, initialMisspelledWords, "before edit"
    );
    await waitForTick();
    const expectedMisspelledWords = test.run(editingHost);
    await waitForOnSpellCheck(
      spellCheckSel, editingHost, expectedMisspelledWords, "after edit"
    );
    test.check(spellCheckSel, editingHost);
    editingHost.remove();
    await waitForTick();
  }
  SimpleTest.finish();
});
</script>
</body>
</html>