Bug 1460509 - part 30: Make HTMLEditRules::ReapplyCachedStyles() 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 23:14:50 +0900
changeset 798748 05a4ba8cb0b3ef67deb6de66bdc764f31e42b993
parent 798747 1ee689274b0e610f38723bbc86798f5fa86100c2
child 798749 2883b6d37cc69778dd9ba3190fd28bebb1048766
push id110840
push usermasayuki@d-toybox.com
push dateWed, 23 May 2018 13:41:58 +0000
reviewersm_kato
bugs1460509
milestone62.0a1
Bug 1460509 - part 30: Make HTMLEditRules::ReapplyCachedStyles() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato MozReview-Commit-ID: HSv2els7Lys
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -1400,17 +1400,19 @@ HTMLEditRules::WillInsert(bool* aCancel)
     }
   }
 
   if (mDidDeleteSelection &&
       (mTheAction == EditAction::insertText ||
        mTheAction == EditAction::insertIMEText ||
        mTheAction == EditAction::deleteSelection)) {
     nsresult rv = ReapplyCachedStyles();
-    NS_ENSURE_SUCCESS_VOID(rv);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return;
+    }
   }
   // For most actions we want to clear the cached styles, but there are
   // exceptions
   if (!IsStyleCachePreservingAction(mTheAction)) {
     ClearCachedStyles();
   }
 }
 
@@ -8534,18 +8536,22 @@ HTMLEditRules::ReapplyCachedStyles()
 
   // remember if we are in css mode
   bool useCSS = HTMLEditorRef().IsCSSEnabled();
 
   if (!SelectionRef().RangeCount()) {
     // Nothing to do
     return NS_OK;
   }
+  const RangeBoundary& atStartOfSelection =
+    SelectionRef().GetRangeAt(0)->StartRef();
   nsCOMPtr<nsIContent> selNode =
-    do_QueryInterface(SelectionRef().GetRangeAt(0)->GetStartContainer());
+    atStartOfSelection.Container() &&
+    atStartOfSelection.Container()->IsContent() ?
+      atStartOfSelection.Container()->AsContent() : nullptr;
   if (!selNode) {
     // Nothing to do
     return NS_OK;
   }
 
   StyleCache styleAtInsertionPoint[SIZE_STYLE_TABLE];
   InitStyleCacheArray(styleAtInsertionPoint);
   nsresult rv = GetInlineStyles(selNode, styleAtInsertionPoint);
@@ -8559,25 +8565,31 @@ HTMLEditRules::ReapplyCachedStyles()
       bFirst = bAny = bAll = false;
 
       nsAutoString curValue;
       if (useCSS) {
         // check computed style first in css case
         bAny = CSSEditUtils::IsCSSEquivalentToHTMLInlineStyleSet(
                  selNode, mCachedStyles[i].tag, mCachedStyles[i].attr, curValue,
                  CSSEditUtils::eComputed);
+        if (NS_WARN_IF(!CanHandleEditAction())) {
+          return NS_ERROR_EDITOR_DESTROYED;
+        }
       }
       if (!bAny) {
         // then check typeinstate and html style
         nsresult rv =
           HTMLEditorRef().GetInlinePropertyBase(*mCachedStyles[i].tag,
                                                 mCachedStyles[i].attr,
                                                 &(mCachedStyles[i].value),
                                                 &bFirst, &bAny, &bAll,
                                                 &curValue);
+        if (NS_WARN_IF(!CanHandleEditAction())) {
+          return NS_ERROR_EDITOR_DESTROYED;
+        }
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return rv;
         }
       }
       // This style has disappeared through deletion.  Let's add the styles to
       // mTypeInState when same style isn't applied to the node already.
       if ((!bAny || IsStyleCachePreservingAction(mTheAction)) &&
            (!styleAtInsertionPoint[i].mPresent ||
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -586,17 +586,24 @@ protected:
    * Finally, aListElement is removed. and its all children are moved to
    * where the aListElement was.
    *
    * @param aListElement        A <ul>, <ol> or <dl> element.
    */
   MOZ_MUST_USE nsresult RemoveListStructure(Element& aListElement);
 
   nsresult CacheInlineStyles(nsINode* aNode);
-  nsresult ReapplyCachedStyles();
+
+  /**
+   * ReapplyCachedStyles() restores some styles which are disappeared during
+   * handling edit action and it should be restored.  This may cause flushing
+   * layout at retrieving computed value of CSS properties.
+   */
+  MOZ_MUST_USE nsresult ReapplyCachedStyles();
+
   void ClearCachedStyles();
 
   /**
    * InsertBRElementToEmptyListItemsAndTableCellsInChangedRange() inserts
    * <br> element into empty list item or table cell elements.
    */
   MOZ_MUST_USE nsresult
   InsertBRElementToEmptyListItemsAndTableCellsInChangedRange();