Bug 1574852 - part 72: Move `HTMLEditRules::ChangeMarginStart()` to `HTMLEditor` and get rid of its wrappers r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 06 Sep 2019 08:51:36 +0000
changeset 492117 4f6733afb731bc19d519ad7a5746b16bf27696e4
parent 492116 c3a59e9eeba81e7756b6f37217b9c3088cebfb18
child 492118 196ce3ca6e9d0a3adc664da8feb05b418d6f6aa2
push id94751
push usermasayuki@d-toybox.com
push dateFri, 06 Sep 2019 23:05:35 +0000
treeherderautoland@196ce3ca6e9d [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 72: Move `HTMLEditRules::ChangeMarginStart()` to `HTMLEditor` and get rid of its wrappers r=m_kato Differential Revision: https://phabricator.services.mozilla.com/D44465
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
editor/libeditor/HTMLEditor.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -1151,17 +1151,17 @@ nsresult HTMLEditRules::GetAlignment(boo
         return NS_OK;
       }
     }
     isFirstNodeToExamine = false;
   }
   return NS_OK;
 }
 
-static nsAtom& MarginPropertyAtomForIndent(nsINode& aNode) {
+static nsStaticAtom& MarginPropertyAtomForIndent(nsINode& aNode) {
   nsAutoString direction;
   CSSEditUtils::GetComputedProperty(aNode, *nsGkAtoms::direction, direction);
   return direction.EqualsLiteral("rtl") ? *nsGkAtoms::marginRight
                                         : *nsGkAtoms::marginLeft;
 }
 
 nsresult HTMLEditRules::GetParagraphState(bool* aMixed, nsAString& outFormat) {
   if (NS_WARN_IF(!aMixed)) {
@@ -5048,17 +5048,19 @@ nsresult HTMLEditRules::IndentAroundSele
     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
     HTMLEditorRef().TopLevelEditSubActionDataRef().mNewBlockElement = theBlock;
-    nsresult rv = IncreaseMarginToIndent(*theBlock);
+    nsresult rv =
+        MOZ_KnownLive(HTMLEditorRef())
+            .ChangeMarginStart(*theBlock, HTMLEditor::ChangeMargin::Increase);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return NS_ERROR_EDITOR_DESTROYED;
     }
     NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to increase indentation");
     // delete anything that was in the list of nodes
     while (!arrayOfNodes.IsEmpty()) {
       OwningNonNull<nsINode> curNode = arrayOfNodes[0];
       rv = MOZ_KnownLive(HTMLEditorRef()).DeleteNodeWithTransaction(*curNode);
@@ -5190,18 +5192,19 @@ nsresult HTMLEditRules::IndentAroundSele
         return rv;
       }
       continue;
     }
 
     // Not a list item.
 
     if (HTMLEditor::NodeIsBlockStatic(*curNode)) {
-      nsresult rv =
-          IncreaseMarginToIndent(MOZ_KnownLive(*curNode->AsElement()));
+      nsresult rv = MOZ_KnownLive(HTMLEditorRef())
+                        .ChangeMarginStart(MOZ_KnownLive(*curNode->AsElement()),
+                                           HTMLEditor::ChangeMargin::Increase);
       if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
         return NS_ERROR_EDITOR_DESTROYED;
       }
       NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to inrease indentation");
       curQuote = nullptr;
       continue;
     }
 
@@ -5223,17 +5226,19 @@ nsresult HTMLEditRules::IndentAroundSele
                      .CreateNodeWithTransaction(*nsGkAtoms::div,
                                                 splitNodeResult.SplitPoint());
       if (NS_WARN_IF(!CanHandleEditAction())) {
         return NS_ERROR_EDITOR_DESTROYED;
       }
       if (NS_WARN_IF(!curQuote)) {
         return NS_ERROR_FAILURE;
       }
-      nsresult rv = IncreaseMarginToIndent(*curQuote);
+      nsresult rv =
+          MOZ_KnownLive(HTMLEditorRef())
+              .ChangeMarginStart(*curQuote, HTMLEditor::ChangeMargin::Increase);
       if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
         return NS_ERROR_EDITOR_DESTROYED;
       }
       NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to increase indentation");
       // remember our new block for postprocessing
       HTMLEditorRef().TopLevelEditSubActionDataRef().mNewBlockElement =
           curQuote;
       // curQuote is now the correct thing to put curNode in
@@ -5761,28 +5766,30 @@ SplitRangeOffFromNodeResult HTMLEditRule
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return SplitRangeOffFromNodeResult(rv);
       }
       continue;
     }
 
     // Is it a block with a 'margin' property?
     if (useCSS && HTMLEditor::NodeIsBlockStatic(curNode)) {
-      nsAtom& marginProperty = MarginPropertyAtomForIndent(curNode);
+      nsStaticAtom& marginProperty = MarginPropertyAtomForIndent(curNode);
       nsAutoString value;
       CSSEditUtils::GetSpecifiedProperty(curNode, marginProperty, value);
       if (NS_WARN_IF(!CanHandleEditAction())) {
         return SplitRangeOffFromNodeResult(NS_ERROR_EDITOR_DESTROYED);
       }
       float f;
       RefPtr<nsAtom> unit;
       CSSEditUtils::ParseLength(value, &f, getter_AddRefs(unit));
       if (f > 0) {
         nsresult rv =
-            DecreaseMarginToOutdent(MOZ_KnownLive(*curNode->AsElement()));
+            MOZ_KnownLive(HTMLEditorRef())
+                .ChangeMarginStart(MOZ_KnownLive(*curNode->AsElement()),
+                                   HTMLEditor::ChangeMargin::Decrease);
         if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
           return SplitRangeOffFromNodeResult(NS_ERROR_EDITOR_DESTROYED);
         }
         NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                              "Failed to decrease indentation");
         continue;
       }
     }
@@ -5866,17 +5873,17 @@ SplitRangeOffFromNodeResult HTMLEditRule
         lastBQChild = curNode;
         break;
       }
 
       if (!useCSS) {
         continue;
       }
 
-      nsAtom& marginProperty = MarginPropertyAtomForIndent(curNode);
+      nsStaticAtom& marginProperty = MarginPropertyAtomForIndent(curNode);
       nsAutoString value;
       CSSEditUtils::GetSpecifiedProperty(*n, marginProperty, value);
       if (NS_WARN_IF(!CanHandleEditAction())) {
         return SplitRangeOffFromNodeResult(NS_ERROR_EDITOR_DESTROYED);
       }
       float f;
       RefPtr<nsAtom> unit;
       CSSEditUtils::ParseLength(value, &f, getter_AddRefs(unit));
@@ -5966,17 +5973,19 @@ SplitRangeOffFromNodeResult HTMLEditRule
       nsCOMPtr<Element> element;
       if (curNode->GetAsText()) {
         // We want to outdent the parent of text nodes
         element = curNode->GetParentElement();
       } else if (curNode->IsElement()) {
         element = curNode->AsElement();
       }
       if (element) {
-        nsresult rv = DecreaseMarginToOutdent(*element);
+        nsresult rv = MOZ_KnownLive(HTMLEditorRef())
+                          .ChangeMarginStart(
+                              *element, HTMLEditor::ChangeMargin::Decrease);
         if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
           return SplitRangeOffFromNodeResult(NS_ERROR_EDITOR_DESTROYED);
         }
         NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                              "Failed to decrease indentation");
       }
       continue;
     }
@@ -6084,18 +6093,21 @@ SplitRangeOffFromNodeResult HTMLEditRule
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return SplitRangeOffFromNodeResult(rv);
     }
     return SplitRangeOffFromNodeResult(splitResult.GetLeftContent(), nullptr,
                                        splitResult.GetRightContent());
   }
 
   if (splitResult.GetMiddleContentAsElement()) {
-    nsresult rv = DecreaseMarginToOutdent(
-        MOZ_KnownLive(*splitResult.GetMiddleContentAsElement()));
+    nsresult rv =
+        MOZ_KnownLive(HTMLEditorRef())
+            .ChangeMarginStart(
+                MOZ_KnownLive(*splitResult.GetMiddleContentAsElement()),
+                HTMLEditor::ChangeMargin::Decrease);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return SplitRangeOffFromNodeResult(rv);
     }
     return splitResult;
   }
 
   return splitResult;
 }
@@ -10814,35 +10826,35 @@ nsresult HTMLEditRules::AlignBlock(Eleme
     return NS_ERROR_EDITOR_DESTROYED;
   }
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
   return NS_OK;
 }
 
-nsresult HTMLEditRules::ChangeMarginStart(Element& aElement, bool aIncrease) {
-  MOZ_ASSERT(IsEditorDataAvailable());
-
-  nsAtom& marginProperty = MarginPropertyAtomForIndent(aElement);
+nsresult HTMLEditor::ChangeMarginStart(Element& aElement,
+                                       ChangeMargin aChangeMargin) {
+  MOZ_ASSERT(IsEditActionDataAvailable());
+
+  nsStaticAtom& marginProperty = MarginPropertyAtomForIndent(aElement);
   nsAutoString value;
-  CSSEditUtils::GetSpecifiedProperty(aElement, MOZ_KnownLive(marginProperty),
-                                     value);
-  if (NS_WARN_IF(!CanHandleEditAction())) {
+  CSSEditUtils::GetSpecifiedProperty(aElement, marginProperty, value);
+  if (NS_WARN_IF(Destroyed())) {
     return NS_ERROR_EDITOR_DESTROYED;
   }
   float f;
   RefPtr<nsAtom> unit;
   CSSEditUtils::ParseLength(value, &f, getter_AddRefs(unit));
   if (!f) {
     nsAutoString defaultLengthUnit;
     CSSEditUtils::GetDefaultLengthUnit(defaultLengthUnit);
     unit = NS_Atomize(defaultLengthUnit);
   }
-  int8_t multiplier = aIncrease ? +1 : -1;
+  int8_t multiplier = aChangeMargin == ChangeMargin::Increase ? 1 : -1;
   if (nsGkAtoms::in == unit) {
     f += NS_EDITOR_INDENT_INCREMENT_IN * multiplier;
   } else if (nsGkAtoms::cm == unit) {
     f += NS_EDITOR_INDENT_INCREMENT_CM * multiplier;
   } else if (nsGkAtoms::mm == unit) {
     f += NS_EDITOR_INDENT_INCREMENT_MM * multiplier;
   } else if (nsGkAtoms::pt == unit) {
     f += NS_EDITOR_INDENT_INCREMENT_PT * multiplier;
@@ -10857,47 +10869,49 @@ nsresult HTMLEditRules::ChangeMarginStar
   } else if (nsGkAtoms::percentage == unit) {
     f += NS_EDITOR_INDENT_INCREMENT_PERCENT * multiplier;
   }
 
   if (0 < f) {
     nsAutoString newValue;
     newValue.AppendFloat(f);
     newValue.Append(nsDependentAtomString(unit));
-    HTMLEditorRef().mCSSEditUtils->SetCSSProperty(
-        aElement, MOZ_KnownLive(marginProperty), newValue);
-    if (NS_WARN_IF(!CanHandleEditAction())) {
+    mCSSEditUtils->SetCSSProperty(aElement, MOZ_KnownLive(marginProperty),
+                                  newValue);
+    if (NS_WARN_IF(Destroyed())) {
       return NS_ERROR_EDITOR_DESTROYED;
     }
     return NS_OK;
   }
 
-  HTMLEditorRef().mCSSEditUtils->RemoveCSSProperty(
-      aElement, MOZ_KnownLive(marginProperty), value);
-  if (NS_WARN_IF(!CanHandleEditAction())) {
+  mCSSEditUtils->RemoveCSSProperty(aElement, MOZ_KnownLive(marginProperty),
+                                   value);
+  if (NS_WARN_IF(Destroyed())) {
     return NS_ERROR_EDITOR_DESTROYED;
   }
 
   // Remove unnecessary divs
   if (!aElement.IsHTMLElement(nsGkAtoms::div) ||
-      &aElement == HTMLEditorRef().GetActiveEditingHost() ||
-      !HTMLEditorRef().IsDescendantOfEditorRoot(&aElement) ||
       HTMLEditor::HasAttributes(&aElement)) {
     return NS_OK;
   }
-
-  nsresult rv =
-      MOZ_KnownLive(HTMLEditorRef()).RemoveContainerWithTransaction(aElement);
-  if (NS_WARN_IF(!CanHandleEditAction())) {
+  // Don't touch editiong host nor node which is outside of it.
+  Element* editingHost = GetActiveEditingHost();
+  if (&aElement == editingHost ||
+      !aElement.IsInclusiveDescendantOf(editingHost)) {
+    return NS_OK;
+  }
+
+  nsresult rv = RemoveContainerWithTransaction(aElement);
+  if (NS_WARN_IF(Destroyed())) {
     return NS_ERROR_EDITOR_DESTROYED;
   }
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
-  return NS_OK;
+  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
+                       "RemoveContainerWithTransaction() failed");
+  return rv;
 }
 
 nsresult HTMLEditRules::WillAbsolutePosition(bool* aCancel, bool* aHandled) {
   MOZ_ASSERT(IsEditorDataAvailable());
   MOZ_ASSERT(aCancel && aHandled);
 
   // FYI: Ignore cancel result of WillInsert().
   nsresult rv = MOZ_KnownLive(HTMLEditorRef()).WillInsert();
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -491,53 +491,16 @@ class HTMLEditRules : public TextEditRul
    */
   enum class ResetAlignOf { ElementAndDescendants, OnlyDescendants };
   MOZ_CAN_RUN_SCRIPT
   MOZ_MUST_USE nsresult AlignBlock(Element& aElement,
                                    const nsAString& aAlignType,
                                    ResetAlignOf aResetAlignOf);
 
   /**
-   * IncreaseMarginToIndent() increases the margin of aElement.  See the
-   * document of ChangeMarginStart() for the detail.
-   * XXX This is not aware of vertical writing-mode.
-   *
-   * @param aElement            The element to be indented.
-   */
-  MOZ_CAN_RUN_SCRIPT
-  MOZ_MUST_USE nsresult IncreaseMarginToIndent(Element& aElement) {
-    return ChangeMarginStart(aElement, true);
-  }
-
-  /**
-   * DecreaseMarginToOutdent() decreases the margin of aElement.  See the
-   * document of ChangeMarginStart() for the detail.
-   * XXX This is not aware of vertical writing-mode.
-   *
-   * @param aElement            The element to be outdented.
-   */
-  MOZ_CAN_RUN_SCRIPT
-  MOZ_MUST_USE nsresult DecreaseMarginToOutdent(Element& aElement) {
-    return ChangeMarginStart(aElement, false);
-  }
-
-  /**
-   * ChangeMarginStart() changes margin of aElement to indent or outdent.
-   * However, use IncreaseMarginToIndent() and DecreaseMarginToOutdent()
-   * instead.  If it's rtl text, margin-right will be changed.  Otherwise,
-   * margin-left.
-   * XXX This is not aware of vertical writing-mode.
-   *
-   * @param aElement            The element to be indented or outdented.
-   * @param aIncrease           true for indent, false for outdent.
-   */
-  MOZ_CAN_RUN_SCRIPT
-  MOZ_MUST_USE nsresult ChangeMarginStart(Element& aElement, bool aIncrease);
-
-  /**
    * DocumentModifiedWorker() is called by DocumentModified() either
    * synchronously or asynchronously.
    */
   MOZ_CAN_RUN_SCRIPT void DocumentModifiedWorker();
 
  protected:
   HTMLEditor* mHTMLEditor;
   bool mInitialized;
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -2318,16 +2318,25 @@ class HTMLEditor final : public TextEdit
 
   /**
    * RemoveListAtSelectionAsSubAction() removes list elements and list item
    * elements at Selection.  And move contents in them where the removed list
    * was.
    */
   MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult RemoveListAtSelectionAsSubAction();
 
+  /**
+   * ChangeMarginStart() changes margin of aElement to indent or outdent.
+   * If it's rtl text, margin-right will be changed.  Otherwise, margin-left.
+   * XXX This is not aware of vertical writing-mode.
+   */
+  enum class ChangeMargin { Increase, Decrease };
+  MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult
+  ChangeMarginStart(Element& aElement, ChangeMargin aChangeMargin);
+
  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();