editor/AsyncSpellCheckTestHelper.jsm

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 this.EXPORTED_SYMBOLS = [
michael@0 6 "onSpellCheck",
michael@0 7 ];
michael@0 8
michael@0 9 const SPELL_CHECK_ENDED_TOPIC = "inlineSpellChecker-spellCheck-ended";
michael@0 10 const SPELL_CHECK_STARTED_TOPIC = "inlineSpellChecker-spellCheck-started";
michael@0 11
michael@0 12 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
michael@0 13
michael@0 14 /**
michael@0 15 * Waits until spell checking has stopped on the given element.
michael@0 16 *
michael@0 17 * When a spell check is pending, this waits indefinitely until the spell check
michael@0 18 * ends. When a spell check is not pending, it waits a small number of turns of
michael@0 19 * the event loop: if a spell check begins, it resumes waiting indefinitely for
michael@0 20 * the end, and otherwise it stops waiting and calls the callback.
michael@0 21 *
michael@0 22 * This this can therefore trap spell checks that have not started at the time
michael@0 23 * of calling, spell checks that have already started, multiple consecutive
michael@0 24 * spell checks, and the absence of spell checks altogether.
michael@0 25 *
michael@0 26 * @param editableElement The element being spell checked.
michael@0 27 * @param callback Called when spell check has completed or enough turns
michael@0 28 * of the event loop have passed to determine it has not
michael@0 29 * started.
michael@0 30 */
michael@0 31 function onSpellCheck(editableElement, callback) {
michael@0 32 let editor = editableElement.editor;
michael@0 33 if (!editor) {
michael@0 34 let win = editableElement.ownerDocument.defaultView;
michael@0 35 editor = win.QueryInterface(Ci.nsIInterfaceRequestor).
michael@0 36 getInterface(Ci.nsIWebNavigation).
michael@0 37 QueryInterface(Ci.nsIInterfaceRequestor).
michael@0 38 getInterface(Ci.nsIEditingSession).
michael@0 39 getEditorForWindow(win);
michael@0 40 }
michael@0 41 if (!editor)
michael@0 42 throw new Error("Unable to find editor for element " + editableElement);
michael@0 43
michael@0 44 try {
michael@0 45 // False is important here. Pass false so that the inline spell checker
michael@0 46 // isn't created if it doesn't already exist.
michael@0 47 var isc = editor.getInlineSpellChecker(false);
michael@0 48 }
michael@0 49 catch (err) {
michael@0 50 // getInlineSpellChecker throws if spell checking is not enabled instead of
michael@0 51 // just returning null, which seems kind of lame. (Spell checking is not
michael@0 52 // enabled on Android.) The point here is only to determine whether spell
michael@0 53 // check is pending, and if getInlineSpellChecker throws, then it's not
michael@0 54 // pending.
michael@0 55 }
michael@0 56 let waitingForEnded = isc && isc.spellCheckPending;
michael@0 57 let count = 0;
michael@0 58
michael@0 59 function observe(subj, topic, data) {
michael@0 60 if (subj != editor)
michael@0 61 return;
michael@0 62 count = 0;
michael@0 63 let expectedTopic = waitingForEnded ? SPELL_CHECK_ENDED_TOPIC :
michael@0 64 SPELL_CHECK_STARTED_TOPIC;
michael@0 65 if (topic != expectedTopic)
michael@0 66 Cu.reportError("Expected " + expectedTopic + " but got " + topic + "!");
michael@0 67 waitingForEnded = !waitingForEnded;
michael@0 68 }
michael@0 69
michael@0 70 let os = Cc["@mozilla.org/observer-service;1"].
michael@0 71 getService(Ci.nsIObserverService);
michael@0 72 os.addObserver(observe, SPELL_CHECK_STARTED_TOPIC, false);
michael@0 73 os.addObserver(observe, SPELL_CHECK_ENDED_TOPIC, false);
michael@0 74
michael@0 75 let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
michael@0 76 timer.init(function tick() {
michael@0 77 // Wait an arbitrarily large number -- 50 -- turns of the event loop before
michael@0 78 // declaring that no spell checks will start.
michael@0 79 if (waitingForEnded || ++count < 50)
michael@0 80 return;
michael@0 81 timer.cancel();
michael@0 82 os.removeObserver(observe, SPELL_CHECK_STARTED_TOPIC);
michael@0 83 os.removeObserver(observe, SPELL_CHECK_ENDED_TOPIC);
michael@0 84 callback();
michael@0 85 }, 0, Ci.nsITimer.TYPE_REPEATING_SLACK);
michael@0 86 };

mercurial