2016-07-28 10:34:13 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["FinderIterator"];
|
|
|
|
|
|
|
|
const { interfaces: Ci, classes: Cc, utils: Cu } = Components;
|
|
|
|
|
2016-08-25 18:11:44 +00:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2016-07-28 10:34:13 +00:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
2016-08-25 18:11:44 +00:00
|
|
|
Cu.import("resource://gre/modules/Timer.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2016-07-28 10:34:13 +00:00
|
|
|
|
2016-08-25 18:11:44 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "NLP", "resource://gre/modules/NLP.jsm");
|
|
|
|
|
|
|
|
const kDebug = false;
|
2016-07-28 10:34:13 +00:00
|
|
|
const kIterationSizeMax = 100;
|
2016-08-25 18:11:44 +00:00
|
|
|
const kTimeoutPref = "findbar.iteratorTimeout";
|
2016-07-28 10:34:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* FinderIterator singleton. See the documentation for the `start()` method to
|
|
|
|
* learn more.
|
|
|
|
*/
|
|
|
|
this.FinderIterator = {
|
|
|
|
_currentParams: null,
|
|
|
|
_listeners: new Map(),
|
|
|
|
_catchingUp: new Set(),
|
|
|
|
_previousParams: null,
|
|
|
|
_previousRanges: [],
|
|
|
|
_spawnId: 0,
|
2016-08-25 18:11:44 +00:00
|
|
|
_timeout: Services.prefs.getIntPref(kTimeoutPref),
|
|
|
|
_timer: null,
|
2016-07-28 10:34:13 +00:00
|
|
|
ranges: [],
|
|
|
|
running: false,
|
|
|
|
|
2016-07-28 10:34:17 +00:00
|
|
|
// Expose `kIterationSizeMax` to the outside world for unit tests to use.
|
2016-07-28 11:15:09 +00:00
|
|
|
get kIterationSizeMax() { return kIterationSizeMax },
|
2016-07-28 10:34:17 +00:00
|
|
|
|
2016-08-02 08:40:21 +00:00
|
|
|
get params() {
|
2016-08-19 15:18:41 +00:00
|
|
|
if (!this._currentParams && !this._previousParams)
|
|
|
|
return null;
|
2016-08-02 08:40:21 +00:00
|
|
|
return Object.assign({}, this._currentParams || this._previousParams);
|
|
|
|
},
|
|
|
|
|
2016-07-28 10:34:13 +00:00
|
|
|
/**
|
|
|
|
* Start iterating the active Finder docShell, using the options below. When
|
|
|
|
* it already started at the request of another consumer, we first yield the
|
|
|
|
* results we already collected before continuing onward to yield fresh results.
|
|
|
|
* We make sure to pause every `kIterationSizeMax` iterations to make sure we
|
|
|
|
* don't block the host process too long. In the case of a break like this, we
|
|
|
|
* yield `undefined`, instead of a range.
|
|
|
|
* Upon re-entrance after a break, we check if `stop()` was called during the
|
|
|
|
* break and if so, we stop iterating.
|
2016-08-19 15:18:41 +00:00
|
|
|
* Results are also passed to the `listener.onIteratorRangeFound` callback
|
|
|
|
* method, along with a flag that specifies if the result comes from the cache
|
|
|
|
* or is fresh. The callback also adheres to the `limit` flag.
|
2016-07-28 10:34:13 +00:00
|
|
|
* The returned promise is resolved when 1) the limit is reached, 2) when all
|
|
|
|
* the ranges have been found or 3) when `stop()` is called whilst iterating.
|
|
|
|
*
|
2016-08-25 18:11:44 +00:00
|
|
|
* @param {Number} [options.allowDistance] Allowed edit distance between the
|
|
|
|
* current word and `options.word`
|
|
|
|
* when the iterator is already running
|
|
|
|
* @param {Boolean} options.caseSensitive Whether to search in case sensitive
|
|
|
|
* mode
|
|
|
|
* @param {Boolean} options.entireWord Whether to search in entire-word mode
|
|
|
|
* @param {Finder} options.finder Currently active Finder instance
|
|
|
|
* @param {Number} [options.limit] Limit the amount of results to be
|
|
|
|
* passed back. Optional, defaults to no
|
|
|
|
* limit.
|
|
|
|
* @param {Boolean} [options.linksOnly] Only yield ranges that are inside a
|
|
|
|
* hyperlink (used by QuickFind).
|
|
|
|
* Optional, defaults to `false`.
|
|
|
|
* @param {Object} options.listener Listener object that implements the
|
|
|
|
* following callback functions:
|
|
|
|
* - onIteratorRangeFound({nsIDOMRange} range);
|
|
|
|
* - onIteratorReset();
|
|
|
|
* - onIteratorRestart({Object} iterParams);
|
|
|
|
* - onIteratorStart({Object} iterParams);
|
|
|
|
* @param {Boolean} [options.useCache] Whether to allow results already
|
|
|
|
* present in the cache or demand fresh.
|
|
|
|
* Optional, defaults to `false`.
|
|
|
|
* @param {String} options.word Word to search for
|
2016-07-28 10:34:13 +00:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2016-08-25 18:11:44 +00:00
|
|
|
start({ allowDistance, caseSensitive, entireWord, finder, limit, linksOnly, listener, useCache, word }) {
|
2016-07-28 10:34:13 +00:00
|
|
|
// Take care of default values for non-required options.
|
2016-08-25 18:11:44 +00:00
|
|
|
if (typeof allowDistance != "number")
|
|
|
|
allowDistance = 0;
|
2016-07-28 10:34:13 +00:00
|
|
|
if (typeof limit != "number")
|
|
|
|
limit = -1;
|
|
|
|
if (typeof linksOnly != "boolean")
|
|
|
|
linksOnly = false;
|
|
|
|
if (typeof useCache != "boolean")
|
|
|
|
useCache = false;
|
|
|
|
|
|
|
|
// Validate the options.
|
2016-08-16 09:25:56 +00:00
|
|
|
if (typeof caseSensitive != "boolean")
|
|
|
|
throw new Error("Missing required option 'caseSensitive'");
|
|
|
|
if (typeof entireWord != "boolean")
|
|
|
|
throw new Error("Missing required option 'entireWord'");
|
2016-07-28 10:34:13 +00:00
|
|
|
if (!finder)
|
|
|
|
throw new Error("Missing required option 'finder'");
|
|
|
|
if (!word)
|
|
|
|
throw new Error("Missing required option 'word'");
|
2016-08-19 15:18:41 +00:00
|
|
|
if (typeof listener != "object" || !listener.onIteratorRangeFound)
|
|
|
|
throw new TypeError("Missing valid, required option 'listener'");
|
|
|
|
|
|
|
|
// If the listener was added before, make sure the promise is resolved before
|
|
|
|
// we replace it with another.
|
|
|
|
if (this._listeners.has(listener)) {
|
|
|
|
let { onEnd } = this._listeners.get(listener);
|
|
|
|
if (onEnd)
|
|
|
|
onEnd();
|
|
|
|
}
|
2016-07-28 10:34:13 +00:00
|
|
|
|
|
|
|
let window = finder._getWindow();
|
|
|
|
let resolver;
|
|
|
|
let promise = new Promise(resolve => resolver = resolve);
|
2016-10-11 11:08:00 +00:00
|
|
|
let iterParams = { caseSensitive, entireWord, linksOnly, useCache, window, word };
|
2016-07-28 10:34:13 +00:00
|
|
|
|
2016-08-19 15:18:41 +00:00
|
|
|
this._listeners.set(listener, { limit, onEnd: resolver });
|
2016-07-28 10:34:13 +00:00
|
|
|
|
|
|
|
// If we're not running anymore and we're requesting the previous result, use it.
|
|
|
|
if (!this.running && this._previousResultAvailable(iterParams)) {
|
2016-08-19 15:18:41 +00:00
|
|
|
this._yieldPreviousResult(listener, window);
|
2016-07-28 10:34:13 +00:00
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.running) {
|
|
|
|
// Double-check if we're not running the iterator with a different set of
|
2016-08-25 18:11:44 +00:00
|
|
|
// parameters, otherwise report an error with the most common reason.
|
|
|
|
if (!this._areParamsEqual(this._currentParams, iterParams, allowDistance)) {
|
|
|
|
if (kDebug) {
|
|
|
|
Cu.reportError(`We're currently iterating over '${this._currentParams.word}', not '${word}'\n` +
|
|
|
|
new Error().stack);
|
|
|
|
}
|
|
|
|
this._listeners.delete(listener);
|
|
|
|
resolver();
|
|
|
|
return promise;
|
|
|
|
}
|
2016-07-28 10:34:13 +00:00
|
|
|
|
|
|
|
// if we're still running, yield the set we have built up this far.
|
2016-08-19 15:18:41 +00:00
|
|
|
this._yieldIntermediateResult(listener, window);
|
2016-07-28 10:34:13 +00:00
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start!
|
|
|
|
this.running = true;
|
|
|
|
this._currentParams = iterParams;
|
2016-10-11 11:08:00 +00:00
|
|
|
this._findAllRanges(finder, ++this._spawnId);
|
2016-07-28 10:34:13 +00:00
|
|
|
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the currently running iterator as soon as possible and optionally cache
|
|
|
|
* the result for later.
|
|
|
|
*
|
|
|
|
* @param {Boolean} [cachePrevious] Whether to save the result for later.
|
|
|
|
* Optional.
|
|
|
|
*/
|
|
|
|
stop(cachePrevious = false) {
|
|
|
|
if (!this.running)
|
|
|
|
return;
|
|
|
|
|
2016-08-25 18:11:44 +00:00
|
|
|
if (this._timer) {
|
|
|
|
clearTimeout(this._timer);
|
|
|
|
this._timer = null;
|
|
|
|
}
|
2016-10-12 13:46:44 +00:00
|
|
|
if (this._runningFindResolver) {
|
|
|
|
this._runningFindResolver();
|
|
|
|
this._runningFindResolver = null;
|
|
|
|
}
|
2016-08-25 18:11:44 +00:00
|
|
|
|
2016-07-28 10:34:13 +00:00
|
|
|
if (cachePrevious) {
|
|
|
|
this._previousRanges = [].concat(this.ranges);
|
|
|
|
this._previousParams = Object.assign({}, this._currentParams);
|
|
|
|
} else {
|
|
|
|
this._previousRanges = [];
|
|
|
|
this._previousParams = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._catchingUp.clear();
|
|
|
|
this._currentParams = null;
|
|
|
|
this.ranges = [];
|
|
|
|
this.running = false;
|
|
|
|
|
|
|
|
for (let [, { onEnd }] of this._listeners)
|
|
|
|
onEnd();
|
2016-08-19 15:18:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the iteration that currently running, if it is, and start a new one
|
|
|
|
* with the exact same params as before.
|
|
|
|
*
|
|
|
|
* @param {Finder} finder Currently active Finder instance
|
|
|
|
*/
|
|
|
|
restart(finder) {
|
|
|
|
// Capture current iterator params before we stop the show.
|
|
|
|
let iterParams = this.params;
|
|
|
|
if (!iterParams)
|
|
|
|
return;
|
|
|
|
this.stop();
|
|
|
|
|
|
|
|
// Restart manually.
|
|
|
|
this.running = true;
|
|
|
|
this._currentParams = iterParams;
|
|
|
|
|
2016-10-11 11:08:00 +00:00
|
|
|
this._findAllRanges(finder, ++this._spawnId);
|
2016-08-19 15:18:41 +00:00
|
|
|
this._notifyListeners("restart", iterParams);
|
2016-07-28 10:34:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the internal state of the iterator. Typically this would be called
|
|
|
|
* when the docShell is not active anymore, which makes the current and cached
|
|
|
|
* previous result invalid.
|
|
|
|
* If the iterator is running, it will be stopped as soon as possible.
|
|
|
|
*/
|
|
|
|
reset() {
|
2016-08-25 18:11:44 +00:00
|
|
|
if (this._timer) {
|
|
|
|
clearTimeout(this._timer);
|
|
|
|
this._timer = null;
|
|
|
|
}
|
2016-10-12 13:46:44 +00:00
|
|
|
if (this._runningFindResolver) {
|
|
|
|
this._runningFindResolver();
|
|
|
|
this._runningFindResolver = null;
|
|
|
|
}
|
2016-08-25 18:11:44 +00:00
|
|
|
|
2016-07-28 10:34:13 +00:00
|
|
|
this._catchingUp.clear();
|
|
|
|
this._currentParams = this._previousParams = null;
|
|
|
|
this._previousRanges = [];
|
|
|
|
this.ranges = [];
|
|
|
|
this.running = false;
|
|
|
|
|
2016-08-19 15:18:41 +00:00
|
|
|
this._notifyListeners("reset");
|
2016-07-28 10:34:13 +00:00
|
|
|
for (let [, { onEnd }] of this._listeners)
|
|
|
|
onEnd();
|
|
|
|
this._listeners.clear();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the currently running iterator parameters are the same as the ones
|
|
|
|
* passed through the arguments. When `true`, we can keep it running as-is and
|
|
|
|
* the consumer should stop the iterator when `false`.
|
|
|
|
*
|
2016-08-16 09:25:56 +00:00
|
|
|
* @param {Boolean} options.caseSensitive Whether to search in case sensitive
|
|
|
|
* mode
|
|
|
|
* @param {Boolean} options.entireWord Whether to search in entire-word mode
|
|
|
|
* @param {Boolean} options.linksOnly Whether to search for the word to be
|
|
|
|
* present in links only
|
|
|
|
* @param {String} options.word The word being searched for
|
2016-07-28 10:34:13 +00:00
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2016-08-16 09:25:56 +00:00
|
|
|
continueRunning({ caseSensitive, entireWord, linksOnly, word }) {
|
2016-07-28 10:34:13 +00:00
|
|
|
return (this.running &&
|
2016-08-16 09:25:56 +00:00
|
|
|
this._currentParams.caseSensitive === caseSensitive &&
|
|
|
|
this._currentParams.entireWord === entireWord &&
|
2016-07-28 10:34:13 +00:00
|
|
|
this._currentParams.linksOnly === linksOnly &&
|
|
|
|
this._currentParams.word == word);
|
|
|
|
},
|
|
|
|
|
2017-01-13 16:27:18 +00:00
|
|
|
/**
|
|
|
|
* The default mode of operation of the iterator is to not accept duplicate
|
|
|
|
* listeners, resolve the promise of the older listeners and replace it with
|
|
|
|
* the new listener.
|
|
|
|
* Consumers may opt-out of this behavior by using this check and not call
|
|
|
|
* start().
|
|
|
|
*
|
|
|
|
* @param {Object} paramSet Property bag with the same signature as you would
|
|
|
|
* pass into `start()`
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
isAlreadyRunning(paramSet) {
|
|
|
|
return (this.running &&
|
|
|
|
this._areParamsEqual(this._currentParams, paramSet) &&
|
|
|
|
this._listeners.has(paramSet.listener));
|
|
|
|
},
|
|
|
|
|
2016-08-19 15:18:41 +00:00
|
|
|
/**
|
|
|
|
* Safely notify all registered listeners that an event has occurred.
|
|
|
|
*
|
|
|
|
* @param {String} callback Name of the callback to invoke
|
|
|
|
* @param {mixed} [params] Optional argument that will be passed to the
|
|
|
|
* callback
|
|
|
|
* @param {Iterable} [listeners] Set of listeners to notify. Optional, defaults
|
|
|
|
* to `this._listeners.keys()`.
|
|
|
|
*/
|
|
|
|
_notifyListeners(callback, params, listeners = this._listeners.keys()) {
|
|
|
|
callback = "onIterator" + callback.charAt(0).toUpperCase() + callback.substr(1);
|
|
|
|
for (let listener of listeners) {
|
|
|
|
try {
|
|
|
|
listener[callback](params);
|
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError("FinderIterator Error: " + ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-07-28 10:34:13 +00:00
|
|
|
/**
|
|
|
|
* Internal; check if an iteration request is available in the previous result
|
|
|
|
* that we cached.
|
|
|
|
*
|
2016-08-19 15:18:41 +00:00
|
|
|
* @param {Boolean} options.caseSensitive Whether to search in case sensitive
|
2016-08-16 09:25:56 +00:00
|
|
|
* mode
|
2016-08-19 15:18:41 +00:00
|
|
|
* @param {Boolean} options.entireWord Whether to search in entire-word mode
|
2016-08-16 09:25:56 +00:00
|
|
|
* @param {Boolean} options.linksOnly Whether to search for the word to be
|
|
|
|
* present in links only
|
|
|
|
* @param {Boolean} options.useCache Whether the consumer wants to use the
|
|
|
|
* cached previous result at all
|
|
|
|
* @param {String} options.word The word being searched for
|
2016-07-28 10:34:13 +00:00
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2016-08-16 09:25:56 +00:00
|
|
|
_previousResultAvailable({ caseSensitive, entireWord, linksOnly, useCache, word }) {
|
2016-07-28 10:34:13 +00:00
|
|
|
return !!(useCache &&
|
2016-08-16 09:25:56 +00:00
|
|
|
this._areParamsEqual(this._previousParams, { caseSensitive, entireWord, linksOnly, word }) &&
|
2016-07-28 10:34:13 +00:00
|
|
|
this._previousRanges.length);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal; compare if two sets of iterator parameters are equivalent.
|
|
|
|
*
|
2016-08-25 18:11:44 +00:00
|
|
|
* @param {Object} paramSet1 First set of params (left hand side)
|
|
|
|
* @param {Object} paramSet2 Second set of params (right hand side)
|
|
|
|
* @param {Number} [allowDistance] Allowed edit distance between the two words.
|
|
|
|
* Optional, defaults to '0', which means 'no
|
|
|
|
* distance'.
|
2016-07-28 10:34:13 +00:00
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2016-08-25 18:11:44 +00:00
|
|
|
_areParamsEqual(paramSet1, paramSet2, allowDistance = 0) {
|
2016-07-28 10:34:13 +00:00
|
|
|
return (!!paramSet1 && !!paramSet2 &&
|
2016-08-16 09:25:56 +00:00
|
|
|
paramSet1.caseSensitive === paramSet2.caseSensitive &&
|
|
|
|
paramSet1.entireWord === paramSet2.entireWord &&
|
2016-07-28 10:34:13 +00:00
|
|
|
paramSet1.linksOnly === paramSet2.linksOnly &&
|
2016-10-11 11:08:00 +00:00
|
|
|
paramSet1.window === paramSet2.window &&
|
2016-08-25 18:11:44 +00:00
|
|
|
NLP.levenshtein(paramSet1.word, paramSet2.word) <= allowDistance);
|
2016-07-28 10:34:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal; iterate over a predefined set of ranges that have been collected
|
|
|
|
* before.
|
|
|
|
* Also here, we make sure to pause every `kIterationSizeMax` iterations to
|
|
|
|
* make sure we don't block the host process too long. In the case of a break
|
|
|
|
* like this, we yield `undefined`, instead of a range.
|
|
|
|
*
|
2016-08-19 15:18:41 +00:00
|
|
|
* @param {Object} listener Listener object
|
2016-07-28 10:34:13 +00:00
|
|
|
* @param {Array} rangeSource Set of ranges to iterate over
|
|
|
|
* @param {nsIDOMWindow} window The window object is only really used
|
|
|
|
* for access to `setTimeout`
|
2016-08-19 15:18:41 +00:00
|
|
|
* @param {Boolean} [withPause] Whether to pause after each `kIterationSizeMax`
|
|
|
|
* number of ranges yielded. Optional, defaults
|
|
|
|
* to `true`.
|
2016-07-28 10:34:13 +00:00
|
|
|
* @yield {nsIDOMRange}
|
|
|
|
*/
|
2016-12-29 23:34:54 +00:00
|
|
|
*_yieldResult(listener, rangeSource, window, withPause = true) {
|
2016-07-28 10:34:13 +00:00
|
|
|
// We keep track of the number of iterations to allow a short pause between
|
|
|
|
// every `kIterationSizeMax` number of iterations.
|
|
|
|
let iterCount = 0;
|
2016-08-19 15:18:41 +00:00
|
|
|
let { limit, onEnd } = this._listeners.get(listener);
|
2016-07-28 10:34:13 +00:00
|
|
|
let ranges = rangeSource.slice(0, limit > -1 ? limit : undefined);
|
|
|
|
for (let range of ranges) {
|
|
|
|
try {
|
|
|
|
range.startContainer;
|
|
|
|
} catch (ex) {
|
|
|
|
// Don't yield dead objects, so use the escape hatch.
|
|
|
|
if (ex.message.includes("dead object"))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass a flag that is `true` when we're returning the result from a
|
|
|
|
// cached previous iteration.
|
2016-08-19 15:18:41 +00:00
|
|
|
listener.onIteratorRangeFound(range, !this.running);
|
2016-07-28 10:34:13 +00:00
|
|
|
yield range;
|
|
|
|
|
2016-08-19 15:18:41 +00:00
|
|
|
if (withPause && ++iterCount >= kIterationSizeMax) {
|
2016-07-28 10:34:13 +00:00
|
|
|
iterCount = 0;
|
|
|
|
// Make sure to save the current limit for later.
|
2016-08-19 15:18:41 +00:00
|
|
|
this._listeners.set(listener, { limit, onEnd });
|
2016-07-28 10:34:13 +00:00
|
|
|
// Sleep for the rest of this cycle.
|
|
|
|
yield new Promise(resolve => window.setTimeout(resolve, 0));
|
|
|
|
// After a sleep, the set of ranges may have updated.
|
|
|
|
ranges = rangeSource.slice(0, limit > -1 ? limit : undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit !== -1 && --limit === 0) {
|
|
|
|
// We've reached our limit; no need to do more work.
|
2016-08-19 15:18:41 +00:00
|
|
|
this._listeners.delete(listener);
|
2016-07-28 10:34:13 +00:00
|
|
|
onEnd();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the updated limit globally.
|
2016-08-19 15:18:41 +00:00
|
|
|
this._listeners.set(listener, { limit, onEnd });
|
2016-07-28 10:34:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal; iterate over the set of previously found ranges. Meanwhile it'll
|
|
|
|
* mark the listener as 'catching up', meaning it will not receive fresh
|
|
|
|
* results from a running iterator.
|
|
|
|
*
|
2016-08-19 15:18:41 +00:00
|
|
|
* @param {Object} listener Listener object
|
|
|
|
* @param {nsIDOMWindow} window The window object is only really used
|
|
|
|
* for access to `setTimeout`
|
2016-07-28 10:34:13 +00:00
|
|
|
* @yield {nsIDOMRange}
|
|
|
|
*/
|
2016-08-19 15:18:41 +00:00
|
|
|
_yieldPreviousResult: Task.async(function* (listener, window) {
|
|
|
|
this._notifyListeners("start", this.params, [listener]);
|
|
|
|
this._catchingUp.add(listener);
|
|
|
|
yield* this._yieldResult(listener, this._previousRanges, window);
|
|
|
|
this._catchingUp.delete(listener);
|
|
|
|
let { onEnd } = this._listeners.get(listener);
|
|
|
|
if (onEnd)
|
2016-07-28 10:34:13 +00:00
|
|
|
onEnd();
|
|
|
|
}),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal; iterate over the set of already found ranges. Meanwhile it'll
|
|
|
|
* mark the listener as 'catching up', meaning it will not receive fresh
|
|
|
|
* results from the running iterator.
|
|
|
|
*
|
2016-08-19 15:18:41 +00:00
|
|
|
* @param {Object} listener Listener object
|
|
|
|
* @param {nsIDOMWindow} window The window object is only really used
|
|
|
|
* for access to `setTimeout`
|
2016-07-28 10:34:13 +00:00
|
|
|
* @yield {nsIDOMRange}
|
|
|
|
*/
|
2016-08-19 15:18:41 +00:00
|
|
|
_yieldIntermediateResult: Task.async(function* (listener, window) {
|
|
|
|
this._notifyListeners("start", this.params, [listener]);
|
|
|
|
this._catchingUp.add(listener);
|
|
|
|
yield* this._yieldResult(listener, this.ranges, window, false);
|
|
|
|
this._catchingUp.delete(listener);
|
2016-07-28 10:34:13 +00:00
|
|
|
}),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal; see the documentation of the start() method above.
|
|
|
|
*
|
|
|
|
* @param {Finder} finder Currently active Finder instance
|
|
|
|
* @param {Number} spawnId Since `stop()` is synchronous and this method
|
|
|
|
* is not, this identifier is used to learn if
|
|
|
|
* it's supposed to still continue after a pause.
|
|
|
|
* @yield {nsIDOMRange}
|
|
|
|
*/
|
2016-10-11 11:08:00 +00:00
|
|
|
_findAllRanges: Task.async(function* (finder, spawnId) {
|
2016-08-25 18:11:44 +00:00
|
|
|
if (this._timeout) {
|
|
|
|
if (this._timer)
|
|
|
|
clearTimeout(this._timer);
|
2016-10-12 13:46:44 +00:00
|
|
|
if (this._runningFindResolver)
|
|
|
|
this._runningFindResolver();
|
|
|
|
|
|
|
|
let timeout = this._timeout;
|
|
|
|
let searchTerm = this._currentParams.word;
|
|
|
|
// Wait a little longer when the first or second character is typed into
|
|
|
|
// the findbar.
|
|
|
|
if (searchTerm.length == 1)
|
|
|
|
timeout *= 4;
|
|
|
|
else if (searchTerm.length == 2)
|
|
|
|
timeout *= 2;
|
|
|
|
yield new Promise(resolve => {
|
|
|
|
this._runningFindResolver = resolve;
|
|
|
|
this._timer = setTimeout(resolve, timeout);
|
|
|
|
});
|
|
|
|
this._timer = this._runningFindResolver = null;
|
2016-08-25 18:11:44 +00:00
|
|
|
// During the timeout, we could have gotten the signal to stop iterating.
|
|
|
|
// Make sure we do here.
|
|
|
|
if (!this.running || spawnId !== this._spawnId)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:18:41 +00:00
|
|
|
this._notifyListeners("start", this.params);
|
|
|
|
|
2016-11-09 22:06:32 +00:00
|
|
|
let { linksOnly, window } = this._currentParams;
|
2016-07-28 10:34:13 +00:00
|
|
|
// First we collect all frames we need to search through, whilst making sure
|
|
|
|
// that the parent window gets dibs.
|
|
|
|
let frames = [window].concat(this._collectFrames(window, finder));
|
|
|
|
let iterCount = 0;
|
|
|
|
for (let frame of frames) {
|
2016-08-16 09:25:56 +00:00
|
|
|
for (let range of this._iterateDocument(this._currentParams, frame)) {
|
2016-07-28 10:34:13 +00:00
|
|
|
// Between iterations, for example after a sleep of one cycle, we could
|
|
|
|
// have gotten the signal to stop iterating. Make sure we do here.
|
|
|
|
if (!this.running || spawnId !== this._spawnId)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Deal with links-only mode here.
|
2016-11-02 12:38:59 +00:00
|
|
|
if (linksOnly && !this._rangeStartsInLink(range))
|
2016-07-28 10:34:13 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
this.ranges.push(range);
|
|
|
|
|
|
|
|
// Call each listener with the range we just found.
|
2016-08-19 15:18:41 +00:00
|
|
|
for (let [listener, { limit, onEnd }] of this._listeners) {
|
|
|
|
if (this._catchingUp.has(listener))
|
2016-07-28 10:34:13 +00:00
|
|
|
continue;
|
|
|
|
|
2016-08-19 15:18:41 +00:00
|
|
|
listener.onIteratorRangeFound(range);
|
2016-07-28 10:34:13 +00:00
|
|
|
|
|
|
|
if (limit !== -1 && --limit === 0) {
|
|
|
|
// We've reached our limit; no need to do more work for this listener.
|
2016-08-19 15:18:41 +00:00
|
|
|
this._listeners.delete(listener);
|
2016-07-28 10:34:13 +00:00
|
|
|
onEnd();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the updated limit globally.
|
2016-08-19 15:18:41 +00:00
|
|
|
this._listeners.set(listener, { limit, onEnd });
|
2016-07-28 10:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
yield range;
|
|
|
|
|
|
|
|
if (++iterCount >= kIterationSizeMax) {
|
|
|
|
iterCount = 0;
|
|
|
|
// Sleep for the rest of this cycle.
|
|
|
|
yield new Promise(resolve => window.setTimeout(resolve, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the iterating has finished, make sure we reset and save the state
|
|
|
|
// properly.
|
|
|
|
this.stop(true);
|
|
|
|
}),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal; basic wrapper around nsIFind that provides a generator yielding
|
|
|
|
* a range each time an occurence of `word` string is found.
|
|
|
|
*
|
2016-08-16 09:25:56 +00:00
|
|
|
* @param {Boolean} options.caseSensitive Whether to search in case
|
|
|
|
* sensitive mode
|
|
|
|
* @param {Boolean} options.entireWord Whether to search in entire-word
|
|
|
|
* mode
|
|
|
|
* @param {String} options.word The word to search for
|
|
|
|
* @param {nsIDOMWindow} window The window to search in
|
2016-07-28 10:34:13 +00:00
|
|
|
* @yield {nsIDOMRange}
|
|
|
|
*/
|
2016-12-29 23:34:54 +00:00
|
|
|
*_iterateDocument({ caseSensitive, entireWord, word }, window) {
|
2016-07-28 10:34:13 +00:00
|
|
|
let doc = window.document;
|
|
|
|
let body = (doc instanceof Ci.nsIDOMHTMLDocument && doc.body) ?
|
|
|
|
doc.body : doc.documentElement;
|
|
|
|
|
|
|
|
if (!body)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let searchRange = doc.createRange();
|
|
|
|
searchRange.selectNodeContents(body);
|
|
|
|
|
|
|
|
let startPt = searchRange.cloneRange();
|
|
|
|
startPt.collapse(true);
|
|
|
|
|
|
|
|
let endPt = searchRange.cloneRange();
|
|
|
|
endPt.collapse(false);
|
|
|
|
|
|
|
|
let retRange = null;
|
|
|
|
|
|
|
|
let nsIFind = Cc["@mozilla.org/embedcomp/rangefind;1"]
|
|
|
|
.createInstance()
|
|
|
|
.QueryInterface(Ci.nsIFind);
|
2016-08-16 09:25:56 +00:00
|
|
|
nsIFind.caseSensitive = caseSensitive;
|
|
|
|
nsIFind.entireWord = entireWord;
|
2016-07-28 10:34:13 +00:00
|
|
|
|
|
|
|
while ((retRange = nsIFind.Find(word, searchRange, startPt, endPt))) {
|
|
|
|
yield retRange;
|
|
|
|
startPt = retRange.cloneRange();
|
|
|
|
startPt.collapse(false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal; helper method for the iterator that recursively collects all
|
|
|
|
* visible (i)frames inside a window.
|
|
|
|
*
|
|
|
|
* @param {nsIDOMWindow} window The window to extract the (i)frames from
|
|
|
|
* @param {Finder} finder The Finder instance
|
|
|
|
* @return {Array} Stack of frames to iterate over
|
|
|
|
*/
|
|
|
|
_collectFrames(window, finder) {
|
|
|
|
let frames = [];
|
2016-08-29 14:22:29 +00:00
|
|
|
if (!("frames" in window) || !window.frames.length)
|
2016-07-28 10:34:13 +00:00
|
|
|
return frames;
|
|
|
|
|
|
|
|
// Casting `window.frames` to an Iterator doesn't work, so we're stuck with
|
|
|
|
// a plain, old for-loop.
|
|
|
|
for (let i = 0, l = window.frames.length; i < l; ++i) {
|
|
|
|
let frame = window.frames[i];
|
2017-04-29 03:12:06 +00:00
|
|
|
// Don't count matches in hidden frames.
|
2016-07-28 10:34:13 +00:00
|
|
|
let frameEl = frame && frame.frameElement;
|
2017-04-29 03:12:06 +00:00
|
|
|
if (!frameEl)
|
|
|
|
continue;
|
|
|
|
// Construct a range around the frame element to check its visiblity.
|
|
|
|
let range = window.document.createRange();
|
|
|
|
range.setStart(frameEl, 0);
|
|
|
|
range.setEnd(frameEl, 0);
|
|
|
|
if (!finder._fastFind.isRangeVisible(range, this._getDocShell(range), true))
|
2016-07-28 10:34:13 +00:00
|
|
|
continue;
|
|
|
|
// All conditions pass, so push the current frame and its children on the
|
|
|
|
// stack.
|
|
|
|
frames.push(frame, ...this._collectFrames(frame, finder));
|
|
|
|
}
|
|
|
|
|
|
|
|
return frames;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal; helper method to extract the docShell reference from a Window or
|
|
|
|
* Range object.
|
|
|
|
*
|
|
|
|
* @param {nsIDOMRange} windowOrRange Window object to query. May also be a
|
|
|
|
* Range, from which the owner window will
|
|
|
|
* be queried.
|
|
|
|
* @return {nsIDocShell}
|
|
|
|
*/
|
|
|
|
_getDocShell(windowOrRange) {
|
|
|
|
let window = windowOrRange;
|
|
|
|
// Ranges may also be passed in, so fetch its window.
|
|
|
|
if (windowOrRange instanceof Ci.nsIDOMRange)
|
2017-01-27 09:51:03 +00:00
|
|
|
window = windowOrRange.startContainer.ownerGlobal;
|
2016-07-28 10:34:13 +00:00
|
|
|
return window.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShell);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal; determines whether a range is inside a link.
|
|
|
|
*
|
|
|
|
* @param {nsIDOMRange} range the range to check
|
|
|
|
* @return {Boolean} True if the range starts in a link
|
|
|
|
*/
|
|
|
|
_rangeStartsInLink(range) {
|
|
|
|
let isInsideLink = false;
|
|
|
|
let node = range.startContainer;
|
|
|
|
|
|
|
|
if (node.nodeType == node.ELEMENT_NODE) {
|
|
|
|
if (node.hasChildNodes) {
|
|
|
|
let childNode = node.item(range.startOffset);
|
|
|
|
if (childNode)
|
|
|
|
node = childNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const XLink_NS = "http://www.w3.org/1999/xlink";
|
|
|
|
const HTMLAnchorElement = (node.ownerDocument || node).defaultView.HTMLAnchorElement;
|
|
|
|
do {
|
|
|
|
if (node instanceof HTMLAnchorElement) {
|
|
|
|
isInsideLink = node.hasAttribute("href");
|
|
|
|
break;
|
|
|
|
} else if (typeof node.hasAttributeNS == "function" &&
|
|
|
|
node.hasAttributeNS(XLink_NS, "href")) {
|
|
|
|
isInsideLink = (node.getAttributeNS(XLink_NS, "type") == "simple");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = node.parentNode;
|
|
|
|
} while (node);
|
|
|
|
|
|
|
|
return isInsideLink;
|
|
|
|
}
|
|
|
|
};
|