Bug 1572375 - part 7: Get rid of `HTMLEditRules::OnModifyDocument()` r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 13 Aug 2019 00:56:57 +0000
changeset 487552 571000b4df5b49b8cbee56e0298233633f4ab932
parent 487551 e14e02842d3eb93f5a83d5deb808cd6e653b8faf
child 487553 3a07d2f4b955d2e8847d4b8c73ae7a657b5195d9
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 7: Get rid of `HTMLEditRules::OnModifyDocument()` r=m_kato `HTMLEditRules::OnModifyDocument()` is same as just calling `EditorBase::EnsureNoPaddingBRElementForEmptyEditor()` and `EditorBase::MaybeCreatePaddingBRElementForEmptyEditor()` so that this patch gets rid of the method, then, creates `HTMLEditor::OnModifyDocumentInternal()` and makes it do same thing. Differential Revision: https://phabricator.services.mozilla.com/D41161
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
editor/libeditor/HTMLEditor.cpp
editor/libeditor/HTMLEditor.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -11113,44 +11113,15 @@ nsresult HTMLEditRules::DocumentModified
 }
 
 void HTMLEditRules::DocumentModifiedWorker() {
   if (NS_WARN_IF(!CanHandleEditAction())) {
     return;
   }
 
   RefPtr<HTMLEditor> htmlEditor(mHTMLEditor);
-  htmlEditor->OnModifyDocument();
-}
-
-void HTMLEditRules::OnModifyDocument() {
-  MOZ_ASSERT(mHTMLEditor);
-
-  AutoSafeEditorData setData(*this, *mHTMLEditor);
-
-  // DeleteNodeWithTransaction() below may cause a flush, which could destroy
-  // the editor
-  nsAutoScriptBlockerSuppressNodeRemoved scriptBlocker;
-
-  // Delete our padding <br> element for empty editor, if we have one, since
-  // the document might not be empty any more.
-  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);
-    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
-                         "Failed to remove the padding <br> element");
-  }
-
-  // Try to recreate the padding <br> element for empty editor if needed.
-  nsresult rv = MOZ_KnownLive(HTMLEditorRef())
-                    .MaybeCreatePaddingBRElementForEmptyEditor();
-  NS_WARNING_ASSERTION(
-      rv != NS_ERROR_EDITOR_DESTROYED,
-      "The editor has been destroyed during creating a padding <br> element");
+  nsresult rv = htmlEditor->OnModifyDocument();
+  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
+                       "HTMLEditor::OnModifyDocument() failed");
   Unused << rv;
 }
 
 }  // namespace mozilla
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -131,23 +131,16 @@ class HTMLEditRules : public TextEditRul
   void DidInsertText(nsINode& aTextNode, int32_t aOffset,
                      const nsAString& aString);
   void DidDeleteText(nsINode& aTextNode, int32_t aOffset, int32_t aLength);
   void WillDeleteSelection();
 
   void StartToListenToEditSubActions() { mListenerEnabled = true; }
   void EndListeningToEditSubActions() { mListenerEnabled = false; }
 
-  /**
-   * OnModifyDocument() is called when DocumentModifiedWorker() calls
-   * HTMLEditor::OnModifyDocument().  The caller guarantees that there
-   * is AutoEditActionDataSetter instance in the editor.
-   */
-  MOZ_CAN_RUN_SCRIPT void OnModifyDocument();
-
  protected:
   virtual ~HTMLEditRules() = default;
 
   HTMLEditor& HTMLEditorRef() const {
     MOZ_ASSERT(mData);
     return mData->HTMLEditorRef();
   }
 
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -5262,27 +5262,47 @@ nsHTMLDocument* HTMLEditor::GetHTMLDocum
     return nullptr;
   }
   if (!doc->IsHTMLOrXHTML()) {
     return nullptr;
   }
   return doc->AsHTMLDocument();
 }
 
-void HTMLEditor::OnModifyDocument() {
-  MOZ_ASSERT(mRules);
-
-  RefPtr<HTMLEditRules> htmlRules = mRules->AsHTMLEditRules();
+nsresult HTMLEditor::OnModifyDocument() {
   if (IsEditActionDataAvailable()) {
-    htmlRules->OnModifyDocument();
-    return;
+    return OnModifyDocumentInternal();
   }
 
   AutoEditActionDataSetter editActionData(
       *this, EditAction::eCreatePaddingBRElementForEmptyEditor);
   if (NS_WARN_IF(!editActionData.CanHandle())) {
-    return;
-  }
-
-  htmlRules->OnModifyDocument();
+    return NS_ERROR_NOT_AVAILABLE;
+  }
+
+  return OnModifyDocumentInternal();
+}
+
+nsresult HTMLEditor::OnModifyDocumentInternal() {
+  MOZ_ASSERT(IsEditActionDataAvailable());
+
+  // EnsureNoPaddingBRElementForEmptyEditor() below may cause a flush, which
+  // could destroy the editor
+  nsAutoScriptBlockerSuppressNodeRemoved scriptBlocker;
+
+  // Delete our padding <br> element for empty editor, if we have one, since
+  // the document might not be empty any more.
+  nsresult rv = EnsureNoPaddingBRElementForEmptyEditor();
+  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
+                       "Failed to remove the padding <br> element");
+  if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
+    return rv;
+  }
+
+  // Try to recreate the padding <br> element for empty editor if needed.
+  rv = MaybeCreatePaddingBRElementForEmptyEditor();
+  NS_WARNING_ASSERTION(
+      rv != NS_ERROR_EDITOR_DESTROYED,
+      "The editor has been destroyed during creating a padding <br> element");
+  return rv;
 }
 
 }  // namespace mozilla
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -1087,17 +1087,17 @@ class HTMLEditor final : public TextEdit
   MOZ_CAN_RUN_SCRIPT nsresult SetPositionToStatic(Element& aElement);
 
   /**
    * OnModifyDocument() is called when the editor is changed.  This should
    * be called only by HTMLEditRules::DocumentModifiedWorker() to call
    * HTMLEditRules::OnModifyDocument() with AutoEditActionDataSetter
    * instance.
    */
-  MOZ_CAN_RUN_SCRIPT void OnModifyDocument();
+  MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult OnModifyDocument();
 
  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
@@ -2533,16 +2533,21 @@ class HTMLEditor final : public TextEdit
    *
    * @param aBlob       The input blob
    * @param aWindow     The global object under which the read should happen.
    * @param aBlobReader The blob reader object to be notified when finished.
    */
   static nsresult SlurpBlob(dom::Blob* aBlob, nsPIDOMWindowOuter* aWindow,
                             BlobReader* aBlobReader);
 
+  /**
+   * OnModifyDocumentInternal() is called by OnModifyDocument().
+   */
+  MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult OnModifyDocumentInternal();
+
  protected:
   RefPtr<TypeInState> mTypeInState;
   RefPtr<ComposerCommandsUpdater> mComposerCommandsUpdater;
 
   bool mCRInParagraphCreatesParagraph;
 
   bool mCSSAware;
   UniquePtr<CSSEditUtils> mCSSEditUtils;