Bug 1460509 - part 55: Make HTMLEditRules::AfterEditInner() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Wed, 16 May 2018 14:32:47 +0900
changeset 798773 a12b9cff5631dee020fa967a21ccbb1325c4790a
parent 798772 1e792c98bfdbc481c6b360a85bcdda6a6bcd6b5a
child 798774 4abbd928c523da899bd2a47ecb780041de3acb4b
push id110840
push usermasayuki@d-toybox.com
push dateWed, 23 May 2018 13:41:58 +0000
reviewersm_kato
bugs1460509
milestone62.0a1
Bug 1460509 - part 55: Make HTMLEditRules::AfterEditInner() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato MozReview-Commit-ID: 2qdCWOdMseM
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -438,16 +438,19 @@ HTMLEditRules::AfterEdit(EditAction aAct
     if (NS_WARN_IF(!selection)) {
       return NS_ERROR_FAILURE;
     }
 
     AutoSafeEditorData setData(*this, *mHTMLEditor, *selection);
 
     // Do all the tricky stuff
     rv = AfterEditInner(aAction, aDirection);
+    // Perhaps, we need to do the following jobs even if the editor has been
+    // destroyed since they adjust some states of HTML document but don't
+    // modify the DOM tree nor Selection.
 
     // Free up selectionState range item
     HTMLEditorRef().mRangeUpdater.DropRangeItem(mRangeItem);
 
     // Reset the contenteditable count to its previous value
     if (mRestoreContentEditableCount) {
       nsHTMLDocument* htmlDoc = HTMLEditorRef().GetHTMLDocument();
       if (NS_WARN_IF(!htmlDoc)) {
@@ -523,16 +526,19 @@ HTMLEditRules::AfterEditInner(EditAction
     }
     NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
       "Failed to insert <br> elements to empty list items and table cells");
 
     // merge any adjacent text nodes
     if (aAction != EditAction::insertText &&
         aAction != EditAction::insertIMEText) {
       nsresult rv = HTMLEditorRef().CollapseAdjacentTextNodes(mDocChangeRange);
+      if (NS_WARN_IF(!CanHandleEditAction())) {
+        return NS_ERROR_EDITOR_DESTROYED;
+      }
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     }
 
     // clean up any empty nodes in the selection
     rv = RemoveEmptyNodesInChangedRange();
     if (NS_WARN_IF(NS_FAILED(rv))) {
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -470,18 +470,23 @@ protected:
    * @param aNode               Typically, Selection start container, where to
    *                            insert a break.
    * @param aOffset             Typically, Selection start offset in the
    *                            start container, where to insert a break.
    */
   MOZ_MUST_USE nsresult
   ReturnInListItem(Element& aListItem, nsINode& aNode, int32_t aOffset);
 
-  nsresult AfterEditInner(EditAction action,
-                          nsIEditor::EDirection aDirection);
+  /**
+   * Called after handling edit action.  This may adjust Selection, remove
+   * unnecessary empty nodes, create <br> elements if needed, etc.
+   */
+  MOZ_MUST_USE nsresult
+  AfterEditInner(EditAction action, nsIEditor::EDirection aDirection);
+
   nsresult RemovePartOfBlock(Element& aBlock, nsIContent& aStartChild,
                              nsIContent& aEndChild);
   void SplitBlock(Element& aBlock,
                   nsIContent& aStartChild,
                   nsIContent& aEndChild,
                   nsIContent** aOutLeftNode = nullptr,
                   nsIContent** aOutRightNode = nullptr,
                   nsIContent** aOutMiddleNode = nullptr);