summaryrefslogtreecommitdiffstats
path: root/editor/spellchecker/tests/test_nsIEditorSpellCheck_ReplaceWord.html
blob: 6088b97e3e03ab18e9987bd878e91f590fc910c4 (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
<!DOCTYPE html>
<html>
<head>
  <title>Test for nsIEditorSpellCheck.ReplaceWord()</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<div contenteditable spellcheck="true" lang="en-US"></div>
<script>
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
  const { maybeOnSpellCheck } = SpecialPowers.ChromeUtils.import(
    "resource://testing-common/AsyncSpellCheckTestHelper.jsm"
  );
  const editor = document.querySelector("div[contenteditable]");
  async function replaceWord(aMisspelledWord, aCorrectWord, aReplaceAll) {
    const editorObj = SpecialPowers.wrap(window).docShell.editingSession.getEditorForWindow(window);
    const inlineSpellChecker = editorObj.getInlineSpellChecker(true);
    await new Promise(resolve => maybeOnSpellCheck(editor, resolve));
    const editorSpellCheck = inlineSpellChecker.spellChecker;
    editorObj.beginTransaction();
    try {
      editorSpellCheck.ReplaceWord(aMisspelledWord, aCorrectWord, aReplaceAll);
    } catch (e) {
      ok(false, `Unexpected exception: ${e.message}`);
    }
    editorObj.endTransaction();
    editorSpellCheck.GetNextMisspelledWord();
  }

  async function testReplaceAllMisspelledWords(aCorrectWord) {
    editor.innerHTML = "<p>def abc def<br>abc def abc</p><p>abc def abc<br>def abc def</p>";
    editor.focus();
    editor.getBoundingClientRect();
    await replaceWord("abc", aCorrectWord, true);
    is(
      editor.innerHTML,
      `<p>def ${aCorrectWord} def<br>${aCorrectWord} def ${aCorrectWord}</p><p>${aCorrectWord} def ${aCorrectWord}<br>def ${aCorrectWord} def</p>`,
      `nsIEditorSpellCheck.ReplaceWord(..., true) should replace all misspelled words with ${
        (() => {
          if (aCorrectWord.length > "abc".length) {
            return "longer";
          }
          return aCorrectWord.length < "abc".length ? "shorter" : "same length"
        })()
      } correct word`
    );
    editor.blur();
    editor.getBoundingClientRect();
  }
  await testReplaceAllMisspelledWords("ABC");
  await testReplaceAllMisspelledWords("ABC!");
  await testReplaceAllMisspelledWords("AB");

  // TODO: Add tests for not all replacing cases.

  SimpleTest.finish();
});
</script>
</body>
</html>