2015-01-16 17:11:00 +03:00
|
|
|
// vim: set ts=2 sw=2 sts=2 tw=80:
|
2013-09-13 18:41:23 +04: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/.
|
|
|
|
|
2015-01-16 17:11:00 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ["Finder","GetClipboardSearchString"];
|
2013-09-13 18:41:23 +04:00
|
|
|
|
2016-03-23 04:18:30 +03:00
|
|
|
const { interfaces: Ci, classes: Cc, utils: Cu } = Components;
|
2013-09-13 18:41:23 +04:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2014-01-09 19:58:26 +04:00
|
|
|
Cu.import("resource://gre/modules/Geometry.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2014-09-18 12:23:00 +04:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
2013-09-13 18:41:23 +04:00
|
|
|
|
2014-02-28 19:07:32 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "TextToSubURIService",
|
|
|
|
"@mozilla.org/intl/texttosuburi;1",
|
|
|
|
"nsITextToSubURI");
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "Clipboard",
|
|
|
|
"@mozilla.org/widget/clipboard;1",
|
|
|
|
"nsIClipboard");
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ClipboardHelper",
|
|
|
|
"@mozilla.org/widget/clipboardhelper;1",
|
|
|
|
"nsIClipboardHelper");
|
|
|
|
|
2015-01-16 17:11:00 +03:00
|
|
|
const kSelectionMaxLen = 150;
|
2014-09-18 12:23:00 +04:00
|
|
|
|
2013-09-13 18:41:23 +04:00
|
|
|
function Finder(docShell) {
|
|
|
|
this._fastFind = Cc["@mozilla.org/typeaheadfind;1"].createInstance(Ci.nsITypeAheadFind);
|
|
|
|
this._fastFind.init(docShell);
|
|
|
|
|
|
|
|
this._docShell = docShell;
|
|
|
|
this._listeners = [];
|
|
|
|
this._previousLink = null;
|
|
|
|
this._searchString = null;
|
2016-06-09 14:30:48 +03:00
|
|
|
this._highlighter = null;
|
2013-09-26 19:21:02 +04:00
|
|
|
|
|
|
|
docShell.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebProgress)
|
|
|
|
.addProgressListener(this, Ci.nsIWebProgress.NOTIFY_LOCATION);
|
2013-09-13 18:41:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Finder.prototype = {
|
2016-07-28 13:34:13 +03:00
|
|
|
get iterator() {
|
|
|
|
if (this._iterator)
|
|
|
|
return this._iterator;
|
|
|
|
this._iterator = Cu.import("resource://gre/modules/FinderIterator.jsm", null).FinderIterator;
|
|
|
|
return this._iterator;
|
|
|
|
},
|
|
|
|
|
2016-06-09 14:30:49 +03:00
|
|
|
destroy: function() {
|
2016-07-28 13:34:13 +03:00
|
|
|
if (this._iterator)
|
|
|
|
this._iterator.reset();
|
2016-06-09 14:30:49 +03:00
|
|
|
if (this._highlighter) {
|
|
|
|
this._highlighter.clear();
|
|
|
|
this._highlighter.hide();
|
|
|
|
}
|
|
|
|
this.listeners = [];
|
|
|
|
this._docShell.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebProgress)
|
|
|
|
.removeProgressListener(this, Ci.nsIWebProgress.NOTIFY_LOCATION);
|
|
|
|
this._listeners = [];
|
|
|
|
this._fastFind = this._docShell = this._previousLink = this._highlighter = null;
|
|
|
|
},
|
|
|
|
|
2013-09-13 18:41:23 +04:00
|
|
|
addResultListener: function (aListener) {
|
|
|
|
if (this._listeners.indexOf(aListener) === -1)
|
|
|
|
this._listeners.push(aListener);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeResultListener: function (aListener) {
|
|
|
|
this._listeners = this._listeners.filter(l => l != aListener);
|
|
|
|
},
|
|
|
|
|
2016-06-09 14:30:49 +03:00
|
|
|
_notify: function (options) {
|
|
|
|
if (typeof options.storeResult != "boolean")
|
|
|
|
options.storeResult = true;
|
|
|
|
|
|
|
|
if (options.storeResult) {
|
|
|
|
this._searchString = options.searchString;
|
|
|
|
this.clipboardSearchString = options.searchString
|
2014-02-28 19:07:32 +04:00
|
|
|
}
|
2016-06-09 14:30:49 +03:00
|
|
|
this._outlineLink(options.drawOutline);
|
2013-09-13 18:41:23 +04:00
|
|
|
|
|
|
|
let foundLink = this._fastFind.foundLink;
|
|
|
|
let linkURL = null;
|
2013-09-26 19:21:02 +04:00
|
|
|
if (foundLink) {
|
2013-09-13 18:41:23 +04:00
|
|
|
let docCharset = null;
|
|
|
|
let ownerDoc = foundLink.ownerDocument;
|
|
|
|
if (ownerDoc)
|
|
|
|
docCharset = ownerDoc.characterSet;
|
|
|
|
|
2014-02-28 19:07:32 +04:00
|
|
|
linkURL = TextToSubURIService.unEscapeURIForUI(docCharset, foundLink.href);
|
2013-09-13 18:41:23 +04:00
|
|
|
}
|
|
|
|
|
2016-06-09 14:30:49 +03:00
|
|
|
options.linkURL = linkURL;
|
|
|
|
options.rect = this._getResultRect();
|
|
|
|
options.searchString = this._searchString;
|
|
|
|
|
2016-07-28 13:34:13 +03:00
|
|
|
if (!this.iterator.continueRunning({
|
|
|
|
linksOnly: options.linksOnly,
|
|
|
|
word: options.searchString
|
|
|
|
})) {
|
|
|
|
this.iterator.stop();
|
|
|
|
}
|
2016-06-09 14:30:49 +03:00
|
|
|
this.highlighter.update(options);
|
2014-01-09 19:58:26 +04:00
|
|
|
|
2013-09-13 18:41:23 +04:00
|
|
|
for (let l of this._listeners) {
|
2014-05-01 15:01:14 +04:00
|
|
|
try {
|
2016-06-09 14:30:49 +03:00
|
|
|
l.onFindResult(options);
|
2014-05-01 15:01:14 +04:00
|
|
|
} catch (ex) {}
|
2013-09-13 18:41:23 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get searchString() {
|
2014-02-28 19:07:32 +04:00
|
|
|
if (!this._searchString && this._fastFind.searchString)
|
|
|
|
this._searchString = this._fastFind.searchString;
|
2013-11-08 21:35:28 +04:00
|
|
|
return this._searchString;
|
2013-09-13 18:41:23 +04:00
|
|
|
},
|
|
|
|
|
2014-02-28 19:07:32 +04:00
|
|
|
get clipboardSearchString() {
|
2015-01-16 17:11:00 +03:00
|
|
|
return GetClipboardSearchString(this._getWindow()
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsILoadContext));
|
2014-02-28 19:07:32 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
set clipboardSearchString(aSearchString) {
|
|
|
|
if (!aSearchString || !Clipboard.supportsFindClipboard())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ClipboardHelper.copyStringToClipboard(aSearchString,
|
2015-05-21 23:49:30 +03:00
|
|
|
Ci.nsIClipboard.kFindClipboard);
|
2014-02-28 19:07:32 +04:00
|
|
|
},
|
|
|
|
|
2013-09-13 18:41:23 +04:00
|
|
|
set caseSensitive(aSensitive) {
|
|
|
|
this._fastFind.caseSensitive = aSensitive;
|
|
|
|
},
|
|
|
|
|
2016-06-28 16:13:53 +03:00
|
|
|
set entireWord(aEntireWord) {
|
|
|
|
this._fastFind.entireWord = aEntireWord;
|
|
|
|
},
|
|
|
|
|
2016-06-09 14:30:48 +03:00
|
|
|
get highlighter() {
|
|
|
|
if (this._highlighter)
|
|
|
|
return this._highlighter;
|
|
|
|
|
|
|
|
const {FinderHighlighter} = Cu.import("resource://gre/modules/FinderHighlighter.jsm", {});
|
|
|
|
return this._highlighter = new FinderHighlighter(this);
|
|
|
|
},
|
|
|
|
|
2014-09-18 12:23:00 +04:00
|
|
|
_lastFindResult: null,
|
|
|
|
|
2013-09-26 19:21:02 +04:00
|
|
|
/**
|
|
|
|
* Used for normal search operations, highlights the first match.
|
|
|
|
*
|
|
|
|
* @param aSearchString String to search for.
|
|
|
|
* @param aLinksOnly Only consider nodes that are links for the search.
|
|
|
|
* @param aDrawOutline Puts an outline around matched links.
|
|
|
|
*/
|
|
|
|
fastFind: function (aSearchString, aLinksOnly, aDrawOutline) {
|
2014-09-18 12:23:00 +04:00
|
|
|
this._lastFindResult = this._fastFind.find(aSearchString, aLinksOnly);
|
2013-11-08 21:35:28 +04:00
|
|
|
let searchString = this._fastFind.searchString;
|
2016-06-09 14:30:49 +03:00
|
|
|
this._notify({
|
|
|
|
searchString,
|
|
|
|
result: this._lastFindResult,
|
|
|
|
findBackwards: false,
|
|
|
|
findAgain: false,
|
2016-07-28 13:34:13 +03:00
|
|
|
drawOutline: aDrawOutline,
|
|
|
|
linksOnly: aLinksOnly
|
2016-06-09 14:30:49 +03:00
|
|
|
});
|
2013-09-13 18:41:23 +04:00
|
|
|
},
|
|
|
|
|
2013-09-26 19:21:02 +04:00
|
|
|
/**
|
|
|
|
* Repeat the previous search. Should only be called after a previous
|
|
|
|
* call to Finder.fastFind.
|
|
|
|
*
|
|
|
|
* @param aFindBackwards Controls the search direction:
|
|
|
|
* true: before current match, false: after current match.
|
|
|
|
* @param aLinksOnly Only consider nodes that are links for the search.
|
|
|
|
* @param aDrawOutline Puts an outline around matched links.
|
|
|
|
*/
|
|
|
|
findAgain: function (aFindBackwards, aLinksOnly, aDrawOutline) {
|
2014-09-18 12:23:00 +04:00
|
|
|
this._lastFindResult = this._fastFind.findAgain(aFindBackwards, aLinksOnly);
|
2013-11-08 21:35:28 +04:00
|
|
|
let searchString = this._fastFind.searchString;
|
2016-06-09 14:30:49 +03:00
|
|
|
this._notify({
|
|
|
|
searchString,
|
|
|
|
result: this._lastFindResult,
|
|
|
|
findBackwards: aFindBackwards,
|
2016-07-28 13:34:13 +03:00
|
|
|
findAgain: true,
|
|
|
|
drawOutline: aDrawOutline,
|
|
|
|
linksOnly: aLinksOnly
|
2016-06-09 14:30:49 +03:00
|
|
|
});
|
2013-09-13 18:41:23 +04:00
|
|
|
},
|
|
|
|
|
2014-02-28 19:07:32 +04:00
|
|
|
/**
|
|
|
|
* Forcibly set the search string of the find clipboard to the currently
|
|
|
|
* selected text in the window, on supported platforms (i.e. OSX).
|
|
|
|
*/
|
|
|
|
setSearchStringToSelection: function() {
|
2015-01-16 17:11:00 +03:00
|
|
|
let searchString = this.getActiveSelectionText();
|
|
|
|
|
2014-02-28 19:07:32 +04:00
|
|
|
// Empty strings are rather useless to search for.
|
|
|
|
if (!searchString.length)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
this.clipboardSearchString = searchString;
|
|
|
|
return searchString;
|
|
|
|
},
|
|
|
|
|
2016-07-28 13:34:13 +03:00
|
|
|
highlight: Task.async(function* (aHighlight, aWord, aLinksOnly) {
|
|
|
|
let found = yield this.highlighter.highlight(aHighlight, aWord, null, aLinksOnly);
|
2016-06-09 14:30:48 +03:00
|
|
|
this.highlighter.notifyFinished(aHighlight);
|
2013-11-08 21:35:28 +04:00
|
|
|
if (aHighlight) {
|
|
|
|
let result = found ? Ci.nsITypeAheadFind.FIND_FOUND
|
|
|
|
: Ci.nsITypeAheadFind.FIND_NOTFOUND;
|
2016-06-09 14:30:49 +03:00
|
|
|
this._notify({
|
|
|
|
searchString: aWord,
|
|
|
|
result,
|
|
|
|
findBackwards: false,
|
|
|
|
findAgain: false,
|
|
|
|
drawOutline: false,
|
|
|
|
storeResult: false
|
|
|
|
});
|
2013-11-08 21:35:28 +04:00
|
|
|
}
|
2014-09-18 12:23:00 +04:00
|
|
|
}),
|
2013-09-13 18:41:23 +04:00
|
|
|
|
2015-01-16 17:11:00 +03:00
|
|
|
getInitialSelection: function() {
|
|
|
|
this._getWindow().setTimeout(() => {
|
|
|
|
let initialSelection = this.getActiveSelectionText();
|
|
|
|
for (let l of this._listeners) {
|
|
|
|
try {
|
|
|
|
l.onCurrentSelection(initialSelection, true);
|
|
|
|
} catch (ex) {}
|
|
|
|
}
|
|
|
|
}, 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
getActiveSelectionText: function() {
|
|
|
|
let focusedWindow = {};
|
|
|
|
let focusedElement =
|
|
|
|
Services.focus.getFocusedElementForWindow(this._getWindow(), true,
|
|
|
|
focusedWindow);
|
|
|
|
focusedWindow = focusedWindow.value;
|
|
|
|
|
|
|
|
let selText;
|
|
|
|
|
|
|
|
if (focusedElement instanceof Ci.nsIDOMNSEditableElement &&
|
|
|
|
focusedElement.editor) {
|
|
|
|
// The user may have a selection in an input or textarea.
|
|
|
|
selText = focusedElement.editor.selectionController
|
|
|
|
.getSelection(Ci.nsISelectionController.SELECTION_NORMAL)
|
|
|
|
.toString();
|
|
|
|
} else {
|
|
|
|
// Look for any selected text on the actual page.
|
|
|
|
selText = focusedWindow.getSelection().toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!selText)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
// Process our text to get rid of unwanted characters.
|
2016-06-06 13:56:55 +03:00
|
|
|
selText = selText.trim().replace(/\s+/g, " ");
|
|
|
|
let truncLength = kSelectionMaxLen;
|
|
|
|
if (selText.length > truncLength) {
|
|
|
|
let truncChar = selText.charAt(truncLength).charCodeAt(0);
|
|
|
|
if (truncChar >= 0xDC00 && truncChar <= 0xDFFF)
|
|
|
|
truncLength++;
|
|
|
|
selText = selText.substr(0, truncLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
return selText;
|
2015-01-16 17:11:00 +03:00
|
|
|
},
|
|
|
|
|
2013-10-16 04:02:23 +04:00
|
|
|
enableSelection: function() {
|
|
|
|
this._fastFind.setSelectionModeAndRepaint(Ci.nsISelectionController.SELECTION_ON);
|
2014-01-24 16:08:38 +04:00
|
|
|
this._restoreOriginalOutline();
|
2013-10-16 04:02:23 +04:00
|
|
|
},
|
|
|
|
|
2013-09-13 18:41:23 +04:00
|
|
|
removeSelection: function() {
|
2014-01-24 16:08:38 +04:00
|
|
|
this._fastFind.collapseSelection();
|
2013-10-16 04:02:23 +04:00
|
|
|
this.enableSelection();
|
2016-06-09 14:30:49 +03:00
|
|
|
this.highlighter.clear();
|
2013-09-13 18:41:23 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
focusContent: function() {
|
2013-10-16 19:04:49 +04:00
|
|
|
// Allow Finder listeners to cancel focusing the content.
|
|
|
|
for (let l of this._listeners) {
|
2014-05-01 15:01:14 +04:00
|
|
|
try {
|
2014-09-04 23:17:47 +04:00
|
|
|
if ("shouldFocusContent" in l &&
|
|
|
|
!l.shouldFocusContent())
|
2014-05-01 15:01:14 +04:00
|
|
|
return;
|
2014-09-04 23:17:47 +04:00
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError(ex);
|
|
|
|
}
|
2013-10-16 19:04:49 +04:00
|
|
|
}
|
2013-09-13 18:41:23 +04:00
|
|
|
|
2013-10-16 19:04:49 +04:00
|
|
|
let fastFind = this._fastFind;
|
|
|
|
const fm = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager);
|
2013-09-13 18:41:23 +04:00
|
|
|
try {
|
2013-10-16 19:04:49 +04:00
|
|
|
// Try to find the best possible match that should receive focus and
|
|
|
|
// block scrolling on focus since find already scrolls. Further
|
|
|
|
// scrolling is due to user action, so don't override this.
|
2013-09-13 18:41:23 +04:00
|
|
|
if (fastFind.foundLink) {
|
2013-10-16 19:04:49 +04:00
|
|
|
fm.setFocus(fastFind.foundLink, fm.FLAG_NOSCROLL);
|
2013-09-13 18:41:23 +04:00
|
|
|
} else if (fastFind.foundEditable) {
|
2013-10-16 19:04:49 +04:00
|
|
|
fm.setFocus(fastFind.foundEditable, fm.FLAG_NOSCROLL);
|
2013-09-13 18:41:23 +04:00
|
|
|
fastFind.collapseSelection();
|
|
|
|
} else {
|
|
|
|
this._getWindow().focus()
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
},
|
|
|
|
|
2016-06-09 14:30:49 +03:00
|
|
|
onFindbarClose: function() {
|
|
|
|
this.enableSelection();
|
2016-07-05 10:59:25 +03:00
|
|
|
this.highlighter.highlight(false);
|
2016-06-09 14:30:49 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onModalHighlightChange(useModalHighlight) {
|
|
|
|
if (this._highlighter)
|
|
|
|
this._highlighter.onModalHighlightChange(useModalHighlight);
|
|
|
|
},
|
|
|
|
|
2016-07-27 17:37:26 +03:00
|
|
|
onHighlightAllChange(highlightAll) {
|
|
|
|
if (this._highlighter)
|
|
|
|
this._highlighter.onHighlightAllChange(highlightAll);
|
|
|
|
},
|
|
|
|
|
2013-09-13 18:41:23 +04:00
|
|
|
keyPress: function (aEvent) {
|
|
|
|
let controller = this._getSelectionController(this._getWindow());
|
|
|
|
|
|
|
|
switch (aEvent.keyCode) {
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_RETURN:
|
2013-10-16 19:45:42 +04:00
|
|
|
if (this._fastFind.foundLink) {
|
|
|
|
let view = this._fastFind.foundLink.ownerDocument.defaultView;
|
|
|
|
this._fastFind.foundLink.dispatchEvent(new view.MouseEvent("click", {
|
|
|
|
view: view,
|
|
|
|
cancelable: true,
|
|
|
|
bubbles: true,
|
|
|
|
ctrlKey: aEvent.ctrlKey,
|
|
|
|
altKey: aEvent.altKey,
|
|
|
|
shiftKey: aEvent.shiftKey,
|
|
|
|
metaKey: aEvent.metaKey
|
|
|
|
}));
|
|
|
|
}
|
2013-09-13 18:41:23 +04:00
|
|
|
break;
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_TAB:
|
2013-09-26 19:21:02 +04:00
|
|
|
let direction = Services.focus.MOVEFOCUS_FORWARD;
|
|
|
|
if (aEvent.shiftKey) {
|
|
|
|
direction = Services.focus.MOVEFOCUS_BACKWARD;
|
|
|
|
}
|
|
|
|
Services.focus.moveFocus(this._getWindow(), null, direction, 0);
|
2013-09-13 18:41:23 +04:00
|
|
|
break;
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_PAGE_UP:
|
|
|
|
controller.scrollPage(false);
|
|
|
|
break;
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_PAGE_DOWN:
|
|
|
|
controller.scrollPage(true);
|
|
|
|
break;
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_UP:
|
|
|
|
controller.scrollLine(false);
|
|
|
|
break;
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_DOWN:
|
|
|
|
controller.scrollLine(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-09-18 12:23:00 +04:00
|
|
|
_notifyMatchesCount: function(result) {
|
|
|
|
for (let l of this._listeners) {
|
|
|
|
try {
|
|
|
|
l.onMatchesCountResult(result);
|
|
|
|
} catch (ex) {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-05-01 15:01:14 +04:00
|
|
|
requestMatchesCount: function(aWord, aMatchLimit, aLinksOnly) {
|
2014-09-18 12:23:00 +04:00
|
|
|
if (this._lastFindResult == Ci.nsITypeAheadFind.FIND_NOTFOUND ||
|
2016-07-28 13:34:13 +03:00
|
|
|
this.searchString == "" || !aWord) {
|
|
|
|
this._notifyMatchesCount({
|
2014-09-18 12:23:00 +04:00
|
|
|
total: 0,
|
|
|
|
current: 0
|
|
|
|
});
|
2016-07-28 13:34:13 +03:00
|
|
|
return;
|
2014-09-18 12:23:00 +04:00
|
|
|
}
|
2014-05-01 15:01:14 +04:00
|
|
|
|
2016-07-28 13:34:13 +03:00
|
|
|
let window = this._getWindow();
|
|
|
|
let result = {
|
2014-05-01 15:01:14 +04:00
|
|
|
total: 0,
|
|
|
|
current: 0,
|
|
|
|
_currentFound: false
|
|
|
|
};
|
|
|
|
let foundRange = this._fastFind.getFoundRange();
|
|
|
|
|
2016-07-28 13:34:13 +03:00
|
|
|
this.iterator.start({
|
|
|
|
finder: this,
|
|
|
|
limit: aMatchLimit,
|
|
|
|
linksOnly: aLinksOnly,
|
|
|
|
onRange: range => {
|
|
|
|
++result.total;
|
|
|
|
if (!result._currentFound) {
|
|
|
|
++result.current;
|
|
|
|
result._currentFound = (foundRange &&
|
2014-09-18 12:23:00 +04:00
|
|
|
range.startContainer == foundRange.startContainer &&
|
|
|
|
range.startOffset == foundRange.startOffset &&
|
|
|
|
range.endContainer == foundRange.endContainer &&
|
|
|
|
range.endOffset == foundRange.endOffset);
|
2014-05-01 15:01:14 +04:00
|
|
|
}
|
2016-07-28 13:34:13 +03:00
|
|
|
},
|
|
|
|
useCache: true,
|
|
|
|
word: aWord
|
|
|
|
}).then(() => {
|
|
|
|
// The `_currentFound` property is only used for internal bookkeeping.
|
|
|
|
delete result._currentFound;
|
2014-05-01 15:01:14 +04:00
|
|
|
|
2016-07-28 13:34:13 +03:00
|
|
|
if (result.total == aMatchLimit)
|
|
|
|
result.total = -1;
|
2014-05-01 15:01:14 +04:00
|
|
|
|
2016-07-28 13:34:13 +03:00
|
|
|
this._notifyMatchesCount(result);
|
|
|
|
});
|
2014-05-01 15:01:14 +04:00
|
|
|
},
|
|
|
|
|
2013-09-13 18:41:23 +04:00
|
|
|
_getWindow: function () {
|
|
|
|
return this._docShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
|
|
|
|
},
|
|
|
|
|
2014-01-09 19:58:26 +04:00
|
|
|
/**
|
|
|
|
* Get the bounding selection rect in CSS px relative to the origin of the
|
|
|
|
* top-level content document.
|
|
|
|
*/
|
|
|
|
_getResultRect: function () {
|
|
|
|
let topWin = this._getWindow();
|
|
|
|
let win = this._fastFind.currentWindow;
|
|
|
|
if (!win)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
let selection = win.getSelection();
|
|
|
|
if (!selection.rangeCount || selection.isCollapsed) {
|
|
|
|
// The selection can be into an input or a textarea element.
|
|
|
|
let nodes = win.document.querySelectorAll("input, textarea");
|
|
|
|
for (let node of nodes) {
|
|
|
|
if (node instanceof Ci.nsIDOMNSEditableElement && node.editor) {
|
2016-03-23 04:18:30 +03:00
|
|
|
try {
|
|
|
|
let sc = node.editor.selectionController;
|
|
|
|
selection = sc.getSelection(Ci.nsISelectionController.SELECTION_NORMAL);
|
|
|
|
if (selection.rangeCount && !selection.isCollapsed) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// If this textarea is hidden, then its selection controller might
|
|
|
|
// not be intialized. Ignore the failure.
|
2014-01-09 19:58:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 00:37:34 +03:00
|
|
|
if (!selection.rangeCount || selection.isCollapsed) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-01-09 19:58:26 +04:00
|
|
|
let utils = topWin.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
|
|
|
|
let scrollX = {}, scrollY = {};
|
|
|
|
utils.getScrollXY(false, scrollX, scrollY);
|
|
|
|
|
|
|
|
for (let frame = win; frame != topWin; frame = frame.parent) {
|
|
|
|
let rect = frame.frameElement.getBoundingClientRect();
|
|
|
|
let left = frame.getComputedStyle(frame.frameElement, "").borderLeftWidth;
|
|
|
|
let top = frame.getComputedStyle(frame.frameElement, "").borderTopWidth;
|
|
|
|
scrollX.value += rect.left + parseInt(left, 10);
|
|
|
|
scrollY.value += rect.top + parseInt(top, 10);
|
|
|
|
}
|
|
|
|
let rect = Rect.fromRect(selection.getRangeAt(0).getBoundingClientRect());
|
|
|
|
return rect.translate(scrollX.value, scrollY.value);
|
|
|
|
},
|
|
|
|
|
2013-09-26 19:21:02 +04:00
|
|
|
_outlineLink: function (aDrawOutline) {
|
2013-09-13 18:41:23 +04:00
|
|
|
let foundLink = this._fastFind.foundLink;
|
|
|
|
|
2013-09-26 19:21:02 +04:00
|
|
|
// Optimization: We are drawing outlines and we matched
|
|
|
|
// the same link before, so don't duplicate work.
|
|
|
|
if (foundLink == this._previousLink && aDrawOutline)
|
2013-09-13 18:41:23 +04:00
|
|
|
return;
|
|
|
|
|
2013-09-26 19:21:02 +04:00
|
|
|
this._restoreOriginalOutline();
|
2013-09-13 18:41:23 +04:00
|
|
|
|
2013-09-26 19:21:02 +04:00
|
|
|
if (foundLink && aDrawOutline) {
|
2013-09-13 18:41:23 +04:00
|
|
|
// Backup original outline
|
|
|
|
this._tmpOutline = foundLink.style.outline;
|
|
|
|
this._tmpOutlineOffset = foundLink.style.outlineOffset;
|
|
|
|
|
|
|
|
// Draw pseudo focus rect
|
|
|
|
// XXX Should we change the following style for FAYT pseudo focus?
|
|
|
|
// XXX Shouldn't we change default design if outline is visible
|
|
|
|
// already?
|
|
|
|
// Don't set the outline-color, we should always use initial value.
|
|
|
|
foundLink.style.outline = "1px dotted";
|
|
|
|
foundLink.style.outlineOffset = "0";
|
2013-09-26 19:21:02 +04:00
|
|
|
|
|
|
|
this._previousLink = foundLink;
|
2013-09-13 18:41:23 +04:00
|
|
|
}
|
2013-09-26 19:21:02 +04:00
|
|
|
},
|
2013-09-13 18:41:23 +04:00
|
|
|
|
2013-09-26 19:21:02 +04:00
|
|
|
_restoreOriginalOutline: function () {
|
|
|
|
// Removes the outline around the last found link.
|
|
|
|
if (this._previousLink) {
|
|
|
|
this._previousLink.style.outline = this._tmpOutline;
|
|
|
|
this._previousLink.style.outlineOffset = this._tmpOutlineOffset;
|
|
|
|
this._previousLink = null;
|
|
|
|
}
|
2013-09-13 18:41:23 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_getSelectionController: function(aWindow) {
|
|
|
|
// display: none iframes don't have a selection controller, see bug 493658
|
2015-01-16 17:11:00 +03:00
|
|
|
try {
|
|
|
|
if (!aWindow.innerWidth || !aWindow.innerHeight)
|
|
|
|
return null;
|
|
|
|
} catch (e) {
|
|
|
|
// If getting innerWidth or innerHeight throws, we can't get a selection
|
|
|
|
// controller.
|
2013-09-13 18:41:23 +04:00
|
|
|
return null;
|
2015-01-16 17:11:00 +03:00
|
|
|
}
|
2013-09-13 18:41:23 +04:00
|
|
|
|
|
|
|
// Yuck. See bug 138068.
|
|
|
|
let docShell = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShell);
|
|
|
|
|
|
|
|
let controller = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsISelectionDisplay)
|
|
|
|
.QueryInterface(Ci.nsISelectionController);
|
|
|
|
return controller;
|
|
|
|
},
|
|
|
|
|
2013-09-26 19:21:02 +04:00
|
|
|
// Start of nsIWebProgressListener implementation.
|
|
|
|
|
|
|
|
onLocationChange: function(aWebProgress, aRequest, aLocation, aFlags) {
|
|
|
|
if (!aWebProgress.isTopLevel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Avoid leaking if we change the page.
|
|
|
|
this._previousLink = null;
|
2016-06-09 14:30:49 +03:00
|
|
|
this.highlighter.onLocationChange();
|
2016-07-28 13:34:13 +03:00
|
|
|
this.iterator.reset();
|
2016-06-09 00:53:57 +03:00
|
|
|
},
|
|
|
|
|
2013-09-26 19:21:02 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener,
|
|
|
|
Ci.nsISupportsWeakReference])
|
2013-09-13 18:41:23 +04:00
|
|
|
};
|
2015-01-16 17:11:00 +03:00
|
|
|
|
|
|
|
function GetClipboardSearchString(aLoadContext) {
|
|
|
|
let searchString = "";
|
|
|
|
if (!Clipboard.supportsFindClipboard())
|
|
|
|
return searchString;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let trans = Cc["@mozilla.org/widget/transferable;1"]
|
|
|
|
.createInstance(Ci.nsITransferable);
|
|
|
|
trans.init(aLoadContext);
|
|
|
|
trans.addDataFlavor("text/unicode");
|
|
|
|
|
|
|
|
Clipboard.getData(trans, Ci.nsIClipboard.kFindClipboard);
|
|
|
|
|
|
|
|
let data = {};
|
|
|
|
let dataLen = {};
|
|
|
|
trans.getTransferData("text/unicode", data, dataLen);
|
|
|
|
if (data.value) {
|
|
|
|
data = data.value.QueryInterface(Ci.nsISupportsString);
|
|
|
|
searchString = data.toString();
|
|
|
|
}
|
|
|
|
} catch (ex) {}
|
|
|
|
|
|
|
|
return searchString;
|
|
|
|
}
|
2015-06-10 03:04:00 +03:00
|
|
|
|
|
|
|
this.Finder = Finder;
|
|
|
|
this.GetClipboardSearchString = GetClipboardSearchString;
|