Bug 1460509 - part 50: Make HTMLEditRules::InsertBRElement() 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 01:47:54 +0900
changeset 798768 4b8964cc9f1759bbdff80dda48bfde1817697c33
parent 798767 635141382655a9adfa90161da1c6ce32b428d582
child 798769 1a7f408752c5f201df8759648d8ea8bd2a63b743
push id110840
push usermasayuki@d-toybox.com
push dateWed, 23 May 2018 13:41:58 +0000
reviewersm_kato
bugs1460509
milestone62.0a1
Bug 1460509 - part 50: Make HTMLEditRules::InsertBRElement() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato MozReview-Commit-ID: 7C3iy7T04M9
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -2014,16 +2014,19 @@ HTMLEditRules::InsertBRElement(const Edi
   bool brElementIsBeforeBlock = false;
 
   // First, insert a <br> element.
   RefPtr<Element> brElement;
   if (IsPlaintextEditor()) {
     brElement =
       HTMLEditorRef().InsertBrElementWithTransaction(SelectionRef(),
                                                      aPointToBreak);
+    if (NS_WARN_IF(!CanHandleEditAction())) {
+      return NS_ERROR_EDITOR_DESTROYED;
+    }
     if (NS_WARN_IF(!brElement)) {
       return NS_ERROR_FAILURE;
     }
   } else {
     EditorDOMPoint pointToBreak(aPointToBreak);
     WSRunObject wsObj(&HTMLEditorRef(), pointToBreak);
     int32_t visOffset = 0;
     WSType wsType;
@@ -2046,23 +2049,29 @@ HTMLEditRules::InsertBRElement(const Edi
       nsCOMPtr<Element> linkNode = do_QueryInterface(linkDOMNode);
       if (NS_WARN_IF(!linkNode)) {
         return NS_ERROR_FAILURE;
       }
       SplitNodeResult splitLinkNodeResult =
         HTMLEditorRef().SplitNodeDeepWithTransaction(
                           *linkNode, pointToBreak,
                           SplitAtEdges::eDoNotCreateEmptyContainer);
+      if (NS_WARN_IF(!CanHandleEditAction())) {
+        return NS_ERROR_EDITOR_DESTROYED;
+      }
       if (NS_WARN_IF(splitLinkNodeResult.Failed())) {
         return splitLinkNodeResult.Rv();
       }
       pointToBreak = splitLinkNodeResult.SplitPoint();
     }
     brElement =
       wsObj.InsertBreak(SelectionRef(), pointToBreak, nsIEditor::eNone);
+    if (NS_WARN_IF(!CanHandleEditAction())) {
+      return NS_ERROR_EDITOR_DESTROYED;
+    }
     if (NS_WARN_IF(!brElement)) {
       return NS_ERROR_FAILURE;
     }
   }
 
   // If the <br> element has already been removed from the DOM tree by a
   // mutation observer, don't continue handling this.
   if (NS_WARN_IF(!brElement->GetParentNode())) {
@@ -2077,16 +2086,20 @@ HTMLEditRules::InsertBRElement(const Edi
     //     modifying the DOM tree.  So, now, the <br> element may not be
     //     between blocks.
     ErrorResult error;
     SelectionRef().SetInterlinePosition(true, error);
     NS_WARNING_ASSERTION(!error.Failed(), "Failed to set interline position");
     EditorRawDOMPoint point(brElement);
     error = NS_OK;
     SelectionRef().Collapse(point, error);
+    if (NS_WARN_IF(!CanHandleEditAction())) {
+      error.SuppressException();
+      return NS_ERROR_EDITOR_DESTROYED;
+    }
     if (NS_WARN_IF(error.Failed())) {
       return error.StealNSResult();
     }
     return NS_OK;
   }
 
   EditorDOMPoint afterBRElement(brElement);
   DebugOnly<bool> advanced = afterBRElement.AdvanceOffset();
@@ -2105,16 +2118,19 @@ HTMLEditRules::InsertBRElement(const Edi
     // SetInterlinePosition().  It will also assure that if the user clicks
     // away and then clicks back on their new blank line, they will still get
     // the style from the line above.
     EditorDOMPoint atSecondBRElement(maybeSecondBRNode);
     if (brElement->GetNextSibling() != maybeSecondBRNode) {
       nsresult rv =
         HTMLEditorRef().MoveNodeWithTransaction(*maybeSecondBRNode->AsContent(),
                                                 afterBRElement);
+      if (NS_WARN_IF(!CanHandleEditAction())) {
+        return NS_ERROR_EDITOR_DESTROYED;
+      }
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     }
   }
 
   // SetInterlinePosition(true) means we want the caret to stick to the
   // content on the "right".  We want the caret to stick to whatever is past
@@ -2127,16 +2143,20 @@ HTMLEditRules::InsertBRElement(const Edi
   ErrorResult error;
   SelectionRef().SetInterlinePosition(!(nextSiblingOfBRElement &&
                                         IsBlockNode(*nextSiblingOfBRElement)),
                                       error);
   NS_WARNING_ASSERTION(!error.Failed(),
     "Failed to set or unset interline position");
   error = NS_OK;
   SelectionRef().Collapse(afterBRElement, error);
+  if (NS_WARN_IF(!CanHandleEditAction())) {
+    error.SuppressException();
+    return NS_ERROR_EDITOR_DESTROYED;
+  }
   if (NS_WARN_IF(error.Failed())) {
     return error.StealNSResult();
   }
   return NS_OK;
 }
 
 nsresult
 HTMLEditRules::SplitMailCites(bool* aHandled)
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -163,17 +163,17 @@ protected:
   void DeleteNodeIfCollapsedText(nsINode& aNode);
 
   /**
    * InsertBRElement() inserts a <br> element into aInsertToBreak.
    *
    * @param aInsertToBreak      The point where new <br> element will be
    *                            inserted before.
    */
-  nsresult InsertBRElement(const EditorDOMPoint& aInsertToBreak);
+  MOZ_MUST_USE nsresult InsertBRElement(const EditorDOMPoint& aInsertToBreak);
 
   /**
    * SplitMailCites() splits mail-cite elements at start of Selection if
    * Selection starts from inside a mail-cite element.  Of course, if it's
    * necessary, this inserts <br> node to new left nodes or existing right
    * nodes.
    *
    * @param aHandled            Returns true if succeeded to split mail-cite