Bug 1416099 - part 1: Make HTMLEditRules::ReturnInParagraph() use EditActionResult r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 10 Nov 2017 14:17:54 +0900
changeset 696344 48a99c120c9995fee123428cb1721b9f15993c72
parent 696343 704c63e201ddc5ea1c03762f775ee7c7225b44ed
child 696345 0268dc157c2ab48aef6a0b7da932962b092a4897
push id88684
push usermasayuki@d-toybox.com
push dateFri, 10 Nov 2017 13:42:43 +0000
reviewersm_kato
bugs1416099
milestone58.0a1
Bug 1416099 - part 1: Make HTMLEditRules::ReturnInParagraph() use EditActionResult r?m_kato For reducing the number of arguments of HTMLEditRules::ReturnInParagraph(), let's make it return EditActionResult and get rid of |aCancel| and |aHandled|. MozReview-Commit-ID: HU1SthEjonn
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -1783,20 +1783,23 @@ HTMLEditRules::WillInsertBreak(Selection
   //     Gmail.  So, let's use traditional odd behavior only when the default
   //     paragraph separator is <br>.  Otherwise, take consistent behavior
   //     between <p> container and <div> container.
   else if ((separator == ParagraphSeparator::br &&
             blockParent->IsHTMLElement(nsGkAtoms::p)) ||
            (separator != ParagraphSeparator::br &&
             blockParent->IsAnyOfHTMLElements(nsGkAtoms::p, nsGkAtoms::div))) {
     // Paragraphs: special rules to look for <br>s
-    nsresult rv =
-      ReturnInParagraph(&aSelection, blockParent, node,
-                        offset, child, aCancel, aHandled);
-    NS_ENSURE_SUCCESS(rv, rv);
+    EditActionResult result =
+      ReturnInParagraph(&aSelection, blockParent, node, offset, child);
+    if (NS_WARN_IF(result.Failed())) {
+      return result.Rv();
+    }
+    *aHandled = result.Handled();
+    *aCancel = result.Canceled();
     // Fall through, we may not have handled it in ReturnInParagraph()
   }
 
   // If not already handled then do the standard thing
   if (!(*aHandled)) {
     *aHandled = true;
     return StandardBreakImpl(node, offset, aSelection);
   }
@@ -6712,131 +6715,132 @@ HTMLEditRules::ReturnInHeader(Selection&
 }
 
 /**
  * ReturnInParagraph() does the right thing for returns pressed in paragraphs.
  * For our purposes, this means either <p> or <div>, which is not in keeping
  * with the semantics of <div>, but is necessary for compatibility with other
  * browsers.
  */
-nsresult
+EditActionResult
 HTMLEditRules::ReturnInParagraph(Selection* aSelection,
                                  nsINode* aPara,
                                  nsINode* aNode,
                                  int32_t aOffset,
-                                 nsIContent* aChildAtOffset,
-                                 bool* aCancel,
-                                 bool* aHandled)
-{
+                                 nsIContent* aChildAtOffset)
+{
+  if (NS_WARN_IF(!mHTMLEditor)) {
+    return EditActionResult(NS_ERROR_NOT_AVAILABLE);
+  }
+
   nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
-  if (!aSelection || !aPara || !node || !aCancel || !aHandled) {
-    return NS_ERROR_NULL_POINTER;
-  }
-  *aCancel = false;
-  *aHandled = false;
+  if (!aSelection || !aPara || !node) {
+    return EditActionResult(NS_ERROR_NULL_POINTER);
+  }
+
+  RefPtr<HTMLEditor> htmlEditor = mHTMLEditor;
 
   int32_t offset;
   nsCOMPtr<nsINode> parent = EditorBase::GetNodeLocation(node, &offset);
 
-  NS_ENSURE_STATE(mHTMLEditor);
-  bool doesCRCreateNewP = mHTMLEditor->GetReturnInParagraphCreatesNewParagraph();
+  bool doesCRCreateNewP = htmlEditor->GetReturnInParagraphCreatesNewParagraph();
 
   bool newBRneeded = false;
   bool newSelNode = false;
   nsCOMPtr<nsIContent> sibling;
   nsCOMPtr<nsIDOMNode> selNode = GetAsDOMNode(aNode);
   int32_t selOffset = aOffset;
 
-  NS_ENSURE_STATE(mHTMLEditor);
   if (aNode == aPara && doesCRCreateNewP) {
     // we are at the edges of the block, newBRneeded not needed!
     sibling = node->AsContent();
   } else if (EditorBase::IsTextNode(aNode)) {
     // at beginning of text node?
     if (!aOffset) {
       // is there a BR prior to it?
-      NS_ENSURE_STATE(mHTMLEditor);
-      sibling = mHTMLEditor->GetPriorHTMLSibling(node);
-      if (!sibling || !mHTMLEditor ||
-          !mHTMLEditor->IsVisibleBRElement(sibling) ||
+      sibling = htmlEditor->GetPriorHTMLSibling(node);
+      if (!sibling ||
+          !htmlEditor->IsVisibleBRElement(sibling) ||
           TextEditUtils::HasMozAttr(GetAsDOMNode(sibling))) {
-        NS_ENSURE_STATE(mHTMLEditor);
         newBRneeded = true;
       }
     } else if (aOffset == static_cast<int32_t>(node->Length())) {
       // we're at the end of text node...
       // is there a BR after to it?
-      NS_ENSURE_STATE(mHTMLEditor);
-      sibling = mHTMLEditor->GetNextHTMLSibling(node);
-      if (!sibling || !mHTMLEditor ||
-          !mHTMLEditor->IsVisibleBRElement(sibling) ||
+      sibling = htmlEditor->GetNextHTMLSibling(node);
+      if (!sibling ||
+          !htmlEditor->IsVisibleBRElement(sibling) ||
           TextEditUtils::HasMozAttr(GetAsDOMNode(sibling))) {
-        NS_ENSURE_STATE(mHTMLEditor);
         newBRneeded = true;
         offset++;
       }
     } else {
       if (doesCRCreateNewP) {
         nsCOMPtr<nsIDOMNode> tmp;
-        if (NS_WARN_IF(!mHTMLEditor)) {
-          return NS_ERROR_UNEXPECTED;
+        nsresult rv =
+          htmlEditor->SplitNode(selNode, aOffset, getter_AddRefs(tmp));
+        if (NS_WARN_IF(NS_FAILED(rv))) {
+          return EditActionResult(rv);
         }
-        nsresult rv =
-          mHTMLEditor->SplitNode(selNode, aOffset, getter_AddRefs(tmp));
-        NS_ENSURE_SUCCESS(rv, rv);
         selNode = tmp;
       }
 
       newBRneeded = true;
       offset++;
     }
   } else {
     // not in a text node.
     // is there a BR prior to it?
     nsCOMPtr<nsIContent> nearNode;
-    NS_ENSURE_STATE(mHTMLEditor);
-    nearNode =
-      mHTMLEditor->GetPreviousEditableHTMLNode(
-                     EditorRawDOMPoint(node, aChildAtOffset, aOffset));
-    NS_ENSURE_STATE(mHTMLEditor);
-    if (!nearNode || !mHTMLEditor->IsVisibleBRElement(nearNode) ||
+    nearNode = htmlEditor->GetPreviousEditableHTMLNode(
+                             EditorRawDOMPoint(node, aChildAtOffset, aOffset));
+    if (!nearNode || !htmlEditor->IsVisibleBRElement(nearNode) ||
         TextEditUtils::HasMozAttr(GetAsDOMNode(nearNode))) {
       // is there a BR after it?
-      NS_ENSURE_STATE(mHTMLEditor);
       nearNode =
-        mHTMLEditor->GetNextEditableHTMLNode(
-                       EditorRawDOMPoint(node, aChildAtOffset, aOffset));
-      NS_ENSURE_STATE(mHTMLEditor);
-      if (!nearNode || !mHTMLEditor->IsVisibleBRElement(nearNode) ||
+        htmlEditor->GetNextEditableHTMLNode(
+                      EditorRawDOMPoint(node, aChildAtOffset, aOffset));
+      if (!nearNode || !htmlEditor->IsVisibleBRElement(nearNode) ||
           TextEditUtils::HasMozAttr(GetAsDOMNode(nearNode))) {
         newBRneeded = true;
         parent = node;
         offset = aOffset;
         newSelNode = true;
       }
     }
     if (!newBRneeded) {
       sibling = nearNode;
     }
   }
   if (newBRneeded) {
+    // Don't modify the DOM tree if mHTMLEditor disappeared.
+    if (NS_WARN_IF(!mHTMLEditor)) {
+      return EditActionResult(NS_ERROR_NOT_AVAILABLE);
+    }
+
     // if CR does not create a new P, default to BR creation
-    NS_ENSURE_TRUE(doesCRCreateNewP, NS_OK);
-
-    NS_ENSURE_STATE(mHTMLEditor);
-    sibling = mHTMLEditor->CreateBR(parent, offset);
+    if (NS_WARN_IF(!doesCRCreateNewP)) {
+      return EditActionResult(NS_OK);
+    }
+
+    sibling = htmlEditor->CreateBR(parent, offset);
     if (newSelNode) {
       // We split the parent after the br we've just inserted.
       selNode = GetAsDOMNode(parent);
       selOffset = offset + 1;
     }
   }
-  *aHandled = true;
-  return SplitParagraph(GetAsDOMNode(aPara), sibling, aSelection,
-                        address_of(selNode), &selOffset);
+  EditActionResult result(
+    SplitParagraph(GetAsDOMNode(aPara), sibling, aSelection,
+                   address_of(selNode), &selOffset));
+  result.MarkAsHandled();
+  if (NS_WARN_IF(result.Failed())) {
+    return result;
+  }
+  return result;
 }
 
 /**
  * SplitParagraph() splits a paragraph at selection point, possibly deleting a
  * br.
  */
 nsresult
 HTMLEditRules::SplitParagraph(nsIDOMNode *aPara,
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -289,20 +289,19 @@ protected:
   void GetInnerContent(nsINode& aNode,
                        nsTArray<OwningNonNull<nsINode>>& aOutArrayOfNodes,
                        int32_t* aIndex, Lists aLists = Lists::yes,
                        Tables aTables = Tables::yes);
   Element* IsInListItem(nsINode* aNode);
   nsAtom& DefaultParagraphSeparator();
   nsresult ReturnInHeader(Selection& aSelection, Element& aHeader,
                           nsINode& aNode, int32_t aOffset);
-  nsresult ReturnInParagraph(Selection* aSelection, nsINode* aHeader,
-                             nsINode* aTextNode, int32_t aOffset,
-                             nsIContent* aChildAtOffset, bool* aCancel,
-                             bool* aHandled);
+  EditActionResult ReturnInParagraph(Selection* aSelection, nsINode* aHeader,
+                                     nsINode* aTextNode, int32_t aOffset,
+                                     nsIContent* aChildAtOffset);
   nsresult SplitParagraph(nsIDOMNode* aPara,
                           nsIContent* aBRNode,
                           Selection* aSelection,
                           nsCOMPtr<nsIDOMNode>* aSelNode,
                           int32_t* aOffset);
   nsresult ReturnInListItem(Selection& aSelection, Element& aHeader,
                             nsINode& aNode, int32_t aOffset);
   nsresult AfterEditInner(EditAction action,