Bug 1191354 part 4 - Clean up nsHTMLEditRules::AlignBlock; r=ehsan
authorAryeh Gregor <ayg@aryeh.name>
Sun, 01 May 2016 16:14:27 +0300
changeset 295642 6c6fd98812c7fe979ce4d0c6f9f38bc712d74d77
parent 295641 142b62061350cfb576d1dc1ab08b61b7245d3e3c
child 295643 47f7ab7dc09b3bd045c7f3357d11b4246ebb50ad
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
bugs1191354
milestone49.0a1
Bug 1191354 part 4 - Clean up nsHTMLEditRules::AlignBlock; r=ehsan
editor/libeditor/nsHTMLEditRules.cpp
editor/libeditor/nsHTMLEditRules.h
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -4584,18 +4584,17 @@ nsHTMLEditRules::WillAlign(Selection& aS
   if (nodeArray.Length() == 1) {
     OwningNonNull<nsINode> node = nodeArray[0];
 
     if (nsHTMLEditUtils::SupportsAlignAttr(GetAsDOMNode(node))) {
       // The node is a table element, an hr, a paragraph, a div or a section
       // header; in HTML 4, it can directly carry the ALIGN attribute and we
       // don't need to make a div! If we are in CSS mode, all the work is done
       // in AlignBlock
-      rv = AlignBlock(static_cast<nsIDOMElement*>(node->AsDOMNode()),
-                       &aAlignType, true);
+      rv = AlignBlock(*node->AsElement(), aAlignType, ContentsOnly::yes);
       NS_ENSURE_SUCCESS(rv, rv);
       return NS_OK;
     }
 
     if (nsTextEditUtils::IsBreak(node)) {
       // The special case emptyDiv code (below) that consumes BRs can cause
       // tables to split if the start node of the selection is not in a table
       // cell or caption, for example parent is a <tr>.  Avoid this unnecessary
@@ -4642,18 +4641,17 @@ nsHTMLEditRules::WillAlign(Selection& aS
       }
     }
     nsCOMPtr<Element> div = mHTMLEditor->CreateNode(nsGkAtoms::div, parent,
                                                     offset);
     NS_ENSURE_STATE(div);
     // Remember our new block for postprocessing
     mNewBlock = div->AsDOMNode();
     // Set up the alignment on the div, using HTML or CSS
-    rv = AlignBlock(static_cast<nsIDOMElement*>(GetAsDOMNode(div)),
-                    &aAlignType, true);
+    rv = AlignBlock(*div, aAlignType, ContentsOnly::yes);
     NS_ENSURE_SUCCESS(rv, rv);
     *aHandled = true;
     // Put in a moz-br so that it won't get deleted
     rv = CreateMozBR(div->AsDOMNode(), 0);
     NS_ENSURE_SUCCESS(rv, rv);
     rv = aSelection.Collapse(div, 0);
     // Don't reset our selection in this case.
     selectionResetter.Abort();
@@ -4681,18 +4679,17 @@ nsHTMLEditRules::WillAlign(Selection& aS
       continue;
     }
 
     // The node is a table element, an hr, a paragraph, a div or a section
     // header; in HTML 4, it can directly carry the ALIGN attribute and we
     // don't need to nest it, just set the alignment.  In CSS, assign the
     // corresponding CSS styles in AlignBlock
     if (nsHTMLEditUtils::SupportsAlignAttr(GetAsDOMNode(curNode))) {
-      rv = AlignBlock(static_cast<nsIDOMElement*>(curNode->AsDOMNode()),
-                      &aAlignType, false);
+      rv = AlignBlock(*curNode->AsElement(), aAlignType, ContentsOnly::no);
       NS_ENSURE_SUCCESS(rv, rv);
       // Clear out curDiv so that we don't put nodes after this one into it
       curDiv = nullptr;
       continue;
     }
 
     nsCOMPtr<nsINode> curParent = curNode->GetParentNode();
     int32_t offset = curParent ? curParent->IndexOf(curNode) : -1;
@@ -4743,18 +4740,17 @@ nsHTMLEditRules::WillAlign(Selection& aS
 
       rv = SplitAsNeeded(*nsGkAtoms::div, curParent, offset);
       NS_ENSURE_SUCCESS(rv, rv);
       curDiv = mHTMLEditor->CreateNode(nsGkAtoms::div, curParent, offset);
       NS_ENSURE_STATE(curDiv);
       // Remember our new block for postprocessing
       mNewBlock = curDiv->AsDOMNode();
       // Set up the alignment on the div
-      rv = AlignBlock(static_cast<nsIDOMElement*>(curDiv->AsDOMNode()),
-                       &aAlignType, true);
+      rv = AlignBlock(*curDiv, aAlignType, ContentsOnly::yes);
     }
 
     NS_ENSURE_STATE(curNode->IsContent());
 
     // Tuck the node into the end of the active div
     rv = mHTMLEditor->MoveNode(curNode->AsContent(), curDiv, -1);
     NS_ENSURE_SUCCESS(rv, rv);
   }
@@ -8466,44 +8462,46 @@ nsHTMLEditRules::MakeSureElemStartsOrEnd
 {
   nsresult res = MakeSureElemStartsOrEndsOnCR(aNode, false);
   NS_ENSURE_SUCCESS(res, res);
   res = MakeSureElemStartsOrEndsOnCR(aNode, true);
   return res;
 }
 
 nsresult
-nsHTMLEditRules::AlignBlock(nsIDOMElement * aElement, const nsAString * aAlignType, bool aContentsOnly)
-{
-  NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER);
-
-  nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aElement);
-  bool isBlock = IsBlockNode(node);
-  if (!isBlock && !nsHTMLEditUtils::IsHR(node)) {
-    // we deal only with blocks; early way out
+nsHTMLEditRules::AlignBlock(Element& aElement, const nsAString& aAlignType,
+                            ContentsOnly aContentsOnly)
+{
+  if (!IsBlockNode(aElement.AsDOMNode()) &&
+      !aElement.IsHTMLElement(nsGkAtoms::hr)) {
+    // We deal only with blocks; early way out
     return NS_OK;
   }
 
-  nsresult res = RemoveAlignment(node, *aAlignType, aContentsOnly);
+  NS_ENSURE_STATE(mHTMLEditor);
+  nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
+
+  nsresult res = RemoveAlignment(aElement.AsDOMNode(), aAlignType,
+                                 aContentsOnly == ContentsOnly::yes);
   NS_ENSURE_SUCCESS(res, res);
   NS_NAMED_LITERAL_STRING(attr, "align");
-  NS_ENSURE_STATE(mHTMLEditor);
   if (mHTMLEditor->IsCSSEnabled()) {
-    // let's use CSS alignment; we use margin-left and margin-right for tables
+    // Let's use CSS alignment; we use margin-left and margin-right for tables
     // and text-align for other block-level elements
-    NS_ENSURE_STATE(mHTMLEditor);
-    res = mHTMLEditor->SetAttributeOrEquivalent(aElement, attr, *aAlignType, false);
+    res = mHTMLEditor->SetAttributeOrEquivalent(
+      static_cast<nsIDOMElement*>(aElement.AsDOMNode()), attr, aAlignType,
+      false);
     NS_ENSURE_SUCCESS(res, res);
-  }
-  else {
+  } else {
     // HTML case; this code is supposed to be called ONLY if the element
     // supports the align attribute but we'll never know...
-    if (nsHTMLEditUtils::SupportsAlignAttr(node)) {
-      NS_ENSURE_STATE(mHTMLEditor);
-      res = mHTMLEditor->SetAttribute(aElement, attr, *aAlignType);
+    if (nsHTMLEditUtils::SupportsAlignAttr(aElement.AsDOMNode())) {
+      res =
+        mHTMLEditor->SetAttribute(static_cast<nsIDOMElement*>(aElement.AsDOMNode()),
+                                  attr, aAlignType);
       NS_ENSURE_SUCCESS(res, res);
     }
   }
   return NS_OK;
 }
 
 nsresult
 nsHTMLEditRules::RelativeChangeIndentationOfElementNode(nsIDOMNode *aNode, int8_t aRelativeChange)
--- a/editor/libeditor/nsHTMLEditRules.h
+++ b/editor/libeditor/nsHTMLEditRules.h
@@ -338,17 +338,19 @@ protected:
   nsresult SelectionEndpointInNode(nsINode *aNode, bool *aResult);
   nsresult UpdateDocChangeRange(nsRange* aRange);
   nsresult ConfirmSelectionInBody();
   nsresult InsertMozBRIfNeeded(nsINode& aNode);
   bool     IsEmptyInline(nsIDOMNode *aNode);
   bool     ListIsEmptyLine(nsTArray<mozilla::OwningNonNull<nsINode>>& arrayOfNodes);
   nsresult RemoveAlignment(nsIDOMNode * aNode, const nsAString & aAlignType, bool aChildrenOnly);
   nsresult MakeSureElemStartsOrEndsOnCR(nsIDOMNode *aNode, bool aStarts);
-  nsresult AlignBlock(nsIDOMElement * aElement, const nsAString * aAlignType, bool aContentsOnly);
+  enum class ContentsOnly { no, yes };
+  nsresult AlignBlock(mozilla::dom::Element& aElement,
+                      const nsAString& aAlignType, ContentsOnly aContentsOnly);
   nsresult RelativeChangeIndentationOfElementNode(nsIDOMNode *aNode, int8_t aRelativeChange);
   void DocumentModifiedWorker();
 
 // data members
 protected:
   nsHTMLEditor           *mHTMLEditor;
   RefPtr<nsRange>       mDocChangeRange;
   bool                    mListenerEnabled;