Bug 1179090 ContentEventHandler should assume that there is selection at beginning of the document when there is no selection range r=smaug
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 03 Jul 2015 13:20:28 +0900
changeset 251299 20245e58fd57d704c756c36fdc45b49da05cf9f8
parent 251298 e9901be6c59c25f51a31a101017966ec7b745104
child 251300 117d439135fbe865b646920e6bcfbb3db8566229
push id28991
push usercbook@mozilla.com
push dateFri, 03 Jul 2015 10:08:14 +0000
treeherdermozilla-central@b6a79816ee71 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1179090
milestone42.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 1179090 ContentEventHandler should assume that there is selection at beginning of the document when there is no selection range r=smaug
dom/events/ContentEventHandler.cpp
--- a/dom/events/ContentEventHandler.cpp
+++ b/dom/events/ContentEventHandler.cpp
@@ -77,22 +77,50 @@ ContentEventHandler::InitCommon()
   nsCOMPtr<nsISelection> sel;
   nsCopySupport::GetSelectionForCopy(mPresShell->GetDocument(),
                                      getter_AddRefs(sel));
   mSelection = static_cast<Selection*>(sel.get());
   if (NS_WARN_IF(!mSelection)) {
     return NS_ERROR_NOT_AVAILABLE;
   }
 
-  // This shell doesn't support selection.
-  if (NS_WARN_IF(!mSelection->RangeCount())) {
-    return NS_ERROR_NOT_AVAILABLE;
+  if (!mSelection->RangeCount()) {
+    // If there is no selection range, we should compute the selection root
+    // from ancestor limiter or root content of the document.
+    rv = mSelection->GetAncestorLimiter(getter_AddRefs(mRootContent));
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return NS_ERROR_FAILURE;
+    }
+    if (!mRootContent) {
+      mRootContent = mPresShell->GetDocument()->GetRootElement();
+      if (NS_WARN_IF(!mRootContent)) {
+        return NS_ERROR_NOT_AVAILABLE;
+      }
+    }
+
+    // Assume that there is selection at beginning of the root content.
+    rv = nsRange::CreateRange(mRootContent, 0, mRootContent, 0,
+                              getter_AddRefs(mFirstSelectedRange));
+    if (NS_WARN_IF(NS_FAILED(rv)) || NS_WARN_IF(!mFirstSelectedRange)) {
+      return NS_ERROR_UNEXPECTED;
+    }
+    return NS_OK;
   }
+
   mFirstSelectedRange = mSelection->GetRangeAt(0);
+  if (NS_WARN_IF(!mFirstSelectedRange)) {
+    return NS_ERROR_UNEXPECTED;
+  }
 
+  // If there is a selection, we should retrieve the selection root from
+  // the range since when the window is inactivated, the ancestor limiter
+  // of mSelection was cleared by blur event handler of nsEditor but the
+  // selection range still keeps storing the nodes.  If the active element of
+  // the deactive window is <input> or <textarea>, we can compute the selection
+  // root from them.
   nsINode* startNode = mFirstSelectedRange->GetStartParent();
   NS_ENSURE_TRUE(startNode, NS_ERROR_FAILURE);
   nsINode* endNode = mFirstSelectedRange->GetEndParent();
   NS_ENSURE_TRUE(endNode, NS_ERROR_FAILURE);
 
   // See bug 537041 comment 5, the range could have removed node.
   NS_ENSURE_TRUE(startNode->GetCurrentDoc() == mPresShell->GetDocument(),
                  NS_ERROR_NOT_AVAILABLE);