Bug 291789 part 2 - Clean up nsHTMLEditRules::InDifferentTableElements; r=ehsan
authorAryeh Gregor <ayg@aryeh.name>
Fri, 27 Jul 2012 17:03:28 +0300
changeset 100880 680da0011a04dc8ee9854ec64112f698b2e81272
parent 100879 eda4a70cc9462bba18f4dacae70b04ef16d72a75
child 100881 72c56a9dc330b32f937683a4f9f3671ab6ac02d3
push id967
push userttaubert@mozilla.com
push dateTue, 31 Jul 2012 14:39:21 +0000
treeherderfx-team@8c63e260c4fa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs291789
milestone17.0a1
Bug 291789 part 2 - Clean up nsHTMLEditRules::InDifferentTableElements; r=ehsan
editor/libeditor/html/nsHTMLEditRules.cpp
editor/libeditor/html/nsHTMLEditRules.h
--- a/editor/libeditor/html/nsHTMLEditRules.cpp
+++ b/editor/libeditor/html/nsHTMLEditRules.cpp
@@ -2089,21 +2089,18 @@ nsHTMLEditRules::WillDeleteSelection(Sel
       {
         res = mHTMLEditor->DeleteNode(otherNode);
         NS_ENSURE_SUCCESS(res, res);
         *aHandled = true;
         bDeletedBR = true;
       }
       
       // don't cross table boundaries
-      if (leftNode && rightNode)
-      {
-        bool bInDifTblElems;
-        res = InDifferentTableElements(leftNode, rightNode, &bInDifTblElems);
-        if (NS_FAILED(res) || bInDifTblElems) return res;
+      if (leftNode && rightNode && InDifferentTableElements(leftNode, rightNode)) {
+        return NS_OK;
       }
       
       if (bDeletedBR)
       {
         // put selection at edge of block and we are done.
         nsCOMPtr<nsIDOMNode> newSelNode;
         PRInt32 newSelOffset;
         res = GetGoodSelPointForNode(leafNode, aAction, address_of(newSelNode), &newSelOffset);
@@ -2168,19 +2165,19 @@ nsHTMLEditRules::WillDeleteSelection(Sel
       // nothing to join
       if (!leftNode || !rightNode)
       {
         *aCancel = true;
         return NS_OK;
       }
 
       // don't cross table boundaries
-      bool bInDifTblElems;
-      res = InDifferentTableElements(leftNode, rightNode, &bInDifTblElems);
-      if (NS_FAILED(res) || bInDifTblElems) return res;
+      if (InDifferentTableElements(leftNode, rightNode)) {
+        return NS_OK;
+      }
 
       // find the relavent blocks
       if (IsBlockNode(leftNode))
         leftParent = leftNode;
       else
         leftParent = mHTMLEditor->GetBlockNodeParent(leftNode);
       if (IsBlockNode(rightNode))
         rightParent = rightNode;
@@ -2988,23 +2985,18 @@ nsHTMLEditRules::WillMakeList(Selection*
     // here's where we actually figure out what to do
     nsCOMPtr<nsIDOMNode> newBlock;
     nsCOMPtr<nsIDOMNode> curNode = arrayOfNodes[i];
     PRInt32 offset;
     curParent = nsEditor::GetNodeLocation(curNode, &offset);
 
     // make sure we don't assemble content that is in different table cells
     // into the same list.  respect table cell boundaries when listifying.
-    if (curList) {
-      bool bInDifTblElems;
-      res = InDifferentTableElements(curList, curNode, &bInDifTblElems);
-      NS_ENSURE_SUCCESS(res, res);
-      if (bInDifTblElems) {
-        curList = nullptr;
-      }
+    if (curList && InDifferentTableElements(curList, curNode)) {
+      curList = nullptr;
     }
 
     // if curNode is a Break, delete it, and quit remembering prev list item
     if (nsTextEditUtils::IsBreak(curNode)) {
       res = mHTMLEditor->DeleteNode(curNode);
       NS_ENSURE_SUCCESS(res, res);
       prevListItem = 0;
       continue;
@@ -3797,23 +3789,18 @@ nsHTMLEditRules::WillHTMLIndent(Selectio
       }
       
       else
       {
         // need to make a blockquote to put things in if we haven't already,
         // or if this node doesn't go in blockquote we used earlier.
         // One reason it might not go in prio blockquote is if we are now
         // in a different table cell. 
-        if (curQuote)
-        {
-          bool bInDifTblElems;
-          res = InDifferentTableElements(curQuote, curNode, &bInDifTblElems);
-          NS_ENSURE_SUCCESS(res, res);
-          if (bInDifTblElems)
-            curQuote = nullptr;
+        if (curQuote && InDifferentTableElements(curQuote, curNode)) {
+          curQuote = nullptr;
         }
         
         if (!curQuote) 
         {
           // First, check that our element can contain a blockquote.
           if (!mEditor->CanContainTag(curParent, nsGkAtoms::blockquote)) {
             return NS_OK; // cancelled
           }
@@ -7562,55 +7549,49 @@ nsHTMLEditRules::FindNearSelectableNode(
     else
       res = mHTMLEditor->GetNextHTMLNode(curNode, address_of(nearNode));
     NS_ENSURE_SUCCESS(res, res);
   }
   
   if (nearNode)
   {
     // don't cross any table elements
-    bool bInDifTblElems;
-    res = InDifferentTableElements(nearNode, aSelNode, &bInDifTblElems);
-    NS_ENSURE_SUCCESS(res, res);
-    if (bInDifTblElems) return NS_OK;  
+    if (InDifferentTableElements(nearNode, aSelNode)) {
+      return NS_OK;
+    }
     
     // otherwise, ok, we have found a good spot to put the selection
     *outSelectableNode = do_QueryInterface(nearNode);
   }
   return res;
 }
 
 
-nsresult
-nsHTMLEditRules::InDifferentTableElements(nsIDOMNode *aNode1, nsIDOMNode *aNode2, bool *aResult)
-{
-  NS_ASSERTION(aNode1 && aNode2 && aResult, "null args");
-  NS_ENSURE_TRUE(aNode1 && aNode2 && aResult, NS_ERROR_NULL_POINTER);
-
-  nsCOMPtr<nsIDOMNode> tn1, tn2, node = aNode1, temp;
-  *aResult = false;
-  
-  while (node && !nsHTMLEditUtils::IsTableElement(node))
-  {
-    node->GetParentNode(getter_AddRefs(temp));
-    node = temp;
-  }
-  tn1 = node;
-  
-  node = aNode2;
-  while (node && !nsHTMLEditUtils::IsTableElement(node))
-  {
-    node->GetParentNode(getter_AddRefs(temp));
-    node = temp;
-  }
-  tn2 = node;
-  
-  *aResult = (tn1 != tn2);
-  
-  return NS_OK;
+bool nsHTMLEditRules::InDifferentTableElements(nsIDOMNode* aNode1,
+                                               nsIDOMNode* aNode2)
+{
+  nsCOMPtr<nsINode> node1 = do_QueryInterface(aNode1);
+  nsCOMPtr<nsINode> node2 = do_QueryInterface(aNode2);
+  return InDifferentTableElements(node1, node2);
+}
+
+bool
+nsHTMLEditRules::InDifferentTableElements(nsINode* aNode1, nsINode* aNode2)
+{
+  MOZ_ASSERT(aNode1 && aNode2);
+
+  while (aNode1 && !nsHTMLEditUtils::IsTableElement(aNode1)) {
+    aNode1 = aNode1->GetNodeParent();
+  }
+
+  while (aNode2 && !nsHTMLEditUtils::IsTableElement(aNode2)) {
+    aNode2 = aNode2->GetNodeParent();
+  }
+
+  return aNode1 != aNode2;
 }
 
 
 nsresult 
 nsHTMLEditRules::RemoveEmptyNodes()
 {
   // some general notes on the algorithm used here: the goal is to examine all the
   // nodes in mDocChangeRange, and remove the empty ones.  We do this by using a
--- a/editor/libeditor/html/nsHTMLEditRules.h
+++ b/editor/libeditor/html/nsHTMLEditRules.h
@@ -303,17 +303,25 @@ protected:
   nsresult AdjustWhitespace(nsISelection *aSelection);
   nsresult PinSelectionToNewBlock(nsISelection *aSelection);
   nsresult CheckInterlinePosition(nsISelection *aSelection);
   nsresult AdjustSelection(nsISelection *aSelection, nsIEditor::EDirection aAction);
   nsresult FindNearSelectableNode(nsIDOMNode *aSelNode, 
                                   PRInt32 aSelOffset, 
                                   nsIEditor::EDirection &aDirection,
                                   nsCOMPtr<nsIDOMNode> *outSelectableNode);
-  nsresult InDifferentTableElements(nsIDOMNode *aNode1, nsIDOMNode *aNode2, bool *aResult);
+  /**
+   * Returns true if aNode1 or aNode2 or both is the descendant of some type of
+   * table element, but their nearest table element ancestors differ.  "Table
+   * element" here includes not just <table> but also <td>, <tbody>, <tr>, etc.
+   * The nodes count as being their own descendants for this purpose, so a
+   * table element is its own nearest table element ancestor.
+   */
+  bool     InDifferentTableElements(nsIDOMNode* aNode1, nsIDOMNode* aNode2);
+  bool     InDifferentTableElements(nsINode* aNode1, nsINode* aNode2);
   nsresult RemoveEmptyNodes();
   nsresult SelectionEndpointInNode(nsINode *aNode, bool *aResult);
   nsresult UpdateDocChangeRange(nsIDOMRange *aRange);
   nsresult ConfirmSelectionInBody();
   nsresult InsertMozBRIfNeeded(nsIDOMNode *aNode);
   bool     IsEmptyInline(nsIDOMNode *aNode);
   bool     ListIsEmptyLine(nsCOMArray<nsIDOMNode> &arrayOfNodes);
   nsresult RemoveAlignment(nsIDOMNode * aNode, const nsAString & aAlignType, bool aChildrenOnly);