Bug 1460509 - part 56: Make HTMLEditRules::AlignBlockContents() 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 14:45:34 +0900
changeset 798774 4abbd928c523da899bd2a47ecb780041de3acb4b
parent 798773 a12b9cff5631dee020fa967a21ccbb1325c4790a
child 798775 4511e5964295d84fad6b73f9085132e2bf5763b6
push id110840
push usermasayuki@d-toybox.com
push dateWed, 23 May 2018 13:41:58 +0000
reviewersm_kato
bugs1460509
milestone62.0a1
Bug 1460509 - part 56: Make HTMLEditRules::AlignBlockContents() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato MozReview-Commit-ID: Jn4m4uTdLtW
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -5947,19 +5947,16 @@ HTMLEditRules::AlignInnerBlocks(nsINode&
       return rv;
     }
   }
 
   return NS_OK;
 }
 
 
-/**
- * AlignBlockContents() aligns contents of a block element.
- */
 nsresult
 HTMLEditRules::AlignBlockContents(nsINode& aNode,
                                   const nsAString& aAlignType)
 {
   MOZ_ASSERT(IsEditorDataAvailable());
 
   nsCOMPtr<nsIContent> firstChild =
     HTMLEditorRef().GetFirstEditableChild(aNode);
@@ -5967,41 +5964,58 @@ HTMLEditRules::AlignBlockContents(nsINod
     // this cell has no content, nothing to align
     return NS_OK;
   }
 
   nsCOMPtr<nsIContent> lastChild = HTMLEditorRef().GetLastEditableChild(aNode);
   if (firstChild == lastChild && firstChild->IsHTMLElement(nsGkAtoms::div)) {
     // the cell already has a div containing all of its content: just
     // act on this div.
-    return HTMLEditorRef().SetAttributeOrEquivalent(firstChild->AsElement(),
-                                                    nsGkAtoms::align,
-                                                    aAlignType, false);
+    nsresult rv =
+      HTMLEditorRef().SetAttributeOrEquivalent(firstChild->AsElement(),
+                                               nsGkAtoms::align,
+                                               aAlignType, false);
+    if (NS_WARN_IF(!CanHandleEditAction())) {
+      return NS_ERROR_EDITOR_DESTROYED;
+    }
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+    return NS_OK;
   }
 
   // else we need to put in a div, set the alignment, and toss in all the
   // children
   EditorRawDOMPoint atStartOfNode(&aNode, 0);
   RefPtr<Element> divElem =
     HTMLEditorRef().CreateNodeWithTransaction(*nsGkAtoms::div, atStartOfNode);
+  if (NS_WARN_IF(!CanHandleEditAction())) {
+    return NS_ERROR_EDITOR_DESTROYED;
+  }
   if (NS_WARN_IF(!divElem)) {
     return NS_ERROR_FAILURE;
   }
   // set up the alignment on the div
   nsresult rv =
     HTMLEditorRef().SetAttributeOrEquivalent(divElem, nsGkAtoms::align,
                                              aAlignType, false);
+  if (NS_WARN_IF(!CanHandleEditAction())) {
+    return NS_ERROR_EDITOR_DESTROYED;
+  }
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
   // tuck the children into the end of the active div
   while (lastChild && (lastChild != divElem)) {
     nsresult rv =
       HTMLEditorRef().MoveNodeWithTransaction(*lastChild,
                                               EditorRawDOMPoint(divElem, 0));
+    if (NS_WARN_IF(!CanHandleEditAction())) {
+      return NS_ERROR_EDITOR_DESTROYED;
+    }
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     lastChild = HTMLEditorRef().GetLastEditableChild(aNode);
   }
   return NS_OK;
 }
 
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -397,17 +397,31 @@ protected:
    * positioned.
    * This method actually changes the element which is computed by
    * WillAbsolutePosition() to absolute positioned.
    * Therefore, this might cause destroying the HTML editor.
    */
   MOZ_MUST_USE nsresult DidAbsolutePosition();
 
   nsresult AlignInnerBlocks(nsINode& aNode, const nsAString& aAlignType);
-  nsresult AlignBlockContents(nsINode& aNode, const nsAString& aAlignType);
+
+  /**
+   * AlignBlockContents() sets align attribute of <div> element which is
+   * only child of aNode to aAlignType.  If aNode has 2 or more children or
+   * does not have a <div> element has only child, inserts a <div> element
+   * into aNode and move all children of aNode into the new <div> element.
+   *
+   * @param aNode               The node whose contents should be aligned
+   *                            to aAlignType.
+   * @param aAlignType          New value of align attribute of <div> which
+   *                            is only child of aNode.
+   */
+  MOZ_MUST_USE nsresult
+  AlignBlockContents(nsINode& aNode, const nsAString& aAlignType);
+
   nsresult AppendInnerFormatNodes(nsTArray<OwningNonNull<nsINode>>& aArray,
                                   nsINode* aNode);
   nsresult GetFormatString(nsINode* aNode, nsAString &outFormat);
   enum class Lists { no, yes };
   enum class Tables { no, yes };
   void GetInnerContent(nsINode& aNode,
                        nsTArray<OwningNonNull<nsINode>>& aOutArrayOfNodes,
                        int32_t* aIndex, Lists aLists = Lists::yes,