Bug 1156062 part 7 - Clean up nsHTMLEditRules::StandardBreakImpl; r=ehsan
authorAryeh Gregor <ayg@aryeh.name>
Sat, 23 Apr 2016 18:32:13 +0900
changeset 318407 02a332642272c61550235152f0e7ff89a139faac
parent 318406 e6a6d2316a3b9972fb2cb77f82837bdd95283cc7
child 318408 f9ca78699bae28da5a8ec8c0d13f556350f441b3
push id9480
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 17:12:58 +0000
treeherdermozilla-aurora@0d6a91c76a9e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1156062
milestone48.0a1
Bug 1156062 part 7 - Clean up nsHTMLEditRules::StandardBreakImpl; r=ehsan
editor/libeditor/nsHTMLEditRules.cpp
editor/libeditor/nsHTMLEditRules.h
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -1563,17 +1563,17 @@ nsHTMLEditRules::WillInsertBreak(Selecti
   // Identify the block
   nsCOMPtr<Element> blockParent = mHTMLEditor->GetBlock(node);
   NS_ENSURE_TRUE(blockParent, NS_ERROR_FAILURE);
 
   // If the active editing host is an inline element, or if the active editing
   // host is the block parent itself, just append a br.
   nsCOMPtr<Element> host = mHTMLEditor->GetActiveEditingHost();
   if (!nsEditorUtils::IsDescendantOf(blockParent, host)) {
-    res = StandardBreakImpl(GetAsDOMNode(node), offset, &aSelection);
+    res = StandardBreakImpl(node, offset, aSelection);
     NS_ENSURE_SUCCESS(res, res);
     *aHandled = true;
     return NS_OK;
   }
 
   // If block is empty, populate with br.  (For example, imagine a div that
   // contains the word "text".  The user selects "text" and types return.
   // "Text" is deleted leaving an empty block.  We want to put in one br to
@@ -1604,122 +1604,115 @@ nsHTMLEditRules::WillInsertBreak(Selecti
                             GetAsDOMNode(node), offset, aCancel, aHandled);
     NS_ENSURE_SUCCESS(res, res);
     // Fall through, we may not have handled it in ReturnInParagraph()
   }
 
   // If not already handled then do the standard thing
   if (!(*aHandled)) {
     *aHandled = true;
-    return StandardBreakImpl(GetAsDOMNode(node), offset, &aSelection);
+    return StandardBreakImpl(node, offset, aSelection);
   }
   return NS_OK;
 }
 
 nsresult
-nsHTMLEditRules::StandardBreakImpl(nsIDOMNode* aNode, int32_t aOffset,
-                                   Selection* aSelection)
-{
-  nsCOMPtr<nsIDOMNode> brNode;
+nsHTMLEditRules::StandardBreakImpl(nsINode& aNode, int32_t aOffset,
+                                   Selection& aSelection)
+{
+  NS_ENSURE_STATE(mHTMLEditor);
+  nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
+
+  nsCOMPtr<Element> brNode;
   bool bAfterBlock = false;
   bool bBeforeBlock = false;
-  nsresult res = NS_OK;
-  nsCOMPtr<nsIDOMNode> node(aNode);
+  nsCOMPtr<nsINode> node = &aNode;
+  nsresult res;
 
   if (IsPlaintextEditor()) {
-    NS_ENSURE_STATE(mHTMLEditor);
-    res = mHTMLEditor->CreateBR(node, aOffset, address_of(brNode));
+    brNode = mHTMLEditor->CreateBR(node, aOffset);
+    NS_ENSURE_STATE(brNode);
   } else {
-    NS_ENSURE_STATE(mHTMLEditor);
     nsWSRunObject wsObj(mHTMLEditor, node, aOffset);
     int32_t visOffset = 0;
     WSType wsType;
-    nsCOMPtr<nsINode> node_(do_QueryInterface(node)), visNode;
-    wsObj.PriorVisibleNode(node_, aOffset, address_of(visNode),
+    nsCOMPtr<nsINode> visNode;
+    wsObj.PriorVisibleNode(node, aOffset, address_of(visNode),
                            &visOffset, &wsType);
     if (wsType & WSType::block) {
       bAfterBlock = true;
     }
-    wsObj.NextVisibleNode(node_, aOffset, address_of(visNode),
+    wsObj.NextVisibleNode(node, aOffset, address_of(visNode),
                           &visOffset, &wsType);
     if (wsType & WSType::block) {
       bBeforeBlock = true;
     }
-    NS_ENSURE_STATE(mHTMLEditor);
-    nsCOMPtr<nsIDOMNode> linkNode;
-    if (mHTMLEditor->IsInLink(node, address_of(linkNode))) {
-      // split the link
-      nsCOMPtr<nsIDOMNode> linkParent;
-      res = linkNode->GetParentNode(getter_AddRefs(linkParent));
-      NS_ENSURE_SUCCESS(res, res);
-      NS_ENSURE_STATE(mHTMLEditor);
-      nsCOMPtr<nsIContent> linkNodeContent = do_QueryInterface(linkNode);
-      NS_ENSURE_STATE(linkNodeContent || !linkNode);
-      aOffset = mHTMLEditor->SplitNodeDeep(*linkNodeContent,
-          *node_->AsContent(), aOffset, nsHTMLEditor::EmptyContainers::no);
+    nsCOMPtr<nsIDOMNode> linkDOMNode;
+    if (mHTMLEditor->IsInLink(GetAsDOMNode(node), address_of(linkDOMNode))) {
+      // Split the link
+      nsCOMPtr<Element> linkNode = do_QueryInterface(linkDOMNode);
+      NS_ENSURE_STATE(linkNode || !linkDOMNode);
+      nsCOMPtr<nsINode> linkParent = linkNode->GetParentNode();
+      aOffset = mHTMLEditor->SplitNodeDeep(*linkNode, *node->AsContent(),
+                                           aOffset,
+                                           nsHTMLEditor::EmptyContainers::no);
       NS_ENSURE_STATE(aOffset != -1);
       node = linkParent;
     }
-    node_ = do_QueryInterface(node);
-    nsCOMPtr<Element> br =
-      wsObj.InsertBreak(address_of(node_), &aOffset, nsIEditor::eNone);
-    node = GetAsDOMNode(node_);
-    brNode = GetAsDOMNode(br);
+    brNode = wsObj.InsertBreak(address_of(node), &aOffset, nsIEditor::eNone);
     NS_ENSURE_TRUE(brNode, NS_ERROR_FAILURE);
   }
-  NS_ENSURE_SUCCESS(res, res);
-  node = nsEditor::GetNodeLocation(brNode, &aOffset);
+  node = brNode->GetParentNode();
   NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
+  int32_t offset = node->IndexOf(brNode);
   if (bAfterBlock && bBeforeBlock) {
-    // we just placed a br between block boundaries.  This is the one case
+    // We just placed a br between block boundaries.  This is the one case
     // where we want the selection to be before the br we just placed, as the
     // br will be on a new line, rather than at end of prior line.
-    aSelection->SetInterlinePosition(true);
-    res = aSelection->Collapse(node, aOffset);
+    aSelection.SetInterlinePosition(true);
+    res = aSelection.Collapse(node, offset);
+    NS_ENSURE_SUCCESS(res, res);
   } else {
-    NS_ENSURE_STATE(mHTMLEditor);
-    nsWSRunObject wsObj(mHTMLEditor, node, aOffset+1);
+    nsWSRunObject wsObj(mHTMLEditor, node, offset + 1);
     nsCOMPtr<nsINode> secondBR;
     int32_t visOffset = 0;
     WSType wsType;
-    nsCOMPtr<nsINode> node_(do_QueryInterface(node));
-    wsObj.NextVisibleNode(node_, aOffset+1, address_of(secondBR),
+    wsObj.NextVisibleNode(node, offset + 1, address_of(secondBR),
                           &visOffset, &wsType);
     if (wsType == WSType::br) {
-      // the next thing after the break we inserted is another break.  Move
-      // the 2nd break to be the first breaks sibling.  This will prevent them
+      // The next thing after the break we inserted is another break.  Move the
+      // second break to be the first break's sibling.  This will prevent them
       // from being in different inline nodes, which would break
       // SetInterlinePosition().  It will also assure that if the user clicks
-      // away and then clicks back on their new blank line, they will still
-      // get the style from the line above.
-      int32_t brOffset;
-      nsCOMPtr<nsIDOMNode> brParent = nsEditor::GetNodeLocation(GetAsDOMNode(secondBR), &brOffset);
-      if (brParent != node || brOffset != aOffset + 1) {
-        NS_ENSURE_STATE(mHTMLEditor);
-        res = mHTMLEditor->MoveNode(secondBR->AsContent(), node_, aOffset + 1);
+      // away and then clicks back on their new blank line, they will still get
+      // the style from the line above.
+      nsCOMPtr<nsINode> brParent = secondBR->GetParentNode();
+      int32_t brOffset = brParent ? brParent->IndexOf(secondBR) : -1;
+      if (brParent != node || brOffset != offset + 1) {
+        res = mHTMLEditor->MoveNode(secondBR->AsContent(), node, offset + 1);
         NS_ENSURE_SUCCESS(res, res);
       }
     }
     // SetInterlinePosition(true) means we want the caret to stick to the
     // content on the "right".  We want the caret to stick to whatever is past
     // the break.  This is because the break is on the same line we were on,
     // but the next content will be on the following line.
 
     // An exception to this is if the break has a next sibling that is a block
     // node.  Then we stick to the left to avoid an uber caret.
-    nsCOMPtr<nsIDOMNode> siblingNode;
-    brNode->GetNextSibling(getter_AddRefs(siblingNode));
-    if (siblingNode && IsBlockNode(siblingNode)) {
-      aSelection->SetInterlinePosition(false);
+    nsCOMPtr<nsIContent> siblingNode = brNode->GetNextSibling();
+    if (siblingNode && IsBlockNode(GetAsDOMNode(siblingNode))) {
+      aSelection.SetInterlinePosition(false);
     } else {
-      aSelection->SetInterlinePosition(true);
-    }
-    res = aSelection->Collapse(node, aOffset+1);
-  }
-  return res;
+      aSelection.SetInterlinePosition(true);
+    }
+    res = aSelection.Collapse(node, offset + 1);
+    NS_ENSURE_SUCCESS(res, res);
+  }
+  return NS_OK;
 }
 
 nsresult
 nsHTMLEditRules::DidInsertBreak(Selection* aSelection, nsresult aResult)
 {
   return NS_OK;
 }
 
--- a/editor/libeditor/nsHTMLEditRules.h
+++ b/editor/libeditor/nsHTMLEditRules.h
@@ -132,18 +132,18 @@ protected:
                             bool            *aCancel,
                             bool            *aHandled,
                             const nsAString *inString,
                             nsAString       *outString,
                             int32_t          aMaxLength);
   nsresult WillLoadHTML(mozilla::dom::Selection* aSelection, bool* aCancel);
   nsresult WillInsertBreak(mozilla::dom::Selection& aSelection,
                            bool* aCancel, bool* aHandled);
-  nsresult StandardBreakImpl(nsIDOMNode* aNode, int32_t aOffset,
-                             mozilla::dom::Selection* aSelection);
+  nsresult StandardBreakImpl(nsINode& aNode, int32_t aOffset,
+                             mozilla::dom::Selection& aSelection);
   nsresult DidInsertBreak(mozilla::dom::Selection* aSelection,
                           nsresult aResult);
   nsresult SplitMailCites(mozilla::dom::Selection* aSelection, bool* aHandled);
   nsresult WillDeleteSelection(mozilla::dom::Selection* aSelection,
                                nsIEditor::EDirection aAction,
                                nsIEditor::EStripWrappers aStripWrappers,
                                bool* aCancel, bool* aHandled);
   nsresult DidDeleteSelection(mozilla::dom::Selection* aSelection,