Bug 1425412 - part 11: Create factory methods for ChangeStyleTransaction and remove CSSEditUtils::CreateCSSPropertyTxn() r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 18 Dec 2017 17:46:57 +0900
changeset 712704 b8622249464775003ed4ecf7d9d2a8958175c1e4
parent 712703 34bda88c0d27cb8f57ff89600493a90edaf0e23a
child 712705 e5ddaa6a7141c92ba254cb45a23e5e1749de7fc9
push id93397
push usermasayuki@d-toybox.com
push dateMon, 18 Dec 2017 16:25:33 +0000
reviewersm_kato
bugs1425412
milestone59.0a1
Bug 1425412 - part 11: Create factory methods for ChangeStyleTransaction and remove CSSEditUtils::CreateCSSPropertyTxn() r?m_kato This patch creates factory methods for ChangeStyleTransaction and removes CSSEditUtils::CreateCSSPropertyTxn(). MozReview-Commit-ID: 1h8ZAj2PP5O
editor/libeditor/CSSEditUtils.cpp
editor/libeditor/CSSEditUtils.h
editor/libeditor/ChangeStyleTransaction.cpp
editor/libeditor/ChangeStyleTransaction.h
--- a/editor/libeditor/CSSEditUtils.cpp
+++ b/editor/libeditor/CSSEditUtils.cpp
@@ -450,22 +450,25 @@ CSSEditUtils::IsCSSEditableProperty(nsIN
 // "aProperty : aValue" to the inline styles carried by aElement
 nsresult
 CSSEditUtils::SetCSSProperty(Element& aElement,
                              nsAtom& aProperty,
                              const nsAString& aValue,
                              bool aSuppressTxn)
 {
   RefPtr<ChangeStyleTransaction> transaction =
-    CreateCSSPropertyTxn(aElement, aProperty, aValue,
-                         ChangeStyleTransaction::eSet);
+    ChangeStyleTransaction::Create(aElement, aProperty, aValue);
   if (aSuppressTxn) {
     return transaction->DoTransaction();
   }
-  return mHTMLEditor->DoTransaction(transaction);
+  if (NS_WARN_IF(!mHTMLEditor)) {
+    return NS_ERROR_NOT_AVAILABLE;
+  }
+  RefPtr<HTMLEditor> htmlEditor(mHTMLEditor);
+  return htmlEditor->DoTransaction(transaction);
 }
 
 nsresult
 CSSEditUtils::SetCSSPropertyPixels(Element& aElement,
                                    nsAtom& aProperty,
                                    int32_t aIntValue)
 {
   nsAutoString s;
@@ -479,34 +482,25 @@ CSSEditUtils::SetCSSPropertyPixels(Eleme
 // the declaration if this property accepts only one value
 nsresult
 CSSEditUtils::RemoveCSSProperty(Element& aElement,
                                 nsAtom& aProperty,
                                 const nsAString& aValue,
                                 bool aSuppressTxn)
 {
   RefPtr<ChangeStyleTransaction> transaction =
-    CreateCSSPropertyTxn(aElement, aProperty, aValue,
-                         ChangeStyleTransaction::eRemove);
+    ChangeStyleTransaction::CreateToRemove(aElement, aProperty, aValue);
   if (aSuppressTxn) {
     return transaction->DoTransaction();
   }
-  return mHTMLEditor->DoTransaction(transaction);
-}
-
-already_AddRefed<ChangeStyleTransaction>
-CSSEditUtils::CreateCSSPropertyTxn(
-                Element& aElement,
-                nsAtom& aAttribute,
-                const nsAString& aValue,
-                ChangeStyleTransaction::EChangeType aChangeType)
-{
-  RefPtr<ChangeStyleTransaction> transaction =
-    new ChangeStyleTransaction(aElement, aAttribute, aValue, aChangeType);
-  return transaction.forget();
+  if (NS_WARN_IF(!mHTMLEditor)) {
+    return NS_ERROR_NOT_AVAILABLE;
+  }
+  RefPtr<HTMLEditor> htmlEditor(mHTMLEditor);
+  return htmlEditor->DoTransaction(transaction);
 }
 
 nsresult
 CSSEditUtils::GetSpecifiedProperty(nsINode& aNode,
                                    nsAtom& aProperty,
                                    nsAString& aValue)
 {
   return GetCSSInlinePropertyBase(&aNode, &aProperty, aValue, eSpecified);
--- a/editor/libeditor/CSSEditUtils.h
+++ b/editor/libeditor/CSSEditUtils.h
@@ -454,30 +454,16 @@ private:
                                             nsAtom* aHTMLProperty,
                                             nsAtom* aAttribute,
                                             const nsAString* aValue,
                                             nsTArray<nsAtom*>& aPropertyArray,
                                             nsTArray<nsString>& aValueArray,
                                             bool aGetOrRemoveRequest);
 
   /**
-   * Creates a Transaction for setting or removing a CSS property.  Never
-   * returns null.
-   *
-   * @param aElement           [IN] A DOM element.
-   * @param aProperty          [IN] A CSS property.
-   * @param aValue             [IN] The value to set for this CSS property.
-   * @param aChangeType        [IN] eSet to set, eRemove to remove.
-   */
-  already_AddRefed<ChangeStyleTransaction>
-  CreateCSSPropertyTxn(dom::Element& aElement,
-                       nsAtom& aProperty, const nsAString& aValue,
-                       ChangeStyleTransaction::EChangeType aChangeType);
-
-  /**
    * Back-end for GetSpecifiedProperty and GetComputedProperty.
    *
    * @param aNode               [IN] A DOM node.
    * @param aProperty           [IN] A CSS property.
    * @param aValue              [OUT] The retrieved value for this property.
    * @param aStyleType          [IN] eSpecified or eComputed.
    */
   nsresult GetCSSInlinePropertyBase(nsINode* aNode, nsAtom* aProperty,
--- a/editor/libeditor/ChangeStyleTransaction.cpp
+++ b/editor/libeditor/ChangeStyleTransaction.cpp
@@ -17,16 +17,54 @@
 #include "nsString.h"                   // for nsAutoString, nsString, etc.
 #include "nsStyledElement.h"            // for nsStyledElement.
 #include "nsUnicharUtils.h"             // for nsCaseInsensitiveStringComparator
 
 namespace mozilla {
 
 using namespace dom;
 
+// static
+already_AddRefed<ChangeStyleTransaction>
+ChangeStyleTransaction::Create(Element& aElement,
+                               nsAtom& aProperty,
+                               const nsAString& aValue)
+{
+  RefPtr<ChangeStyleTransaction> transaction =
+    new ChangeStyleTransaction(aElement, aProperty, aValue, false);
+  return transaction.forget();
+}
+
+// static
+already_AddRefed<ChangeStyleTransaction>
+ChangeStyleTransaction::CreateToRemove(Element& aElement,
+                                       nsAtom& aProperty,
+                                       const nsAString& aValue)
+{
+  RefPtr<ChangeStyleTransaction> transaction =
+    new ChangeStyleTransaction(aElement, aProperty, aValue, true);
+  return transaction.forget();
+}
+
+ChangeStyleTransaction::ChangeStyleTransaction(Element& aElement,
+                                               nsAtom& aProperty,
+                                               const nsAString& aValue,
+                                               bool aRemove)
+  : EditTransactionBase()
+  , mElement(&aElement)
+  , mProperty(&aProperty)
+  , mValue(aValue)
+  , mRemoveProperty(aRemove)
+  , mUndoValue()
+  , mRedoValue()
+  , mUndoAttributeWasSet(false)
+  , mRedoAttributeWasSet(false)
+{
+}
+
 #define kNullCh (char16_t('\0'))
 
 NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeStyleTransaction, EditTransactionBase,
                                    mElement)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ChangeStyleTransaction)
 NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
 
@@ -114,32 +152,16 @@ ChangeStyleTransaction::RemoveValueFromL
       outString.Append(char16_t(' '));
     }
 
     start = ++end;
   }
   aValues.Assign(outString);
 }
 
-ChangeStyleTransaction::ChangeStyleTransaction(Element& aElement,
-                                               nsAtom& aProperty,
-                                               const nsAString& aValue,
-                                               EChangeType aChangeType)
-  : EditTransactionBase()
-  , mElement(&aElement)
-  , mProperty(&aProperty)
-  , mValue(aValue)
-  , mRemoveProperty(aChangeType == eRemove)
-  , mUndoValue()
-  , mRedoValue()
-  , mUndoAttributeWasSet(false)
-  , mRedoAttributeWasSet(false)
-{
-}
-
 NS_IMETHODIMP
 ChangeStyleTransaction::DoTransaction()
 {
   nsCOMPtr<nsStyledElement> inlineStyles = do_QueryInterface(mElement);
   NS_ENSURE_TRUE(inlineStyles, NS_ERROR_NULL_POINTER);
 
   nsCOMPtr<nsICSSDeclaration> cssDecl = inlineStyles->Style();
  
--- a/editor/libeditor/ChangeStyleTransaction.h
+++ b/editor/libeditor/ChangeStyleTransaction.h
@@ -20,39 +20,56 @@ class Element;
 } // namespace dom
 
 /**
  * A transaction that changes the value of a CSS inline style of a content
  * node.  This transaction covers add, remove, and change a property's value.
  */
 class ChangeStyleTransaction final : public EditTransactionBase
 {
+protected:
+  ChangeStyleTransaction(dom::Element& aElement,
+                         nsAtom& aProperty,
+                         const nsAString& aValue,
+                         bool aRemove);
+
 public:
+  /**
+   * Creates a change style transaction.  This never returns nullptr.
+   *
+   * @param aNode       The node whose style attribute will be changed.
+   * @param aProperty   The name of the property to change.
+   * @param aValue      New value for aProperty.
+   */
+  static already_AddRefed<ChangeStyleTransaction>
+  Create(dom::Element& aElement,
+         nsAtom& aProperty,
+         const nsAString& aValue);
+
+  /**
+   * Creates a change style transaction.  This never returns nullptr.
+   *
+   * @param aNode       The node whose style attribute will be changed.
+   * @param aProperty   The name of the property to change.
+   * @param aValue      The value to remove from aProperty.
+   */
+  static already_AddRefed<ChangeStyleTransaction>
+  CreateToRemove(dom::Element& aElement,
+                 nsAtom& aProperty,
+                 const nsAString& aValue);
+
   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ChangeStyleTransaction,
                                            EditTransactionBase)
 
   NS_DECL_ISUPPORTS_INHERITED
 
   NS_DECL_EDITTRANSACTIONBASE
 
   NS_IMETHOD RedoTransaction() override;
 
-  enum EChangeType { eSet, eRemove };
-
-  /**
-   * @param aNode           [IN] the node whose style attribute will be changed
-   * @param aProperty       [IN] the name of the property to change
-   * @param aValue          [IN] new value for aProperty, or value to remove
-   * @param aChangeType     [IN] whether to set or remove
-   */
-  ChangeStyleTransaction(dom::Element& aElement,
-                         nsAtom& aProperty,
-                         const nsAString& aValue,
-                         EChangeType aChangeType);
-
   /**
    * Returns true if the list of white-space separated values contains aValue
    *
    * @param aValueList      [IN] a list of white-space separated values
    * @param aValue          [IN] the value to look for in the list
    * @return                true if the value is in the list of values
    */
   static bool ValueIncludes(const nsAString& aValueList,