2014-06-24 22:12:07 -07:00
|
|
|
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
|
2013-03-21 16:50:42 -07: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";
|
|
|
|
|
|
|
|
var SelectionHandler = {
|
|
|
|
HANDLE_TYPE_START: "START",
|
|
|
|
HANDLE_TYPE_MIDDLE: "MIDDLE",
|
|
|
|
HANDLE_TYPE_END: "END",
|
|
|
|
|
|
|
|
TYPE_NONE: 0,
|
|
|
|
TYPE_CURSOR: 1,
|
|
|
|
TYPE_SELECTION: 2,
|
|
|
|
|
2013-12-17 18:18:35 -08:00
|
|
|
SELECT_ALL: 0,
|
|
|
|
SELECT_AT_POINT: 1,
|
|
|
|
|
2013-03-21 16:50:42 -07:00
|
|
|
// Keeps track of data about the dimensions of the selection. Coordinates
|
2013-03-28 13:21:58 -07:00
|
|
|
// stored here are relative to the _contentWindow window.
|
|
|
|
_cache: null,
|
2013-03-21 16:50:42 -07:00
|
|
|
_activeType: 0, // TYPE_NONE
|
2014-03-26 18:34:46 -07:00
|
|
|
_draggingHandles: false, // True while user drags text selection handles
|
2013-11-18 20:48:49 -08:00
|
|
|
_ignoreCompositionChanges: false, // Persist caret during IME composition updates
|
2014-04-26 09:46:41 -07:00
|
|
|
_prevHandlePositions: [], // Avoid issuing duplicate "TextSelection:Position" messages
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2014-04-25 21:49:50 -07:00
|
|
|
// TargetElement changes (text <--> no text) trigger actionbar UI update
|
|
|
|
_prevTargetElementHasText: null,
|
2013-03-21 16:50:42 -07:00
|
|
|
|
|
|
|
// The window that holds the selection (can be a sub-frame)
|
2013-03-28 13:21:58 -07:00
|
|
|
get _contentWindow() {
|
|
|
|
if (this._contentWindowRef)
|
|
|
|
return this._contentWindowRef.get();
|
2013-03-21 16:50:42 -07:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2013-03-28 13:21:58 -07:00
|
|
|
set _contentWindow(aContentWindow) {
|
|
|
|
this._contentWindowRef = Cu.getWeakReference(aContentWindow);
|
2013-03-21 16:50:42 -07:00
|
|
|
},
|
|
|
|
|
2013-03-28 13:21:58 -07:00
|
|
|
get _targetElement() {
|
|
|
|
if (this._targetElementRef)
|
|
|
|
return this._targetElementRef.get();
|
2013-03-21 16:50:42 -07:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2013-03-28 13:21:58 -07:00
|
|
|
set _targetElement(aTargetElement) {
|
|
|
|
this._targetElementRef = Cu.getWeakReference(aTargetElement);
|
2013-03-21 16:50:42 -07:00
|
|
|
},
|
|
|
|
|
2013-03-28 13:21:58 -07:00
|
|
|
get _domWinUtils() {
|
2013-03-21 16:50:42 -07:00
|
|
|
return BrowserApp.selectedBrowser.contentWindow.QueryInterface(Ci.nsIInterfaceRequestor).
|
|
|
|
getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
},
|
|
|
|
|
|
|
|
_isRTL: false,
|
|
|
|
|
|
|
|
_addObservers: function sh_addObservers() {
|
|
|
|
Services.obs.addObserver(this, "Gesture:SingleTap", false);
|
|
|
|
Services.obs.addObserver(this, "Tab:Selected", false);
|
|
|
|
Services.obs.addObserver(this, "after-viewport-change", false);
|
|
|
|
Services.obs.addObserver(this, "TextSelection:Move", false);
|
|
|
|
Services.obs.addObserver(this, "TextSelection:Position", false);
|
2013-11-19 11:57:37 -08:00
|
|
|
Services.obs.addObserver(this, "TextSelection:End", false);
|
|
|
|
Services.obs.addObserver(this, "TextSelection:Action", false);
|
2014-04-26 09:46:41 -07:00
|
|
|
Services.obs.addObserver(this, "TextSelection:LayerReflow", false);
|
2014-02-19 19:19:43 -08:00
|
|
|
|
|
|
|
BrowserApp.deck.addEventListener("pagehide", this, false);
|
|
|
|
BrowserApp.deck.addEventListener("blur", this, true);
|
2014-03-31 21:19:16 -07:00
|
|
|
BrowserApp.deck.addEventListener("scroll", this, true);
|
2013-03-21 16:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_removeObservers: function sh_removeObservers() {
|
|
|
|
Services.obs.removeObserver(this, "Gesture:SingleTap");
|
|
|
|
Services.obs.removeObserver(this, "Tab:Selected");
|
|
|
|
Services.obs.removeObserver(this, "after-viewport-change");
|
|
|
|
Services.obs.removeObserver(this, "TextSelection:Move");
|
|
|
|
Services.obs.removeObserver(this, "TextSelection:Position");
|
2013-11-19 11:57:37 -08:00
|
|
|
Services.obs.removeObserver(this, "TextSelection:End");
|
|
|
|
Services.obs.removeObserver(this, "TextSelection:Action");
|
2014-04-26 09:46:41 -07:00
|
|
|
Services.obs.removeObserver(this, "TextSelection:LayerReflow");
|
2014-02-19 19:19:43 -08:00
|
|
|
|
2014-04-07 12:37:15 -07:00
|
|
|
BrowserApp.deck.removeEventListener("pagehide", this, false);
|
|
|
|
BrowserApp.deck.removeEventListener("blur", this, true);
|
|
|
|
BrowserApp.deck.removeEventListener("scroll", this, true);
|
2013-03-21 16:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function sh_observe(aSubject, aTopic, aData) {
|
|
|
|
switch (aTopic) {
|
2014-04-26 09:46:41 -07:00
|
|
|
// Update handle/caret position on page reflow (keyboard open/close,
|
|
|
|
// dynamic DOM changes, orientation updates, etc).
|
|
|
|
case "TextSelection:LayerReflow": {
|
|
|
|
if (this._activeType == this.TYPE_SELECTION) {
|
|
|
|
this._updateCacheForSelection();
|
|
|
|
}
|
|
|
|
if (this._activeType != this.TYPE_NONE) {
|
|
|
|
this._positionHandlesOnChange();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-19 19:19:43 -08:00
|
|
|
// Update caret position on keyboard activity
|
|
|
|
case "TextSelection:UpdateCaretPos":
|
|
|
|
// Generated by IME close, autoCorrection / styling
|
|
|
|
this._positionHandles();
|
|
|
|
break;
|
|
|
|
|
2013-03-21 16:50:42 -07:00
|
|
|
case "Gesture:SingleTap": {
|
2014-02-19 13:01:51 -08:00
|
|
|
if (this._activeType == this.TYPE_SELECTION) {
|
|
|
|
let data = JSON.parse(aData);
|
|
|
|
if (!this._pointInSelection(data.x, data.y))
|
|
|
|
this._closeSelection();
|
|
|
|
} else if (this._activeType == this.TYPE_CURSOR) {
|
2013-08-20 11:50:57 -07:00
|
|
|
// attachCaret() is called in the "Gesture:SingleTap" handler in BrowserEventHandler
|
|
|
|
// We're guaranteed to call this first, because this observer was added last
|
2013-08-28 17:44:03 -07:00
|
|
|
this._deactivate();
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "Tab:Selected":
|
2013-11-19 11:57:37 -08:00
|
|
|
case "TextSelection:End":
|
2013-03-25 15:25:19 -07:00
|
|
|
this._closeSelection();
|
|
|
|
break;
|
2013-11-19 11:57:37 -08:00
|
|
|
case "TextSelection:Action":
|
|
|
|
for (let type in this.actions) {
|
|
|
|
if (this.actions[type].id == aData) {
|
|
|
|
this.actions[type].action(this._targetElement);
|
|
|
|
break;
|
|
|
|
}
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "after-viewport-change": {
|
|
|
|
if (this._activeType == this.TYPE_SELECTION) {
|
|
|
|
// Update the cache after the viewport changes (e.g. panning, zooming).
|
2013-03-28 13:22:00 -07:00
|
|
|
this._updateCacheForSelection();
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "TextSelection:Move": {
|
|
|
|
let data = JSON.parse(aData);
|
2013-08-28 12:45:42 -07:00
|
|
|
if (this._activeType == this.TYPE_SELECTION) {
|
2014-03-26 18:34:46 -07:00
|
|
|
this._startDraggingHandles();
|
2013-03-28 13:22:00 -07:00
|
|
|
this._moveSelection(data.handleType == this.HANDLE_TYPE_START, data.x, data.y);
|
2014-03-26 18:34:46 -07:00
|
|
|
|
2013-08-28 12:45:42 -07:00
|
|
|
} else if (this._activeType == this.TYPE_CURSOR) {
|
2014-04-26 09:46:41 -07:00
|
|
|
this._startDraggingHandles();
|
|
|
|
|
2013-11-18 20:48:49 -08:00
|
|
|
// Ignore IMM composition notifications when caret movement starts
|
|
|
|
this._ignoreCompositionChanges = true;
|
2014-04-28 17:21:19 -07:00
|
|
|
this._moveCaret(data.x, data.y);
|
2013-03-21 16:50:42 -07:00
|
|
|
|
|
|
|
// Move the handle directly under the caret
|
2013-03-28 13:22:00 -07:00
|
|
|
this._positionHandles();
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "TextSelection:Position": {
|
|
|
|
if (this._activeType == this.TYPE_SELECTION) {
|
2014-03-26 18:34:46 -07:00
|
|
|
this._startDraggingHandles();
|
|
|
|
|
2013-04-22 17:16:06 -07:00
|
|
|
// Check to see if the handles should be reversed.
|
|
|
|
let isStartHandle = JSON.parse(aData).handleType == this.HANDLE_TYPE_START;
|
2014-01-10 22:51:27 -08:00
|
|
|
try {
|
|
|
|
let selectionReversed = this._updateCacheForSelection(isStartHandle);
|
|
|
|
if (selectionReversed) {
|
|
|
|
// Reverse the anchor and focus to correspond to the new start and end handles.
|
|
|
|
let selection = this._getSelection();
|
|
|
|
let anchorNode = selection.anchorNode;
|
|
|
|
let anchorOffset = selection.anchorOffset;
|
|
|
|
selection.collapse(selection.focusNode, selection.focusOffset);
|
|
|
|
selection.extend(anchorNode, anchorOffset);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// User finished handle positioning with one end off the screen
|
|
|
|
this._closeSelection();
|
|
|
|
break;
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
2014-01-10 22:51:27 -08:00
|
|
|
|
2014-03-26 18:34:46 -07:00
|
|
|
this._stopDraggingHandles();
|
2014-01-10 22:51:27 -08:00
|
|
|
this._positionHandles();
|
2014-04-25 21:49:50 -07:00
|
|
|
// Changes to handle position can affect selection context and actionbar display
|
|
|
|
this._updateMenu();
|
2013-11-18 20:48:49 -08:00
|
|
|
|
|
|
|
} else if (this._activeType == this.TYPE_CURSOR) {
|
|
|
|
// Act on IMM composition notifications after caret movement ends
|
|
|
|
this._ignoreCompositionChanges = false;
|
2014-04-26 09:46:41 -07:00
|
|
|
this._stopDraggingHandles();
|
2014-01-10 22:51:27 -08:00
|
|
|
this._positionHandles();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Cu.reportError("Ignored \"TextSelection:Position\" message during invalid selection status");
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
2014-01-10 22:51:27 -08:00
|
|
|
|
2013-03-21 16:50:42 -07:00
|
|
|
break;
|
|
|
|
}
|
2013-08-06 13:58:41 -07:00
|
|
|
|
|
|
|
case "TextSelection:Get":
|
|
|
|
sendMessageToJava({
|
|
|
|
type: "TextSelection:Data",
|
|
|
|
requestId: aData,
|
|
|
|
text: this._getSelectedText()
|
|
|
|
});
|
|
|
|
break;
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-26 18:34:46 -07:00
|
|
|
// Ignore selectionChange notifications during handle dragging, disable dynamic
|
|
|
|
// IME text compositions (autoSuggest, autoCorrect, etc)
|
|
|
|
_startDraggingHandles: function sh_startDraggingHandles() {
|
|
|
|
if (!this._draggingHandles) {
|
|
|
|
this._draggingHandles = true;
|
2014-04-11 15:06:30 -07:00
|
|
|
sendMessageToJava({ type: "TextSelection:DraggingHandle", dragging: true });
|
2014-03-26 18:34:46 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Act on selectionChange notifications when not dragging handles, allow dynamic
|
|
|
|
// IME text compositions (autoSuggest, autoCorrect, etc)
|
|
|
|
_stopDraggingHandles: function sh_stopDraggingHandles() {
|
|
|
|
if (this._draggingHandles) {
|
|
|
|
this._draggingHandles = false;
|
2014-04-11 15:06:30 -07:00
|
|
|
sendMessageToJava({ type: "TextSelection:DraggingHandle", dragging: false });
|
2014-03-26 18:34:46 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-03-21 16:50:42 -07:00
|
|
|
handleEvent: function sh_handleEvent(aEvent) {
|
|
|
|
switch (aEvent.type) {
|
2014-03-31 21:19:16 -07:00
|
|
|
case "scroll":
|
|
|
|
// Maintain position when top-level document is scrolled
|
2014-04-26 09:46:41 -07:00
|
|
|
this._positionHandlesOnChange();
|
2014-03-31 21:19:16 -07:00
|
|
|
break;
|
|
|
|
|
2013-03-21 16:50:42 -07:00
|
|
|
case "pagehide":
|
|
|
|
case "blur":
|
2013-03-25 15:25:19 -07:00
|
|
|
this._closeSelection();
|
2013-03-21 16:50:42 -07:00
|
|
|
break;
|
|
|
|
|
2014-02-19 19:19:43 -08:00
|
|
|
// Update caret position on keyboard activity
|
|
|
|
case "keyup":
|
|
|
|
// Not generated by Swiftkeyboard
|
|
|
|
case "compositionupdate":
|
2013-03-21 16:50:42 -07:00
|
|
|
case "compositionend":
|
2014-02-19 19:19:43 -08:00
|
|
|
// Generated by SwiftKeyboard, et. al.
|
|
|
|
if (!this._ignoreCompositionChanges) {
|
|
|
|
this._positionHandles();
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/** Returns true if the provided element can be selected in text selection, false otherwise. */
|
|
|
|
canSelect: function sh_canSelect(aElement) {
|
|
|
|
return !(aElement instanceof Ci.nsIDOMHTMLButtonElement ||
|
|
|
|
aElement instanceof Ci.nsIDOMHTMLEmbedElement ||
|
|
|
|
aElement instanceof Ci.nsIDOMHTMLImageElement ||
|
|
|
|
aElement instanceof Ci.nsIDOMHTMLMediaElement) &&
|
|
|
|
aElement.style.MozUserSelect != 'none';
|
|
|
|
},
|
|
|
|
|
2013-08-06 01:38:32 -07:00
|
|
|
_getScrollPos: function sh_getScrollPos() {
|
|
|
|
// Get the current display position
|
|
|
|
let scrollX = {}, scrollY = {};
|
|
|
|
this._contentWindow.top.QueryInterface(Ci.nsIInterfaceRequestor).
|
|
|
|
getInterface(Ci.nsIDOMWindowUtils).getScrollXY(false, scrollX, scrollY);
|
|
|
|
return {
|
|
|
|
X: scrollX.value,
|
|
|
|
Y: scrollY.value
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2013-08-16 18:51:41 -07:00
|
|
|
notifySelectionChanged: function sh_notifySelectionChanged(aDocument, aSelection, aReason) {
|
2013-08-28 12:45:42 -07:00
|
|
|
// Ignore selectionChange notifications during handle movements
|
2014-03-26 18:34:46 -07:00
|
|
|
if (this._draggingHandles) {
|
2013-08-28 12:45:42 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-16 18:51:41 -07:00
|
|
|
// If the selection was collapsed to Start or to End, always close it
|
|
|
|
if ((aReason & Ci.nsISelectionListener.COLLAPSETOSTART_REASON) ||
|
|
|
|
(aReason & Ci.nsISelectionListener.COLLAPSETOEND_REASON)) {
|
|
|
|
this._closeSelection();
|
2013-08-19 17:20:35 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If selected text no longer exists, close
|
|
|
|
if (!aSelection.toString()) {
|
|
|
|
this._closeSelection();
|
2013-08-16 18:51:41 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-03-28 13:22:02 -07:00
|
|
|
/*
|
|
|
|
* Called from browser.js when the user long taps on text or chooses
|
|
|
|
* the "Select Word" context menu item. Initializes SelectionHandler,
|
|
|
|
* starts a selection, and positions the text selection handles.
|
|
|
|
*
|
2013-12-17 18:18:35 -08:00
|
|
|
* @param aOptions list of options describing how to start selection
|
|
|
|
* Options include:
|
|
|
|
* mode - SELECT_ALL to select everything in the target
|
|
|
|
* element, or SELECT_AT_POINT to select a word.
|
|
|
|
* x - The x-coordinate for SELECT_AT_POINT.
|
|
|
|
* y - The y-coordinate for SELECT_AT_POINT.
|
2013-03-28 13:22:02 -07:00
|
|
|
*/
|
2013-12-17 18:18:35 -08:00
|
|
|
startSelection: function sh_startSelection(aElement, aOptions = { mode: SelectionHandler.SELECT_ALL }) {
|
2013-03-28 13:22:02 -07:00
|
|
|
// Clear out any existing active selection
|
2013-03-25 15:25:19 -07:00
|
|
|
this._closeSelection();
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2014-03-06 19:14:22 -08:00
|
|
|
this._initTargetInfo(aElement, this.TYPE_SELECTION);
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2014-01-07 01:32:07 -08:00
|
|
|
// Perform the appropriate selection method, if we can't determine method, or it fails, return
|
|
|
|
if (!this._performSelection(aOptions)) {
|
2013-08-28 17:44:03 -07:00
|
|
|
this._deactivate();
|
2013-11-19 11:57:37 -08:00
|
|
|
return false;
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
|
|
|
|
2014-01-07 01:32:07 -08:00
|
|
|
// Double check results of successful selection operation
|
2013-03-28 13:22:02 -07:00
|
|
|
let selection = this._getSelection();
|
2013-11-19 11:57:37 -08:00
|
|
|
if (!selection || selection.rangeCount == 0 || selection.getRangeAt(0).collapsed) {
|
2013-08-28 17:44:03 -07:00
|
|
|
this._deactivate();
|
2013-11-19 11:57:37 -08:00
|
|
|
return false;
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
|
|
|
|
2014-03-12 18:16:33 -07:00
|
|
|
if (this._isPhoneNumber(selection.toString())) {
|
|
|
|
let anchorNode = selection.anchorNode;
|
|
|
|
let anchorOffset = selection.anchorOffset;
|
|
|
|
let focusNode = null;
|
|
|
|
let focusOffset = null;
|
|
|
|
while (this._isPhoneNumber(selection.toString().trim())) {
|
|
|
|
focusNode = selection.focusNode;
|
|
|
|
focusOffset = selection.focusOffset;
|
|
|
|
selection.modify("extend", "forward", "word");
|
|
|
|
// if we hit the end of the text on the page, we can't advance the selection
|
|
|
|
if (focusNode == selection.focusNode && focusOffset == selection.focusOffset) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse selection
|
|
|
|
selection.collapse(focusNode, focusOffset);
|
|
|
|
selection.extend(anchorNode, anchorOffset);
|
|
|
|
|
|
|
|
anchorNode = focusNode;
|
|
|
|
anchorOffset = focusOffset
|
|
|
|
|
|
|
|
while (this._isPhoneNumber(selection.toString().trim())) {
|
|
|
|
focusNode = selection.focusNode;
|
|
|
|
focusOffset = selection.focusOffset;
|
|
|
|
selection.modify("extend", "backward", "word");
|
|
|
|
// if we hit the end of the text on the page, we can't advance the selection
|
|
|
|
if (focusNode == selection.focusNode && focusOffset == selection.focusOffset) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
selection.collapse(focusNode, focusOffset);
|
|
|
|
selection.extend(anchorNode, anchorOffset);
|
|
|
|
}
|
|
|
|
|
2013-08-16 18:51:41 -07:00
|
|
|
// Add a listener to end the selection if it's removed programatically
|
|
|
|
selection.QueryInterface(Ci.nsISelectionPrivate).addSelectionListener(this);
|
2013-08-28 17:44:03 -07:00
|
|
|
this._activeType = this.TYPE_SELECTION;
|
2013-08-16 18:51:41 -07:00
|
|
|
|
2013-03-21 16:50:42 -07:00
|
|
|
// Initialize the cache
|
2013-03-28 13:21:58 -07:00
|
|
|
this._cache = { start: {}, end: {}};
|
2013-03-28 13:22:00 -07:00
|
|
|
this._updateCacheForSelection();
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2013-08-06 01:38:32 -07:00
|
|
|
let scroll = this._getScrollPos();
|
|
|
|
// Figure out the distance between the selection and the click
|
|
|
|
let positions = this._getHandlePositions(scroll);
|
|
|
|
|
2013-12-17 19:51:01 -08:00
|
|
|
if (aOptions.mode == this.SELECT_AT_POINT && !this._selectionNearClick(scroll.X + aOptions.x,
|
2013-12-17 18:18:35 -08:00
|
|
|
scroll.Y + aOptions.y,
|
|
|
|
positions)) {
|
|
|
|
this._closeSelection();
|
|
|
|
return false;
|
2013-08-06 01:38:32 -07:00
|
|
|
}
|
|
|
|
|
2014-04-25 21:49:50 -07:00
|
|
|
// Determine position and show handles, open actionbar
|
2013-08-06 01:38:32 -07:00
|
|
|
this._positionHandles(positions);
|
2014-04-25 21:49:50 -07:00
|
|
|
sendMessageToJava({
|
|
|
|
type: "TextSelection:ShowHandles",
|
|
|
|
handles: [this.HANDLE_TYPE_START, this.HANDLE_TYPE_END]
|
|
|
|
});
|
|
|
|
this._updateMenu();
|
2013-11-19 11:57:37 -08:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2014-01-07 01:32:07 -08:00
|
|
|
/*
|
|
|
|
* Called to perform a selection operation, given a target element, selection method, starting point etc.
|
|
|
|
*/
|
|
|
|
_performSelection: function sh_performSelection(aOptions) {
|
|
|
|
if (aOptions.mode == this.SELECT_AT_POINT) {
|
|
|
|
return this._domWinUtils.selectAtPoint(aOptions.x, aOptions.y, Ci.nsIDOMWindowUtils.SELECT_WORDNOSPACE);
|
|
|
|
}
|
|
|
|
|
2014-01-17 21:31:05 -08:00
|
|
|
if (aOptions.mode != this.SELECT_ALL) {
|
|
|
|
Cu.reportError("SelectionHandler.js: _performSelection() Invalid selection mode " + aOptions.mode);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTMLPreElement is a #text node, SELECT_ALL implies entire paragraph
|
|
|
|
if (this._targetElement instanceof HTMLPreElement) {
|
|
|
|
return this._domWinUtils.selectAtPoint(1, 1, Ci.nsIDOMWindowUtils.SELECT_PARAGRAPH);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else default to selectALL Document
|
2014-06-16 08:54:57 -07:00
|
|
|
let editor = this._getEditor();
|
|
|
|
if (editor) {
|
|
|
|
editor.selectAll();
|
|
|
|
} else {
|
|
|
|
this._getSelectionController().selectAll();
|
|
|
|
}
|
2014-01-17 21:31:05 -08:00
|
|
|
|
|
|
|
// Selection is entire HTMLHtmlElement, remove any trailing document whitespace
|
|
|
|
let selection = this._getSelection();
|
|
|
|
let lastNode = selection.focusNode;
|
|
|
|
while (lastNode && lastNode.lastChild) {
|
|
|
|
lastNode = lastNode.lastChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastNode instanceof Text) {
|
|
|
|
try {
|
|
|
|
selection.extend(lastNode, lastNode.length);
|
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError("SelectionHandler.js: _performSelection() whitespace trim fails: lastNode[" + lastNode +
|
|
|
|
"] lastNode.length[" + lastNode.length + "]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-01-07 01:32:07 -08:00
|
|
|
},
|
|
|
|
|
2013-12-17 18:18:35 -08:00
|
|
|
/* Return true if the current selection (given by aPositions) is near to where the coordinates passed in */
|
|
|
|
_selectionNearClick: function(aX, aY, aPositions) {
|
|
|
|
let distance = 0;
|
|
|
|
|
|
|
|
// Check if the click was in the bounding box of the selection handles
|
|
|
|
if (aPositions[0].left < aX && aX < aPositions[1].left
|
|
|
|
&& aPositions[0].top < aY && aY < aPositions[1].top) {
|
|
|
|
distance = 0;
|
|
|
|
} else {
|
|
|
|
// If it was outside, check the distance to the center of the selection
|
|
|
|
let selectposX = (aPositions[0].left + aPositions[1].left) / 2;
|
|
|
|
let selectposY = (aPositions[0].top + aPositions[1].top) / 2;
|
|
|
|
|
|
|
|
let dx = Math.abs(selectposX - aX);
|
|
|
|
let dy = Math.abs(selectposY - aY);
|
|
|
|
distance = dx + dy;
|
|
|
|
}
|
|
|
|
|
|
|
|
let maxSelectionDistance = Services.prefs.getIntPref("browser.ui.selection.distance");
|
|
|
|
return (distance < maxSelectionDistance);
|
|
|
|
},
|
2013-11-19 11:57:37 -08:00
|
|
|
|
|
|
|
/* Reads a value from an action. If the action defines the value as a function, will return the result of calling
|
|
|
|
the function. Otherwise, will return the value itself. If the value isn't defined for this action, will return a default */
|
|
|
|
_getValue: function(obj, name, defaultValue) {
|
|
|
|
if (!(name in obj))
|
|
|
|
return defaultValue;
|
|
|
|
|
|
|
|
if (typeof obj[name] == "function")
|
|
|
|
return obj[name](this._targetElement);
|
|
|
|
|
|
|
|
return obj[name];
|
|
|
|
},
|
|
|
|
|
2014-04-25 21:49:50 -07:00
|
|
|
addAction: function(action) {
|
|
|
|
if (!action.id)
|
|
|
|
action.id = uuidgen.generateUUID().toString()
|
|
|
|
|
|
|
|
if (this.actions[action.id])
|
|
|
|
throw "Action with id " + action.id + " already added";
|
|
|
|
|
|
|
|
// Update actions list and actionbar UI if active.
|
|
|
|
this.actions[action.id] = action;
|
|
|
|
this._updateMenu();
|
|
|
|
return action.id;
|
|
|
|
},
|
|
|
|
|
|
|
|
removeAction: function(id) {
|
|
|
|
// Update actions list and actionbar UI if active.
|
|
|
|
delete this.actions[id];
|
|
|
|
this._updateMenu();
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateMenu: function() {
|
|
|
|
if (this._activeType == this.TYPE_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update actionbar UI.
|
2013-11-19 11:57:37 -08:00
|
|
|
let actions = [];
|
|
|
|
for (let type in this.actions) {
|
|
|
|
let action = this.actions[type];
|
2014-04-08 05:13:04 -07:00
|
|
|
if (action.selector.matches(this._targetElement)) {
|
2013-11-19 11:57:37 -08:00
|
|
|
let a = {
|
|
|
|
id: action.id,
|
|
|
|
label: this._getValue(action, "label", ""),
|
|
|
|
icon: this._getValue(action, "icon", "drawable://ic_status_logo"),
|
|
|
|
showAsAction: this._getValue(action, "showAsAction", true),
|
2013-12-10 10:46:08 -08:00
|
|
|
order: this._getValue(action, "order", 0)
|
2013-11-19 11:57:37 -08:00
|
|
|
};
|
|
|
|
actions.push(a);
|
|
|
|
}
|
|
|
|
}
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2013-12-10 10:46:08 -08:00
|
|
|
actions.sort((a, b) => b.order - a.order);
|
|
|
|
|
2013-03-21 16:50:42 -07:00
|
|
|
sendMessageToJava({
|
2014-04-25 21:49:50 -07:00
|
|
|
type: "TextSelection:Update",
|
|
|
|
actions: actions
|
2013-03-21 16:50:42 -07:00
|
|
|
});
|
2013-03-28 13:22:02 -07:00
|
|
|
},
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2014-04-08 05:13:04 -07:00
|
|
|
/*
|
|
|
|
* Actionbar methods.
|
|
|
|
*/
|
2013-11-19 11:57:37 -08:00
|
|
|
actions: {
|
|
|
|
SELECT_ALL: {
|
|
|
|
label: Strings.browser.GetStringFromName("contextmenu.selectAll"),
|
|
|
|
id: "selectall_action",
|
2014-01-16 13:33:00 -08:00
|
|
|
icon: "drawable://ab_select_all",
|
2013-11-19 11:57:37 -08:00
|
|
|
action: function(aElement) {
|
2014-04-08 05:13:04 -07:00
|
|
|
SelectionHandler.startSelection(aElement)
|
2013-11-19 11:57:37 -08:00
|
|
|
},
|
2014-01-15 13:40:00 -08:00
|
|
|
order: 5,
|
2014-04-08 05:13:04 -07:00
|
|
|
selector: {
|
|
|
|
matches: function(aElement) {
|
|
|
|
return (aElement.textLength != 0);
|
|
|
|
}
|
|
|
|
}
|
2013-11-19 11:57:37 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
CUT: {
|
|
|
|
label: Strings.browser.GetStringFromName("contextmenu.cut"),
|
|
|
|
id: "cut_action",
|
2014-01-16 13:33:00 -08:00
|
|
|
icon: "drawable://ab_cut",
|
2013-11-19 11:57:37 -08:00
|
|
|
action: function(aElement) {
|
|
|
|
let start = aElement.selectionStart;
|
|
|
|
let end = aElement.selectionEnd;
|
|
|
|
|
|
|
|
SelectionHandler.copySelection();
|
|
|
|
aElement.value = aElement.value.substring(0, start) + aElement.value.substring(end)
|
|
|
|
|
2013-12-03 14:47:37 -08:00
|
|
|
// copySelection closes the selection. Show a caret where we just cut the text.
|
|
|
|
SelectionHandler.attachCaret(aElement);
|
2013-11-19 11:57:37 -08:00
|
|
|
},
|
2014-01-15 13:40:00 -08:00
|
|
|
order: 4,
|
2014-04-08 05:13:04 -07:00
|
|
|
selector: {
|
|
|
|
matches: function(aElement) {
|
|
|
|
return SelectionHandler.isElementEditableText(aElement) ?
|
|
|
|
SelectionHandler.isSelectionActive() : false;
|
|
|
|
}
|
|
|
|
}
|
2013-11-19 11:57:37 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
COPY: {
|
|
|
|
label: Strings.browser.GetStringFromName("contextmenu.copy"),
|
|
|
|
id: "copy_action",
|
2014-01-16 13:33:00 -08:00
|
|
|
icon: "drawable://ab_copy",
|
2013-11-19 11:57:37 -08:00
|
|
|
action: function() {
|
|
|
|
SelectionHandler.copySelection();
|
|
|
|
},
|
2014-01-15 13:40:00 -08:00
|
|
|
order: 3,
|
2014-04-08 05:13:04 -07:00
|
|
|
selector: {
|
|
|
|
matches: function(aElement) {
|
|
|
|
// Don't include "copy" for password fields.
|
|
|
|
// mozIsTextField(true) tests for only non-password fields.
|
|
|
|
if (aElement instanceof Ci.nsIDOMHTMLInputElement && !aElement.mozIsTextField(true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return SelectionHandler.isSelectionActive();
|
|
|
|
}
|
|
|
|
}
|
2013-11-19 11:57:37 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
PASTE: {
|
|
|
|
label: Strings.browser.GetStringFromName("contextmenu.paste"),
|
|
|
|
id: "paste_action",
|
2014-01-16 13:33:00 -08:00
|
|
|
icon: "drawable://ab_paste",
|
2013-11-19 11:57:37 -08:00
|
|
|
action: function(aElement) {
|
2014-04-08 05:13:04 -07:00
|
|
|
if (aElement && (aElement instanceof Ci.nsIDOMNSEditableElement)) {
|
|
|
|
let target = aElement.QueryInterface(Ci.nsIDOMNSEditableElement);
|
|
|
|
target.editor.paste(Ci.nsIClipboard.kGlobalClipboard);
|
|
|
|
target.focus();
|
|
|
|
SelectionHandler._closeSelection();
|
|
|
|
}
|
2013-11-19 11:57:37 -08:00
|
|
|
},
|
2014-01-15 13:40:00 -08:00
|
|
|
order: 2,
|
2014-04-08 05:13:04 -07:00
|
|
|
selector: {
|
|
|
|
matches: function(aElement) {
|
|
|
|
if (SelectionHandler.isElementEditableText(aElement)) {
|
|
|
|
let flavors = ["text/unicode"];
|
|
|
|
return Services.clipboard.hasDataMatchingFlavors(flavors, flavors.length, Ci.nsIClipboard.kGlobalClipboard);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-11-19 11:57:37 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
SHARE: {
|
|
|
|
label: Strings.browser.GetStringFromName("contextmenu.share"),
|
|
|
|
id: "share_action",
|
|
|
|
icon: "drawable://ic_menu_share",
|
|
|
|
action: function() {
|
|
|
|
SelectionHandler.shareSelection();
|
|
|
|
},
|
2014-04-08 05:13:04 -07:00
|
|
|
selector: {
|
|
|
|
matches: function() {
|
|
|
|
return SelectionHandler.isSelectionActive();
|
|
|
|
}
|
|
|
|
}
|
2013-11-19 11:57:37 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
SEARCH: {
|
|
|
|
label: function() {
|
|
|
|
return Strings.browser.formatStringFromName("contextmenu.search", [Services.search.defaultEngine.name], 1);
|
|
|
|
},
|
|
|
|
id: "search_action",
|
2014-01-16 13:33:00 -08:00
|
|
|
icon: "drawable://ab_search",
|
2013-11-19 11:57:37 -08:00
|
|
|
action: function() {
|
|
|
|
SelectionHandler.searchSelection();
|
|
|
|
SelectionHandler._closeSelection();
|
|
|
|
},
|
2014-01-15 13:40:00 -08:00
|
|
|
order: 1,
|
2014-04-08 05:13:04 -07:00
|
|
|
selector: {
|
|
|
|
matches: function() {
|
|
|
|
return SelectionHandler.isSelectionActive();
|
|
|
|
}
|
|
|
|
}
|
2013-11-19 11:57:37 -08:00
|
|
|
},
|
|
|
|
|
2014-03-11 12:30:40 -07:00
|
|
|
CALL: {
|
|
|
|
label: Strings.browser.GetStringFromName("contextmenu.call"),
|
|
|
|
id: "call_action",
|
|
|
|
icon: "drawable://phone",
|
|
|
|
action: function() {
|
|
|
|
SelectionHandler.callSelection();
|
|
|
|
},
|
|
|
|
order: 1,
|
|
|
|
selector: {
|
2014-04-08 05:13:04 -07:00
|
|
|
matches: function () {
|
|
|
|
return SelectionHandler._getSelectedPhoneNumber() != null;
|
2014-03-11 12:30:40 -07:00
|
|
|
}
|
2014-04-08 05:13:04 -07:00
|
|
|
}
|
|
|
|
}
|
2013-11-19 11:57:37 -08:00
|
|
|
},
|
|
|
|
|
2013-03-28 13:22:02 -07:00
|
|
|
/*
|
|
|
|
* Called by BrowserEventHandler when the user taps in a form input.
|
|
|
|
* Initializes SelectionHandler and positions the caret handle.
|
|
|
|
*
|
|
|
|
* @param aX, aY tap location in client coordinates.
|
|
|
|
*/
|
|
|
|
attachCaret: function sh_attachCaret(aElement) {
|
2014-04-08 05:13:04 -07:00
|
|
|
// Ensure it isn't disabled, isn't handled by Android native dialog, and is editable text element
|
|
|
|
if (aElement.disabled || InputWidgetHelper.hasInputWidget(aElement) || !this.isElementEditableText(aElement)) {
|
2013-11-19 11:57:37 -08:00
|
|
|
return;
|
2014-04-08 05:13:04 -07:00
|
|
|
}
|
2014-02-19 19:19:43 -08:00
|
|
|
|
2014-03-06 19:14:22 -08:00
|
|
|
this._initTargetInfo(aElement, this.TYPE_CURSOR);
|
2013-03-28 13:22:02 -07:00
|
|
|
|
2014-02-19 19:19:43 -08:00
|
|
|
// Caret-specific observer/listeners
|
|
|
|
Services.obs.addObserver(this, "TextSelection:UpdateCaretPos", false);
|
|
|
|
BrowserApp.deck.addEventListener("keyup", this, false);
|
|
|
|
BrowserApp.deck.addEventListener("compositionupdate", this, false);
|
|
|
|
BrowserApp.deck.addEventListener("compositionend", this, false);
|
2013-03-28 13:22:02 -07:00
|
|
|
|
|
|
|
this._activeType = this.TYPE_CURSOR;
|
|
|
|
|
2014-04-25 21:49:50 -07:00
|
|
|
// Determine position and show caret, open actionbar
|
|
|
|
this._positionHandles();
|
|
|
|
sendMessageToJava({
|
|
|
|
type: "TextSelection:ShowHandles",
|
|
|
|
handles: [this.HANDLE_TYPE_MIDDLE]
|
|
|
|
});
|
|
|
|
this._updateMenu();
|
2013-03-28 13:22:02 -07:00
|
|
|
},
|
|
|
|
|
2014-02-19 19:19:43 -08:00
|
|
|
// Target initialization for both TYPE_CURSOR and TYPE_SELECTION
|
2014-03-06 19:14:22 -08:00
|
|
|
_initTargetInfo: function sh_initTargetInfo(aElement, aSelectionType) {
|
2013-03-28 13:22:02 -07:00
|
|
|
this._targetElement = aElement;
|
|
|
|
if (aElement instanceof Ci.nsIDOMNSEditableElement) {
|
2014-03-06 19:14:22 -08:00
|
|
|
if (aSelectionType === this.TYPE_SELECTION) {
|
|
|
|
// Blur the targetElement to force IME code to undo previous style compositions
|
|
|
|
// (visible underlines / etc generated by autoCorrection, autoSuggestion)
|
|
|
|
aElement.blur();
|
|
|
|
}
|
2014-02-19 19:19:43 -08:00
|
|
|
// Ensure targetElement is now focused normally
|
2013-03-21 16:50:42 -07:00
|
|
|
aElement.focus();
|
2013-03-28 13:22:02 -07:00
|
|
|
}
|
|
|
|
|
2014-03-26 18:34:46 -07:00
|
|
|
this._stopDraggingHandles();
|
2013-03-28 13:22:02 -07:00
|
|
|
this._contentWindow = aElement.ownerDocument.defaultView;
|
|
|
|
this._isRTL = (this._contentWindow.getComputedStyle(aElement, "").direction == "rtl");
|
|
|
|
|
|
|
|
this._addObservers();
|
2013-03-21 16:50:42 -07:00
|
|
|
},
|
|
|
|
|
2013-03-28 13:22:00 -07:00
|
|
|
_getSelection: function sh_getSelection() {
|
2013-03-28 13:21:58 -07:00
|
|
|
if (this._targetElement instanceof Ci.nsIDOMNSEditableElement)
|
|
|
|
return this._targetElement.QueryInterface(Ci.nsIDOMNSEditableElement).editor.selection;
|
2013-03-21 16:50:42 -07:00
|
|
|
else
|
2013-03-28 13:21:58 -07:00
|
|
|
return this._contentWindow.getSelection();
|
2013-03-21 16:50:42 -07:00
|
|
|
},
|
|
|
|
|
2013-08-06 13:58:41 -07:00
|
|
|
_getSelectedText: function sh_getSelectedText() {
|
2013-07-28 13:36:47 -07:00
|
|
|
if (!this._contentWindow)
|
|
|
|
return "";
|
|
|
|
|
2013-03-28 13:22:00 -07:00
|
|
|
let selection = this._getSelection();
|
2013-11-20 10:57:20 -08:00
|
|
|
if (!selection)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
if (this._targetElement instanceof Ci.nsIDOMHTMLTextAreaElement) {
|
|
|
|
return selection.QueryInterface(Ci.nsISelectionPrivate).
|
|
|
|
toStringWithFormat("text/plain", Ci.nsIDocumentEncoder.OutputPreformatted | Ci.nsIDocumentEncoder.OutputRaw, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return selection.toString().trim();
|
2013-03-25 15:25:19 -07:00
|
|
|
},
|
|
|
|
|
2014-06-16 08:54:57 -07:00
|
|
|
_getEditor: function sh_getEditor() {
|
|
|
|
if (this._targetElement instanceof Ci.nsIDOMNSEditableElement) {
|
|
|
|
return this._targetElement.QueryInterface(Ci.nsIDOMNSEditableElement).editor;
|
|
|
|
}
|
|
|
|
return this._contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIEditingSession)
|
|
|
|
.getEditorForWindow(this._contentWindow);
|
|
|
|
},
|
|
|
|
|
2013-03-28 13:22:00 -07:00
|
|
|
_getSelectionController: function sh_getSelectionController() {
|
2013-03-28 13:21:58 -07:00
|
|
|
if (this._targetElement instanceof Ci.nsIDOMNSEditableElement)
|
|
|
|
return this._targetElement.QueryInterface(Ci.nsIDOMNSEditableElement).editor.selectionController;
|
2013-03-21 16:50:42 -07:00
|
|
|
else
|
2013-03-28 13:21:58 -07:00
|
|
|
return this._contentWindow.QueryInterface(Ci.nsIInterfaceRequestor).
|
|
|
|
getInterface(Ci.nsIWebNavigation).
|
|
|
|
QueryInterface(Ci.nsIInterfaceRequestor).
|
|
|
|
getInterface(Ci.nsISelectionDisplay).
|
|
|
|
QueryInterface(Ci.nsISelectionController);
|
2013-03-21 16:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// Used by the contextmenu "matches" functions in ClipboardHelper
|
2013-11-19 11:57:37 -08:00
|
|
|
isSelectionActive: function sh_isSelectionActive() {
|
|
|
|
return (this._activeType == this.TYPE_SELECTION);
|
2013-03-21 16:50:42 -07:00
|
|
|
},
|
|
|
|
|
2014-04-08 05:13:04 -07:00
|
|
|
isElementEditableText: function (aElement) {
|
|
|
|
return ((aElement instanceof HTMLInputElement && aElement.mozIsTextField(false)) ||
|
|
|
|
(aElement instanceof HTMLTextAreaElement));
|
2013-03-21 16:50:42 -07:00
|
|
|
},
|
|
|
|
|
2014-01-25 16:15:31 -08:00
|
|
|
/*
|
|
|
|
* Helper function for moving the selection inside an editable element.
|
|
|
|
*
|
|
|
|
* @param aAnchorX the stationary handle's x-coordinate in client coordinates
|
|
|
|
* @param aX the moved handle's x-coordinate in client coordinates
|
|
|
|
* @param aCaretPos the current position of the caret
|
|
|
|
*/
|
|
|
|
_moveSelectionInEditable: function sh_moveSelectionInEditable(aAnchorX, aX, aCaretPos) {
|
|
|
|
let anchorOffset = aX < aAnchorX ? this._targetElement.selectionEnd
|
|
|
|
: this._targetElement.selectionStart;
|
|
|
|
let newOffset = aCaretPos.offset;
|
|
|
|
let [start, end] = anchorOffset <= newOffset ?
|
|
|
|
[anchorOffset, newOffset] :
|
|
|
|
[newOffset, anchorOffset];
|
|
|
|
this._targetElement.setSelectionRange(start, end);
|
|
|
|
},
|
|
|
|
|
2013-04-22 17:16:06 -07:00
|
|
|
/*
|
|
|
|
* Moves the selection as the user drags a selection handle.
|
|
|
|
*
|
|
|
|
* @param aIsStartHandle whether the user is moving the start handle (as opposed to the end handle)
|
|
|
|
* @param aX, aY selection point in client coordinates
|
|
|
|
*/
|
2013-03-28 13:22:00 -07:00
|
|
|
_moveSelection: function sh_moveSelection(aIsStartHandle, aX, aY) {
|
2013-04-22 17:16:06 -07:00
|
|
|
// XXX We should be smarter about the coordinates we pass to caretPositionFromPoint, especially
|
2014-04-28 17:21:19 -07:00
|
|
|
// in editable targets. We should factor out the logic that's currently in _moveCaret.
|
2013-04-22 17:16:06 -07:00
|
|
|
let viewOffset = this._getViewOffset();
|
|
|
|
let caretPos = this._contentWindow.document.caretPositionFromPoint(aX - viewOffset.x, aY - viewOffset.y);
|
2014-01-07 23:59:08 -08:00
|
|
|
if (!caretPos) {
|
|
|
|
// User moves handle offscreen while positioning
|
|
|
|
return;
|
|
|
|
}
|
2013-04-22 17:16:06 -07:00
|
|
|
|
|
|
|
// Constrain text selection within editable elements.
|
|
|
|
let targetIsEditable = this._targetElement instanceof Ci.nsIDOMNSEditableElement;
|
|
|
|
if (targetIsEditable && (caretPos.offsetNode != this._targetElement)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the cache as the handle is dragged (keep the cache in client coordinates).
|
2013-03-21 16:50:42 -07:00
|
|
|
if (aIsStartHandle) {
|
2013-03-28 13:21:58 -07:00
|
|
|
this._cache.start.x = aX;
|
|
|
|
this._cache.start.y = aY;
|
2013-03-21 16:50:42 -07:00
|
|
|
} else {
|
2013-03-28 13:21:58 -07:00
|
|
|
this._cache.end.x = aX;
|
|
|
|
this._cache.end.y = aY;
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
|
|
|
|
2013-04-22 17:16:06 -07:00
|
|
|
let selection = this._getSelection();
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2013-04-22 17:16:06 -07:00
|
|
|
// The handles work the same on both LTR and RTL pages, but the anchor/focus nodes
|
|
|
|
// are reversed, so we need to reverse the logic to extend the selection.
|
|
|
|
if ((aIsStartHandle && !this._isRTL) || (!aIsStartHandle && this._isRTL)) {
|
|
|
|
if (targetIsEditable) {
|
2014-01-25 16:15:31 -08:00
|
|
|
let anchorX = this._isRTL ? this._cache.start.x : this._cache.end.x;
|
|
|
|
this._moveSelectionInEditable(anchorX, aX, caretPos);
|
2013-04-22 17:16:06 -07:00
|
|
|
} else {
|
|
|
|
let focusNode = selection.focusNode;
|
|
|
|
let focusOffset = selection.focusOffset;
|
|
|
|
selection.collapse(caretPos.offsetNode, caretPos.offset);
|
|
|
|
selection.extend(focusNode, focusOffset);
|
|
|
|
}
|
2013-03-21 16:50:42 -07:00
|
|
|
} else {
|
2013-04-22 17:16:06 -07:00
|
|
|
if (targetIsEditable) {
|
2014-01-25 16:15:31 -08:00
|
|
|
let anchorX = this._isRTL ? this._cache.end.x : this._cache.start.x;
|
|
|
|
this._moveSelectionInEditable(anchorX, aX, caretPos);
|
2013-04-22 17:16:06 -07:00
|
|
|
} else {
|
|
|
|
selection.extend(caretPos.offsetNode, caretPos.offset);
|
|
|
|
}
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-04-28 17:21:19 -07:00
|
|
|
_moveCaret: function sh_moveCaret(aX, aY) {
|
2014-04-28 17:21:19 -07:00
|
|
|
// Get rect of text inside element
|
|
|
|
let range = document.createRange();
|
|
|
|
range.selectNodeContents(this._targetElement.QueryInterface(Ci.nsIDOMNSEditableElement).editor.rootElement);
|
|
|
|
let textBounds = range.getBoundingClientRect();
|
|
|
|
|
|
|
|
// Get rect of editor
|
|
|
|
let editorBounds = this._domWinUtils.sendQueryContentEvent(this._domWinUtils.QUERY_EDITOR_RECT, 0, 0, 0, 0,
|
|
|
|
this._domWinUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
|
|
|
|
// the return value from sendQueryContentEvent is in LayoutDevice pixels and we want CSS pixels, so
|
|
|
|
// divide by the pixel ratio
|
|
|
|
let editorRect = new Rect(editorBounds.left / window.devicePixelRatio,
|
|
|
|
editorBounds.top / window.devicePixelRatio,
|
|
|
|
editorBounds.width / window.devicePixelRatio,
|
|
|
|
editorBounds.height / window.devicePixelRatio);
|
|
|
|
|
|
|
|
// Use intersection of the text rect and the editor rect
|
|
|
|
let rect = new Rect(textBounds.left, textBounds.top, textBounds.width, textBounds.height);
|
|
|
|
rect.restrictTo(editorRect);
|
|
|
|
|
|
|
|
// Clamp vertically and scroll if handle is at bounds. The top and bottom
|
|
|
|
// must be restricted by an additional pixel since clicking on the top
|
|
|
|
// edge of an input field moves the cursor to the beginning of that
|
|
|
|
// field's text (and clicking the bottom moves the cursor to the end).
|
|
|
|
if (aY < rect.y + 1) {
|
|
|
|
aY = rect.y + 1;
|
|
|
|
this._getSelectionController().scrollLine(false);
|
|
|
|
} else if (aY > rect.y + rect.height - 1) {
|
|
|
|
aY = rect.y + rect.height - 1;
|
|
|
|
this._getSelectionController().scrollLine(true);
|
|
|
|
}
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2014-04-28 17:21:19 -07:00
|
|
|
// Clamp horizontally and scroll if handle is at bounds
|
|
|
|
if (aX < rect.x) {
|
|
|
|
aX = rect.x;
|
|
|
|
this._getSelectionController().scrollCharacter(false);
|
|
|
|
} else if (aX > rect.x + rect.width) {
|
|
|
|
aX = rect.x + rect.width;
|
|
|
|
this._getSelectionController().scrollCharacter(true);
|
|
|
|
}
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2014-04-28 17:21:19 -07:00
|
|
|
this._domWinUtils.sendMouseEventToWindow("mousedown", aX, aY, 0, 0, 0, true);
|
|
|
|
this._domWinUtils.sendMouseEventToWindow("mouseup", aX, aY, 0, 0, 0, true);
|
2013-03-21 16:50:42 -07:00
|
|
|
},
|
|
|
|
|
2013-03-25 15:25:19 -07:00
|
|
|
copySelection: function sh_copySelection() {
|
2013-08-06 13:58:41 -07:00
|
|
|
let selectedText = this._getSelectedText();
|
2013-03-25 15:25:19 -07:00
|
|
|
if (selectedText.length) {
|
|
|
|
let clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper);
|
2013-03-28 13:21:58 -07:00
|
|
|
clipboard.copyString(selectedText, this._contentWindow.document);
|
2013-03-25 15:25:19 -07:00
|
|
|
NativeWindow.toast.show(Strings.browser.GetStringFromName("selectionHelper.textCopied"), "short");
|
|
|
|
}
|
|
|
|
this._closeSelection();
|
|
|
|
},
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2013-03-25 15:25:19 -07:00
|
|
|
shareSelection: function sh_shareSelection() {
|
2013-08-06 13:58:41 -07:00
|
|
|
let selectedText = this._getSelectedText();
|
2013-03-25 15:25:19 -07:00
|
|
|
if (selectedText.length) {
|
|
|
|
sendMessageToJava({
|
|
|
|
type: "Share:Text",
|
|
|
|
text: selectedText
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this._closeSelection();
|
|
|
|
},
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2013-07-29 09:17:32 -07:00
|
|
|
searchSelection: function sh_searchSelection() {
|
2013-08-06 13:58:41 -07:00
|
|
|
let selectedText = this._getSelectedText();
|
2013-07-29 09:17:32 -07:00
|
|
|
if (selectedText.length) {
|
|
|
|
let req = Services.search.defaultEngine.getSubmission(selectedText);
|
2013-08-27 11:58:26 -07:00
|
|
|
let parent = BrowserApp.selectedTab;
|
|
|
|
let isPrivate = PrivateBrowsingUtils.isWindowPrivate(parent.browser.contentWindow);
|
|
|
|
// Set current tab as parent of new tab, and set new tab as private if the parent is.
|
|
|
|
BrowserApp.addTab(req.uri.spec, {parentId: parent.id,
|
|
|
|
selected: true,
|
|
|
|
isPrivate: isPrivate});
|
2013-07-29 09:17:32 -07:00
|
|
|
}
|
|
|
|
this._closeSelection();
|
|
|
|
},
|
|
|
|
|
2014-03-12 18:16:33 -07:00
|
|
|
_phoneRegex: /^\+?[0-9\s,-.\(\)*#pw]{1,30}$/,
|
2014-03-11 12:30:40 -07:00
|
|
|
|
2014-03-12 18:16:33 -07:00
|
|
|
_getSelectedPhoneNumber: function sh_getSelectedPhoneNumber() {
|
|
|
|
return this._isPhoneNumber(this._getSelectedText().trim());
|
|
|
|
},
|
|
|
|
|
|
|
|
_isPhoneNumber: function sh_isPhoneNumber(selectedText) {
|
|
|
|
return (this._phoneRegex.test(selectedText) ? selectedText : null);
|
2014-03-11 12:30:40 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
callSelection: function sh_callSelection() {
|
|
|
|
let selectedText = this._getSelectedPhoneNumber();
|
|
|
|
if (selectedText) {
|
|
|
|
BrowserApp.loadURI("tel:" + selectedText);
|
|
|
|
}
|
|
|
|
this._closeSelection();
|
|
|
|
},
|
|
|
|
|
2013-03-25 15:25:19 -07:00
|
|
|
/*
|
|
|
|
* Shuts SelectionHandler down.
|
|
|
|
*/
|
|
|
|
_closeSelection: function sh_closeSelection() {
|
|
|
|
// Bail if there's no active selection
|
|
|
|
if (this._activeType == this.TYPE_NONE)
|
|
|
|
return;
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2013-08-28 17:44:03 -07:00
|
|
|
if (this._activeType == this.TYPE_SELECTION)
|
|
|
|
this._clearSelection();
|
|
|
|
|
|
|
|
this._deactivate();
|
|
|
|
},
|
|
|
|
|
|
|
|
_clearSelection: function sh_clearSelection() {
|
|
|
|
let selection = this._getSelection();
|
|
|
|
if (selection) {
|
|
|
|
// Remove our listener before we clear the selection
|
|
|
|
selection.QueryInterface(Ci.nsISelectionPrivate).removeSelectionListener(this);
|
|
|
|
// Clear selection without clearing the anchorNode or focusNode
|
2013-09-09 14:48:19 -07:00
|
|
|
if (selection.rangeCount != 0) {
|
|
|
|
selection.collapseToStart();
|
|
|
|
}
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
2013-08-28 17:44:03 -07:00
|
|
|
},
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2013-08-28 17:44:03 -07:00
|
|
|
_deactivate: function sh_deactivate() {
|
2014-03-26 18:34:46 -07:00
|
|
|
this._stopDraggingHandles();
|
2014-04-25 21:49:50 -07:00
|
|
|
// Hide handle/caret, close actionbar
|
2013-03-25 15:25:19 -07:00
|
|
|
sendMessageToJava({ type: "TextSelection:HideHandles" });
|
2013-03-21 16:50:42 -07:00
|
|
|
|
|
|
|
this._removeObservers();
|
2014-02-19 19:19:43 -08:00
|
|
|
|
|
|
|
// Only observed for caret positioning
|
|
|
|
if (this._activeType == this.TYPE_CURSOR) {
|
|
|
|
Services.obs.removeObserver(this, "TextSelection:UpdateCaretPos");
|
|
|
|
BrowserApp.deck.removeEventListener("keyup", this);
|
|
|
|
BrowserApp.deck.removeEventListener("compositionupdate", this);
|
|
|
|
BrowserApp.deck.removeEventListener("compositionend", this);
|
|
|
|
}
|
|
|
|
|
2013-03-28 13:21:58 -07:00
|
|
|
this._contentWindow = null;
|
|
|
|
this._targetElement = null;
|
2013-03-21 16:50:42 -07:00
|
|
|
this._isRTL = false;
|
2013-03-28 13:21:58 -07:00
|
|
|
this._cache = null;
|
2013-11-18 20:48:49 -08:00
|
|
|
this._ignoreCompositionChanges = false;
|
2014-04-26 09:46:41 -07:00
|
|
|
this._prevHandlePositions = [];
|
2014-04-25 21:49:50 -07:00
|
|
|
this._prevTargetElementHasText = null;
|
2014-02-19 19:19:43 -08:00
|
|
|
|
|
|
|
this._activeType = this.TYPE_NONE;
|
2013-03-21 16:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_getViewOffset: function sh_getViewOffset() {
|
|
|
|
let offset = { x: 0, y: 0 };
|
2013-03-28 13:21:58 -07:00
|
|
|
let win = this._contentWindow;
|
2013-03-21 16:50:42 -07:00
|
|
|
|
|
|
|
// Recursively look through frames to compute the total position offset.
|
|
|
|
while (win.frameElement) {
|
|
|
|
let rect = win.frameElement.getBoundingClientRect();
|
|
|
|
offset.x += rect.left;
|
|
|
|
offset.y += rect.top;
|
|
|
|
|
|
|
|
win = win.parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
},
|
|
|
|
|
|
|
|
_pointInSelection: function sh_pointInSelection(aX, aY) {
|
|
|
|
let offset = this._getViewOffset();
|
2013-03-28 13:22:00 -07:00
|
|
|
let rangeRect = this._getSelection().getRangeAt(0).getBoundingClientRect();
|
2013-03-21 16:50:42 -07:00
|
|
|
let radius = ElementTouchHelper.getTouchRadius();
|
|
|
|
return (aX - offset.x > rangeRect.left - radius.left &&
|
|
|
|
aX - offset.x < rangeRect.right + radius.right &&
|
|
|
|
aY - offset.y > rangeRect.top - radius.top &&
|
|
|
|
aY - offset.y < rangeRect.bottom + radius.bottom);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns true if the selection has been reversed. Takes optional aIsStartHandle
|
|
|
|
// param to decide whether the selection has been reversed.
|
2013-03-28 13:22:00 -07:00
|
|
|
_updateCacheForSelection: function sh_updateCacheForSelection(aIsStartHandle) {
|
2014-01-10 22:51:27 -08:00
|
|
|
let rects = this._getSelection().getRangeAt(0).getClientRects();
|
|
|
|
if (!rects[0]) {
|
|
|
|
// nsISelection object exists, but there's nothing actually selected
|
|
|
|
throw "Failed to update cache for invalid selection";
|
|
|
|
}
|
|
|
|
|
2013-03-21 16:50:42 -07:00
|
|
|
let start = { x: this._isRTL ? rects[0].right : rects[0].left, y: rects[0].bottom };
|
|
|
|
let end = { x: this._isRTL ? rects[rects.length - 1].left : rects[rects.length - 1].right, y: rects[rects.length - 1].bottom };
|
|
|
|
|
|
|
|
let selectionReversed = false;
|
2013-03-28 13:21:58 -07:00
|
|
|
if (this._cache.start) {
|
2013-03-21 16:50:42 -07:00
|
|
|
// If the end moved past the old end, but we're dragging the start handle, then that handle should become the end handle (and vice versa)
|
2013-03-28 13:21:58 -07:00
|
|
|
selectionReversed = (aIsStartHandle && (end.y > this._cache.end.y || (end.y == this._cache.end.y && end.x > this._cache.end.x))) ||
|
|
|
|
(!aIsStartHandle && (start.y < this._cache.start.y || (start.y == this._cache.start.y && start.x < this._cache.start.x)));
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
|
|
|
|
2013-03-28 13:21:58 -07:00
|
|
|
this._cache.start = start;
|
|
|
|
this._cache.end = end;
|
2013-03-21 16:50:42 -07:00
|
|
|
|
|
|
|
return selectionReversed;
|
|
|
|
},
|
|
|
|
|
2013-08-06 01:38:32 -07:00
|
|
|
_getHandlePositions: function sh_getHandlePositions(scroll) {
|
2013-03-21 16:50:42 -07:00
|
|
|
// the checkHidden function tests to see if the given point is hidden inside an
|
|
|
|
// iframe/subdocument. this is so that if we select some text inside an iframe and
|
|
|
|
// scroll the iframe so the selection is out of view, we hide the handles rather
|
|
|
|
// than having them float on top of the main page content.
|
|
|
|
let checkHidden = function(x, y) {
|
|
|
|
return false;
|
|
|
|
};
|
2013-03-28 13:21:58 -07:00
|
|
|
if (this._contentWindow.frameElement) {
|
|
|
|
let bounds = this._contentWindow.frameElement.getBoundingClientRect();
|
2013-03-21 16:50:42 -07:00
|
|
|
checkHidden = function(x, y) {
|
|
|
|
return x < 0 || y < 0 || x > bounds.width || y > bounds.height;
|
2013-08-06 01:38:32 -07:00
|
|
|
};
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let positions = null;
|
|
|
|
if (this._activeType == this.TYPE_CURSOR) {
|
|
|
|
// The left and top properties returned are relative to the client area
|
|
|
|
// of the window, so we don't need to account for a sub-frame offset.
|
2014-04-25 16:52:14 -07:00
|
|
|
let cursor = this._domWinUtils.sendQueryContentEvent(this._domWinUtils.QUERY_CARET_RECT, this._targetElement.selectionEnd, 0, 0, 0,
|
|
|
|
this._domWinUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
|
2013-07-11 14:58:13 -07:00
|
|
|
// the return value from sendQueryContentEvent is in LayoutDevice pixels and we want CSS pixels, so
|
|
|
|
// divide by the pixel ratio
|
|
|
|
let x = cursor.left / window.devicePixelRatio;
|
|
|
|
let y = (cursor.top + cursor.height) / window.devicePixelRatio;
|
2013-08-06 01:38:32 -07:00
|
|
|
return [{ handle: this.HANDLE_TYPE_MIDDLE,
|
|
|
|
left: x + scroll.X,
|
|
|
|
top: y + scroll.Y,
|
|
|
|
hidden: checkHidden(x, y) }];
|
2013-03-21 16:50:42 -07:00
|
|
|
} else {
|
2013-03-28 13:21:58 -07:00
|
|
|
let sx = this._cache.start.x;
|
|
|
|
let sy = this._cache.start.y;
|
|
|
|
let ex = this._cache.end.x;
|
|
|
|
let ey = this._cache.end.y;
|
2013-03-21 16:50:42 -07:00
|
|
|
|
|
|
|
// Translate coordinates to account for selections in sub-frames. We can't cache
|
|
|
|
// this because the top-level page may have scrolled since selection started.
|
|
|
|
let offset = this._getViewOffset();
|
|
|
|
|
2013-08-06 01:38:32 -07:00
|
|
|
return [{ handle: this.HANDLE_TYPE_START,
|
|
|
|
left: sx + offset.x + scroll.X,
|
|
|
|
top: sy + offset.y + scroll.Y,
|
|
|
|
hidden: checkHidden(sx, sy) },
|
|
|
|
{ handle: this.HANDLE_TYPE_END,
|
|
|
|
left: ex + offset.x + scroll.X,
|
|
|
|
top: ey + offset.y + scroll.Y,
|
|
|
|
hidden: checkHidden(ex, ey) }];
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
2013-08-06 01:38:32 -07:00
|
|
|
},
|
2013-03-21 16:50:42 -07:00
|
|
|
|
2014-04-26 09:46:41 -07:00
|
|
|
// Position handles, but avoid superfluous re-positioning (helps during
|
|
|
|
// "TextSelection:LayerReflow", "scroll" of top-level document, etc).
|
|
|
|
_positionHandlesOnChange: function() {
|
|
|
|
// Helper function to compare position messages
|
|
|
|
let samePositions = function(aPrev, aCurr) {
|
|
|
|
if (aPrev.length != aCurr.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < aPrev.length; i++) {
|
|
|
|
if (aPrev[i].left != aCurr[i].left ||
|
|
|
|
aPrev[i].top != aCurr[i].top ||
|
|
|
|
aPrev[i].hidden != aCurr[i].hidden) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let positions = this._getHandlePositions(this._getScrollPos());
|
|
|
|
if (!samePositions(this._prevHandlePositions, positions)) {
|
|
|
|
this._positionHandles(positions);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Position handles, allow for re-position, in case user drags handle
|
|
|
|
// to invalid position, then releases, we can put it back where it started
|
2013-08-06 01:38:32 -07:00
|
|
|
// positions is an array of objects with data about handle positions,
|
|
|
|
// which we get from _getHandlePositions.
|
|
|
|
_positionHandles: function sh_positionHandles(positions) {
|
|
|
|
if (!positions) {
|
|
|
|
positions = this._getHandlePositions(this._getScrollPos());
|
|
|
|
}
|
2013-03-21 16:50:42 -07:00
|
|
|
sendMessageToJava({
|
|
|
|
type: "TextSelection:PositionHandles",
|
|
|
|
positions: positions,
|
|
|
|
rtl: this._isRTL
|
|
|
|
});
|
2014-04-26 09:46:41 -07:00
|
|
|
this._prevHandlePositions = positions;
|
2014-04-25 21:49:50 -07:00
|
|
|
|
|
|
|
// Text state transitions (text <--> no text) will affect selection context and actionbar display
|
|
|
|
let currTargetElementHasText = (this._targetElement.textLength > 0);
|
|
|
|
if (currTargetElementHasText != this._prevTargetElementHasText) {
|
|
|
|
this._prevTargetElementHasText = currTargetElementHasText;
|
|
|
|
this._updateMenu();
|
|
|
|
}
|
2013-03-21 16:50:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
subdocumentScrolled: function sh_subdocumentScrolled(aElement) {
|
|
|
|
if (this._activeType == this.TYPE_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let scrollView = aElement.ownerDocument.defaultView;
|
2013-03-28 13:21:58 -07:00
|
|
|
let view = this._contentWindow;
|
2013-03-21 16:50:42 -07:00
|
|
|
while (true) {
|
|
|
|
if (view == scrollView) {
|
|
|
|
// The selection is in a view (or sub-view) of the view that scrolled.
|
|
|
|
// So we need to reposition the handles.
|
|
|
|
if (this._activeType == this.TYPE_SELECTION) {
|
2013-03-28 13:22:00 -07:00
|
|
|
this._updateCacheForSelection();
|
2013-03-21 16:50:42 -07:00
|
|
|
}
|
2013-03-28 13:22:00 -07:00
|
|
|
this._positionHandles();
|
2013-03-21 16:50:42 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (view == view.parent) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
view = view.parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|