Bug 1460509 - part 35: Make HTMLEditRules::ApplyBlockStyle() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 15 May 2018 00:08:46 +0900
changeset 798753 f94116dd9e963c971e6b9bc9027188fa4646f6d6
parent 798752 cf57752d514063481155c116e01d856a4cd1b63f
child 798754 b6d111b1f3d4eae47fb96f3b00bbd3f91af8413c
push id110840
push usermasayuki@d-toybox.com
push dateWed, 23 May 2018 13:41:58 +0000
reviewersm_kato
bugs1460509
milestone62.0a1
Bug 1460509 - part 35: Make HTMLEditRules::ApplyBlockStyle() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato MozReview-Commit-ID: 1ddHNx7lF9P
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -8151,20 +8151,16 @@ HTMLEditRules::RemoveBlockStyle(nsTArray
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     firstNode = lastNode = curBlock = nullptr;
   }
   return NS_OK;
 }
 
-/**
- * ApplyBlockStyle() does whatever it takes to make the list of nodes into one
- * or more blocks of type aBlockTag.
- */
 nsresult
 HTMLEditRules::ApplyBlockStyle(nsTArray<OwningNonNull<nsINode>>& aNodeArray,
                                nsAtom& aBlockTag)
 {
   // Intent of this routine is to be used for converting to/from headers,
   // paragraphs, pre, and address.  Those blocks that pretty much just contain
   // inline things...
   MOZ_ASSERT(IsEditorDataAvailable());
@@ -8189,16 +8185,19 @@ HTMLEditRules::ApplyBlockStyle(nsTArray<
     // XXX: pre can't hold everything the others can
     if (HTMLEditUtils::IsMozDiv(curNode) ||
         HTMLEditUtils::IsFormatNode(curNode)) {
       // Forget any previous block used for previous inline nodes
       curBlock = nullptr;
       newBlock =
         HTMLEditorRef().ReplaceContainerAndCloneAttributesWithTransaction(
                           *curNode->AsElement(), aBlockTag);
+      if (NS_WARN_IF(!CanHandleEditAction())) {
+        return NS_ERROR_EDITOR_DESTROYED;
+      }
       if (NS_WARN_IF(!newBlock)) {
         return NS_ERROR_FAILURE;
       }
       continue;
     }
 
     if (HTMLEditUtils::IsTable(curNode) ||
         HTMLEditUtils::IsList(curNode) ||
@@ -8225,56 +8224,68 @@ HTMLEditRules::ApplyBlockStyle(nsTArray<
       SplitNodeResult splitNodeResult =
         MaybeSplitAncestorsForInsertWithTransaction(aBlockTag, atCurNode);
       if (NS_WARN_IF(splitNodeResult.Failed())) {
         return splitNodeResult.Rv();
       }
       RefPtr<Element> theBlock =
         HTMLEditorRef().CreateNodeWithTransaction(aBlockTag,
                                                   splitNodeResult.SplitPoint());
+      if (NS_WARN_IF(!CanHandleEditAction())) {
+        return NS_ERROR_EDITOR_DESTROYED;
+      }
       if (NS_WARN_IF(!theBlock)) {
         return NS_ERROR_FAILURE;
       }
       // Remember our new block for postprocessing
       mNewBlock = theBlock;
       continue;
     }
 
     if (curNode->IsHTMLElement(nsGkAtoms::br)) {
       // If the node is a break, we honor it by putting further nodes in a new
       // parent
       if (curBlock) {
         // Forget any previous block used for previous inline nodes
         curBlock = nullptr;
         nsresult rv = HTMLEditorRef().DeleteNodeWithTransaction(*curNode);
+        if (NS_WARN_IF(!CanHandleEditAction())) {
+          return NS_ERROR_EDITOR_DESTROYED;
+        }
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return rv;
         }
         continue;
       }
 
       // The break is the first (or even only) node we encountered.  Create a
       // block for it.
       SplitNodeResult splitNodeResult =
         MaybeSplitAncestorsForInsertWithTransaction(aBlockTag, atCurNode);
       if (NS_WARN_IF(splitNodeResult.Failed())) {
         return splitNodeResult.Rv();
       }
       curBlock =
         HTMLEditorRef().CreateNodeWithTransaction(aBlockTag,
                                                   splitNodeResult.SplitPoint());
+      if (NS_WARN_IF(!CanHandleEditAction())) {
+        return NS_ERROR_EDITOR_DESTROYED;
+      }
       if (NS_WARN_IF(!curBlock)) {
         return NS_ERROR_FAILURE;
       }
       // Remember our new block for postprocessing
       mNewBlock = curBlock;
       // Note: doesn't matter if we set mNewBlock multiple times.
       nsresult rv =
         HTMLEditorRef().MoveNodeToEndWithTransaction(*curNode->AsContent(),
                                                      *curBlock);
+      if (NS_WARN_IF(!CanHandleEditAction())) {
+        return NS_ERROR_EDITOR_DESTROYED;
+      }
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
       continue;
     }
 
     if (IsInlineNode(curNode)) {
       // If curNode is inline, pull it into curBlock.  Note: it's assumed that
@@ -8297,16 +8308,19 @@ HTMLEditRules::ApplyBlockStyle(nsTArray<
         SplitNodeResult splitNodeResult =
           MaybeSplitAncestorsForInsertWithTransaction(aBlockTag, atCurNode);
         if (NS_WARN_IF(splitNodeResult.Failed())) {
           return splitNodeResult.Rv();
         }
         curBlock =
           HTMLEditorRef().CreateNodeWithTransaction(
                             aBlockTag, splitNodeResult.SplitPoint());
+        if (NS_WARN_IF(!CanHandleEditAction())) {
+          return NS_ERROR_EDITOR_DESTROYED;
+        }
         if (NS_WARN_IF(!curBlock)) {
           return NS_ERROR_FAILURE;
         }
         // Remember our new block for postprocessing
         mNewBlock = curBlock;
         // Note: doesn't matter if we set mNewBlock multiple times.
       }
 
@@ -8317,16 +8331,19 @@ HTMLEditRules::ApplyBlockStyle(nsTArray<
 
       // XXX If curNode is a br, replace it with a return if going to <pre>
 
       // This is a continuation of some inline nodes that belong together in
       // the same block item.  Use curBlock.
       nsresult rv =
         HTMLEditorRef().MoveNodeToEndWithTransaction(*curNode->AsContent(),
                                                      *curBlock);
+      if (NS_WARN_IF(!CanHandleEditAction())) {
+        return NS_ERROR_EDITOR_DESTROYED;
+      }
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     }
   }
   return NS_OK;
 }
 
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -500,18 +500,19 @@ protected:
    * elements.
    * If aNodeArray has a block element, this calls itself with children of
    * the block element.  Then, new block element will be created when there
    * are some remaining inline elements.
    *
    * @param aNodeArray      Must be descendants of a node.
    * @param aBlockTag       The element name of new block elements.
    */
-  nsresult ApplyBlockStyle(nsTArray<OwningNonNull<nsINode>>& aNodeArray,
-                           nsAtom& aBlockTag);
+  MOZ_MUST_USE nsresult
+  ApplyBlockStyle(nsTArray<OwningNonNull<nsINode>>& aNodeArray,
+                  nsAtom& aBlockTag);
 
   nsresult MakeBlockquote(nsTArray<OwningNonNull<nsINode>>& aNodeArray);
 
   /**
    * MaybeSplitAncestorsForInsertWithTransaction() does nothing if container of
    * aStartOfDeepestRightNode can have an element whose tag name is aTag.
    * Otherwise, looks for an ancestor node which is or is in active editing
    * host and can have an element whose name is aTag.  If there is such