Bug 755264 - Use nsINode in nsHTMLEditRules::DeleteNonTableElements; r=ehsan
authorMs2ger <ms2ger@gmail.com>
Fri, 18 May 2012 10:29:39 +0200
changeset 94318 270ac87cffba7322a47bedb244153dd81b6bb836
parent 94317 b0f2d35f10e94e0945b66009cc29082d9d4df579
child 94319 26033f7ad48d96c9d14ea9603f79a5ef4f86dec8
push id22706
push userMs2ger@gmail.com
push dateFri, 18 May 2012 08:31:47 +0000
treeherdermozilla-central@4b74c82dd5ab [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs755264
milestone15.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 755264 - Use nsINode in nsHTMLEditRules::DeleteNonTableElements; r=ehsan
editor/libeditor/html/nsHTMLEditRules.cpp
editor/libeditor/html/nsHTMLEditRules.h
editor/libeditor/html/nsHTMLEditUtils.cpp
editor/libeditor/html/nsHTMLEditUtils.h
--- a/editor/libeditor/html/nsHTMLEditRules.cpp
+++ b/editor/libeditor/html/nsHTMLEditRules.cpp
@@ -2398,30 +2398,31 @@ nsHTMLEditRules::WillDeleteSelection(nsI
           res = iter.Init(range);
           NS_ENSURE_SUCCESS(res, res);
           res = iter.AppendList(functor, arrayOfNodes);
           NS_ENSURE_SUCCESS(res, res);
       
           // now that we have the list, delete non table elements
           PRInt32 listCount = arrayOfNodes.Count();
           for (PRInt32 j = 0; j < listCount; j++) {
-            nsIDOMNode* somenode = arrayOfNodes[0];
-            res = DeleteNonTableElements(somenode);
+            nsCOMPtr<nsINode> somenode = do_QueryInterface(arrayOfNodes[0]);
+            NS_ENSURE_STATE(somenode);
+            DeleteNonTableElements(somenode);
             arrayOfNodes.RemoveObjectAt(0);
             // If something visible is deleted, no need to join.
             // Visible means all nodes except non-visible textnodes and breaks.
             if (join && origCollapsed) {
               nsCOMPtr<nsIContent> content = do_QueryInterface(somenode);
               if (!content) {
                 join = false;
               } else if (content->NodeType() == nsIDOMNode::TEXT_NODE) {
                 mHTMLEditor->IsVisTextNode(content, &join, true);
               } else {
                 join = content->IsHTML(nsGkAtoms::br) &&
-                       !mHTMLEditor->IsVisBreak(somenode);
+                       !mHTMLEditor->IsVisBreak(somenode->AsDOMNode());
               }
             }
           }
         }
         
         // check endopints for possible text deletion.
         // we can assume that if text node is found, we can
         // delete to end or to begining as appropriate,
@@ -2845,46 +2846,31 @@ nsHTMLEditRules::MoveContents(nsIDOMNode
     NS_ENSURE_SUCCESS(res, res);
     aSource->GetFirstChild(getter_AddRefs(child));
   }
   return NS_OK;
 }
 
 
 nsresult
-nsHTMLEditRules::DeleteNonTableElements(nsIDOMNode *aNode)
-{
-  NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
-  nsresult res = NS_OK;
-  if (nsHTMLEditUtils::IsTableElementButNotTable(aNode))
-  {
-    nsCOMPtr<nsIDOMNodeList> children;
-    aNode->GetChildNodes(getter_AddRefs(children));
-    if (children)
-    {
-      PRUint32 len;
-      children->GetLength(&len);
-      NS_ENSURE_TRUE(len, NS_OK);
-      PRInt32 j;
-      for (j=len-1; j>=0; j--)
-      {
-        nsCOMPtr<nsIDOMNode> node;
-        children->Item(j,getter_AddRefs(node));
-        res = DeleteNonTableElements(node);
-        NS_ENSURE_SUCCESS(res, res);
-
-      }
-    }
-  }
-  else
-  {
-    res = mHTMLEditor->DeleteNode(aNode);
-    NS_ENSURE_SUCCESS(res, res);
-  }
-  return res;
+nsHTMLEditRules::DeleteNonTableElements(nsINode* aNode)
+{
+  MOZ_ASSERT(aNode);
+  if (!aNode->IsElement() ||
+      !nsHTMLEditUtils::IsTableElementButNotTable(aNode->AsElement())) {
+    return mHTMLEditor->DeleteNode(aNode->AsDOMNode());
+  }
+
+  for (nsIContent* child = aNode->GetLastChild();
+       child;
+       child = child->GetPreviousSibling()) {
+    nsresult rv = DeleteNonTableElements(child);
+    NS_ENSURE_SUCCESS(rv, rv);
+  }
+  return NS_OK;
 }
 
 nsresult
 nsHTMLEditRules::DidDeleteSelection(nsISelection *aSelection, 
                                     nsIEditor::EDirection aDir, 
                                     nsresult aResult)
 {
   if (!aSelection) { return NS_ERROR_NULL_POINTER; }
--- a/editor/libeditor/html/nsHTMLEditRules.h
+++ b/editor/libeditor/html/nsHTMLEditRules.h
@@ -162,17 +162,17 @@ protected:
                               nsresult aResult);
   nsresult InsertBRIfNeeded(nsISelection *aSelection);
   nsresult GetGoodSelPointForNode(nsIDOMNode *aNode, nsIEditor::EDirection aAction, 
                                   nsCOMPtr<nsIDOMNode> *outSelNode, PRInt32 *outSelOffset);
   nsresult JoinBlocks(nsCOMPtr<nsIDOMNode> *aLeftBlock, nsCOMPtr<nsIDOMNode> *aRightBlock, bool *aCanceled);
   nsresult MoveBlock(nsIDOMNode *aLeft, nsIDOMNode *aRight, PRInt32 aLeftOffset, PRInt32 aRightOffset);
   nsresult MoveNodeSmart(nsIDOMNode *aSource, nsIDOMNode *aDest, PRInt32 *aOffset);
   nsresult MoveContents(nsIDOMNode *aSource, nsIDOMNode *aDest, PRInt32 *aOffset);
-  nsresult DeleteNonTableElements(nsIDOMNode *aNode);
+  nsresult DeleteNonTableElements(nsINode* aNode);
   nsresult WillMakeList(nsISelection *aSelection, const nsAString *aListType, bool aEntireList, const nsAString *aBulletType, bool *aCancel, bool *aHandled, const nsAString *aItemType=nsnull);
   nsresult WillRemoveList(nsISelection *aSelection, bool aOrderd, bool *aCancel, bool *aHandled);
   nsresult WillIndent(nsISelection *aSelection, bool *aCancel, bool *aHandled);
   nsresult WillCSSIndent(nsISelection *aSelection, bool *aCancel, bool *aHandled);
   nsresult WillHTMLIndent(nsISelection *aSelection, bool *aCancel, bool *aHandled);
   nsresult WillOutdent(nsISelection *aSelection, bool *aCancel, bool *aHandled);
   nsresult WillAlign(nsISelection *aSelection, const nsAString *alignType, bool *aCancel, bool *aHandled);
   nsresult WillAbsolutePosition(nsISelection *aSelection, bool *aCancel, bool * aHandled);
--- a/editor/libeditor/html/nsHTMLEditUtils.cpp
+++ b/editor/libeditor/html/nsHTMLEditUtils.cpp
@@ -239,17 +239,25 @@ nsHTMLEditUtils::IsTableElement(dom::Ele
 
 ///////////////////////////////////////////////////////////////////////////
 // IsTableElementButNotTable: true if node an html td, tr, ... (doesn't include table)
 //                  
 bool 
 nsHTMLEditUtils::IsTableElementButNotTable(nsIDOMNode *node)
 {
   NS_PRECONDITION(node, "null node passed to nsHTMLEditor::IsTableElementButNotTable");
-  nsCOMPtr<nsIAtom> nodeAtom = nsEditor::GetTag(node);
+  nsCOMPtr<dom::Element> element = do_QueryInterface(node);
+  return element && IsTableElementButNotTable(element);
+}
+
+bool
+nsHTMLEditUtils::IsTableElementButNotTable(dom::Element* aNode)
+{
+  MOZ_ASSERT(aNode);
+  nsCOMPtr<nsIAtom> nodeAtom = aNode->Tag();
   return (nodeAtom == nsEditProperty::tr)
       || (nodeAtom == nsEditProperty::td)
       || (nodeAtom == nsEditProperty::th)
       || (nodeAtom == nsEditProperty::thead)
       || (nodeAtom == nsEditProperty::tfoot)
       || (nodeAtom == nsEditProperty::tbody)
       || (nodeAtom == nsEditProperty::caption);
 }
--- a/editor/libeditor/html/nsHTMLEditUtils.h
+++ b/editor/libeditor/html/nsHTMLEditUtils.h
@@ -65,16 +65,17 @@ public:
   static bool IsParagraph(nsIDOMNode *aNode);
   static bool IsHR(nsIDOMNode *aNode);
   static bool IsListItem(mozilla::dom::Element* aNode);
   static bool IsListItem(nsIDOMNode *aNode);
   static bool IsTable(nsIDOMNode *aNode);
   static bool IsTableRow(nsIDOMNode *aNode);
   static bool IsTableElement(mozilla::dom::Element* aNode);
   static bool IsTableElement(nsIDOMNode *aNode);
+  static bool IsTableElementButNotTable(mozilla::dom::Element* aNode);
   static bool IsTableElementButNotTable(nsIDOMNode *aNode);
   static bool IsTableCell(mozilla::dom::Element* node);
   static bool IsTableCell(nsIDOMNode *aNode);
   static bool IsTableCellOrCaption(nsIDOMNode *aNode);
   static bool IsList(mozilla::dom::Element* aNode);
   static bool IsList(nsIDOMNode *aNode);
   static bool IsOrderedList(nsIDOMNode *aNode);
   static bool IsUnorderedList(nsIDOMNode *aNode);