Bug 1572375 - part 6: Get rid of `HTMLEditRules::WillLoadHTML()` r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 13 Aug 2019 00:09:46 +0000
changeset 487551 e14e02842d3eb93f5a83d5deb808cd6e653b8faf
parent 487550 8c7339d7cd3b9679b17c02b8f6d2d5d12cd1b949
child 487552 571000b4df5b49b8cbee56e0298233633f4ab932
push id36425
push userbtara@mozilla.com
push dateTue, 13 Aug 2019 09:54:32 +0000
treeherdermozilla-central@e29ba984dad2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1572375
milestone70.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 1572375 - part 6: Get rid of `HTMLEditRules::WillLoadHTML()` r=m_kato `HTMLEditRules::WillLoadHTML()` does exactly same thing as `EditorBase::EnsureNoPaddingBRElementForEmptyEditor()`. Therefore, we can get rid of it and make `HTMLEditor::LoadHTML()` not use `HTMLEditRules`. Differential Revision: https://phabricator.services.mozilla.com/D41160
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
editor/libeditor/HTMLEditorDataTransfer.cpp
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -710,18 +710,16 @@ nsresult HTMLEditRules::WillDoAction(Edi
   }
 
   switch (aInfo.mEditSubAction) {
     case EditSubAction::eInsertText:
     case EditSubAction::eInsertTextComingFromIME:
       UndefineCaretBidiLevel();
       return WillInsertText(aInfo.mEditSubAction, aCancel, aHandled,
                             aInfo.inString, aInfo.outString, aInfo.maxLength);
-    case EditSubAction::eInsertHTMLSource:
-      return WillLoadHTML();
     case EditSubAction::eInsertParagraphSeparator: {
       UndefineCaretBidiLevel();
       EditActionResult result = WillInsertParagraphSeparator();
       if (NS_WARN_IF(result.Failed())) {
         return result.Rv();
       }
       *aCancel = result.Canceled();
       *aHandled = result.Handled();
@@ -768,16 +766,17 @@ nsresult HTMLEditRules::WillDoAction(Edi
       }
       NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "WillInsert() failed");
       return NS_OK;
     }
     case EditSubAction::eDecreaseZIndex:
       return WillRelativeChangeZIndex(-1, aCancel, aHandled);
     case EditSubAction::eIncreaseZIndex:
       return WillRelativeChangeZIndex(1, aCancel, aHandled);
+    case EditSubAction::eInsertHTMLSource:
     case EditSubAction::eUndo:
     case EditSubAction::eRedo:
       MOZ_ASSERT_UNREACHABLE("This path should've been dead code");
       return NS_ERROR_UNEXPECTED;
     default:
       return TextEditRules::WillDoAction(aInfo, aCancel, aHandled);
   }
 }
@@ -808,16 +807,17 @@ nsresult HTMLEditRules::DidDoAction(Edit
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
       return DidAbsolutePosition();
     }
     case EditSubAction::eInsertElement:
     case EditSubAction::eInsertQuotedText:
       return NS_OK;
+    case EditSubAction::eInsertHTMLSource:
     case EditSubAction::eUndo:
     case EditSubAction::eRedo:
       MOZ_ASSERT_UNREACHABLE("This path should've been dead code");
       return NS_ERROR_UNEXPECTED;
     default:
       return TextEditRules::DidDoAction(aInfo, aResult);
   }
 }
@@ -1661,39 +1661,16 @@ nsresult HTMLEditRules::WillInsertText(E
 
   rv = mDocChangeRange->CollapseTo(pointToInsert);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
   return NS_OK;
 }
 
-nsresult HTMLEditRules::WillLoadHTML() {
-  MOZ_ASSERT(IsEditorDataAvailable());
-
-  // Delete mPaddingBRElementForEmptyEditor if it exists. If we really
-  // need one, it will be added during post-processing in AfterEditInner().
-  if (HTMLEditorRef().mPaddingBRElementForEmptyEditor) {
-    // A mutation event listener may recreate padding <br> element for empty
-    // editor again during the call of DeleteNodeWithTransaction().  So, move
-    // it first.
-    RefPtr<HTMLBRElement> paddingBRElement(
-        std::move(HTMLEditorRef().mPaddingBRElementForEmptyEditor));
-    DebugOnly<nsresult> rv = MOZ_KnownLive(HTMLEditorRef())
-                                 .DeleteNodeWithTransaction(*paddingBRElement);
-    if (NS_WARN_IF(!CanHandleEditAction())) {
-      return NS_ERROR_EDITOR_DESTROYED;
-    }
-    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
-                         "Failed to remove the padding <br> element");
-  }
-
-  return NS_OK;
-}
-
 bool HTMLEditRules::CanContainParagraph(Element& aElement) const {
   MOZ_ASSERT(IsEditorDataAvailable());
 
   if (HTMLEditorRef().CanContainTag(aElement, *nsGkAtoms::p)) {
     return true;
   }
 
   // Even if the element cannot have a <p> element as a child, it can contain
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -187,22 +187,16 @@ class HTMLEditRules : public TextEditRul
   MOZ_CAN_RUN_SCRIPT
   MOZ_MUST_USE nsresult WillInsertText(EditSubAction aEditSubAction,
                                        bool* aCancel, bool* aHandled,
                                        const nsAString* inString,
                                        nsAString* outString,
                                        int32_t aMaxLength);
 
   /**
-   * WillLoadHTML() is called before loading enter document from source.
-   * This removes padding <br> element for empty editor if there is.
-   */
-  MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult WillLoadHTML();
-
-  /**
    * WillInsertParagraphSeparator() is called when insertParagraph command is
    * executed or something equivalent.  This method actually tries to insert
    * new paragraph or <br> element, etc.
    */
   MOZ_CAN_RUN_SCRIPT
   MOZ_MUST_USE EditActionResult WillInsertParagraphSeparator();
 
   /**
--- a/editor/libeditor/HTMLEditorDataTransfer.cpp
+++ b/editor/libeditor/HTMLEditorDataTransfer.cpp
@@ -93,87 +93,76 @@ nsresult HTMLEditor::LoadHTML(const nsAS
   MOZ_ASSERT(IsEditActionDataAvailable());
 
   if (NS_WARN_IF(!mRules)) {
     return NS_ERROR_NOT_INITIALIZED;
   }
 
   // force IME commit; set up rules sniffing and batching
   CommitComposition();
+  if (NS_WARN_IF(Destroyed())) {
+    return NS_ERROR_EDITOR_DESTROYED;
+  }
+
   AutoPlaceholderBatch treatAsOneTransaction(*this);
   AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction(
       *this, EditSubAction::eInsertHTMLSource, nsIEditor::eNext);
 
-  EditSubActionInfo subActionInfo(EditSubAction::eInsertHTMLSource);
-  bool cancel, handled;
-  // Protect the edit rules object from dying
-  RefPtr<TextEditRules> rules(mRules);
-  nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled);
+  nsresult rv = EnsureNoPaddingBRElementForEmptyEditor();
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
-  if (cancel) {
-    return NS_OK;  // rules canceled the operation
-  }
 
-  if (!handled) {
-    // Delete Selection, but only if it isn't collapsed, see bug #106269
-    if (!SelectionRefPtr()->IsCollapsed()) {
-      rv = DeleteSelectionAsSubAction(eNone, eStrip);
-      if (NS_WARN_IF(NS_FAILED(rv))) {
-        return rv;
-      }
-    }
-
-    // Get the first range in the selection, for context:
-    RefPtr<nsRange> range = SelectionRefPtr()->GetRangeAt(0);
-    if (NS_WARN_IF(!range)) {
-      return NS_ERROR_FAILURE;
-    }
-
-    // Create fragment for pasted HTML.
-    ErrorResult error;
-    RefPtr<DocumentFragment> documentFragment =
-        range->CreateContextualFragment(aInputString, error);
-    if (NS_WARN_IF(error.Failed())) {
-      return error.StealNSResult();
-    }
-
-    // Put the fragment into the document at start of selection.
-    EditorDOMPoint pointToInsert(range->StartRef());
-    // XXX We need to make pointToInsert store offset for keeping traditional
-    //     behavior since using only child node to pointing insertion point
-    //     changes the behavior when inserted child is moved by mutation
-    //     observer.  We need to investigate what we should do here.
-    Unused << pointToInsert.Offset();
-    for (nsCOMPtr<nsIContent> contentToInsert =
-             documentFragment->GetFirstChild();
-         contentToInsert; contentToInsert = documentFragment->GetFirstChild()) {
-      rv = InsertNodeWithTransaction(*contentToInsert, pointToInsert);
-      if (NS_WARN_IF(NS_FAILED(rv))) {
-        return rv;
-      }
-      // XXX If the inserted node has been moved by mutation observer,
-      //     incrementing offset will cause odd result.  Next new node
-      //     will be inserted after existing node and the offset will be
-      //     overflown from the container node.
-      pointToInsert.Set(pointToInsert.GetContainer(),
-                        pointToInsert.Offset() + 1);
-      if (NS_WARN_IF(!pointToInsert.Offset())) {
-        // Append the remaining children to the container if offset is
-        // overflown.
-        pointToInsert.SetToEndOf(pointToInsert.GetContainer());
-      }
+  // Delete Selection, but only if it isn't collapsed, see bug #106269
+  if (!SelectionRefPtr()->IsCollapsed()) {
+    rv = DeleteSelectionAsSubAction(eNone, eStrip);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
     }
   }
 
-  rv = rules->DidDoAction(subActionInfo, rv);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
+  // Get the first range in the selection, for context:
+  RefPtr<nsRange> range = SelectionRefPtr()->GetRangeAt(0);
+  if (NS_WARN_IF(!range)) {
+    return NS_ERROR_FAILURE;
+  }
+
+  // Create fragment for pasted HTML.
+  ErrorResult error;
+  RefPtr<DocumentFragment> documentFragment =
+      range->CreateContextualFragment(aInputString, error);
+  if (NS_WARN_IF(error.Failed())) {
+    return error.StealNSResult();
   }
+
+  // Put the fragment into the document at start of selection.
+  EditorDOMPoint pointToInsert(range->StartRef());
+  // XXX We need to make pointToInsert store offset for keeping traditional
+  //     behavior since using only child node to pointing insertion point
+  //     changes the behavior when inserted child is moved by mutation
+  //     observer.  We need to investigate what we should do here.
+  Unused << pointToInsert.Offset();
+  for (nsCOMPtr<nsIContent> contentToInsert = documentFragment->GetFirstChild();
+       contentToInsert; contentToInsert = documentFragment->GetFirstChild()) {
+    rv = InsertNodeWithTransaction(*contentToInsert, pointToInsert);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+    // XXX If the inserted node has been moved by mutation observer,
+    //     incrementing offset will cause odd result.  Next new node
+    //     will be inserted after existing node and the offset will be
+    //     overflown from the container node.
+    pointToInsert.Set(pointToInsert.GetContainer(), pointToInsert.Offset() + 1);
+    if (NS_WARN_IF(!pointToInsert.Offset())) {
+      // Append the remaining children to the container if offset is
+      // overflown.
+      pointToInsert.SetToEndOf(pointToInsert.GetContainer());
+    }
+  }
+
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HTMLEditor::InsertHTML(const nsAString& aInString) {
   nsresult rv = InsertHTMLAsAction(aInString);
   NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to insert HTML");
   return rv;