Bug 1275914 part.6 ContentEventHandler::OnQuerySelectedText() shouldn't refer anchor and focus of selection if there are 2 or more selection ranges r?smaug draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Sat, 11 Jun 2016 22:22:10 +0900
changeset 377867 1f336d20063937e217527e2c82f5a02b66a07871
parent 377866 0b1d5b19047780a17f878323f6c19416b73821b0
child 377868 5ae8107545004d2215b66f40b0d744dd4af680d4
push id20876
push usermasayuki@d-toybox.com
push dateMon, 13 Jun 2016 08:27:53 +0000
reviewerssmaug
bugs1275914
milestone50.0a1
Bug 1275914 part.6 ContentEventHandler::OnQuerySelectedText() shouldn't refer anchor and focus of selection if there are 2 or more selection ranges r?smaug Selection's focus and anchor node and offset are stored only for the last range. However, ContentEventHandler needs its first range. Therefore, ContentEventHandler shouldn't refer them if there are two or more selection ranges. MozReview-Commit-ID: ACflFE3ZrOM
dom/events/ContentEventHandler.cpp
--- a/dom/events/ContentEventHandler.cpp
+++ b/dom/events/ContentEventHandler.cpp
@@ -1218,47 +1218,43 @@ ContentEventHandler::OnQuerySelectedText
   LineBreakType lineBreakType = GetLineBreakType(aEvent);
   rv = GetFlatTextLengthBefore(mFirstSelectedRange,
                                &aEvent->mReply.mOffset, lineBreakType);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsINode> anchorNode, focusNode;
   int32_t anchorOffset, focusOffset;
   if (mSelection->RangeCount()) {
-    anchorNode = mSelection->GetAnchorNode();
-    focusNode = mSelection->GetFocusNode();
-    if (NS_WARN_IF(!anchorNode) || NS_WARN_IF(!focusNode)) {
-      return NS_ERROR_FAILURE;
+    // If there is only one selection range, the anchor/focus node and offset
+    // are the information of the range.  Therefore, we have the direction
+    // information.
+    if (mSelection->RangeCount() == 1) {
+      anchorNode = mSelection->GetAnchorNode();
+      focusNode = mSelection->GetFocusNode();
+      if (NS_WARN_IF(!anchorNode) || NS_WARN_IF(!focusNode)) {
+        return NS_ERROR_FAILURE;
+      }
+      anchorOffset = static_cast<int32_t>(mSelection->AnchorOffset());
+      focusOffset = static_cast<int32_t>(mSelection->FocusOffset());
+      if (NS_WARN_IF(anchorOffset < 0) || NS_WARN_IF(focusOffset < 0)) {
+        return NS_ERROR_FAILURE;
+      }
+
+      int16_t compare = nsContentUtils::ComparePoints(anchorNode, anchorOffset,
+                                                      focusNode, focusOffset);
+      aEvent->mReply.mReversed = compare > 0;
     }
-    anchorOffset = static_cast<int32_t>(mSelection->AnchorOffset());
-    focusOffset = static_cast<int32_t>(mSelection->FocusOffset());
-    if (NS_WARN_IF(anchorOffset < 0) || NS_WARN_IF(focusOffset < 0)) {
-      return NS_ERROR_FAILURE;
+    // However, if there are 2 or more selection ranges, we have no information
+    // of that.
+    else {
+      aEvent->mReply.mReversed = false;
     }
 
-    int16_t compare = nsContentUtils::ComparePoints(anchorNode, anchorOffset,
-                                                    focusNode, focusOffset);
-    aEvent->mReply.mReversed = compare > 0;
-
-    if (compare) {
-      RefPtr<nsRange> range;
-      if (mSelection->RangeCount() == 1) {
-        range = mFirstSelectedRange;
-      } else {
-        rv = nsRange::CreateRange(anchorNode, anchorOffset,
-                                  focusNode, focusOffset,
-                                  getter_AddRefs(range));
-        if (NS_WARN_IF(NS_FAILED(rv))) {
-          return rv;
-        }
-        if (NS_WARN_IF(!range)) {
-          return NS_ERROR_FAILURE;
-        }
-      }
-      rv = GenerateFlatTextContent(range, aEvent->mReply.mString,
+    if (!mFirstSelectedRange->Collapsed()) {
+      rv = GenerateFlatTextContent(mFirstSelectedRange, aEvent->mReply.mString,
                                    lineBreakType);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     } else {
       aEvent->mReply.mString.Truncate();
     }
   } else {