Bug 1191356 part 4 - Clean up nsHTMLEditRules::MoveNodeSmart, MoveContents; r=ehsan
authorAryeh Gregor <ayg@aryeh.name>
Sun, 01 May 2016 16:17:03 +0300
changeset 295655 cb399cd2f5be6639a81d671294e7ef188cf50e20
parent 295654 d0f212d477d665b44b7749894be623cc1ac38dcc
child 295656 24149bd5c7747d2740085876eb62cf11f7ef73b0
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 4 - Clean up nsHTMLEditRules::MoveNodeSmart, MoveContents; r=ehsan
editor/libeditor/nsHTMLEditRules.cpp
editor/libeditor/nsHTMLEditRules.h
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -2691,18 +2691,17 @@ nsHTMLEditRules::JoinBlocks(nsIContent& 
         NS_ENSURE_STATE(trackingLeftBlock->GetParentElement());
         leftBlock = trackingLeftBlock->GetParentElement();
       }
     }
     // Do br adjustment.
     nsCOMPtr<Element> brNode =
       CheckForInvisibleBR(*leftBlock, BRLocation::beforeBlock, leftOffset);
     if (mergeLists) {
-      res = MoveContents(GetAsDOMNode(rightList), GetAsDOMNode(leftList),
-                         &leftOffset);
+      res = MoveContents(*rightList, *leftList, &leftOffset);
     } else {
       // Left block is a parent of right block, and the parent of the previous
       // visible content.  Right block is a child and contains the contents we
       // want to move.
 
       int32_t previousContentOffset;
       nsCOMPtr<nsINode> previousContentParent;
 
@@ -2809,95 +2808,86 @@ nsHTMLEditRules::MoveBlock(Element& aLef
   nsresult res = GetNodesFromPoint(::DOMPoint(&aRightBlock, aRightOffset),
                                    EditAction::makeList, arrayOfNodes,
                                    TouchContent::yes);
   NS_ENSURE_SUCCESS(res, res);
   for (uint32_t i = 0; i < arrayOfNodes.Length(); i++) {
     // get the node to act on
     if (IsBlockNode(arrayOfNodes[i])) {
       // For block nodes, move their contents only, then delete block.
-      res = MoveContents(arrayOfNodes[i]->AsDOMNode(), aLeftBlock.AsDOMNode(),
+      res = MoveContents(*arrayOfNodes[i]->AsElement(), aLeftBlock,
                          &aLeftOffset);
       NS_ENSURE_SUCCESS(res, res);
       NS_ENSURE_STATE(mHTMLEditor);
       res = mHTMLEditor->DeleteNode(arrayOfNodes[i]);
     } else {
       // Otherwise move the content as is, checking against the DTD.
-      res = MoveNodeSmart(arrayOfNodes[i]->AsDOMNode(), aLeftBlock.AsDOMNode(),
+      res = MoveNodeSmart(*arrayOfNodes[i]->AsContent(), aLeftBlock,
                           &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
-*         int32_t *aOffset          offset in aDest to move content to
-*/
+/**
+ * This method is used to move node aNode to (aDestElement, aInOutDestOffset).
+ * DTD containment rules are followed throughout.  aInOutDestOffset is updated
+ * to point _after_ inserted content.
+ */
 nsresult
-nsHTMLEditRules::MoveNodeSmart(nsIDOMNode *aSource, nsIDOMNode *aDest, int32_t *aOffset)
-{
-  nsCOMPtr<nsIContent> source = do_QueryInterface(aSource);
-  nsCOMPtr<nsINode> dest = do_QueryInterface(aDest);
-  NS_ENSURE_TRUE(source && dest && aOffset, NS_ERROR_NULL_POINTER);
-
+nsHTMLEditRules::MoveNodeSmart(nsIContent& aNode, Element& aDestElement,
+                               int32_t* aInOutDestOffset)
+{
+  MOZ_ASSERT(aInOutDestOffset);
+
+  NS_ENSURE_STATE(mHTMLEditor);
+  nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
   nsresult res;
-  // check if this node can go into the destination node
-  NS_ENSURE_STATE(mHTMLEditor);
-  if (mHTMLEditor->CanContain(*dest, *source)) {
-    // if it can, move it there
-    NS_ENSURE_STATE(mHTMLEditor);
-    res = mHTMLEditor->MoveNode(source, dest, *aOffset);
+
+  // Check if this node can go into the destination node
+  if (mHTMLEditor->CanContain(aDestElement, aNode)) {
+    // If it can, move it there
+    res = mHTMLEditor->MoveNode(&aNode, &aDestElement, *aInOutDestOffset);
     NS_ENSURE_SUCCESS(res, res);
-    if (*aOffset != -1) ++(*aOffset);
-  }
-  else
-  {
-    // if it can't, move its children, and then delete it.
-    res = MoveContents(aSource, aDest, aOffset);
-    NS_ENSURE_SUCCESS(res, res);
-    NS_ENSURE_STATE(mHTMLEditor);
-    res = mHTMLEditor->DeleteNode(aSource);
+    if (*aInOutDestOffset != -1) {
+      (*aInOutDestOffset)++;
+    }
+  } else {
+    // If it can't, move its children (if any), and then delete it.
+    if (aNode.IsElement()) {
+      res = MoveContents(*aNode.AsElement(), aDestElement, aInOutDestOffset);
+      NS_ENSURE_SUCCESS(res, res);
+    }
+
+    res = mHTMLEditor->DeleteNode(&aNode);
     NS_ENSURE_SUCCESS(res, res);
   }
   return NS_OK;
 }
 
-/*****************************************************************************************************
-*    MoveContents: this method is used to move node the _contents_ of aSource to (aDest,aOffset).
-*    DTD containment rules are followed throughout.  aOffset is updated to point _after_
-*    inserted content.  aSource is deleted.
-*         nsIDOMNode *aSource       the selection.
-*         nsIDOMNode *aDest         parent to receive moved content
-*         int32_t *aOffset          offset in aDest to move content to
-*/
+/**
+ * Moves the _contents_ of aElement to (aDestElement, aInOutDestOffset).  DTD
+ * containment rules are followed throughout.  aInOutDestOffset is updated to
+ * point _after_ inserted content.
+ */
 nsresult
-nsHTMLEditRules::MoveContents(nsIDOMNode *aSource, nsIDOMNode *aDest, int32_t *aOffset)
-{
-  NS_ENSURE_TRUE(aSource && aDest && aOffset, NS_ERROR_NULL_POINTER);
-  if (aSource == aDest) return NS_ERROR_ILLEGAL_VALUE;
-  NS_ENSURE_STATE(mHTMLEditor);
-  NS_ASSERTION(!mHTMLEditor->IsTextNode(aSource), "#text does not have contents");
-
-  nsCOMPtr<nsIDOMNode> child;
-  nsAutoString tag;
-  nsresult res;
-  aSource->GetFirstChild(getter_AddRefs(child));
-  while (child)
-  {
-    res = MoveNodeSmart(child, aDest, aOffset);
+nsHTMLEditRules::MoveContents(Element& aElement, Element& aDestElement,
+                              int32_t* aInOutDestOffset)
+{
+  MOZ_ASSERT(aInOutDestOffset);
+
+  NS_ENSURE_TRUE(&aElement != &aDestElement, NS_ERROR_ILLEGAL_VALUE);
+
+  while (aElement.GetFirstChild()) {
+    nsresult res = MoveNodeSmart(*aElement.GetFirstChild(), aDestElement,
+                                 aInOutDestOffset);
     NS_ENSURE_SUCCESS(res, res);
-    aSource->GetFirstChild(getter_AddRefs(child));
   }
   return NS_OK;
 }
 
 
 nsresult
 nsHTMLEditRules::DeleteNonTableElements(nsINode* aNode)
 {
--- a/editor/libeditor/nsHTMLEditRules.h
+++ b/editor/libeditor/nsHTMLEditRules.h
@@ -144,18 +144,20 @@ protected:
                               nsresult aResult);
   nsresult InsertBRIfNeeded(Selection* aSelection);
   ::DOMPoint GetGoodSelPointForNode(nsINode& aNode,
                                     nsIEditor::EDirection aAction);
   nsresult JoinBlocks(nsIContent& aLeftNode, nsIContent& aRightNode,
                       bool* aCanceled);
   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 MoveNodeSmart(nsIContent& aNode, Element& aDestElement,
+                         int32_t* aOffset);
+  nsresult MoveContents(Element& aElement, Element& aDestElement,
+                        int32_t* aOffset);
   nsresult DeleteNonTableElements(nsINode* aNode);
   nsresult WillMakeList(Selection* aSelection,
                         const nsAString* aListType,
                         bool aEntireList,
                         const nsAString* aBulletType,
                         bool* aCancel, bool* aHandled,
                         const nsAString* aItemType = nullptr);
   nsresult WillRemoveList(Selection* aSelection, bool aOrdered, bool* aCancel,