diff --git a/editor/libeditor/html/nsHTMLEditRules.cpp b/editor/libeditor/html/nsHTMLEditRules.cpp
index b8ed9e948ecb..637e06581b3b 100644
--- a/editor/libeditor/html/nsHTMLEditRules.cpp
+++ b/editor/libeditor/html/nsHTMLEditRules.cpp
@@ -274,6 +274,9 @@ nsHTMLEditRules::Init(nsPlaintextEditor *aEditor)
NS_IMETHODIMP
nsHTMLEditRules::DetachEditor()
{
+ if (mHTMLEditor) {
+ mHTMLEditor->RemoveEditActionListener(this);
+ }
mHTMLEditor = nullptr;
return nsTextEditRules::DetachEditor();
}
diff --git a/editor/libeditor/html/nsHTMLEditor.cpp b/editor/libeditor/html/nsHTMLEditor.cpp
index 0bae02cc8053..e56ebd08137a 100644
--- a/editor/libeditor/html/nsHTMLEditor.cpp
+++ b/editor/libeditor/html/nsHTMLEditor.cpp
@@ -484,6 +484,7 @@ nsHTMLEditor::SetFlags(uint32_t aFlags)
NS_IMETHODIMP
nsHTMLEditor::InitRules()
{
+ MOZ_ASSERT(!mRules);
// instantiate the rules for the html editor
mRules = new nsHTMLEditRules();
return mRules->Init(static_cast(this));
diff --git a/editor/libeditor/text/nsPlaintextEditor.cpp b/editor/libeditor/text/nsPlaintextEditor.cpp
index 93648859e0ad..05aaefbeb62b 100644
--- a/editor/libeditor/text/nsPlaintextEditor.cpp
+++ b/editor/libeditor/text/nsPlaintextEditor.cpp
@@ -121,6 +121,10 @@ NS_IMETHODIMP nsPlaintextEditor::Init(nsIDOMDocument *aDoc,
NS_ENSURE_TRUE(aDoc, NS_ERROR_NULL_POINTER);
nsresult res = NS_OK, rulesRes = NS_OK;
+ if (mRules) {
+ mRules->DetachEditor();
+ mRules = nullptr;
+ }
if (1)
{
@@ -313,6 +317,7 @@ nsPlaintextEditor::UpdateMetaCharset(nsIDOMDocument* aDocument,
NS_IMETHODIMP nsPlaintextEditor::InitRules()
{
+ MOZ_ASSERT(!mRules);
// instantiate the rules for this text editor
mRules = new nsTextEditRules();
return mRules->Init(this);