test_nsIEditorSpellCheck_ReplaceWord.html (2390B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Test for nsIEditorSpellCheck.ReplaceWord()</title> 5 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> 7 </head> 8 <body> 9 <div contenteditable spellcheck="true" lang="en-US"></div> 10 <script> 11 "use strict"; 12 13 SimpleTest.waitForExplicitFinish(); 14 SimpleTest.waitForFocus(async () => { 15 const { maybeOnSpellCheck } = SpecialPowers.ChromeUtils.importESModule( 16 "resource://testing-common/AsyncSpellCheckTestHelper.sys.mjs" 17 ); 18 const editor = document.querySelector("div[contenteditable]"); 19 async function replaceWord(aMisspelledWord, aCorrectWord, aReplaceAll) { 20 const editorObj = SpecialPowers.wrap(window).docShell.editingSession.getEditorForWindow(window); 21 const inlineSpellChecker = editorObj.getInlineSpellChecker(true); 22 await new Promise(resolve => maybeOnSpellCheck(editor, resolve)); 23 const editorSpellCheck = inlineSpellChecker.spellChecker; 24 editorObj.beginTransaction(); 25 try { 26 editorSpellCheck.ReplaceWord(aMisspelledWord, aCorrectWord, aReplaceAll); 27 } catch (e) { 28 ok(false, `Unexpected exception: ${e.message}`); 29 } 30 editorObj.endTransaction(); 31 editorSpellCheck.GetNextMisspelledWord(); 32 } 33 34 async function testReplaceAllMisspelledWords(aCorrectWord) { 35 editor.innerHTML = "<p>def abc def<br>abc def abc</p><p>abc def abc<br>def abc def</p>"; 36 editor.focus(); 37 editor.getBoundingClientRect(); 38 await replaceWord("abc", aCorrectWord, true); 39 is( 40 editor.innerHTML, 41 `<p>def ${aCorrectWord} def<br>${aCorrectWord} def ${aCorrectWord}</p><p>${aCorrectWord} def ${aCorrectWord}<br>def ${aCorrectWord} def</p>`, 42 `nsIEditorSpellCheck.ReplaceWord(..., true) should replace all misspelled words with ${ 43 (() => { 44 if (aCorrectWord.length > "abc".length) { 45 return "longer"; 46 } 47 return aCorrectWord.length < "abc".length ? "shorter" : "same length" 48 })() 49 } correct word` 50 ); 51 editor.blur(); 52 editor.getBoundingClientRect(); 53 } 54 await testReplaceAllMisspelledWords("ABC"); 55 await testReplaceAllMisspelledWords("ABC!"); 56 await testReplaceAllMisspelledWords("AB"); 57 58 // TODO: Add tests for not all replacing cases. 59 60 SimpleTest.finish(); 61 }); 62 </script> 63 </body> 64 </html>