Bug 1190172 part 11 - Clean up nsHTMLEditRules::IsEmptyBlock; r=ehsan
authorAryeh Gregor <ayg@aryeh.name>
Sun, 01 May 2016 16:13:27 +0300
changeset 295637 66bdae3372cea5dd69adbdca7c77c7f470ea5040
parent 295636 6dabbd41ffa254694a008a45517c7de38a9a3e11
child 295638 34765a76738e04e7df6d1cdb4a46bbaf5549407d
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
bugs1190172
milestone49.0a1
Bug 1190172 part 11 - Clean up nsHTMLEditRules::IsEmptyBlock; r=ehsan
editor/libeditor/nsHTMLEditRules.cpp
editor/libeditor/nsHTMLEditRules.h
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -1551,17 +1551,17 @@ nsHTMLEditRules::WillInsertBreak(Selecti
     return NS_OK;
   }
 
   // If block is empty, populate with br.  (For example, imagine a div that
   // contains the word "text".  The user selects "text" and types return.
   // "Text" is deleted leaving an empty block.  We want to put in one br to
   // make block have a line.  Then code further below will put in a second br.)
   bool isEmpty;
-  IsEmptyBlock(GetAsDOMNode(blockParent), &isEmpty);
+  IsEmptyBlock(*blockParent, &isEmpty);
   if (isEmpty) {
     nsCOMPtr<Element> br = mHTMLEditor->CreateBR(blockParent,
                                                  blockParent->Length());
     NS_ENSURE_STATE(br);
   }
 
   nsCOMPtr<Element> listItem = IsInListItem(blockParent);
   if (listItem && listItem != host) {
@@ -4521,39 +4521,34 @@ nsHTMLEditRules::CreateStyleForInsertTex
   if (weDidSomething) {
     return aSelection.Collapse(node, offset);
   }
 
   return NS_OK;
 }
 
 
-///////////////////////////////////////////////////////////////////////////
-// IsEmptyBlock: figure out if aNode is (or is inside) an empty block.
-//               A block can have children and still be considered empty,
-//               if the children are empty or non-editable.
-//
+/**
+ * Figure out if aNode is (or is inside) an empty block.  A block can have
+ * children and still be considered empty, if the children are empty or
+ * non-editable.
+ */
 nsresult
-nsHTMLEditRules::IsEmptyBlock(nsIDOMNode *aNode,
-                              bool *outIsEmptyBlock,
-                              bool aMozBRDoesntCount,
-                              bool aListItemsNotEmpty)
-{
-  NS_ENSURE_TRUE(aNode && outIsEmptyBlock, NS_ERROR_NULL_POINTER);
-  *outIsEmptyBlock = true;
-
-//  nsresult res = NS_OK;
-  nsCOMPtr<nsIDOMNode> nodeToTest;
-  if (IsBlockNode(aNode)) nodeToTest = do_QueryInterface(aNode);
-//  else nsCOMPtr<nsIDOMElement> block;
-//  looks like I forgot to finish this.  Wonder what I was going to do?
-
-  NS_ENSURE_TRUE(nodeToTest, NS_ERROR_NULL_POINTER);
-  return mHTMLEditor->IsEmptyNode(nodeToTest, outIsEmptyBlock,
-                     aMozBRDoesntCount, aListItemsNotEmpty);
+nsHTMLEditRules::IsEmptyBlock(Element& aNode,
+                              bool* aOutIsEmptyBlock,
+                              MozBRCounts aMozBRCounts)
+{
+  MOZ_ASSERT(aOutIsEmptyBlock);
+  *aOutIsEmptyBlock = true;
+
+  NS_ENSURE_TRUE(IsBlockNode(aNode.AsDOMNode()), NS_ERROR_NULL_POINTER);
+
+  return mHTMLEditor->IsEmptyNode(aNode.AsDOMNode(), aOutIsEmptyBlock,
+                                  aMozBRCounts == MozBRCounts::yes ? false
+                                                                   : true);
 }
 
 
 nsresult
 nsHTMLEditRules::WillAlign(Selection& aSelection,
                            const nsAString& aAlignType,
                            bool* aCancel,
                            bool* aHandled)
@@ -6321,17 +6316,17 @@ nsHTMLEditRules::ReturnInHeader(Selectio
     if (isEmptyNode) {
       res = CreateMozBR(prevItem->AsDOMNode(), 0);
       NS_ENSURE_SUCCESS(res, res);
     }
   }
 
   // If the new (righthand) header node is empty, delete it
   bool isEmpty;
-  res = IsEmptyBlock(aHeader.AsDOMNode(), &isEmpty, true);
+  res = IsEmptyBlock(aHeader, &isEmpty, MozBRCounts::no);
   NS_ENSURE_SUCCESS(res, res);
   if (isEmpty) {
     res = mHTMLEditor->DeleteNode(&aHeader);
     NS_ENSURE_SUCCESS(res, res);
     // Layout tells the caret to blink in a weird place if we don't place a
     // break after the header.
     nsCOMPtr<nsIContent> sibling =
       mHTMLEditor->GetNextHTMLSibling(headerParent, offset + 1);
@@ -6574,17 +6569,17 @@ nsHTMLEditRules::ReturnInListItem(Select
 
   nsCOMPtr<Element> list = aListItem.GetParentElement();
   int32_t itemOffset = list ? list->IndexOf(&aListItem) : -1;
 
   // If we are in an empty item, then we want to pop up out of the list, but
   // only if prefs say it's okay and if the parent isn't the active editing
   // host.
   bool isEmpty;
-  nsresult res = IsEmptyBlock(aListItem.AsDOMNode(), &isEmpty, true, false);
+  nsresult res = IsEmptyBlock(aListItem, &isEmpty, MozBRCounts::no);
   NS_ENSURE_SUCCESS(res, res);
   if (isEmpty && root != list && mReturnInEmptyLIKillsList) {
     // Get the list offset now -- before we might eventually split the list
     nsCOMPtr<nsINode> listParent = list->GetParentNode();
     int32_t offset = listParent ? listParent->IndexOf(list) : -1;
 
     // Are we the last list item in the list?
     bool isLast;
--- a/editor/libeditor/nsHTMLEditRules.h
+++ b/editor/libeditor/nsHTMLEditRules.h
@@ -244,20 +244,20 @@ protected:
                            nsIAtom* aItemType);
   nsresult ConvertListType(mozilla::dom::Element* aList,
                            mozilla::dom::Element** aOutList,
                            nsIAtom* aListType,
                            nsIAtom* aItemType);
 
   nsresult CreateStyleForInsertText(mozilla::dom::Selection& aSelection,
                                     nsIDocument& aDoc);
-  nsresult IsEmptyBlock(nsIDOMNode *aNode,
-                        bool *outIsEmptyBlock,
-                        bool aMozBRDoesntCount = false,
-                        bool aListItemsNotEmpty = false);
+  enum class MozBRCounts { yes, no };
+  nsresult IsEmptyBlock(mozilla::dom::Element& aNode,
+                        bool* aOutIsEmptyBlock,
+                        MozBRCounts aMozBRCounts = MozBRCounts::yes);
   nsresult CheckForEmptyBlock(nsINode* aStartNode,
                               mozilla::dom::Element* aBodyNode,
                               mozilla::dom::Selection* aSelection,
                               nsIEditor::EDirection aAction,
                               bool* aHandled);
   nsresult CheckForInvisibleBR(nsIDOMNode *aBlock, nsHTMLEditRules::BRLocation aWhere,
                                nsCOMPtr<nsIDOMNode> *outBRNode, int32_t aOffset=0);
   nsresult ExpandSelectionForDeletion(mozilla::dom::Selection* aSelection);