Bug 1574852 - part 99: Move `HTMLEditRules::WillRelativeChangeZIndex()` to `HTMLEditor` r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Thu, 12 Sep 2019 06:20:49 +0000
changeset 492787 dc0f654c9ccdc10edfcbb72fcfddabe3aca66376
parent 492786 11b6d1bbafe71b695c1930985cea4199e6622d71
child 492788 886ccc65f1c996ce61a8568971eac4aa86645d0f
push id95146
push usermasayuki@d-toybox.com
push dateThu, 12 Sep 2019 08:47:04 +0000
treeherderautoland@886ccc65f1c9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1574852
milestone71.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1574852 - part 99: Move `HTMLEditRules::WillRelativeChangeZIndex()` to `HTMLEditor` r=m_kato Differential Revision: https://phabricator.services.mozilla.com/D45293
editor/libeditor/HTMLAbsPositionEditor.cpp
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
editor/libeditor/HTMLEditor.h
editor/libeditor/HTMLEditorCommands.cpp
--- a/editor/libeditor/HTMLAbsPositionEditor.cpp
+++ b/editor/libeditor/HTMLAbsPositionEditor.cpp
@@ -133,40 +133,19 @@ nsresult HTMLEditor::AddZIndexAsAction(i
   MOZ_ASSERT(IsEditActionDataAvailable());
 
   AutoEditActionDataSetter editActionData(
       *this, EditAction::eIncreaseOrDecreaseZIndex, aPrincipal);
   if (NS_WARN_IF(!editActionData.CanHandle())) {
     return NS_ERROR_NOT_INITIALIZED;
   }
 
-  AutoPlaceholderBatch treatAsOneTransaction(*this);
-  AutoEditSubActionNotifier startToHandleEditSubAction(
-      *this,
-      aChange < 0 ? EditSubAction::eDecreaseZIndex
-                  : EditSubAction::eIncreaseZIndex,
-      nsIEditor::eNext);
-
-  // brade: can we get rid of this comment?
-  // Find out if the selection is collapsed:
-  EditSubActionInfo subActionInfo(aChange < 0 ? EditSubAction::eDecreaseZIndex
-                                              : EditSubAction::eIncreaseZIndex);
-  bool cancel, handled;
-  // Protect the edit rules object from dying
-  RefPtr<TextEditRules> rules(mRules);
-  nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled);
-  if (cancel || NS_FAILED(rv)) {
-    return EditorBase::ToGenericNSResult(rv);
-  }
-
-  rv = rules->DidDoAction(subActionInfo, rv);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return EditorBase::ToGenericNSResult(rv);
-  }
-  return NS_OK;
+  EditActionResult result = AddZIndexAsSubAction(aChange);
+  NS_WARNING_ASSERTION(result.Succeeded(), "AddZIndexAsSubAction() failed");
+  return EditorBase::ToGenericNSResult(result.Rv());
 }
 
 int32_t HTMLEditor::GetZIndex(Element& aElement) {
   AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing);
   if (NS_WARN_IF(!editActionData.CanHandle())) {
     return 0;
   }
 
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -793,23 +793,21 @@ nsresult HTMLEditRules::WillDoAction(Edi
     case EditSubAction::eInsertQuotedText: {
       nsresult rv = MOZ_KnownLive(HTMLEditorRef()).WillInsert(aCancel);
       if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
         return NS_ERROR_EDITOR_DESTROYED;
       }
       NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "WillInsert() failed");
       return NS_OK;
     }
-    case EditSubAction::eDecreaseZIndex:
-      return WillRelativeChangeZIndex(-1, aCancel, aHandled);
-    case EditSubAction::eIncreaseZIndex:
-      return WillRelativeChangeZIndex(1, aCancel, aHandled);
     case EditSubAction::eCreateOrChangeDefinitionListItem:
     case EditSubAction::eCreateOrChangeList:
     case EditSubAction::eCreateOrRemoveBlock:
+    case EditSubAction::eDecreaseZIndex:
+    case EditSubAction::eIncreaseZIndex:
     case EditSubAction::eIndent:
     case EditSubAction::eInsertHTMLSource:
     case EditSubAction::eInsertParagraphSeparator:
     case EditSubAction::eOutdent:
     case EditSubAction::eUndo:
     case EditSubAction::eRedo:
     case EditSubAction::eRemoveList:
     case EditSubAction::eSetOrClearAlignment:
@@ -838,16 +836,18 @@ nsresult HTMLEditRules::DidDoAction(Edit
     case EditSubAction::eDeleteSelectedContent:
       return DidDeleteSelection();
     case EditSubAction::eInsertElement:
     case EditSubAction::eInsertQuotedText:
       return NS_OK;
     case EditSubAction::eCreateOrChangeDefinitionListItem:
     case EditSubAction::eCreateOrChangeList:
     case EditSubAction::eCreateOrRemoveBlock:
+    case EditSubAction::eDecreaseZIndex:
+    case EditSubAction::eIncreaseZIndex:
     case EditSubAction::eIndent:
     case EditSubAction::eInsertHTMLSource:
     case EditSubAction::eInsertParagraphSeparator:
     case EditSubAction::eOutdent:
     case EditSubAction::eUndo:
     case EditSubAction::eRedo:
     case EditSubAction::eRemoveList:
     case EditSubAction::eSetOrClearAlignment:
@@ -11237,59 +11237,61 @@ EditActionResult HTMLEditor::SetSelectio
     }
   }
 
   // Restoring Selection might cause destroying the HTML editor.
   return NS_WARN_IF(Destroyed()) ? EditActionHandled(NS_ERROR_EDITOR_DESTROYED)
                                  : EditActionHandled(NS_OK);
 }
 
-nsresult HTMLEditRules::WillRelativeChangeZIndex(int32_t aChange, bool* aCancel,
-                                                 bool* aHandled) {
-  MOZ_ASSERT(IsEditorDataAvailable());
-
-  if (NS_WARN_IF(!aCancel) || NS_WARN_IF(!aHandled)) {
-    return NS_ERROR_INVALID_ARG;
+EditActionResult HTMLEditor::AddZIndexAsSubAction(int32_t aChange) {
+  MOZ_ASSERT(IsEditActionDataAvailable());
+
+  AutoPlaceholderBatch treatAsOneTransaction(*this);
+  AutoEditSubActionNotifier startToHandleEditSubAction(
+      *this,
+      aChange < 0 ? EditSubAction::eDecreaseZIndex
+                  : EditSubAction::eIncreaseZIndex,
+      nsIEditor::eNext);
+
+  EditActionResult result = CanHandleHTMLEditSubAction();
+  if (NS_WARN_IF(result.Failed()) || result.Canceled()) {
+    return result;
   }
 
   // FYI: Ignore cancel result of WillInsert().
-  nsresult rv = MOZ_KnownLive(HTMLEditorRef()).WillInsert();
+  nsresult rv = WillInsert();
   if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
-    return NS_ERROR_EDITOR_DESTROYED;
-  }
-  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "WillInsert() failed");
-
-  *aCancel = false;
-  *aHandled = true;
-
-  RefPtr<Element> element =
-      HTMLEditorRef().GetAbsolutelyPositionedSelectionContainer();
-  if (NS_WARN_IF(!element)) {
-    return NS_ERROR_FAILURE;
+    return EditActionHandled(NS_ERROR_EDITOR_DESTROYED);
+  }
+  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "WillInsert() failed, but ignored");
+
+  RefPtr<Element> absolutelyPositionedElement =
+      GetAbsolutelyPositionedSelectionContainer();
+  if (NS_WARN_IF(!absolutelyPositionedElement)) {
+    return EditActionHandled(NS_ERROR_FAILURE);
   }
 
   {
-    AutoSelectionRestorer restoreSelectionLater(HTMLEditorRef());
+    AutoSelectionRestorer restoreSelectionLater(*this);
 
     int32_t zIndex;
-    nsresult rv = MOZ_KnownLive(HTMLEditorRef())
-                      .RelativeChangeElementZIndex(*element, aChange, &zIndex);
-    if (NS_WARN_IF(!CanHandleEditAction())) {
-      return NS_ERROR_EDITOR_DESTROYED;
+    nsresult rv = RelativeChangeElementZIndex(*absolutelyPositionedElement,
+                                              aChange, &zIndex);
+    if (NS_WARN_IF(Destroyed())) {
+      return EditActionHandled(NS_ERROR_EDITOR_DESTROYED);
     }
     if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
+      return EditActionHandled(rv);
     }
   }
 
   // Restoring Selection might cause destroying the HTML editor.
-  if (NS_WARN_IF(!CanHandleEditAction())) {
-    return NS_ERROR_EDITOR_DESTROYED;
-  }
-  return NS_OK;
+  return NS_WARN_IF(Destroyed()) ? EditActionHandled(NS_ERROR_EDITOR_DESTROYED)
+                                 : EditActionHandled(NS_OK);
 }
 
 nsresult HTMLEditRules::DocumentModified() {
   nsContentUtils::AddScriptRunner(
       NewRunnableMethod("HTMLEditRules::DocumentModifiedWorker", this,
                         &HTMLEditRules::DocumentModifiedWorker));
   // Be aware, if DocumentModifiedWorker() is called synchronously, the
   // editor might have been destroyed here.
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -108,30 +108,16 @@ class HTMLEditRules : public TextEditRul
 
   /**
    * Called after deleting selected content.
    * This method removes unnecessary empty nodes and/or inserts <br> if
    * necessary.
    */
   MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult DidDeleteSelection();
 
-  /**
-   * Called before changing z-index.
-   * This method actually changes z-index of nearest absolute positioned
-   * element relatively.  Therefore, this might cause destroying the HTML
-   * editor.
-   *
-   * @param aChange             Amount to change z-index.
-   * @param aCancel             Returns true if the operation is canceled.
-   * @param aHandled            Returns true if the edit action is handled.
-   */
-  MOZ_CAN_RUN_SCRIPT
-  MOZ_MUST_USE nsresult WillRelativeChangeZIndex(int32_t aChange, bool* aCancel,
-                                                 bool* aHandled);
-
   nsresult AppendInnerFormatNodes(nsTArray<OwningNonNull<nsINode>>& aArray,
                                   nsINode* aNode);
   nsresult GetFormatString(nsINode* aNode, nsAString& outFormat);
 
   MOZ_CAN_RUN_SCRIPT
   nsresult GetParagraphFormatNodes(
       nsTArray<OwningNonNull<nsINode>>& outArrayOfNodes);
 
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -421,18 +421,18 @@ class HTMLEditor final : public TextEdit
   /**
    * adds aChange to the z-index of the currently positioned element.
    *
    * @param aChange [IN] relative change to apply to current z-index
    * @param aPrincipal          Set subject principal if it may be called by
    *                            JS.  If set to nullptr, will be treated as
    *                            called by system.
    */
-  nsresult AddZIndexAsAction(int32_t aChange,
-                             nsIPrincipal* aPrincipal = nullptr);
+  MOZ_CAN_RUN_SCRIPT nsresult
+  AddZIndexAsAction(int32_t aChange, nsIPrincipal* aPrincipal = nullptr);
 
   MOZ_CAN_RUN_SCRIPT nsresult SetBackgroundColorAsAction(
       const nsAString& aColor, nsIPrincipal* aPrincipal = nullptr);
 
   /**
    * SetInlinePropertyAsAction() sets a property which changes inline style of
    * text.  E.g., bold, italic, super and sub.
    * This automatically removes exclusive style, however, treats all changes
@@ -2682,16 +2682,25 @@ class HTMLEditor final : public TextEdit
 
   /**
    * SetSelectionToStaticAsSubAction() sets the `position` property of a
    * selection parent's block whose `position` is `absolute` to `static`.
    */
   MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE EditActionResult
   SetSelectionToStaticAsSubAction();
 
+  /**
+   * AddZIndexAsSubAction() adds aChange to `z-index` of nearest parent
+   * absolute-positioned element from current selection.
+   *
+   * @param aChange     Amount to change `z-index`.
+   */
+  MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE EditActionResult
+  AddZIndexAsSubAction(int32_t aChange);
+
  protected:  // Called by helper classes.
   virtual void OnStartToHandleTopLevelEditSubAction(
       EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override;
   MOZ_CAN_RUN_SCRIPT
   virtual void OnEndHandlingTopLevelEditSubAction() override;
 
  protected:  // Shouldn't be used by friend classes
   virtual ~HTMLEditor();
--- a/editor/libeditor/HTMLEditorCommands.cpp
+++ b/editor/libeditor/HTMLEditorCommands.cpp
@@ -967,17 +967,17 @@ bool DecreaseZIndexCommand::IsCommandEna
 
 nsresult DecreaseZIndexCommand::DoCommand(Command aCommand,
                                           TextEditor& aTextEditor,
                                           nsIPrincipal* aPrincipal) const {
   HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
   if (NS_WARN_IF(!htmlEditor)) {
     return NS_ERROR_FAILURE;
   }
-  nsresult rv = htmlEditor->AddZIndexAsAction(-1, aPrincipal);
+  nsresult rv = MOZ_KnownLive(htmlEditor)->AddZIndexAsAction(-1, aPrincipal);
   NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "AddZIndexAsAction() failed");
   return rv;
 }
 
 nsresult DecreaseZIndexCommand::GetCommandStateParams(
     Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
     nsIEditingSession* aEditingSession) const {
   return aParams.SetBool(STATE_ENABLED,
@@ -1007,17 +1007,17 @@ bool IncreaseZIndexCommand::IsCommandEna
 
 nsresult IncreaseZIndexCommand::DoCommand(Command aCommand,
                                           TextEditor& aTextEditor,
                                           nsIPrincipal* aPrincipal) const {
   HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
   if (NS_WARN_IF(!htmlEditor)) {
     return NS_ERROR_FAILURE;
   }
-  nsresult rv = htmlEditor->AddZIndexAsAction(1, aPrincipal);
+  nsresult rv = MOZ_KnownLive(htmlEditor)->AddZIndexAsAction(1, aPrincipal);
   NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "AddZIndexAsAction() failed");
   return rv;
 }
 
 nsresult IncreaseZIndexCommand::GetCommandStateParams(
     Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
     nsIEditingSession* aEditingSession) const {
   return aParams.SetBool(STATE_ENABLED,