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 ChangeAttributeTransaction_h
7 #define ChangeAttributeTransaction_h
8 
9 #include "mozilla/Attributes.h"           // override
10 #include "mozilla/EditTransactionBase.h"  // base class
11 #include "nsCOMPtr.h"                     // nsCOMPtr members
12 #include "nsCycleCollectionParticipant.h"  // NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED
13 #include "nsISupportsImpl.h"               // NS_DECL_ISUPPORTS_INHERITED
14 #include "nsString.h"                      // nsString members
15 
16 class nsAtom;
17 
18 namespace mozilla {
19 
20 namespace dom {
21 class Element;
22 }  // namespace dom
23 
24 /**
25  * A transaction that changes an attribute of a content node.  This transaction
26  * covers add, remove, and change attribute.
27  */
28 class ChangeAttributeTransaction final : public EditTransactionBase {
29  protected:
30   ChangeAttributeTransaction(dom::Element& aElement, nsAtom& aAttribute,
31                              const nsAString* aValue);
32 
33  public:
34   /**
35    * Creates a change attribute transaction to set an attribute to something.
36    * This method never returns nullptr.
37    *
38    * @param aElement    The element whose attribute will be changed.
39    * @param aAttribute  The name of the attribute to change.
40    * @param aValue      The new value for aAttribute.
41    */
42   static already_AddRefed<ChangeAttributeTransaction> Create(
43       dom::Element& aElement, nsAtom& aAttribute, const nsAString& aValue);
44 
45   /**
46    * Creates a change attribute transaction to remove an attribute.  This
47    * method never returns nullptr.
48    *
49    * @param aElement    The element whose attribute will be changed.
50    * @param aAttribute  The name of the attribute to remove.
51    */
52   static already_AddRefed<ChangeAttributeTransaction> CreateToRemove(
53       dom::Element& aElement, nsAtom& aAttribute);
54 
55   NS_DECL_ISUPPORTS_INHERITED
56   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ChangeAttributeTransaction,
57                                            EditTransactionBase)
58 
59   NS_DECL_EDITTRANSACTIONBASE
60   NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(ChangeAttributeTransaction)
61 
62   MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
63 
64   friend std::ostream& operator<<(
65       std::ostream& aStream, const ChangeAttributeTransaction& aTransaction);
66 
67  private:
68   virtual ~ChangeAttributeTransaction() = default;
69 
70   // The element to operate upon
71   nsCOMPtr<dom::Element> mElement;
72 
73   // The attribute to change
74   RefPtr<nsAtom> mAttribute;
75 
76   // The value to set the attribute to (ignored if mRemoveAttribute==true)
77   nsString mValue;
78 
79   // The value to set the attribute to for undo
80   nsString mUndoValue;
81 
82   // True if the operation is to remove mAttribute from mElement
83   bool mRemoveAttribute;
84 
85   // True if the mAttribute was set on mElement at the time of execution
86   bool mAttributeWasSet;
87 };
88 
89 }  // namespace mozilla
90 
91 #endif  // #ifndef ChangeAttributeTransaction_h
92