Bug 1623858: part 12) Factor determining "selectstart" event target out. r=smaug
authorMirko Brodesser <mbrodesser@mozilla.com>
Wed, 25 Mar 2020 15:39:37 +0000
changeset 520389 3067255d5eb23423cd47094abeece614b48e6a13
parent 520388 e4ca6d27aa78da0608970d617f2115341037397e
child 520390 0c829e76b0c663f59f19f5b44699ffb6c7ad4dd1
push id37249
push userdvarga@mozilla.com
push dateWed, 25 Mar 2020 21:39:06 +0000
treeherdermozilla-central@b3c3f7d0f044 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1623858
milestone76.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1623858: part 12) Factor determining "selectstart" event target out. r=smaug Depends on D68008 Differential Revision: https://phabricator.services.mozilla.com/D68009
dom/base/Selection.cpp
--- a/dom/base/Selection.cpp
+++ b/dom/base/Selection.cpp
@@ -801,16 +801,34 @@ static void UserSelectRangesToAdd(nsRang
     // Don't mess with the selection ranges for editing, editor doesn't really
     // deal well with multi-range selections.
     aRangesToAdd.AppendElement(aItem);
   } else {
     aItem->ExcludeNonSelectableNodes(&aRangesToAdd);
   }
 }
 
+static nsINode* DetermineSelectstartEventTarget(
+    const bool aSelectionEventsOnTextControlsEnabled, const nsRange& aRange) {
+  nsINode* target = aRange.GetStartContainer();
+  if (aSelectionEventsOnTextControlsEnabled) {
+    // Get the first element which isn't in a native anonymous subtree
+    while (target && target->IsInNativeAnonymousSubtree()) {
+      target = target->GetParent();
+    }
+  } else {
+    if (target->IsInNativeAnonymousSubtree()) {
+      // This is a selection under a text control, so don't dispatch the
+      // event.
+      target = nullptr;
+    }
+  }
+  return target;
+}
+
 nsresult Selection::AddRangesForUserSelectableNodes(
     nsRange* aRange, int32_t* aOutIndex,
     const DispatchSelectstartEvent aDispatchSelectstartEvent) {
   MOZ_ASSERT(mUserInitiated);
   MOZ_ASSERT(aOutIndex);
 
   if (!aRange) {
     return NS_ERROR_NULL_POINTER;
@@ -837,44 +855,34 @@ nsresult Selection::AddRangesForUserSele
     RefPtr<nsRange> scratchRange = aRange->CloneRange();
     UserSelectRangesToAdd(scratchRange, rangesToAdd);
     bool newRangesNonEmpty =
         rangesToAdd.Length() > 1 ||
         (rangesToAdd.Length() == 1 && !rangesToAdd[0]->Collapsed());
 
     MOZ_ASSERT(!newRangesNonEmpty || nsContentUtils::IsSafeToRunScript());
     if (newRangesNonEmpty && nsContentUtils::IsSafeToRunScript()) {
-      // We consider a selection to be starting if we are currently collapsed,
-      // and the selection is becoming uncollapsed, and this is caused by a
-      // user initiated event.
-      bool defaultAction = true;
-
       // The spec currently doesn't say that we should dispatch this event
       // on text controls, so for now we only support doing that under a
       // pref, disabled by default.
       // See https://github.com/w3c/selection-api/issues/53.
-      bool dispatchEvent = true;
-      nsCOMPtr<nsINode> target = aRange->GetStartContainer();
-      if (nsFrameSelection::sSelectionEventsOnTextControlsEnabled) {
-        // Get the first element which isn't in a native anonymous subtree
-        while (target && target->IsInNativeAnonymousSubtree()) {
-          target = target->GetParent();
-        }
-      } else {
-        if (target->IsInNativeAnonymousSubtree()) {
-          // This is a selection under a text control, so don't dispatch the
-          // event.
-          dispatchEvent = false;
-        }
-      }
-
-      if (dispatchEvent) {
+      nsCOMPtr<nsINode> selectstartEventTarget =
+          DetermineSelectstartEventTarget(
+              nsFrameSelection::sSelectionEventsOnTextControlsEnabled, *aRange);
+
+      if (selectstartEventTarget) {
+        // We consider a selection to be starting if we are currently collapsed,
+        // and the selection is becoming uncollapsed, and this is caused by a
+        // user initiated event.
+        bool defaultAction = true;
+
         nsContentUtils::DispatchTrustedEvent(
-            GetDocument(), target, NS_LITERAL_STRING("selectstart"),
-            CanBubble::eYes, Cancelable::eYes, &defaultAction);
+            GetDocument(), selectstartEventTarget,
+            NS_LITERAL_STRING("selectstart"), CanBubble::eYes, Cancelable::eYes,
+            &defaultAction);
 
         if (!defaultAction) {
           return NS_OK;
         }
 
         // As we just dispatched an event to the DOM, something could have
         // changed under our feet. Re-generate the rangesToAdd array, and
         // ensure that the range we are about to add is still valid.