Bug 1191356 part 3 - Clean up nsHTMLEditRules::MoveBlock; r=ehsan
authorAryeh Gregor <ayg@aryeh.name>
Sun, 01 May 2016 16:16:50 +0300
changeset 295654 d0f212d477d665b44b7749894be623cc1ac38dcc
parent 295653 1c306ee11299c610d1d389f1515de565a37474ea
child 295655 cb399cd2f5be6639a81d671294e7ef188cf50e20
push id19015
push usercbook@mozilla.com
push dateMon, 02 May 2016 09:39:23 +0000
treeherderfx-team@2080375bc69d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1191356
milestone49.0a1
Bug 1191356 part 3 - Clean up nsHTMLEditRules::MoveBlock; r=ehsan
editor/libeditor/nsHTMLEditRules.cpp
editor/libeditor/nsHTMLEditRules.h
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -2655,18 +2655,17 @@ nsHTMLEditRules::JoinBlocks(nsIContent& 
       // The idea here is to take all children in rightList that are past
       // offset, and pull them into leftlist.
       for (nsCOMPtr<nsIContent> child = rightList->GetChildAt(offset);
            child; child = rightList->GetChildAt(rightOffset)) {
         res = mHTMLEditor->MoveNode(child, leftList, -1);
         NS_ENSURE_SUCCESS(res, res);
       }
     } else {
-      res = MoveBlock(GetAsDOMNode(leftBlock), GetAsDOMNode(rightBlock),
-                      leftOffset, rightOffset);
+      res = MoveBlock(*leftBlock, *rightBlock, leftOffset, rightOffset);
     }
     if (brNode) {
       mHTMLEditor->DeleteNode(brNode);
     }
   // Offset below is where you find yourself in leftBlock when you traverse
   // upwards from rightBlock
   } else if (nsEditorUtils::IsDescendantOf(rightBlock, leftBlock,
                                            &leftOffset)) {
@@ -2746,19 +2745,20 @@ nsHTMLEditRules::JoinBlocks(nsIContent& 
 
         if (splittedPreviousContent) {
           previousContentParent = splittedPreviousContent->GetParentNode();
           previousContentOffset = previousContentParent ?
             previousContentParent->IndexOf(splittedPreviousContent) : -1;
         }
       }
 
-      res = MoveBlock(GetAsDOMNode(previousContentParent),
-                      GetAsDOMNode(rightBlock),
-                      previousContentOffset, rightOffset);
+      NS_ENSURE_TRUE(previousContentParent, NS_ERROR_NULL_POINTER);
+
+      res = MoveBlock(*previousContentParent->AsElement(), *rightBlock,
+          previousContentOffset, rightOffset);
       NS_ENSURE_SUCCESS(res, res);
     }
     if (brNode) {
       mHTMLEditor->DeleteNode(brNode);
     }
   } else {
     // Normal case.  Blocks are siblings, or at least close enough.  An example
     // of the latter is <p>paragraph</p><ul><li>one<li>two<li>three</ul>.  The
@@ -2777,63 +2777,63 @@ nsHTMLEditRules::JoinBlocks(nsIContent& 
       ::DOMPoint pt = JoinNodesSmart(*leftBlock, *rightBlock);
       if (pt.node && mergeLists) {
         nsCOMPtr<Element> newBlock;
         res = ConvertListType(rightBlock, getter_AddRefs(newBlock),
                               existingList, nsGkAtoms::li);
       }
     } else {
       // Nodes are dissimilar types.
-      res = MoveBlock(GetAsDOMNode(leftBlock), GetAsDOMNode(rightBlock),
-                      leftOffset, rightOffset);
+      res = MoveBlock(*leftBlock, *rightBlock, leftOffset, rightOffset);
       NS_ENSURE_SUCCESS(res, res);
     }
     if (brNode) {
       res = mHTMLEditor->DeleteNode(brNode);
       NS_ENSURE_SUCCESS(res, res);
     }
   }
   return NS_OK;
 }
 
 
-/*****************************************************************************************************
-*    MoveBlock: this method is used to move the content from rightBlock into leftBlock
-*    Note that the "block" might merely be inline nodes between <br>s, or between blocks, etc.
-*    DTD containment rules are followed throughout.
-*         nsIDOMNode *aLeftBlock         parent to receive moved content
-*         nsIDOMNode *aRightBlock        parent to provide moved content
-*         int32_t aLeftOffset            offset in aLeftBlock to move content to
-*         int32_t aRightOffset           offset in aRightBlock to move content from
-*/
+/**
+ * Moves the content from aRightBlock starting from aRightOffset into
+ * aLeftBlock at aLeftOffset. Note that the "block" might merely be inline
+ * nodes between <br>s, or between blocks, etc.  DTD containment rules are
+ * followed throughout.
+ */
 nsresult
-nsHTMLEditRules::MoveBlock(nsIDOMNode *aLeftBlock, nsIDOMNode *aRightBlock, int32_t aLeftOffset, int32_t aRightOffset)
+nsHTMLEditRules::MoveBlock(Element& aLeftBlock, Element& aRightBlock,
+                           int32_t aLeftOffset, int32_t aRightOffset)
 {
   nsTArray<OwningNonNull<nsINode>> arrayOfNodes;
   // GetNodesFromPoint is the workhorse that figures out what we wnat to move.
-  nsresult res = GetNodesFromPoint(::DOMPoint(aRightBlock,aRightOffset),
+  nsresult res = GetNodesFromPoint(::DOMPoint(&aRightBlock, aRightOffset),
                                    EditAction::makeList, arrayOfNodes,
                                    TouchContent::yes);
   NS_ENSURE_SUCCESS(res, res);
-  for (auto& curNode : arrayOfNodes) {
+  for (uint32_t i = 0; i < arrayOfNodes.Length(); i++) {
     // get the node to act on
-    if (IsBlockNode(curNode)) {
+    if (IsBlockNode(arrayOfNodes[i])) {
       // For block nodes, move their contents only, then delete block.
-      res = MoveContents(GetAsDOMNode(curNode), aLeftBlock, &aLeftOffset);
+      res = MoveContents(arrayOfNodes[i]->AsDOMNode(), aLeftBlock.AsDOMNode(),
+                         &aLeftOffset);
       NS_ENSURE_SUCCESS(res, res);
       NS_ENSURE_STATE(mHTMLEditor);
-      res = mHTMLEditor->DeleteNode(curNode);
-    }
-    else
-    {
-      // otherwise move the content as is, checking against the dtd.
-      res = MoveNodeSmart(GetAsDOMNode(curNode), aLeftBlock, &aLeftOffset);
-    }
-  }
-  return res;
+      res = mHTMLEditor->DeleteNode(arrayOfNodes[i]);
+    } else {
+      // Otherwise move the content as is, checking against the DTD.
+      res = MoveNodeSmart(arrayOfNodes[i]->AsDOMNode(), aLeftBlock.AsDOMNode(),
+                          &aLeftOffset);
+    }
+  }
+
+  // XXX We're only checking return value of the last iteration
+  NS_ENSURE_SUCCESS(res, res);
+  return NS_OK;
 }
 
 /*****************************************************************************************************
 *    MoveNodeSmart: this method is used to move node aSource to (aDest,aOffset).
 *    DTD containment rules are followed throughout.  aOffset is updated to point _after_
 *    inserted content.
 *         nsIDOMNode *aSource       the selection.
 *         nsIDOMNode *aDest         parent to receive moved content
--- a/editor/libeditor/nsHTMLEditRules.h
+++ b/editor/libeditor/nsHTMLEditRules.h
@@ -142,17 +142,18 @@ protected:
   nsresult DidDeleteSelection(Selection* aSelection,
                               nsIEditor::EDirection aDir,
                               nsresult aResult);
   nsresult InsertBRIfNeeded(Selection* aSelection);
   ::DOMPoint GetGoodSelPointForNode(nsINode& aNode,
                                     nsIEditor::EDirection aAction);
   nsresult JoinBlocks(nsIContent& aLeftNode, nsIContent& aRightNode,
                       bool* aCanceled);
-  nsresult MoveBlock(nsIDOMNode *aLeft, nsIDOMNode *aRight, int32_t aLeftOffset, int32_t aRightOffset);
+  nsresult MoveBlock(Element& aLeftBlock, Element& aRightBlock,
+                     int32_t aLeftOffset, int32_t aRightOffset);
   nsresult MoveNodeSmart(nsIDOMNode *aSource, nsIDOMNode *aDest, int32_t *aOffset);
   nsresult MoveContents(nsIDOMNode *aSource, nsIDOMNode *aDest, int32_t *aOffset);
   nsresult DeleteNonTableElements(nsINode* aNode);
   nsresult WillMakeList(Selection* aSelection,
                         const nsAString* aListType,
                         bool aEntireList,
                         const nsAString* aBulletType,
                         bool* aCancel, bool* aHandled,