Bug 1460509 - part 29: Make HTMLEditRules::AdjustSpecialBreaks() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 14 May 2018 22:56:06 +0900
changeset 798747 1ee689274b0e610f38723bbc86798f5fa86100c2
parent 798746 b8fbae3ccecd66392b37df3b750a56c789f2f5bf
child 798748 05a4ba8cb0b3ef67deb6de66bdc764f31e42b993
push id110840
push usermasayuki@d-toybox.com
push dateWed, 23 May 2018 13:41:58 +0000
reviewersm_kato
bugs1460509
milestone62.0a1
Bug 1460509 - part 29: Make HTMLEditRules::AdjustSpecialBreaks() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato And this patch renames it to InsertBRElementToEmptyListItemsAndTableCellsInChangedRange(). MozReview-Commit-ID: 1DGhcI93YAk
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -296,22 +296,27 @@ HTMLEditRules::Init(TextEditor* aTextEdi
   // set up mDocChangeRange to be whole doc
   // temporarily turn off rules sniffing
   AutoLockRulesSniffing lockIt(this);
   if (!mDocChangeRange) {
     mDocChangeRange = new nsRange(node);
   }
 
   if (node->IsElement()) {
-    ErrorResult rv;
-    mDocChangeRange->SelectNode(*node, rv);
-    if (NS_WARN_IF(rv.Failed())) {
-      return rv.StealNSResult();
-    }
-    AdjustSpecialBreaks();
+    ErrorResult error;
+    mDocChangeRange->SelectNode(*node, error);
+    if (NS_WARN_IF(error.Failed())) {
+      return error.StealNSResult();
+    }
+    nsresult rv = InsertBRElementToEmptyListItemsAndTableCellsInChangedRange();
+    if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
+      return NS_ERROR_EDITOR_DESTROYED;
+    }
+    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
+      "Failed to insert <br> elements to empty list items and table cells");
   }
 
   StartToListenToEditActions();
 
   return NS_OK;
 }
 
 nsresult
@@ -507,29 +512,34 @@ HTMLEditRules::AfterEditInner(EditAction
     if (aAction == EditAction::deleteSelection && mDidRangedDelete) {
       nsresult rv = InsertBRIfNeeded();
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     }
 
     // add in any needed <br>s, and remove any unneeded ones.
-    AdjustSpecialBreaks();
+    nsresult rv = InsertBRElementToEmptyListItemsAndTableCellsInChangedRange();
+    if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
+      return NS_ERROR_EDITOR_DESTROYED;
+    }
+    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(NS_FAILED(rv))) {
         return rv;
       }
     }
 
     // clean up any empty nodes in the selection
-    nsresult rv = RemoveEmptyNodesInChangedRange();
+    rv = RemoveEmptyNodesInChangedRange();
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
     // attempt to transform any unneeded nbsp's into spaces after doing various operations
     if (aAction == EditAction::insertText ||
         aAction == EditAction::insertIMEText ||
         aAction == EditAction::deleteSelection ||
@@ -8587,45 +8597,46 @@ HTMLEditRules::ClearCachedStyles()
 {
   // clear the mPresent bits in mCachedStyles array
   for (size_t j = 0; j < SIZE_STYLE_TABLE; j++) {
     mCachedStyles[j].mPresent = false;
     mCachedStyles[j].value.Truncate();
   }
 }
 
-void
-HTMLEditRules::AdjustSpecialBreaks()
+nsresult
+HTMLEditRules::InsertBRElementToEmptyListItemsAndTableCellsInChangedRange()
 {
   MOZ_ASSERT(IsEditorDataAvailable());
 
   // Gather list of empty nodes
   nsTArray<OwningNonNull<nsINode>> nodeArray;
   EmptyEditableFunctor functor(&HTMLEditorRef());
   DOMIterator iter;
   if (NS_WARN_IF(NS_FAILED(iter.Init(*mDocChangeRange)))) {
-    return;
+    return NS_ERROR_FAILURE;
   }
   iter.AppendList(functor, nodeArray);
 
   // Put moz-br's into these empty li's and td's
   for (auto& node : nodeArray) {
     // Need to put br at END of node.  It may have empty containers in it and
     // still pass the "IsEmptyNode" test, and we want the br's to be after
     // them.  Also, we want the br to be after the selection if the selection
     // is in this node.
     EditorRawDOMPoint endOfNode;
     endOfNode.SetToEndOf(node);
     // XXX This method should return nsreuslt due to may be destroyed by this
     //     CreateMozBr() call.
     CreateElementResult createMozBrResult = CreateMozBR(endOfNode);
     if (NS_WARN_IF(createMozBrResult.Failed())) {
-      return;
-    }
-  }
+      return createMozBrResult.Rv();
+    }
+  }
+  return NS_OK;
 }
 
 nsresult
 HTMLEditRules::AdjustWhitespace()
 {
   MOZ_ASSERT(IsEditorDataAvailable());
 
   EditorRawDOMPoint selectionStartPoint(
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -588,17 +588,23 @@ protected:
    *
    * @param aListElement        A <ul>, <ol> or <dl> element.
    */
   MOZ_MUST_USE nsresult RemoveListStructure(Element& aListElement);
 
   nsresult CacheInlineStyles(nsINode* aNode);
   nsresult ReapplyCachedStyles();
   void ClearCachedStyles();
-  void AdjustSpecialBreaks();
+
+  /**
+   * InsertBRElementToEmptyListItemsAndTableCellsInChangedRange() inserts
+   * <br> element into empty list item or table cell elements.
+   */
+  MOZ_MUST_USE nsresult
+  InsertBRElementToEmptyListItemsAndTableCellsInChangedRange();
 
   /**
    * AdjustWhitespace() may replace whitespaces with NBSP or something.
    * See WSRunObject::AdjustWhitespace() for the detail.
    */
   MOZ_MUST_USE nsresult AdjustWhitespace();
 
   /**