Bug 1149163 part 6 - Clean up nsHTMLEditRules::LookInsideDivBQandList; r=froydnj
authorAryeh Gregor <ayg@aryeh.name>
Fri, 24 Apr 2015 14:27:34 +0300
changeset 259341 c91c9f72c54592be58a184deddfd98e257ef72f8
parent 259340 ced7f5966cd55c508544508b64b7d0aebd891d60
child 259342 a9315e45f4e9b1278da89d9db3d828e59040866c
push id1728
push usermartin.thomson@gmail.com
push dateFri, 24 Apr 2015 18:56:35 +0000
reviewersfroydnj
bugs1149163
milestone40.0a1
Bug 1149163 part 6 - Clean up nsHTMLEditRules::LookInsideDivBQandList; r=froydnj
editor/libeditor/nsHTMLEditRules.cpp
editor/libeditor/nsHTMLEditRules.h
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -3088,41 +3088,41 @@ nsHTMLEditRules::WillMakeList(Selection*
 
   *aHandled = true;
 
   res = NormalizeSelection(aSelection);
   NS_ENSURE_SUCCESS(res, res);
   NS_ENSURE_STATE(mHTMLEditor);
   nsAutoSelectionReset selectionResetter(aSelection, mHTMLEditor);
 
-  nsCOMArray<nsIDOMNode> arrayOfNodes;
-  res = GetListActionNodes(arrayOfNodes, aEntireList);
-  NS_ENSURE_SUCCESS(res, res);
-
-  int32_t listCount = arrayOfNodes.Count();
+  nsCOMArray<nsIDOMNode> arrayOfDOMNodes;
+  res = GetListActionNodes(arrayOfDOMNodes, aEntireList);
+  NS_ENSURE_SUCCESS(res, res);
+
+  int32_t listCount = arrayOfDOMNodes.Count();
 
   // check if all our nodes are <br>s, or empty inlines
   bool bOnlyBreaks = true;
   for (int32_t j = 0; j < listCount; j++) {
-    nsIDOMNode* curNode = arrayOfNodes[j];
+    nsIDOMNode* curNode = arrayOfDOMNodes[j];
     // if curNode is not a Break or empty inline, we're done
     if (!nsTextEditUtils::IsBreak(curNode) && !IsEmptyInline(curNode)) {
       bOnlyBreaks = false;
       break;
     }
   }
 
   // if no nodes, we make empty list.  Ditto if the user tried to make a list
   // of some # of breaks.
   if (!listCount || bOnlyBreaks) {
     // if only breaks, delete them
     if (bOnlyBreaks) {
       for (int32_t j = 0; j < (int32_t)listCount; j++) {
         NS_ENSURE_STATE(mHTMLEditor);
-        res = mHTMLEditor->DeleteNode(arrayOfNodes[j]);
+        res = mHTMLEditor->DeleteNode(arrayOfDOMNodes[j]);
         NS_ENSURE_SUCCESS(res, res);
       }
     }
 
     // get selection location
     NS_ENSURE_STATE(aSelection->RangeCount());
     nsCOMPtr<nsINode> parent = aSelection->GetRangeAt(0)->GetStartParent();
     int32_t offset = aSelection->GetRangeAt(0)->StartOffset();
@@ -3154,31 +3154,36 @@ nsHTMLEditRules::WillMakeList(Selection*
     selectionResetter.Abort();
     *aHandled = true;
     return res;
   }
 
   // if there is only one node in the array, and it is a list, div, or
   // blockquote, then look inside of it until we find inner list or content.
 
-  res = LookInsideDivBQandList(arrayOfNodes);
-  NS_ENSURE_SUCCESS(res, res);
+  nsTArray<nsCOMPtr<nsINode>> arrayOfNodes;
+  for (int32_t i = 0; i < arrayOfDOMNodes.Count(); i++) {
+    nsCOMPtr<nsINode> node = do_QueryInterface(arrayOfDOMNodes[i]);
+    NS_ENSURE_STATE(node);
+    arrayOfNodes.AppendElement(node);
+  }
+  LookInsideDivBQandList(arrayOfNodes);
 
   // Ok, now go through all the nodes and put then in the list,
   // or whatever is approriate.  Wohoo!
 
-  listCount = arrayOfNodes.Count();
+  listCount = arrayOfNodes.Length();
   nsCOMPtr<nsINode> curParent;
   nsCOMPtr<Element> curList, prevListItem;
 
   for (int32_t i = 0; i < listCount; i++) {
     // here's where we actually figure out what to do
     nsCOMPtr<nsIDOMNode> newBlock;
-    nsCOMPtr<nsIContent> curNode = do_QueryInterface(arrayOfNodes[i]);
-    NS_ENSURE_STATE(curNode);
+    NS_ENSURE_STATE(arrayOfNodes[i]->IsContent());
+    nsCOMPtr<nsIContent> curNode = arrayOfNodes[i]->AsContent();
     int32_t 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 && InDifferentTableElements(curList, curNode)) {
       curList = nullptr;
     }
@@ -3288,22 +3293,21 @@ nsHTMLEditRules::WillMakeList(Selection*
       continue;
     }
 
     // if we hit a div clear our prevListItem, insert divs contents
     // into our node array, and remove the div
     if (curNode->IsHTMLElement(nsGkAtoms::div)) {
       prevListItem = nullptr;
       int32_t j = i + 1;
-      res = GetInnerContent(curNode->AsDOMNode(), arrayOfNodes, &j);
-      NS_ENSURE_SUCCESS(res, res);
+      GetInnerContent(*curNode, arrayOfNodes, &j);
       NS_ENSURE_STATE(mHTMLEditor);
       res = mHTMLEditor->RemoveContainer(curNode);
       NS_ENSURE_SUCCESS(res, res);
-      listCount = arrayOfNodes.Count();
+      listCount = arrayOfNodes.Length();
       continue;
     }
 
     // need to make a list to put things in if we haven't already,
     if (!curList) {
       res = SplitAsNeeded(listType, curParent, offset);
       NS_ENSURE_SUCCESS(res, res);
       NS_ENSURE_STATE(mHTMLEditor);
@@ -6113,77 +6117,78 @@ nsHTMLEditRules::GetListActionNodes(nsCO
       outArrayOfNodes.RemoveObjectAt(i);
       res = GetInnerContent(testNode, outArrayOfNodes, &j, false);
       NS_ENSURE_SUCCESS(res, res);
     }
   }
 
   // if there is only one node in the array, and it is a list, div, or blockquote,
   // then look inside of it until we find inner list or content.
-  res = LookInsideDivBQandList(outArrayOfNodes);
-  return res;
-}
-
-
-///////////////////////////////////////////////////////////////////////////
-// LookInsideDivBQandList: 
-//                       
-nsresult 
-nsHTMLEditRules::LookInsideDivBQandList(nsCOMArray<nsIDOMNode>& aNodeArray)
-{
-  // if there is only one node in the array, and it is a list, div, or blockquote,
-  // then look inside of it until we find inner list or content.
-  int32_t listCount = aNodeArray.Count();
+  nsTArray<nsCOMPtr<nsINode>> arrayOfNodes;
+  for (int32_t i = 0; i < outArrayOfNodes.Count(); i++) {
+    nsCOMPtr<nsINode> node = do_QueryInterface(outArrayOfNodes[i]);
+    NS_ENSURE_STATE(node);
+    arrayOfNodes.AppendElement(node);
+  }
+  LookInsideDivBQandList(arrayOfNodes);
+  outArrayOfNodes.Clear();
+  for (auto& node : arrayOfNodes) {
+    outArrayOfNodes.AppendObject(GetAsDOMNode(node));
+  }
+  return NS_OK;
+}
+
+
+void
+nsHTMLEditRules::LookInsideDivBQandList(nsTArray<nsCOMPtr<nsINode>>& aNodeArray)
+{
+  NS_ENSURE_TRUE(mHTMLEditor, );
+  nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
+
+  // If there is only one node in the array, and it is a list, div, or
+  // blockquote, then look inside of it until we find inner list or content.
+  int32_t listCount = aNodeArray.Length();
   if (listCount != 1) {
-    return NS_OK;
-  }
-
-  nsCOMPtr<nsINode> curNode = do_QueryInterface(aNodeArray[0]);
-  NS_ENSURE_STATE(curNode);
-
-  while (curNode->IsElement() &&
-         (curNode->IsHTMLElement(nsGkAtoms::div) ||
-          nsHTMLEditUtils::IsList(curNode) ||
-          curNode->IsHTMLElement(nsGkAtoms::blockquote))) {
-    // dive as long as there is only one child, and it is a list, div, blockquote
-    NS_ENSURE_STATE(mHTMLEditor);
+    return;
+  }
+
+  nsCOMPtr<nsINode> curNode = aNodeArray[0];
+
+  while (curNode->IsHTMLElement(nsGkAtoms::div) ||
+         nsHTMLEditUtils::IsList(curNode) ||
+         curNode->IsHTMLElement(nsGkAtoms::blockquote)) {
+    // Dive as long as there's only one child, and it's a list, div, blockquote
     uint32_t numChildren = mHTMLEditor->CountEditableChildren(curNode);
     if (numChildren != 1) {
       break;
     }
 
-    // keep diving
-    // XXX One would expect to dive into the one editable node.
-    nsIContent* tmp = curNode->GetFirstChild();
-    if (!tmp->IsElement()) {
+    // Keep diving!  XXX One would expect to dive into the one editable node.
+    nsCOMPtr<nsIContent> child = curNode->GetFirstChild();
+    if (!child->IsHTMLElement(nsGkAtoms::div) &&
+        !nsHTMLEditUtils::IsList(child) &&
+        !child->IsHTMLElement(nsGkAtoms::blockquote)) {
       break;
     }
 
-    dom::Element* element = tmp->AsElement();
-    if (!element->IsHTMLElement(nsGkAtoms::div) &&
-        !nsHTMLEditUtils::IsList(element) &&
-        !element->IsHTMLElement(nsGkAtoms::blockquote)) {
-      break;
-    }
-
-    // check editablility XXX floppy moose
-    curNode = tmp;
-  }
-
-  // we've found innermost list/blockquote/div: 
-  // replace the one node in the array with these nodes
-  aNodeArray.RemoveObjectAt(0);
+    // check editability XXX floppy moose
+    curNode = child;
+  }
+
+  // We've found innermost list/blockquote/div: replace the one node in the
+  // array with these nodes
+  aNodeArray.RemoveElementAt(0);
   if (curNode->IsAnyOfHTMLElements(nsGkAtoms::div,
                                    nsGkAtoms::blockquote)) {
     int32_t j = 0;
-    return GetInnerContent(curNode->AsDOMNode(), aNodeArray, &j, false, false);
-  }
-
-  aNodeArray.AppendObject(curNode->AsDOMNode());
-  return NS_OK;
+    GetInnerContent(*curNode, aNodeArray, &j, Lists::no, Tables::no);
+    return;
+  }
+
+  aNodeArray.AppendElement(curNode);
 }
 
 
 ///////////////////////////////////////////////////////////////////////////
 // GetDefinitionListItemTypes: 
 //                       
 void
 nsHTMLEditRules::GetDefinitionListItemTypes(dom::Element* aElement, bool* aDT, bool* aDD)
--- a/editor/libeditor/nsHTMLEditRules.h
+++ b/editor/libeditor/nsHTMLEditRules.h
@@ -289,17 +289,17 @@ protected:
                              bool dontTouchContent);
   nsresult GetNodesFromSelection(mozilla::dom::Selection* selection,
                                  EditAction operation,
                                  nsCOMArray<nsIDOMNode>& arrayOfNodes,
                                  bool aDontTouchContent=false);
   nsresult GetListActionNodes(nsCOMArray<nsIDOMNode> &outArrayOfNodes, bool aEntireList, bool aDontTouchContent=false);
   void GetDefinitionListItemTypes(mozilla::dom::Element* aElement, bool* aDT, bool* aDD);
   nsresult GetParagraphFormatNodes(nsCOMArray<nsIDOMNode>& outArrayOfNodes, bool aDontTouchContent=false);
-  nsresult LookInsideDivBQandList(nsCOMArray<nsIDOMNode>& aNodeArray);
+  void LookInsideDivBQandList(nsTArray<nsCOMPtr<nsINode>>& aNodeArray);
   nsresult BustUpInlinesAtRangeEndpoints(nsRangeStore &inRange);
   nsresult BustUpInlinesAtBRs(nsINode& aNode,
                               nsTArray<nsCOMPtr<nsINode>>& aOutArrayOfNodes);
   nsCOMPtr<nsIDOMNode> GetHighestInlineParent(nsIDOMNode* aNode);
   nsresult MakeTransitionList(nsCOMArray<nsIDOMNode>& inArrayOfNodes, 
                               nsTArray<bool> &inTransitionArray);
   nsresult RemoveBlockStyle(nsCOMArray<nsIDOMNode>& arrayOfNodes);
   nsresult ApplyBlockStyle(nsCOMArray<nsIDOMNode>& arrayOfNodes, const nsAString *aBlockTag);