Bug 1460509 - part 4: Make TextEditRules::CreateBogusNodeIfNeeded() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 11 May 2018 16:24:15 +0900
changeset 798722 5ccf7ab97dc41b8a5df61c8af929d318aaf94046
parent 798721 7b2ba37b4f038296072318633aa749a9435962a1
child 798723 284b619f2e968cb81ccd55b941a5d85bd63c3f48
push id110840
push usermasayuki@d-toybox.com
push dateWed, 23 May 2018 13:41:58 +0000
reviewersm_kato
bugs1460509
milestone62.0a1
Bug 1460509 - part 4: Make TextEditRules::CreateBogusNodeIfNeeded() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato Note that HTMLEditRules::DocumentModifiedWorker() is a runnable method. So, it cannot return nsresult. Therefore, it just checks the result only with NS_WARNING_ASSERTION(). MozReview-Commit-ID: KBSpv5H5KGU
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/TextEditRules.cpp
editor/libeditor/TextEditRules.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -10307,12 +10307,14 @@ HTMLEditRules::DocumentModifiedWorker()
     DebugOnly<nsresult> rv =
       HTMLEditorRef().DeleteNodeWithTransaction(*mBogusNode);
     NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
       "Failed to remove the bogus node");
     mBogusNode = nullptr;
   }
 
   // Try to recreate the bogus node if needed.
-  CreateBogusNodeIfNeeded();
+  DebugOnly<nsresult> rv = CreateBogusNodeIfNeeded();
+  NS_WARNING_ASSERTION(rv.value != NS_ERROR_EDITOR_DESTROYED,
+    "The editor has been destroyed during creating a bogus node");
 }
 
 } // namespace mozilla
--- a/editor/libeditor/TextEditRules.cpp
+++ b/editor/libeditor/TextEditRules.cpp
@@ -1454,38 +1454,47 @@ TextEditRules::CreateBogusNodeIfNeeded()
   // Skip adding the bogus node if body is read-only.
   if (!TextEditorRef().IsModifiableNode(rootElement)) {
     return NS_OK;
   }
 
   // Create a br.
   RefPtr<Element> newBrElement =
     TextEditorRef().CreateHTMLContent(nsGkAtoms::br);
+  if (NS_WARN_IF(!CanHandleEditAction())) {
+    return NS_ERROR_EDITOR_DESTROYED;
+  }
   if (NS_WARN_IF(!newBrElement)) {
     return NS_ERROR_FAILURE;
   }
 
   // set mBogusNode to be the newly created <br>
   mBogusNode = newBrElement;
 
   // Give it a special attribute.
   newBrElement->SetAttr(kNameSpaceID_None, kMOZEditorBogusNodeAttrAtom,
                         kMOZEditorBogusNodeValue, false);
 
   // Put the node in the document.
   nsresult rv =
     TextEditorRef().InsertNodeWithTransaction(
                       *mBogusNode, EditorRawDOMPoint(rootElement, 0));
+  if (NS_WARN_IF(!CanHandleEditAction())) {
+    return NS_ERROR_EDITOR_DESTROYED;
+  }
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   // Set selection.
   IgnoredErrorResult error;
   SelectionRef().Collapse(EditorRawDOMPoint(rootElement, 0), error);
+  if (NS_WARN_IF(!CanHandleEditAction())) {
+    return NS_ERROR_EDITOR_DESTROYED;
+  }
   NS_WARNING_ASSERTION(!error.Failed(),
     "Failed to collapse selection at start of the root element");
   return NS_OK;
 }
 
 
 nsresult
 TextEditRules::TruncateInsertionIfNeeded(const nsAString* aInString,
--- a/editor/libeditor/TextEditRules.h
+++ b/editor/libeditor/TextEditRules.h
@@ -182,19 +182,19 @@ protected:
   nsresult RemoveRedundantTrailingBR();
 
   /**
    * Creates a trailing break in the text doc if there is not one already.
    */
   nsresult CreateTrailingBRIfNeeded();
 
   /**
-   * Creates a bogus text node if the document has no editable content.
+   * Creates a bogus <br> node if the root element has no editable content.
    */
-  nsresult CreateBogusNodeIfNeeded();
+  MOZ_MUST_USE nsresult CreateBogusNodeIfNeeded();
 
   /**
    * Returns a truncated insertion string if insertion would place us over
    * aMaxLength
    */
   nsresult TruncateInsertionIfNeeded(const nsAString* aInString,
                                      nsAString* aOutString,
                                      int32_t aMaxLength,