Bug 1149163 part 1 - Clean up nsHTMLEditRules::GetInnerContent; r=froydnj
☠☠ backed out by 329dd852c06b ☠ ☠
authorAryeh Gregor <ayg@aryeh.name>
Wed, 22 Apr 2015 14:26:57 +0300
changeset 240496 4392cf02f1c07e79475cb621c861aa8efa6912e4
parent 240495 ce2a46e97e38e8866cd8598742522dcccd312d14
child 240497 bc75a87a2b7d47a26e832682c13841ea7b97703f
push id28636
push userkwierso@gmail.com
push dateThu, 23 Apr 2015 00:16:12 +0000
treeherdermozilla-central@a5af73b32ac8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1149163
milestone40.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 1149163 part 1 - Clean up nsHTMLEditRules::GetInnerContent; r=froydnj
editor/libeditor/nsHTMLEditRules.cpp
editor/libeditor/nsHTMLEditRules.h
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -8,16 +8,17 @@
 
 #include <stdlib.h>
 
 #include "mozilla/Assertions.h"
 #include "mozilla/MathAlgorithms.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/dom/Selection.h"
 #include "mozilla/dom/Element.h"
+#include "mozilla/dom/OwningNonNull.h"
 #include "mozilla/mozalloc.h"
 #include "nsAString.h"
 #include "nsAlgorithm.h"
 #include "nsCOMArray.h"
 #include "nsCRT.h"
 #include "nsCRTGlue.h"
 #include "nsComponentManagerUtils.h"
 #include "nsContentUtils.h"
@@ -5153,54 +5154,68 @@ nsHTMLEditRules::CheckForInvisibleBR(nsI
       *outBRNode = GetAsDOMNode(wsTester.mStartReasonNode);
     }
   }
 
   return NS_OK;
 }
 
 
-///////////////////////////////////////////////////////////////////////////
-// GetInnerContent: aList and aTbl allow the caller to specify what kind 
-//                  of content to "look inside".  If aTbl is true, look inside
-//                  any table content, and insert the inner content into the
-//                  supplied issupportsarray at offset aIndex.  
-//                  Similarly with aList and list content.
-//                  aIndex is updated to point past inserted elements.
-//                  
+////////////////////////////////////////////////////////////////////////////////
+// GetInnerContent: aList and aTbl allow the caller to specify what kind of
+//                  content to "look inside".  If aTbl is true, look inside any
+//                  table content, and insert the inner content into the
+//                  supplied issupportsarray at offset aIndex.  Similarly with
+//                  aList and list content.  aIndex is updated to point past
+//                  inserted elements.
+//
 nsresult
-nsHTMLEditRules::GetInnerContent(nsIDOMNode *aNode, nsCOMArray<nsIDOMNode> &outArrayOfNodes, 
-                                 int32_t *aIndex, bool aList, bool aTbl)
-{
-  nsCOMPtr<nsINode> aNode_ = do_QueryInterface(aNode);
-  NS_ENSURE_TRUE(aNode_ && aIndex, NS_ERROR_NULL_POINTER);
-
-  nsCOMPtr<nsIDOMNode> node =
-    GetAsDOMNode(mHTMLEditor->GetFirstEditableChild(*aNode_));
-  nsresult res = NS_OK;
-  while (NS_SUCCEEDED(res) && node)
-  {
-    if (  ( aList && (nsHTMLEditUtils::IsList(node)     || 
-                      nsHTMLEditUtils::IsListItem(node) ) )
-       || ( aTbl && nsHTMLEditUtils::IsTableElement(node) )  )
-    {
-      res = GetInnerContent(node, outArrayOfNodes, aIndex, aList, aTbl);
-      NS_ENSURE_SUCCESS(res, res);
-    }
-    else
-    {
-      outArrayOfNodes.InsertObjectAt(node, *aIndex);
+nsHTMLEditRules::GetInnerContent(nsIDOMNode* aNode,
+                                 nsCOMArray<nsIDOMNode>& aOutArrayOfDOMNodes,
+                                 int32_t* aIndex, bool aLists, bool aTables)
+{
+  // To be removed in a subsequent patch
+  nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
+  NS_ENSURE_STATE(node);
+  nsTArray<nsCOMPtr<nsINode>> aOutArrayOfNodes;
+  for (int32_t i = 0; i < aOutArrayOfDOMNodes.Count(); i++) {
+    nsCOMPtr<nsINode> newNode = do_QueryInterface(aOutArrayOfDOMNodes[i]);
+    if (newNode) {
+      aOutArrayOfNodes.AppendElement(newNode);
+    }
+  }
+  GetInnerContent(*node, aOutArrayOfNodes, aIndex,
+                  aLists ? Lists::yes : Lists::no,
+                  aTables ? Tables::yes : Tables::no);
+  aOutArrayOfDOMNodes.Clear();
+  for (auto& node : aOutArrayOfNodes) {
+    aOutArrayOfDOMNodes.AppendElement(GetAsDOMNode(node));
+  }
+
+  return NS_OK;
+}
+
+void
+nsHTMLEditRules::GetInnerContent(nsINode& aNode,
+                                 nsTArray<nsCOMPtr<nsINode>>& aOutArrayOfNodes,
+                                 int32_t* aIndex, Lists aLists, Tables aTables)
+{
+  MOZ_ASSERT(aIndex);
+
+  for (nsCOMPtr<nsIContent> node = mHTMLEditor->GetFirstEditableChild(aNode);
+       node; node = node->GetNextSibling()) {
+    if ((aLists == Lists::yes && (nsHTMLEditUtils::IsList(node) ||
+                                  nsHTMLEditUtils::IsListItem(node))) ||
+        (aTables == Tables::yes && nsHTMLEditUtils::IsTableElement(node))) {
+      GetInnerContent(*node, aOutArrayOfNodes, aIndex, aLists, aTables);
+    } else {
+      aOutArrayOfNodes.InsertElementAt(*aIndex, node);
       (*aIndex)++;
     }
-    nsCOMPtr<nsIDOMNode> tmp;
-    res = node->GetNextSibling(getter_AddRefs(tmp));
-    node = tmp;
-  }
-
-  return res;
+  }
 }
 
 ///////////////////////////////////////////////////////////////////////////
 // ExpandSelectionForDeletion: this promotes our selection to include blocks
 // that have all their children selected.
 //                  
 nsresult
 nsHTMLEditRules::ExpandSelectionForDeletion(Selection* aSelection)
--- a/editor/libeditor/nsHTMLEditRules.h
+++ b/editor/libeditor/nsHTMLEditRules.h
@@ -193,17 +193,26 @@ protected:
   nsresult DidAbsolutePosition();
   nsresult AlignInnerBlocks(nsIDOMNode *aNode, const nsAString *alignType);
   nsresult AlignBlockContents(nsIDOMNode *aNode, const nsAString *alignType);
   nsresult AppendInnerFormatNodes(nsCOMArray<nsIDOMNode>& aArray,
                                   nsINode* aNode);
   nsresult AppendInnerFormatNodes(nsCOMArray<nsIDOMNode>& aArray,
                                   nsIDOMNode *aNode);
   nsresult GetFormatString(nsIDOMNode *aNode, nsAString &outFormat);
-  nsresult GetInnerContent(nsIDOMNode *aNode, nsCOMArray<nsIDOMNode>& outArrayOfNodes, int32_t *aIndex, bool aList = true, bool aTble = true);
+  enum class Lists { no, yes };
+  enum class Tables { no, yes };
+  nsresult GetInnerContent(nsIDOMNode* aNode,
+                           nsCOMArray<nsIDOMNode>& aOutArrayOfNodes,
+                           int32_t* aIndex, bool aLists = true,
+                           bool aTables = true);
+  void GetInnerContent(nsINode& aNode,
+                       nsTArray<nsCOMPtr<nsINode>>& aOutArrayOfNodes,
+                       int32_t* aIndex, Lists aLists = Lists::yes,
+                       Tables aTables = Tables::yes);
   already_AddRefed<nsIDOMNode> IsInListItem(nsIDOMNode* aNode);
   mozilla::dom::Element* IsInListItem(nsINode* aNode);
   nsresult ReturnInHeader(mozilla::dom::Selection* aSelection,
                           nsIDOMNode* aHeader, nsIDOMNode* aTextNode,
                           int32_t aOffset);
   nsresult ReturnInParagraph(mozilla::dom::Selection* aSelection,
                              nsIDOMNode* aHeader, nsIDOMNode* aTextNode,
                              int32_t aOffset, bool* aCancel, bool* aHandled);