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 380219 707cdb4c9d7c07b1bc232cd07ecd1c53a8310666
parent 380218 d91d6527a822d62a6abb24904b3f23a606ef39e2
child 380220 ffb04ea86fa925b12c7f89b8fb26bbc50dfbe1b7
push id21167
push usermasayuki@d-toybox.com
push dateTue, 21 Jun 2016 06:55:55 +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
@@ -1228,47 +1228,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 {