diff --git a/editor/libeditor/HTMLEditRules.cpp b/editor/libeditor/HTMLEditRules.cpp
index fcd278a3f226..70850f64e3ef 100644
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -11118,38 +11118,9 @@ void HTMLEditRules::DocumentModifiedWorker() {
}
RefPtr 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
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
element for empty
- // editor again during the call of DeleteNodeWithTransaction(). So, move
- // it first.
- RefPtr paddingBRElement(
- std::move(HTMLEditorRef().mPaddingBRElementForEmptyEditor));
- DebugOnly rv = MOZ_KnownLive(HTMLEditorRef())
- .DeleteNodeWithTransaction(*paddingBRElement);
- NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
- "Failed to remove the padding
element");
- }
-
- // Try to recreate the padding
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
element");
+ nsresult rv = htmlEditor->OnModifyDocument();
+ NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
+ "HTMLEditor::OnModifyDocument() failed");
Unused << rv;
}
diff --git a/editor/libeditor/HTMLEditRules.h b/editor/libeditor/HTMLEditRules.h
index 55f14203ec9c..7ccd3c3e08d5 100644
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -136,13 +136,6 @@ class HTMLEditRules : public TextEditRules {
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;
diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp
index 21e8c1ca5305..57639b3bce63 100644
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -5267,22 +5267,42 @@ nsHTMLDocument* HTMLEditor::GetHTMLDocument() const {
return doc->AsHTMLDocument();
}
-void HTMLEditor::OnModifyDocument() {
- MOZ_ASSERT(mRules);
-
- RefPtr 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;
+ return NS_ERROR_NOT_AVAILABLE;
}
- htmlRules->OnModifyDocument();
+ return OnModifyDocumentInternal();
+}
+
+nsresult HTMLEditor::OnModifyDocumentInternal() {
+ MOZ_ASSERT(IsEditActionDataAvailable());
+
+ // EnsureNoPaddingBRElementForEmptyEditor() below may cause a flush, which
+ // could destroy the editor
+ nsAutoScriptBlockerSuppressNodeRemoved scriptBlocker;
+
+ // Delete our padding
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
element");
+ if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
+ return rv;
+ }
+
+ // Try to recreate the padding
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
element");
+ return rv;
}
} // namespace mozilla
diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h
index ba0aad9be15f..b185ed077873 100644
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -1092,7 +1092,7 @@ class HTMLEditor final : public TextEditor,
* 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(
@@ -2538,6 +2538,11 @@ class HTMLEditor final : public TextEditor,
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 mTypeInState;
RefPtr mComposerCommandsUpdater;