Bug 1574852 - part 48: Move `HTMLEditRules::ReturnInHeader()` to `HTMLEditor` r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 02 Sep 2019 09:14:23 +0000
changeset 491605 15b069730cb63846f51e6807702e6781e693aad0
parent 491604 90908044afc6b4898a6bf92141efb4db3ce49eee
child 491606 55386bf32ae058915984b22d2cbecf04b8b0ca77
push id36531
push usercbrindusan@mozilla.com
push dateWed, 04 Sep 2019 09:43:19 +0000
treeherdermozilla-central@174361d15292 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1574852
milestone71.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 1574852 - part 48: Move `HTMLEditRules::ReturnInHeader()` to `HTMLEditor` r=m_kato Differential Revision: https://phabricator.services.mozilla.com/D44185
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
editor/libeditor/HTMLEditor.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -1938,19 +1938,21 @@ EditActionResult HTMLEditRules::WillInse
     }
     NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                          "Failed to insert break into list item");
     return EditActionHandled();
   }
 
   if (HTMLEditUtils::IsHeader(*blockParent)) {
     // Headers: close (or split) header
-    nsresult rv = ReturnInHeader(
-        *blockParent, MOZ_KnownLive(*atStartOfSelection.GetContainer()),
-        atStartOfSelection.Offset());
+    nsresult rv =
+        MOZ_KnownLive(HTMLEditorRef())
+            .HandleInsertParagraphInHeadingElement(
+                *blockParent, MOZ_KnownLive(*atStartOfSelection.GetContainer()),
+                atStartOfSelection.Offset());
     if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
       return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED);
     }
     NS_WARNING_ASSERTION(
         NS_SUCCEEDED(rv),
         "Failed to handle insertParagraph in the heading element");
     return EditActionHandled();
   }
@@ -7950,159 +7952,151 @@ Element* HTMLEditRules::IsInListItem(nsI
     if (HTMLEditUtils::IsListItem(parent)) {
       return parent;
     }
     parent = parent->GetParentElement();
   }
   return nullptr;
 }
 
-nsresult HTMLEditRules::ReturnInHeader(Element& aHeader, nsINode& aNode,
-                                       int32_t aOffset) {
-  MOZ_ASSERT(IsEditorDataAvailable());
+nsresult HTMLEditor::HandleInsertParagraphInHeadingElement(Element& aHeader,
+                                                           nsINode& aNode,
+                                                           int32_t aOffset) {
+  MOZ_ASSERT(IsTopLevelEditSubActionDataAvailable());
 
   // Remember where the header is
   nsCOMPtr<nsINode> headerParent = aHeader.GetParentNode();
   int32_t offset = headerParent ? headerParent->ComputeIndexOf(&aHeader) : -1;
 
   // Get ws code to adjust any ws
   nsCOMPtr<nsINode> node = &aNode;
-  nsresult rv = WSRunObject::PrepareToSplitAcrossBlocks(
-      MOZ_KnownLive(&HTMLEditorRef()), address_of(node), &aOffset);
-  if (NS_WARN_IF(!CanHandleEditAction())) {
+  nsresult rv =
+      WSRunObject::PrepareToSplitAcrossBlocks(this, address_of(node), &aOffset);
+  if (NS_WARN_IF(Destroyed())) {
     return NS_ERROR_EDITOR_DESTROYED;
   }
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
   if (NS_WARN_IF(!node->IsContent())) {
     return NS_ERROR_FAILURE;
   }
 
   // Split the header
   SplitNodeResult splitHeaderResult =
-      MOZ_KnownLive(HTMLEditorRef())
-          .SplitNodeDeepWithTransaction(
-              aHeader, EditorDOMPoint(node, aOffset),
-              SplitAtEdges::eAllowToCreateEmptyContainer);
-  if (NS_WARN_IF(!CanHandleEditAction())) {
+      SplitNodeDeepWithTransaction(aHeader, EditorDOMPoint(node, aOffset),
+                                   SplitAtEdges::eAllowToCreateEmptyContainer);
+  if (NS_WARN_IF(Destroyed())) {
     return NS_ERROR_EDITOR_DESTROYED;
   }
   NS_WARNING_ASSERTION(splitHeaderResult.Succeeded(),
                        "Failed to split aHeader");
 
   // If the previous heading of split point is empty, put a padding <br>
   // element for empty last line into it.
-  nsCOMPtr<nsIContent> prevItem = HTMLEditorRef().GetPriorHTMLSibling(&aHeader);
+  nsCOMPtr<nsIContent> prevItem = GetPriorHTMLSibling(&aHeader);
   if (prevItem) {
     MOZ_DIAGNOSTIC_ASSERT(HTMLEditUtils::IsHeader(*prevItem));
     bool isEmptyNode;
-    rv = HTMLEditorRef().IsEmptyNode(prevItem, &isEmptyNode);
+    rv = IsEmptyNode(prevItem, &isEmptyNode);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     if (isEmptyNode) {
       CreateElementResult createPaddingBRResult =
-          MOZ_KnownLive(HTMLEditorRef())
-              .InsertPaddingBRElementForEmptyLastLineWithTransaction(
-                  EditorDOMPoint(prevItem, 0));
+          InsertPaddingBRElementForEmptyLastLineWithTransaction(
+              EditorDOMPoint(prevItem, 0));
       if (NS_WARN_IF(createPaddingBRResult.Failed())) {
         return createPaddingBRResult.Rv();
       }
     }
   }
 
   // If the new (righthand) header node is empty, delete it
-  if (HTMLEditorRef().IsEmptyBlockElement(aHeader,
-                                          HTMLEditor::IgnoreSingleBR::Yes)) {
-    rv = MOZ_KnownLive(HTMLEditorRef()).DeleteNodeWithTransaction(aHeader);
-    if (NS_WARN_IF(!CanHandleEditAction())) {
+  if (IsEmptyBlockElement(aHeader, IgnoreSingleBR::Yes)) {
+    rv = DeleteNodeWithTransaction(aHeader);
+    if (NS_WARN_IF(Destroyed())) {
       return NS_ERROR_EDITOR_DESTROYED;
     }
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     // Layout tells the caret to blink in a weird place if we don't place a
     // break after the header.
     nsCOMPtr<nsIContent> sibling;
     if (aHeader.GetNextSibling()) {
-      sibling = HTMLEditorRef().GetNextHTMLSibling(aHeader.GetNextSibling());
+      sibling = GetNextHTMLSibling(aHeader.GetNextSibling());
     }
     if (!sibling || !sibling->IsHTMLElement(nsGkAtoms::br)) {
-      HTMLEditorRef()
-          .TopLevelEditSubActionDataRef()
-          .mCachedInlineStyles.Clear();
-      HTMLEditorRef().mTypeInState->ClearAllProps();
+      TopLevelEditSubActionDataRef().mCachedInlineStyles.Clear();
+      mTypeInState->ClearAllProps();
 
       // Create a paragraph
-      nsStaticAtom& paraAtom =
-          HTMLEditorRef().DefaultParagraphSeparatorTagName();
+      nsStaticAtom& paraAtom = DefaultParagraphSeparatorTagName();
       // We want a wrapper element even if we separate with <br>
       EditorDOMPoint nextToHeader(headerParent, offset + 1);
-      RefPtr<Element> pNode =
-          MOZ_KnownLive(HTMLEditorRef())
-              .CreateNodeWithTransaction(&paraAtom == nsGkAtoms::br
-                                             ? *nsGkAtoms::p
-                                             : MOZ_KnownLive(paraAtom),
-                                         nextToHeader);
-      if (NS_WARN_IF(!CanHandleEditAction())) {
+      RefPtr<Element> pNode = CreateNodeWithTransaction(
+          &paraAtom == nsGkAtoms::br ? *nsGkAtoms::p : MOZ_KnownLive(paraAtom),
+          nextToHeader);
+      if (NS_WARN_IF(Destroyed())) {
         return NS_ERROR_EDITOR_DESTROYED;
       }
       if (NS_WARN_IF(!pNode)) {
         return NS_ERROR_FAILURE;
       }
 
       // Append a <br> to it
       RefPtr<Element> brElement =
-          MOZ_KnownLive(HTMLEditorRef())
-              .InsertBRElementWithTransaction(EditorDOMPoint(pNode, 0));
-      if (NS_WARN_IF(!CanHandleEditAction())) {
+          InsertBRElementWithTransaction(EditorDOMPoint(pNode, 0));
+      if (NS_WARN_IF(Destroyed())) {
         return NS_ERROR_EDITOR_DESTROYED;
       }
       if (NS_WARN_IF(!brElement)) {
         return NS_ERROR_FAILURE;
       }
 
       // Set selection to before the break
       ErrorResult error;
       SelectionRefPtr()->Collapse(EditorRawDOMPoint(pNode, 0), error);
-      if (NS_WARN_IF(!CanHandleEditAction())) {
+      if (NS_WARN_IF(Destroyed())) {
         error.SuppressException();
         return NS_ERROR_EDITOR_DESTROYED;
       }
       if (NS_WARN_IF(error.Failed())) {
         return error.StealNSResult();
       }
-    } else {
-      EditorRawDOMPoint afterSibling(sibling);
-      if (NS_WARN_IF(!afterSibling.AdvanceOffset())) {
-        return NS_ERROR_FAILURE;
-      }
-      // Put selection after break
-      ErrorResult error;
-      SelectionRefPtr()->Collapse(afterSibling, error);
-      if (NS_WARN_IF(!CanHandleEditAction())) {
-        error.SuppressException();
-        return NS_ERROR_EDITOR_DESTROYED;
-      }
-      if (NS_WARN_IF(error.Failed())) {
-        return error.StealNSResult();
-      }
-    }
-  } else {
-    // Put selection at front of righthand heading
+      return NS_OK;
+    }
+
+    EditorRawDOMPoint afterSibling(sibling);
+    if (NS_WARN_IF(!afterSibling.AdvanceOffset())) {
+      return NS_ERROR_FAILURE;
+    }
+    // Put selection after break
     ErrorResult error;
-    SelectionRefPtr()->Collapse(RawRangeBoundary(&aHeader, 0), error);
-    if (NS_WARN_IF(!CanHandleEditAction())) {
+    SelectionRefPtr()->Collapse(afterSibling, error);
+    if (NS_WARN_IF(Destroyed())) {
       error.SuppressException();
       return NS_ERROR_EDITOR_DESTROYED;
     }
     if (NS_WARN_IF(error.Failed())) {
       return error.StealNSResult();
     }
+    return NS_OK;
+  }
+
+  // Put selection at front of righthand heading
+  ErrorResult error;
+  SelectionRefPtr()->Collapse(RawRangeBoundary(&aHeader, 0), error);
+  if (NS_WARN_IF(Destroyed())) {
+    error.SuppressException();
+    return NS_ERROR_EDITOR_DESTROYED;
+  }
+  if (NS_WARN_IF(error.Failed())) {
+    return error.StealNSResult();
   }
   return NS_OK;
 }
 
 EditActionResult HTMLEditor::HandleInsertParagraphInParagraph(
     Element& aParentDivOrP) {
   MOZ_ASSERT(IsEditActionDataAvailable());
 
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -452,33 +452,16 @@ class HTMLEditRules : public TextEditRul
   /**
    * If aNode is the descendant of a listitem, return that li.  But table
    * element boundaries are stoppers on the search.  Also stops on the active
    * editor host (contenteditable).  Also test if aNode is an li itself.
    */
   Element* IsInListItem(nsINode* aNode);
 
   /**
-   * ReturnInHeader() handles insertParagraph command (i.e., handling Enter
-   * key press) in a heading element.  This splits aHeader element at
-   * aOffset in aNode.  Then, if right heading element is empty, it'll be
-   * removed and new paragraph is created (its type is decided with default
-   * paragraph separator).
-   *
-   * @param aHeader             The heading element to be split.
-   * @param aNode               Typically, Selection start container,
-   *                            where to be split.
-   * @param aOffset             Typically, Selection start offset in the
-   *                            start container, where to be split.
-   */
-  MOZ_CAN_RUN_SCRIPT
-  MOZ_MUST_USE nsresult ReturnInHeader(Element& aHeader, nsINode& aNode,
-                                       int32_t aOffset);
-
-  /**
    * ReturnInListItem() handles insertParagraph command (i.e., handling
    * Enter key press) in a list item element.
    *
    * @param aListItem           The list item which has the following point.
    * @param aNode               Typically, Selection start container, where to
    *                            insert a break.
    * @param aOffset             Typically, Selection start offset in the
    *                            start container, where to insert a break.
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -1807,16 +1807,33 @@ class HTMLEditor final : public TextEdit
    *                        element.
    * @return                Returns with NS_OK if this doesn't meat any
    *                        unexpected situation.  If this method tries to
    *                        split the paragraph, marked as handled.
    */
   MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE EditActionResult
   HandleInsertParagraphInParagraph(Element& aParentDivOrP);
 
+  /**
+   * HandleInsertParagraphInHeadingElement() handles insertParagraph command
+   * (i.e., handling Enter key press) in a heading element.  This splits
+   * aHeader element at aOffset in aNode.  Then, if right heading element is
+   * empty, it'll be removed and new paragraph is created (its type is decided
+   * with default paragraph separator).
+   *
+   * @param aHeader             The heading element to be split.
+   * @param aNode               Typically, Selection start container,
+   *                            where to be split.
+   * @param aOffset             Typically, Selection start offset in the
+   *                            start container, where to be split.
+   */
+  MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult
+  HandleInsertParagraphInHeadingElement(Element& aHeader, nsINode& aNode,
+                                        int32_t aOffset);
+
  protected:  // Called by helper classes.
   virtual void OnStartToHandleTopLevelEditSubAction(
       EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override;
   MOZ_CAN_RUN_SCRIPT
   virtual void OnEndHandlingTopLevelEditSubAction() override;
 
  protected:  // Shouldn't be used by friend classes
   virtual ~HTMLEditor();