Bug 1460509 - part 74: Make HTMLEditRules::DidDeleteSelection() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Thu, 17 May 2018 23:21:52 +0900
changeset 798792 1aacc148d12799d83bc74b115da1e2377f063c69
parent 798791 e98e1f80a80fdfcfd57d56579a8589285c9a5df1
child 798793 2790be26fe1665a79b37887ebd6123a7939beaaa
push id110840
push usermasayuki@d-toybox.com
push dateWed, 23 May 2018 13:41:58 +0000
reviewersm_kato
bugs1460509
milestone62.0a1
Bug 1460509 - part 74: Make HTMLEditRules::DidDeleteSelection() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato MozReview-Commit-ID: E3LGBAbaw7N
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -767,17 +767,17 @@ HTMLEditRules::DidDoAction(Selection* aS
   AutoSafeEditorData setData(*this, *mHTMLEditor, *aSelection);
 
   switch (aInfo->action) {
     case EditAction::insertText:
     case EditAction::insertBreak:
     case EditAction::insertIMEText:
       return NS_OK;
     case EditAction::deleteSelection:
-      return DidDeleteSelection(aInfo->collapsedAction, aResult);
+      return DidDeleteSelection();
     case EditAction::makeBasicBlock:
     case EditAction::indent:
     case EditAction::outdent:
     case EditAction::align:
       return DidMakeBasicBlock();
     case EditAction::setAbsolutePosition: {
       nsresult rv = DidMakeBasicBlock();
       if (NS_WARN_IF(NS_FAILED(rv))) {
@@ -3810,18 +3810,17 @@ HTMLEditRules::DeleteNonTableElements(ns
   for (const auto& child: childList) {
     nsresult rv = DeleteNonTableElements(child);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   return NS_OK;
 }
 
 nsresult
-HTMLEditRules::DidDeleteSelection(nsIEditor::EDirection aDir,
-                                  nsresult aResult)
+HTMLEditRules::DidDeleteSelection()
 {
   MOZ_ASSERT(IsEditorDataAvailable());
 
   // find where we are
   EditorDOMPoint atStartOfSelection(EditorBase::GetStartPoint(&SelectionRef()));
   if (NS_WARN_IF(!atStartOfSelection.IsSet())) {
     return NS_ERROR_FAILURE;
   }
@@ -3833,37 +3832,50 @@ HTMLEditRules::DidDeleteSelection(nsIEdi
     bool isEmpty = true, seenBR = false;
     HTMLEditorRef().IsEmptyNodeImpl(citeNode, &isEmpty, true, true, false,
                                     &seenBR);
     if (isEmpty) {
       EditorDOMPoint atCiteNode(citeNode);
       {
         AutoEditorDOMPointChildInvalidator lockOffset(atCiteNode);
         nsresult rv = HTMLEditorRef().DeleteNodeWithTransaction(*citeNode);
+        if (NS_WARN_IF(!CanHandleEditAction())) {
+          return NS_ERROR_EDITOR_DESTROYED;
+        }
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return rv;
         }
       }
       if (atCiteNode.IsSet() && seenBR) {
         RefPtr<Element> brElement =
           HTMLEditorRef().InsertBrElementWithTransaction(SelectionRef(),
                                                          atCiteNode);
+        if (NS_WARN_IF(!CanHandleEditAction())) {
+          return NS_ERROR_EDITOR_DESTROYED;
+        }
         if (NS_WARN_IF(!brElement)) {
           return NS_ERROR_FAILURE;
         }
         IgnoredErrorResult error;
         SelectionRef().Collapse(EditorRawDOMPoint(brElement), error);
+        if (NS_WARN_IF(!CanHandleEditAction())) {
+          return NS_ERROR_EDITOR_DESTROYED;
+        }
         NS_WARNING_ASSERTION(!error.Failed(),
           "Failed to collapse selection at the new <br> element");
       }
     }
   }
 
   // call through to base class
-  return TextEditRules::DidDeleteSelection();
+  nsresult rv = TextEditRules::DidDeleteSelection();
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+  return NS_OK;
 }
 
 nsresult
 HTMLEditRules::WillMakeList(const nsAString* aListType,
                             bool aEntireList,
                             const nsAString* aBulletType,
                             bool* aCancel,
                             bool* aHandled,
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -214,18 +214,24 @@ protected:
    * @param aHandled            Returns true if succeeded to split mail-cite
    *                            elements.
    */
   MOZ_MUST_USE nsresult SplitMailCites(bool* aHandled);
 
   nsresult WillDeleteSelection(nsIEditor::EDirection aAction,
                                nsIEditor::EStripWrappers aStripWrappers,
                                bool* aCancel, bool* aHandled);
-  nsresult DidDeleteSelection(nsIEditor::EDirection aDir,
-                              nsresult aResult);
+
+  /**
+   * Called after deleting selected content.
+   * This method removes unnecessary empty nodes and/or inserts <br> if
+   * necessary.
+   */
+  MOZ_MUST_USE nsresult DidDeleteSelection();
+
   nsresult InsertBRIfNeeded();
 
   /**
    * CanContainParagraph() returns true if aElement can have a <p> element as
    * its child or its descendant.
    */
   bool CanContainParagraph(Element& aElement) const;