Blame editor/AsyncSpellCheckTestHelper.jsm

Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
var EXPORTED_SYMBOLS = [
Packit f0b94e
  "onSpellCheck",
Packit f0b94e
];
Packit f0b94e
Packit f0b94e
const SPELL_CHECK_ENDED_TOPIC = "inlineSpellChecker-spellCheck-ended";
Packit f0b94e
const SPELL_CHECK_STARTED_TOPIC = "inlineSpellChecker-spellCheck-started";
Packit f0b94e
Packit f0b94e
/**
Packit f0b94e
 * Waits until spell checking has stopped on the given element.
Packit f0b94e
 *
Packit f0b94e
 * When a spell check is pending, this waits indefinitely until the spell check
Packit f0b94e
 * ends.  When a spell check is not pending, it waits a small number of turns of
Packit f0b94e
 * the event loop: if a spell check begins, it resumes waiting indefinitely for
Packit f0b94e
 * the end, and otherwise it stops waiting and calls the callback.
Packit f0b94e
 *
Packit f0b94e
 * This this can therefore trap spell checks that have not started at the time
Packit f0b94e
 * of calling, spell checks that have already started, multiple consecutive
Packit f0b94e
 * spell checks, and the absence of spell checks altogether.
Packit f0b94e
 *
Packit f0b94e
 * @param editableElement  The element being spell checked.
Packit f0b94e
 * @param callback         Called when spell check has completed or enough turns
Packit f0b94e
 *                         of the event loop have passed to determine it has not
Packit f0b94e
 *                         started.
Packit f0b94e
 */
Packit f0b94e
function onSpellCheck(editableElement, callback) {
Packit f0b94e
  let editor = editableElement.editor;
Packit f0b94e
  if (!editor) {
Packit f0b94e
    let win = editableElement.ownerGlobal;
Packit f0b94e
    editor = win.QueryInterface(Ci.nsIInterfaceRequestor).
Packit f0b94e
                 getInterface(Ci.nsIWebNavigation).
Packit f0b94e
                 QueryInterface(Ci.nsIInterfaceRequestor).
Packit f0b94e
                 getInterface(Ci.nsIEditingSession).
Packit f0b94e
                 getEditorForWindow(win);
Packit f0b94e
  }
Packit f0b94e
  if (!editor)
Packit f0b94e
    throw new Error("Unable to find editor for element " + editableElement);
Packit f0b94e
Packit f0b94e
  try {
Packit f0b94e
    // False is important here.  Pass false so that the inline spell checker
Packit f0b94e
    // isn't created if it doesn't already exist.
Packit f0b94e
    var isc = editor.getInlineSpellChecker(false);
Packit f0b94e
  }
Packit f0b94e
  catch (err) {
Packit f0b94e
    // getInlineSpellChecker throws if spell checking is not enabled instead of
Packit f0b94e
    // just returning null, which seems kind of lame.  (Spell checking is not
Packit f0b94e
    // enabled on Android.)  The point here is only to determine whether spell
Packit f0b94e
    // check is pending, and if getInlineSpellChecker throws, then it's not
Packit f0b94e
    // pending.
Packit f0b94e
  }
Packit f0b94e
  let waitingForEnded = isc && isc.spellCheckPending;
Packit f0b94e
  let count = 0;
Packit f0b94e
Packit f0b94e
  function observe(subj, topic, data) {
Packit f0b94e
    if (subj != editor)
Packit f0b94e
      return;
Packit f0b94e
    count = 0;
Packit f0b94e
    let expectedTopic = waitingForEnded ? SPELL_CHECK_ENDED_TOPIC :
Packit f0b94e
                        SPELL_CHECK_STARTED_TOPIC;
Packit f0b94e
    if (topic != expectedTopic)
Packit f0b94e
      Cu.reportError("Expected " + expectedTopic + " but got " + topic + "!");
Packit f0b94e
    waitingForEnded = !waitingForEnded;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  let os = Cc["@mozilla.org/observer-service;1"].
Packit f0b94e
           getService(Ci.nsIObserverService);
Packit f0b94e
  os.addObserver(observe, SPELL_CHECK_STARTED_TOPIC);
Packit f0b94e
  os.addObserver(observe, SPELL_CHECK_ENDED_TOPIC);
Packit f0b94e
Packit f0b94e
  let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
Packit f0b94e
  timer.init(function tick() {
Packit f0b94e
    // Wait an arbitrarily large number -- 50 -- turns of the event loop before
Packit f0b94e
    // declaring that no spell checks will start.
Packit f0b94e
    if (waitingForEnded || ++count < 50)
Packit f0b94e
      return;
Packit f0b94e
    timer.cancel();
Packit f0b94e
    os.removeObserver(observe, SPELL_CHECK_STARTED_TOPIC);
Packit f0b94e
    os.removeObserver(observe, SPELL_CHECK_ENDED_TOPIC);
Packit f0b94e
    callback();
Packit f0b94e
  }, 0, Ci.nsITimer.TYPE_REPEATING_SLACK);
Packit f0b94e
};