tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ChangeStyleTransaction.h (4915B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef ChangeStyleTransaction_h
      7 #define ChangeStyleTransaction_h
      8 
      9 #include "EditTransactionBase.h"  // base class
     10 
     11 #include "EditorForwards.h"
     12 
     13 #include "nsCycleCollectionParticipant.h"  // various macros
     14 #include "nsString.h"                      // nsString members
     15 
     16 class nsAtom;
     17 class nsStyledElement;
     18 
     19 namespace mozilla {
     20 
     21 namespace dom {
     22 class Element;
     23 }  // namespace dom
     24 
     25 /**
     26 * A transaction that changes the value of a CSS inline style of a content
     27 * node.  This transaction covers add, remove, and change a property's value.
     28 */
     29 class ChangeStyleTransaction final : public EditTransactionBase {
     30 protected:
     31  ChangeStyleTransaction(HTMLEditor& aHTMLEditor,
     32                         nsStyledElement& aStyledElement, nsAtom& aProperty,
     33                         const nsAString& aValue, bool aRemove);
     34 
     35 public:
     36  /**
     37   * Creates a change style transaction.  This never returns nullptr.
     38   *
     39   * @param aStyledElement  The node whose style attribute will be changed.
     40   * @param aProperty       The name of the property to change.
     41   * @param aValue          New value for aProperty.
     42   */
     43  static already_AddRefed<ChangeStyleTransaction> Create(
     44      HTMLEditor& aHTMLEditor, nsStyledElement& aStyledElement,
     45      nsAtom& aProperty, const nsAString& aValue);
     46 
     47  /**
     48   * Creates a change style transaction.  This never returns nullptr.
     49   *
     50   * @param aStyledElement  The node whose style attribute will be changed.
     51   * @param aProperty       The name of the property to change.
     52   * @param aValue          The value to remove from aProperty.
     53   */
     54  static already_AddRefed<ChangeStyleTransaction> CreateToRemove(
     55      HTMLEditor& aHTMLEditor, nsStyledElement& aStyledElement,
     56      nsAtom& aProperty, const nsAString& aValue);
     57 
     58  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ChangeStyleTransaction,
     59                                           EditTransactionBase)
     60 
     61  NS_DECL_ISUPPORTS_INHERITED
     62 
     63  NS_DECL_EDITTRANSACTIONBASE
     64  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(ChangeStyleTransaction)
     65 
     66  MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
     67 
     68  /**
     69   * Returns true if the list of white-space separated values contains aValue
     70   *
     71   * @param aValueList      [IN] a list of white-space separated values
     72   * @param aValue          [IN] the value to look for in the list
     73   * @return                true if the value is in the list of values
     74   */
     75  static bool ValueIncludes(const nsACString& aValueList,
     76                            const nsACString& aValue);
     77 
     78  friend std::ostream& operator<<(std::ostream& aStream,
     79                                  const ChangeStyleTransaction& aTransaction);
     80 
     81 private:
     82  virtual ~ChangeStyleTransaction() = default;
     83 
     84  /**
     85   * Build new text-decoration value to set/remove specific values to/from the
     86   * rule which already has aCurrentValues.
     87   */
     88  void BuildTextDecorationValueToSet(const nsACString& aCurrentValues,
     89                                     const nsACString& aAddingValues,
     90                                     nsACString& aOutValues);
     91  void BuildTextDecorationValueToRemove(const nsACString& aCurrentValues,
     92                                        const nsACString& aRemovingValues,
     93                                        nsACString& aOutValues);
     94 
     95  /**
     96   * Helper method for above methods.
     97   */
     98  void BuildTextDecorationValue(bool aUnderline, bool aOverline,
     99                                bool aLineThrough, nsACString& aOutValues);
    100 
    101  /**
    102   * If the boolean is true and if the value is not the empty string,
    103   * set the property in the transaction to that value; if the value
    104   * is empty, remove the property from element's styles. If the boolean
    105   * is false, just remove the style attribute.
    106   */
    107  MOZ_CAN_RUN_SCRIPT nsresult SetStyle(bool aAttributeWasSet,
    108                                       nsACString& aValue);
    109 
    110  RefPtr<HTMLEditor> mHTMLEditor;
    111 
    112  // The element to operate upon.
    113  RefPtr<nsStyledElement> mStyledElement;
    114 
    115  // The CSS property to change.
    116  RefPtr<nsAtom> mProperty;
    117 
    118  // The value to set the property to (ignored if mRemoveProperty==true).
    119  nsCString mValue;
    120 
    121  // The value to set the property to for undo.
    122  nsCString mUndoValue;
    123  // The value to set the property to for redo.
    124  nsCString mRedoValue;
    125 
    126  // true if the operation is to remove mProperty from mElement.
    127  bool mRemoveProperty;
    128 
    129  // True if the style attribute was present and not empty before DoTransaction.
    130  bool mUndoAttributeWasSet;
    131  // True if the style attribute is present and not empty after DoTransaction.
    132  bool mRedoAttributeWasSet;
    133 };
    134 
    135 }  // namespace mozilla
    136 
    137 #endif  // #ifndef ChangeStyleTransaction_h