Bug 1391978 - Part 9. Replace nsISelection::GetAnchorNode with Selection::GetAnchorNode. r?masayuki draft
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Fri, 25 Aug 2017 16:12:39 +0900
changeset 654200 a739151e75d8920dd76b9729fd0d698f574218ed
parent 654199 7d3d869f21669486e9dd3bb925451e5bd20fa3b0
child 728493 44c4f65b6e067419295a1b64aee198547b26e239
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 9. Replace nsISelection::GetAnchorNode with Selection::GetAnchorNode. r?masayuki MozReview-Commit-ID: CiIClvsiNxX
editor/libeditor/HTMLEditor.cpp
editor/libeditor/HTMLTableEditor.cpp
editor/libeditor/TextEditorTest.cpp
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -1578,42 +1578,43 @@ HTMLEditor::InsertElementAtSelection(nsI
       // For all other tags, we insert AFTER the selection
       if (HTMLEditUtils::IsNamedAnchor(node)) {
         selection->CollapseToStart();
       } else {
         selection->CollapseToEnd();
       }
     }
 
-    nsCOMPtr<nsIDOMNode> parentSelectedNode;
-    rv = selection->GetAnchorNode(getter_AddRefs(parentSelectedNode));
+    nsINode* parentSelectedNode = selection->GetAnchorNode();
     if (parentSelectedNode) {
       int32_t offsetForInsert = selection->AnchorOffset();
       // Adjust position based on the node we are going to insert.
-      NormalizeEOLInsertPosition(element, address_of(parentSelectedNode),
+      nsCOMPtr<nsIDOMNode> parentSelectedDOMNode =
+        GetAsDOMNode(parentSelectedNode);
+      NormalizeEOLInsertPosition(element, address_of(parentSelectedDOMNode),
                                  &offsetForInsert);
 
-      rv = InsertNodeAtPoint(node, address_of(parentSelectedNode),
+      rv = InsertNodeAtPoint(node, address_of(parentSelectedDOMNode),
                              &offsetForInsert, false);
       NS_ENSURE_SUCCESS(rv, rv);
       // Set caret after element, but check for special case
       //  of inserting table-related elements: set in first cell instead
       if (!SetCaretInTableCell(aElement)) {
         rv = SetCaretAfterElement(aElement);
         NS_ENSURE_SUCCESS(rv, rv);
       }
       // check for inserting a whole table at the end of a block. If so insert
       // a br after it.
       if (HTMLEditUtils::IsTable(node)) {
         if (IsLastEditableChild(element)) {
           nsCOMPtr<nsIDOMNode> brNode;
-          rv = CreateBR(parentSelectedNode, offsetForInsert + 1,
+          rv = CreateBR(parentSelectedDOMNode, offsetForInsert + 1,
                         address_of(brNode));
           NS_ENSURE_SUCCESS(rv, rv);
-          selection->Collapse(parentSelectedNode, offsetForInsert+1);
+          selection->Collapse(parentSelectedDOMNode, offsetForInsert+1);
         }
       }
     }
   }
   rv = rules->DidDoAction(selection, &ruleInfo, rv);
   return rv;
 }
 
@@ -2456,35 +2457,33 @@ HTMLEditor::GetSelectedElement(const nsA
     }
   }
 
   if (!bNodeFound) {
     if (isLinkTag) {
       // Link tag is a special case - we return the anchor node
       //  found for any selection that is totally within a link,
       //  included a collapsed selection (just a caret in a link)
-      nsCOMPtr<nsIDOMNode> anchorNode;
-      rv = selection->GetAnchorNode(getter_AddRefs(anchorNode));
-      NS_ENSURE_SUCCESS(rv, rv);
+      nsCOMPtr<nsINode> anchorNode = selection->GetAnchorNode();
       int32_t anchorOffset = -1;
       if (anchorNode) {
         anchorOffset = selection->AnchorOffset();
       }
 
       nsCOMPtr<nsINode> focusNode = selection->GetFocusNode();
       int32_t focusOffset = -1;
       if (focusNode) {
         focusOffset = selection->FocusOffset();
       }
 
       // Link node must be the same for both ends of selection
       if (anchorNode) {
         nsCOMPtr<nsIDOMElement> parentLinkOfAnchor;
         rv = GetElementOrParentByTagName(NS_LITERAL_STRING("href"),
-                                         anchorNode,
+                                         GetAsDOMNode(anchorNode),
                                          getter_AddRefs(parentLinkOfAnchor));
         // XXX: ERROR_HANDLING  can parentLinkOfAnchor be null?
         if (NS_SUCCEEDED(rv) && parentLinkOfAnchor) {
           if (isCollapsed) {
             // We have just a caret in the link
             bNodeFound = true;
           } else if (focusNode) {
             // Link node must be the same for both ends of selection.
@@ -2495,26 +2494,24 @@ HTMLEditor::GetSelectedElement(const nsA
             if (NS_SUCCEEDED(rv) && parentLinkOfFocus == parentLinkOfAnchor) {
               bNodeFound = true;
             }
           }
 
           // We found a link node parent
           if (bNodeFound) {
             // GetElementOrParentByTagName addref'd this, so we don't need to do it here
-            *aReturn = parentLinkOfAnchor;
-            NS_IF_ADDREF(*aReturn);
+            parentLinkOfAnchor.forget(aReturn);
             return NS_OK;
           }
         } else if (anchorOffset >= 0) {
           // Check if link node is the only thing selected
-          nsCOMPtr<nsIDOMNode> anchorChild;
-          anchorChild = GetChildAt(anchorNode,anchorOffset);
+          nsINode* anchorChild = anchorNode->GetChildAt(anchorOffset);
           if (anchorChild && HTMLEditUtils::IsLink(anchorChild) &&
-              anchorNode == GetAsDOMNode(focusNode) &&
+              anchorNode == focusNode &&
               focusOffset == anchorOffset + 1) {
             selectedElement = do_QueryInterface(anchorChild);
             bNodeFound = true;
           }
         }
       }
     }
 
@@ -3587,26 +3584,25 @@ HTMLEditor::SelectEntireDocument(Selecti
 NS_IMETHODIMP
 HTMLEditor::SelectAll()
 {
   CommitComposition();
 
   RefPtr<Selection> selection = GetSelection();
   NS_ENSURE_STATE(selection);
 
-  nsCOMPtr<nsIDOMNode> anchorNode;
-  nsresult rv = selection->GetAnchorNode(getter_AddRefs(anchorNode));
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  nsCOMPtr<nsIContent> anchorContent = do_QueryInterface(anchorNode, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  nsIContent *rootContent;
+  nsINode* anchorNode = selection->GetAnchorNode();
+  if (NS_WARN_IF(!anchorNode) || NS_WARN_IF(!anchorNode->IsContent())) {
+    return NS_ERROR_FAILURE;
+  }
+
+  nsIContent* anchorContent = anchorNode->AsContent();
+  nsIContent* rootContent;
   if (anchorContent->HasIndependentSelection()) {
-    rv = selection->SetAncestorLimiter(nullptr);
+    nsresult rv = selection->SetAncestorLimiter(nullptr);
     NS_ENSURE_SUCCESS(rv, rv);
     rootContent = mRootElement;
   } else {
     nsCOMPtr<nsIPresShell> ps = GetPresShell();
     rootContent = anchorContent->GetSelectionRootContent(ps);
   }
 
   NS_ENSURE_TRUE(rootContent, NS_ERROR_UNEXPECTED);
--- a/editor/libeditor/HTMLTableEditor.cpp
+++ b/editor/libeditor/HTMLTableEditor.cpp
@@ -3196,59 +3196,50 @@ HTMLEditor::GetSelectedOrParentTableElem
   NS_NAMED_LITERAL_STRING(tdName, "td");
 
   if (tableOrCellElement) {
       // Each cell is in its own selection range,
       //  so count signals multiple-cell selection
       *aSelectedCount = selection->RangeCount();
       aTagName = tdName;
   } else {
-    nsCOMPtr<nsIDOMNode> anchorNode;
-    rv = selection->GetAnchorNode(getter_AddRefs(anchorNode));
-    if (NS_FAILED(rv)) {
-      return rv;
+    nsCOMPtr<nsINode> anchorNode = selection->GetAnchorNode();
+    if (NS_WARN_IF(!anchorNode)) {
+      return NS_ERROR_FAILURE;
     }
-    NS_ENSURE_TRUE(anchorNode, NS_ERROR_FAILURE);
-
-    nsCOMPtr<nsIDOMNode> selectedNode;
 
     // Get child of anchor node, if exists
-    bool hasChildren;
-    anchorNode->HasChildNodes(&hasChildren);
-
-    if (hasChildren) {
+    if (anchorNode->HasChildNodes()) {
       int32_t anchorOffset = selection->AnchorOffset();
-      selectedNode = GetChildAt(anchorNode, anchorOffset);
+      nsINode* selectedNode = anchorNode->GetChildAt(anchorOffset);
       if (!selectedNode) {
         selectedNode = anchorNode;
         // If anchor doesn't have a child, we can't be selecting a table element,
         //  so don't do the following:
       } else {
-        nsCOMPtr<nsIAtom> atom = EditorBase::GetTag(selectedNode);
-
-        if (atom == nsGkAtoms::td) {
+        if (selectedNode->IsHTMLElement(nsGkAtoms::td)) {
           tableOrCellElement = do_QueryInterface(selectedNode);
           aTagName = tdName;
           // Each cell is in its own selection range,
           //  so count signals multiple-cell selection
           *aSelectedCount = selection->RangeCount();
-        } else if (atom == nsGkAtoms::table) {
+        } else if (selectedNode->IsHTMLElement(nsGkAtoms::table)) {
           tableOrCellElement = do_QueryInterface(selectedNode);
           aTagName.AssignLiteral("table");
           *aSelectedCount = 1;
-        } else if (atom == nsGkAtoms::tr) {
+        } else if (selectedNode->IsHTMLElement(nsGkAtoms::tr)) {
           tableOrCellElement = do_QueryInterface(selectedNode);
           aTagName.AssignLiteral("tr");
           *aSelectedCount = 1;
         }
       }
     }
     if (!tableOrCellElement) {
       // Didn't find a table element -- find a cell parent
-      rv = GetElementOrParentByTagName(tdName, anchorNode,
+      rv = GetElementOrParentByTagName(tdName, GetAsDOMNode(anchorNode),
                                        getter_AddRefs(tableOrCellElement));
       if (NS_FAILED(rv)) {
         return rv;
       }
       if (tableOrCellElement) {
         aTagName = tdName;
       }
     }
--- a/editor/libeditor/TextEditorTest.cpp
+++ b/editor/libeditor/TextEditorTest.cpp
@@ -104,21 +104,20 @@ nsresult TextEditorTest::InitDoc()
 }
 
 nsresult TextEditorTest::TestInsertBreak()
 {
   nsCOMPtr<nsISelection>selection;
   nsresult rv = mEditor->GetSelection(getter_AddRefs(selection));
   TEST_RESULT(rv);
   TEST_POINTER(selection.get());
-  nsCOMPtr<nsIDOMNode>anchor;
-  rv = selection->GetAnchorNode(getter_AddRefs(anchor));
+  nsCOMPtr<nsINode> anchor = selection->AsSelection()->GetAnchorNode();
   TEST_RESULT(rv);
   TEST_POINTER(anchor.get());
-  selection->Collapse(anchor, 0);
+  selection->AsSelection()->Collapse(anchor, 0);
   // insert one break
   printf("inserting a break\n");
   rv = mTextEditor->InsertLineBreak();
   TEST_RESULT(rv);
   mEditor->DebugDumpContent();
 
   // insert a second break adjacent to the first
   printf("inserting a second break\n");