Bug 1391978 - Part 6. Replace nsISelection::Extend with Selection::Extend. r?masayuki draft
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Mon, 28 Aug 2017 17:54:34 +0900
changeset 654197 b217dd1d506229d848126b9c2494d1b4f9dc3a35
parent 654196 3e57ddbebc7985ec0e5ada4b1e83631d6f1c83b5
child 654198 3171ed5808d9045b268cc76989c7d06ea220d393
push id76495
push userbmo:m_kato@ga2.so-net.ne.jp
push dateMon, 28 Aug 2017 10:39:27 +0000
reviewersmasayuki
bugs1391978
milestone57.0a1
Bug 1391978 - Part 6. Replace nsISelection::Extend with Selection::Extend. r?masayuki Except to HTMLEditRules::NormalizeSelection, I replace nsISelection::Extend with Selection::Extend. MozReview-Commit-ID: H83zpvAo5Xa *** P6 MozReview-Commit-ID: 5E9lRnc9uB3
editor/libeditor/HTMLEditor.cpp
editor/libeditor/TextEditor.cpp
editor/libeditor/TextEditorTest.cpp
editor/txtsvc/nsTextServicesDocument.cpp
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -1695,27 +1695,28 @@ HTMLEditor::SelectElement(nsIDOMElement*
 
   // Must be sure that element is contained in the document body
   if (!IsDescendantOfEditorRoot(element)) {
     return NS_ERROR_NULL_POINTER;
   }
 
   RefPtr<Selection> selection = GetSelection();
   NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
-  nsCOMPtr<nsIDOMNode>parent;
-  nsresult rv = aElement->GetParentNode(getter_AddRefs(parent));
-  if (NS_SUCCEEDED(rv) && parent) {
-    int32_t offsetInParent = GetChildOffset(aElement, parent);
-
-    // Collapse selection to just before desired element,
-    rv = selection->Collapse(parent, offsetInParent);
-    if (NS_SUCCEEDED(rv)) {
-      // then extend it to just after
-      rv = selection->Extend(parent, offsetInParent + 1);
-    }
+  nsINode* parent = element->GetParentNode();
+  if (NS_WARN_IF(!parent)) {
+    return NS_ERROR_FAILURE;
+  }
+
+  int32_t offsetInParent = parent->IndexOf(element);
+
+  // Collapse selection to just before desired element,
+  nsresult rv = selection->Collapse(parent, offsetInParent);
+  if (NS_SUCCEEDED(rv)) {
+    // then extend it to just after
+    rv = selection->Extend(parent, offsetInParent + 1);
   }
   return rv;
 }
 
 NS_IMETHODIMP
 HTMLEditor::SetCaretAfterElement(nsIDOMElement* aElement)
 {
   nsCOMPtr<Element> element = do_QueryInterface(aElement);
--- a/editor/libeditor/TextEditor.cpp
+++ b/editor/libeditor/TextEditor.cpp
@@ -1635,25 +1635,25 @@ TextEditor::SelectEntireDocument(Selecti
   }
 
   SelectionBatcher selectionBatcher(aSelection);
   nsresult rv = EditorBase::SelectEntireDocument(aSelection);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Don't select the trailing BR node if we have one
   int32_t selOffset;
-  nsCOMPtr<nsIDOMNode> selNode;
+  nsCOMPtr<nsINode> selNode;
   rv = GetEndNodeAndOffset(aSelection, getter_AddRefs(selNode), &selOffset);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIDOMNode> childNode = GetChildAt(selNode, selOffset - 1);
+  nsINode* childNode = selNode->GetChildAt(selOffset - 1);
 
   if (childNode && TextEditUtils::IsMozBR(childNode)) {
     int32_t parentOffset;
-    nsCOMPtr<nsIDOMNode> parentNode = GetNodeLocation(childNode, &parentOffset);
+    nsINode* parentNode = GetNodeLocation(childNode, &parentOffset);
 
     return aSelection->Extend(parentNode, parentOffset);
   }
 
   return NS_OK;
 }
 
 already_AddRefed<EventTarget>
--- a/editor/libeditor/TextEditorTest.cpp
+++ b/editor/libeditor/TextEditorTest.cpp
@@ -139,33 +139,31 @@ nsresult TextEditorTest::TestTextPropert
   // XXX This is broken, text nodes are not elements.
   nsAutoString textTag(NS_LITERAL_STRING("#text"));
   rv = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList));
   TEST_RESULT(rv);
   TEST_POINTER(nodeList.get());
   uint32_t count;
   nodeList->GetLength(&count);
   NS_ASSERTION(0!=count, "there are no text nodes in the document!");
-  nsCOMPtr<nsIDOMNode>textNode;
-  rv = nodeList->Item(count - 1, getter_AddRefs(textNode));
+  nsCOMPtr<nsIDOMNode> domTextNode;
+  rv = nodeList->Item(count - 1, getter_AddRefs(domTextNode));
   TEST_RESULT(rv);
-  TEST_POINTER(textNode.get());
+  TEST_POINTER(domTextNode.get());
 
   // set the whole text node to bold
   printf("set the whole first text node to bold\n");
   nsCOMPtr<nsISelection>selection;
   rv = mEditor->GetSelection(getter_AddRefs(selection));
   TEST_RESULT(rv);
   TEST_POINTER(selection.get());
-  nsCOMPtr<nsIDOMCharacterData>textData;
-  textData = do_QueryInterface(textNode);
-  uint32_t length;
-  textData->GetLength(&length);
-  selection->Collapse(textNode, 0);
-  selection->Extend(textNode, length);
+  nsCOMPtr<nsINode> textNode = do_QueryInterface(domTextNode);
+  uint32_t length = textNode->Length();
+  selection->AsSelection()->Collapse(textNode, 0);
+  selection->AsSelection()->Extend(textNode, length);
 
   nsCOMPtr<nsIHTMLEditor> htmlEditor (do_QueryInterface(mTextEditor));
   NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
 
   bool any = false;
   bool all = false;
   bool first=false;
 
@@ -196,18 +194,18 @@ nsresult TextEditorTest::TestTextPropert
   TEST_RESULT(rv);
   NS_ASSERTION(false==first, "first should be false");
   NS_ASSERTION(false==any, "any should be false");
   NS_ASSERTION(false==all, "all should be false");
   mEditor->DebugDumpContent();
 
   // set all but the first and last character to bold
   printf("set the first text node (1, length-1) to bold and italic, and (2, length-1) to underline.\n");
-  selection->Collapse(textNode, 1);
-  selection->Extend(textNode, length-1);
+  selection->AsSelection()->Collapse(textNode, 1);
+  selection->AsSelection()->Extend(textNode, length-1);
   rv = htmlEditor->SetInlineProperty(nsGkAtoms::b, empty, empty);
   TEST_RESULT(rv);
   rv = htmlEditor->GetInlineProperty(nsGkAtoms::b, empty, empty, &first,
                                      &any, &all);
   TEST_RESULT(rv);
   NS_ASSERTION(true==first, "first should be true");
   NS_ASSERTION(true==any, "any should be true");
   NS_ASSERTION(true==all, "all should be true");
@@ -230,24 +228,24 @@ nsresult TextEditorTest::TestTextPropert
   mEditor->DebugDumpContent();
 
   // make all the text underlined, except for the first 2 and last 2 characters
   rv = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList));
   TEST_RESULT(rv);
   TEST_POINTER(nodeList.get());
   nodeList->GetLength(&count);
   NS_ASSERTION(0!=count, "there are no text nodes in the document!");
-  rv = nodeList->Item(count-2, getter_AddRefs(textNode));
+  rv = nodeList->Item(count-2, getter_AddRefs(domTextNode));
   TEST_RESULT(rv);
   TEST_POINTER(textNode.get());
-  textData = do_QueryInterface(textNode);
-  textData->GetLength(&length);
+  textNode = do_QueryInterface(domTextNode);
+  length = textNode->Length();
   NS_ASSERTION(length==915, "wrong text node");
-  selection->Collapse(textNode, 1);
-  selection->Extend(textNode, length-2);
+  selection->AsSelection()->Collapse(textNode, 1);
+  selection->AsSelection()->Extend(textNode, length-2);
   rv = htmlEditor->SetInlineProperty(nsGkAtoms::u, empty, empty);
   TEST_RESULT(rv);
   rv = htmlEditor->GetInlineProperty(nsGkAtoms::u, empty, empty, &first,
                                      &any, &all);
   TEST_RESULT(rv);
   NS_ASSERTION(true==first, "first should be true");
   NS_ASSERTION(true==any, "any should be true");
   NS_ASSERTION(true==all, "all should be true");
--- a/editor/txtsvc/nsTextServicesDocument.cpp
+++ b/editor/txtsvc/nsTextServicesDocument.cpp
@@ -2146,33 +2146,33 @@ nsTextServicesDocument::IsTextNode(nsIDO
   return IsTextNode(content);
 }
 
 nsresult
 nsTextServicesDocument::SetSelectionInternal(int32_t aOffset, int32_t aLength, bool aDoUpdate)
 {
   NS_ENSURE_TRUE(mSelCon && aOffset >= 0 && aLength >= 0, NS_ERROR_FAILURE);
 
-  nsIDOMNode *sNode = 0, *eNode = 0;
-  int32_t sOffset = 0, eOffset = 0;
+  nsCOMPtr<nsINode> startNode;
+  int32_t startNodeOffset = 0;
   OffsetEntry *entry;
 
   // Find start of selection in node offset terms:
 
-  for (size_t i = 0; !sNode && i < mOffsetTable.Length(); i++) {
+  for (size_t i = 0; !startNode && i < mOffsetTable.Length(); i++) {
     entry = mOffsetTable[i];
     if (entry->mIsValid) {
       if (entry->mIsInsertedText) {
         // Caret can only be placed at the end of an
         // inserted text offset entry, if the offsets
         // match exactly!
 
         if (entry->mStrOffset == aOffset) {
-          sNode   = entry->mNode;
-          sOffset = entry->mNodeOffset + entry->mLength;
+          startNode = do_QueryInterface(entry->mNode);
+          startNodeOffset = entry->mNodeOffset + entry->mLength;
         }
       } else if (aOffset >= entry->mStrOffset) {
         bool foundEntry = false;
         int32_t strEndOffset = entry->mStrOffset + entry->mLength;
 
         if (aOffset < strEndOffset) {
           foundEntry = true;
         } else if (aOffset == strEndOffset) {
@@ -2188,43 +2188,43 @@ nsTextServicesDocument::SetSelectionInte
               // Next offset entry isn't an exact match, so we'll
               // just use the current entry.
               foundEntry = true;
             }
           }
         }
 
         if (foundEntry) {
-          sNode   = entry->mNode;
-          sOffset = entry->mNodeOffset + aOffset - entry->mStrOffset;
+          startNode = do_QueryInterface(entry->mNode);
+          startNodeOffset = entry->mNodeOffset + aOffset - entry->mStrOffset;
         }
       }
 
-      if (sNode) {
+      if (startNode) {
         mSelStartIndex = static_cast<int32_t>(i);
         mSelStartOffset = aOffset;
       }
     }
   }
 
-  NS_ENSURE_TRUE(sNode, NS_ERROR_FAILURE);
+  NS_ENSURE_TRUE(startNode, NS_ERROR_FAILURE);
 
   // XXX: If we ever get a SetSelection() method in nsIEditor, we should
   //      use it.
 
   nsCOMPtr<nsISelection> selection;
 
   if (aDoUpdate) {
     nsresult rv =
       mSelCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
                             getter_AddRefs(selection));
 
     NS_ENSURE_SUCCESS(rv, rv);
 
-    rv = selection->Collapse(sNode, sOffset);
+    rv = selection->AsSelection()->Collapse(startNode, startNodeOffset);
 
     NS_ENSURE_SUCCESS(rv, rv);
    }
 
   if (aLength <= 0) {
     // We have a collapsed selection. (Caret)
 
     mSelEndIndex  = mSelStartIndex;
@@ -2233,44 +2233,46 @@ nsTextServicesDocument::SetSelectionInte
    //**** KDEBUG ****
    // printf("\n* Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
    //**** KDEBUG ****
 
     return NS_OK;
   }
 
   // Find the end of the selection in node offset terms:
+  nsCOMPtr<nsINode> endNode;
+  int32_t endNodeOffset = 0;
   int32_t endOffset = aOffset + aLength;
-  for (int32_t i = mOffsetTable.Length() - 1; !eNode && i >= 0; i--) {
+  for (int32_t i = mOffsetTable.Length() - 1; !endNode && i >= 0; i--) {
     entry = mOffsetTable[i];
 
     if (entry->mIsValid) {
       if (entry->mIsInsertedText) {
-        if (entry->mStrOffset == eOffset) {
+        if (entry->mStrOffset == endNodeOffset) {
           // If the selection ends on an inserted text offset entry,
           // the selection includes the entire entry!
 
-          eNode   = entry->mNode;
-          eOffset = entry->mNodeOffset + entry->mLength;
+          endNode = do_QueryInterface(entry->mNode);
+          endNodeOffset = entry->mNodeOffset + entry->mLength;
         }
       } else if (endOffset >= entry->mStrOffset &&
                  endOffset <= entry->mStrOffset + entry->mLength) {
-        eNode   = entry->mNode;
-        eOffset = entry->mNodeOffset + endOffset - entry->mStrOffset;
+        endNode = do_QueryInterface(entry->mNode);
+        endNodeOffset = entry->mNodeOffset + endOffset - entry->mStrOffset;
       }
 
-      if (eNode) {
+      if (endNode) {
         mSelEndIndex = i;
         mSelEndOffset = endOffset;
       }
     }
   }
 
-  if (aDoUpdate && eNode) {
-    nsresult rv = selection->Extend(eNode, eOffset);
+  if (aDoUpdate && endNode) {
+    nsresult rv = selection->AsSelection()->Extend(endNode, endNodeOffset);
 
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   //**** KDEBUG ****
   // printf("\n * Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
   //**** KDEBUG ****