Bug 1460509 - part 9: Make TextEditRules::WillSetText() 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 17:33:55 +0900
changeset 798727 36e927743abd7e646870751ac4126c5edc1d7bc6
parent 798726 9271e9273e1af33f91eb4b0d731d022883927f21
child 798728 8c820293c9b1e29b8f170c001f985920de16a060
push id110840
push usermasayuki@d-toybox.com
push dateWed, 23 May 2018 13:41:58 +0000
reviewersm_kato
bugs1460509
milestone62.0a1
Bug 1460509 - part 9: Make TextEditRules::WillSetText() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato MozReview-Commit-ID: 9ksPugTVVqh
editor/libeditor/TextEditRules.cpp
editor/libeditor/TextEditRules.h
--- a/editor/libeditor/TextEditRules.cpp
+++ b/editor/libeditor/TextEditRules.cpp
@@ -901,16 +901,19 @@ TextEditRules::WillSetText(bool* aCancel
     }
     RefPtr<nsTextNode> newNode = EditorBase::CreateTextNode(*doc, tString);
     if (NS_WARN_IF(!newNode)) {
       return NS_OK;
     }
     nsresult rv =
       TextEditorRef().InsertNodeWithTransaction(
                         *newNode, EditorRawDOMPoint(rootElement, 0));
+    if (NS_WARN_IF(!CanHandleEditAction())) {
+      return NS_ERROR_EDITOR_DESTROYED;
+    }
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     *aHandled = true;
 
     ASSERT_PASSWORD_LENGTHS_EQUAL();
 
     return NS_OK;
@@ -920,16 +923,19 @@ TextEditRules::WillSetText(bool* aCancel
   if (NS_WARN_IF(!EditorBase::IsTextNode(curNode))) {
     return NS_OK;
   }
 
   // Even if empty text, we don't remove text node and set empty text
   // for performance
   nsresult rv = TextEditorRef().SetTextImpl(SelectionRef(), tString,
                                             *curNode->GetAsText());
+  if (NS_WARN_IF(!CanHandleEditAction())) {
+    return NS_ERROR_EDITOR_DESTROYED;
+  }
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   *aHandled = true;
 
   ASSERT_PASSWORD_LENGTHS_EQUAL();
 
--- a/editor/libeditor/TextEditRules.h
+++ b/editor/libeditor/TextEditRules.h
@@ -134,20 +134,30 @@ protected:
                           bool* aCancel,
                           bool* aHandled,
                           const nsAString* inString,
                           nsAString* outString,
                           int32_t aMaxLength);
 
   nsresult WillInsertBreak(bool* aCancel, bool* aHandled, int32_t aMaxLength);
 
-  nsresult WillSetText(bool* aCancel,
-                       bool* aHandled,
-                       const nsAString* inString,
-                       int32_t aMaxLength);
+  /**
+   * Called before setting text to the text editor.
+   * This method may actually set text to it.  Therefore, this might cause
+   * destroying the text editor.
+   *
+   * @param aCancel             Returns true if the operation is canceled.
+   * @param aHandled            Returns true if the edit action is handled.
+   * @param inString            String to be set.
+   * @param aMaxLength          The maximum string length which the text editor
+   *                            allows to set.
+   */
+  MOZ_MUST_USE nsresult
+  WillSetText(bool* aCancel, bool* aHandled,
+              const nsAString* inString, int32_t aMaxLength);
 
   void WillInsert(bool* aCancel);
 
   /**
    * Called before deleting selected content.
    * This method may actually remove the selected content with
    * DeleteSelectionWithTransaction().  So, this might cause destroying the
    * editor.