diff --git a/dom/base/Selection.cpp b/dom/base/Selection.cpp index 96cfc8247e1d..1032523d6c4d 100644 --- a/dom/base/Selection.cpp +++ b/dom/base/Selection.cpp @@ -525,7 +525,8 @@ nsresult Selection::MaybeAddTableCellRange(nsRange& aRange, bool* aDidAddRange, } *aDidAddRange = true; - return AddRangesForSelectableNodes(&aRange, aOutIndex); + return AddRangesForSelectableNodes(&aRange, aOutIndex, + DispatchSelectstartEvent::Maybe); } Selection::Selection(SelectionType aSelectionType, @@ -805,9 +806,9 @@ static void UserSelectRangesToAdd(nsRange* aItem, } } -nsresult Selection::AddRangesForSelectableNodes(nsRange* aRange, - int32_t* aOutIndex, - bool aNoStartSelect) { +nsresult Selection::AddRangesForSelectableNodes( + nsRange* aRange, int32_t* aOutIndex, + const DispatchSelectstartEvent aDispatchSelectstartEvent) { if (!aRange) { return NS_ERROR_NULL_POINTER; } @@ -827,9 +828,9 @@ nsresult Selection::AddRangesForSelectableNodes(nsRange* aRange, StaticPrefs::dom_select_events_enabled() || (doc && doc->NodePrincipal()->IsSystemPrincipal()); - if (!aNoStartSelect && mSelectionType == SelectionType::eNormal && - selectEventsEnabled && IsCollapsed() && - !IsBlockingSelectionChangeEvents()) { + if (aDispatchSelectstartEvent == DispatchSelectstartEvent::Maybe && + mSelectionType == SelectionType::eNormal && selectEventsEnabled && + IsCollapsed() && !IsBlockingSelectionChangeEvents()) { // First, we generate the ranges to add with a scratch range, which is a // clone of the original range passed in. We do this seperately, because // the selectstart event could have caused the world to change, and @@ -1887,7 +1888,8 @@ void Selection::AddRangeAndSelectFramesAndNotifyListeners(nsRange& aRange, } if (!didAddRange) { - result = AddRangesForSelectableNodes(range, &rangeIndex); + result = AddRangesForSelectableNodes(range, &rangeIndex, + DispatchSelectstartEvent::Maybe); if (NS_FAILED(result)) { aRv.Throw(result); return; @@ -2110,7 +2112,8 @@ void Selection::Collapse(const RawRangeBoundary& aPoint, ErrorResult& aRv) { #endif int32_t rangeIndex = -1; - result = AddRangesForSelectableNodes(range, &rangeIndex); + result = AddRangesForSelectableNodes(range, &rangeIndex, + DispatchSelectstartEvent::Maybe); if (NS_FAILED(result)) { aRv.Throw(result); return; @@ -2228,13 +2231,16 @@ utility function nsresult Selection::SetAnchorFocusToRange(nsRange* aRange) { NS_ENSURE_STATE(mAnchorFocusRange); - bool collapsed = IsCollapsed(); + const DispatchSelectstartEvent dispatchSelectstartEvent = + IsCollapsed() ? DispatchSelectstartEvent::Maybe + : DispatchSelectstartEvent::No; nsresult res = RemoveRangeInternal(*mAnchorFocusRange); if (NS_FAILED(res)) return res; int32_t aOutIndex = -1; - res = AddRangesForSelectableNodes(aRange, &aOutIndex, !collapsed); + res = + AddRangesForSelectableNodes(aRange, &aOutIndex, dispatchSelectstartEvent); if (NS_FAILED(res)) return res; SetAnchorFocusRange(aOutIndex); diff --git a/dom/base/Selection.h b/dom/base/Selection.h index 15607cea81eb..3c9ead692786 100644 --- a/dom/base/Selection.h +++ b/dom/base/Selection.h @@ -152,6 +152,14 @@ class Selection final : public nsSupportsWeakReference, nsTArray* aOutput); private: + /** + * https://w3c.github.io/selection-api/#selectstart-event. + */ + enum class DispatchSelectstartEvent { + No, + Maybe, + }; + /** * Adds aRange to this Selection. If mUserInitiated is true, * then aRange is first scanned for -moz-user-select:none nodes and split up @@ -163,8 +171,9 @@ class Selection final : public nsSupportsWeakReference, * containing it. -1 if mRanges was empty and no range was * added. */ - nsresult AddRangesForSelectableNodes(nsRange* aRange, int32_t* aOutIndex, - bool aNoStartSelect = false); + nsresult AddRangesForSelectableNodes( + nsRange* aRange, int32_t* aOutIndex, + DispatchSelectstartEvent aDispatchSelectstartEvent); /** * Doesn't remove `aRange` from `mAnchorFocusRange`.