Bug 864589 - Show/hide text selection handles if a selection is programatically added/removed, r=margaret, ehsan

This commit is contained in:
Mark Capella 2013-08-16 21:51:41 -04:00
parent d508e3a958
commit 0d5545ac39
3 changed files with 24 additions and 1 deletions

View File

@ -8,7 +8,7 @@
interface nsIDOMDocument;
interface nsISelection;
[scriptable, uuid(A6CF90E2-15B3-11d2-932E-00805F8ADD32)]
[scriptable, uuid(280cd784-23c2-468d-8624-354e0b3804bd)]
interface nsISelectionListener : nsISupports
{
const short NO_REASON=0;
@ -17,6 +17,8 @@ interface nsISelectionListener : nsISupports
const short MOUSEUP_REASON=4;/*bitflags*/
const short KEYPRESS_REASON=8;/*bitflags*/
const short SELECTALL_REASON=16;
const short COLLAPSETOSTART_REASON=32;
const short COLLAPSETOEND_REASON=64;
void notifySelectionChanged(in nsIDOMDocument doc, in nsISelection sel, in short reason);
};

View File

@ -4444,6 +4444,10 @@ Selection::CollapseToStart()
if (!firstRange)
return NS_ERROR_FAILURE;
if (mFrameSelection) {
int16_t reason = mFrameSelection->PopReason() | nsISelectionListener::COLLAPSETOSTART_REASON;
mFrameSelection->PostReason(reason);
}
return Collapse(firstRange->GetStartParent(), firstRange->StartOffset());
}
@ -4464,6 +4468,10 @@ Selection::CollapseToEnd()
if (!lastRange)
return NS_ERROR_FAILURE;
if (mFrameSelection) {
int16_t reason = mFrameSelection->PopReason() | nsISelectionListener::COLLAPSETOEND_REASON;
mFrameSelection->PostReason(reason);
}
return Collapse(lastRange->GetEndParent(), lastRange->EndOffset());
}

View File

@ -174,6 +174,14 @@ var SelectionHandler = {
};
},
notifySelectionChanged: function sh_notifySelectionChanged(aDocument, aSelection, aReason) {
// 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();
}
},
/*
* Called from browser.js when the user long taps on text or chooses
* the "Select Word" context menu item. Initializes SelectionHandler,
@ -203,6 +211,9 @@ var SelectionHandler = {
return;
}
// Add a listener to end the selection if it's removed programatically
selection.QueryInterface(Ci.nsISelectionPrivate).addSelectionListener(this);
// Initialize the cache
this._cache = { start: {}, end: {}};
this._updateCacheForSelection();
@ -463,6 +474,8 @@ var SelectionHandler = {
if (this._activeType == this.TYPE_SELECTION) {
let selection = this._getSelection();
if (selection) {
// Remove our listener before we removeAllRanges()
selection.QueryInterface(Ci.nsISelectionPrivate).removeSelectionListener(this);
selection.removeAllRanges();
}
}