editor/AsyncSpellCheckTestHelper.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/AsyncSpellCheckTestHelper.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +this.EXPORTED_SYMBOLS = [
     1.9 +  "onSpellCheck",
    1.10 +];
    1.11 +
    1.12 +const SPELL_CHECK_ENDED_TOPIC = "inlineSpellChecker-spellCheck-ended";
    1.13 +const SPELL_CHECK_STARTED_TOPIC = "inlineSpellChecker-spellCheck-started";
    1.14 +
    1.15 +const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
    1.16 +
    1.17 +/**
    1.18 + * Waits until spell checking has stopped on the given element.
    1.19 + *
    1.20 + * When a spell check is pending, this waits indefinitely until the spell check
    1.21 + * ends.  When a spell check is not pending, it waits a small number of turns of
    1.22 + * the event loop: if a spell check begins, it resumes waiting indefinitely for
    1.23 + * the end, and otherwise it stops waiting and calls the callback.
    1.24 + *
    1.25 + * This this can therefore trap spell checks that have not started at the time
    1.26 + * of calling, spell checks that have already started, multiple consecutive
    1.27 + * spell checks, and the absence of spell checks altogether.
    1.28 + *
    1.29 + * @param editableElement  The element being spell checked.
    1.30 + * @param callback         Called when spell check has completed or enough turns
    1.31 + *                         of the event loop have passed to determine it has not
    1.32 + *                         started.
    1.33 + */
    1.34 +function onSpellCheck(editableElement, callback) {
    1.35 +  let editor = editableElement.editor;
    1.36 +  if (!editor) {
    1.37 +    let win = editableElement.ownerDocument.defaultView;
    1.38 +    editor = win.QueryInterface(Ci.nsIInterfaceRequestor).
    1.39 +                 getInterface(Ci.nsIWebNavigation).
    1.40 +                 QueryInterface(Ci.nsIInterfaceRequestor).
    1.41 +                 getInterface(Ci.nsIEditingSession).
    1.42 +                 getEditorForWindow(win);
    1.43 +  }
    1.44 +  if (!editor)
    1.45 +    throw new Error("Unable to find editor for element " + editableElement);
    1.46 +
    1.47 +  try {
    1.48 +    // False is important here.  Pass false so that the inline spell checker
    1.49 +    // isn't created if it doesn't already exist.
    1.50 +    var isc = editor.getInlineSpellChecker(false);
    1.51 +  }
    1.52 +  catch (err) {
    1.53 +    // getInlineSpellChecker throws if spell checking is not enabled instead of
    1.54 +    // just returning null, which seems kind of lame.  (Spell checking is not
    1.55 +    // enabled on Android.)  The point here is only to determine whether spell
    1.56 +    // check is pending, and if getInlineSpellChecker throws, then it's not
    1.57 +    // pending.
    1.58 +  }
    1.59 +  let waitingForEnded = isc && isc.spellCheckPending;
    1.60 +  let count = 0;
    1.61 +
    1.62 +  function observe(subj, topic, data) {
    1.63 +    if (subj != editor)
    1.64 +      return;
    1.65 +    count = 0;
    1.66 +    let expectedTopic = waitingForEnded ? SPELL_CHECK_ENDED_TOPIC :
    1.67 +                        SPELL_CHECK_STARTED_TOPIC;
    1.68 +    if (topic != expectedTopic)
    1.69 +      Cu.reportError("Expected " + expectedTopic + " but got " + topic + "!");
    1.70 +    waitingForEnded = !waitingForEnded;
    1.71 +  }
    1.72 +
    1.73 +  let os = Cc["@mozilla.org/observer-service;1"].
    1.74 +           getService(Ci.nsIObserverService);
    1.75 +  os.addObserver(observe, SPELL_CHECK_STARTED_TOPIC, false);
    1.76 +  os.addObserver(observe, SPELL_CHECK_ENDED_TOPIC, false);
    1.77 +
    1.78 +  let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    1.79 +  timer.init(function tick() {
    1.80 +    // Wait an arbitrarily large number -- 50 -- turns of the event loop before
    1.81 +    // declaring that no spell checks will start.
    1.82 +    if (waitingForEnded || ++count < 50)
    1.83 +      return;
    1.84 +    timer.cancel();
    1.85 +    os.removeObserver(observe, SPELL_CHECK_STARTED_TOPIC);
    1.86 +    os.removeObserver(observe, SPELL_CHECK_ENDED_TOPIC);
    1.87 +    callback();
    1.88 +  }, 0, Ci.nsITimer.TYPE_REPEATING_SLACK);
    1.89 +};

mercurial