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 #include "ChangeAttributeTransaction.h"
7 
8 #include "mozilla/dom/Element.h"        // for Element
9 
10 #include "nsAString.h"
11 #include "nsError.h"                    // for NS_ERROR_NOT_INITIALIZED, etc.
12 
13 namespace mozilla {
14 
15 using namespace dom;
16 
ChangeAttributeTransaction(Element & aElement,nsIAtom & aAttribute,const nsAString * aValue)17 ChangeAttributeTransaction::ChangeAttributeTransaction(Element& aElement,
18                                                        nsIAtom& aAttribute,
19                                                        const nsAString* aValue)
20   : EditTransactionBase()
21   , mElement(&aElement)
22   , mAttribute(&aAttribute)
23   , mValue(aValue ? *aValue : EmptyString())
24   , mRemoveAttribute(!aValue)
25   , mAttributeWasSet(false)
26   , mUndoValue()
27 {
28 }
29 
~ChangeAttributeTransaction()30 ChangeAttributeTransaction::~ChangeAttributeTransaction()
31 {
32 }
33 
NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTransaction,EditTransactionBase,mElement)34 NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTransaction,
35                                    EditTransactionBase,
36                                    mElement)
37 
38 NS_IMPL_ADDREF_INHERITED(ChangeAttributeTransaction, EditTransactionBase)
39 NS_IMPL_RELEASE_INHERITED(ChangeAttributeTransaction, EditTransactionBase)
40 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ChangeAttributeTransaction)
41 NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
42 
43 NS_IMETHODIMP
44 ChangeAttributeTransaction::DoTransaction()
45 {
46   // Need to get the current value of the attribute and save it, and set
47   // mAttributeWasSet
48   mAttributeWasSet = mElement->GetAttr(kNameSpaceID_None, mAttribute,
49                                        mUndoValue);
50 
51   // XXX: hack until attribute-was-set code is implemented
52   if (!mUndoValue.IsEmpty()) {
53     mAttributeWasSet = true;
54   }
55   // XXX: end hack
56 
57   // Now set the attribute to the new value
58   if (mRemoveAttribute) {
59     return mElement->UnsetAttr(kNameSpaceID_None, mAttribute, true);
60   }
61 
62   return mElement->SetAttr(kNameSpaceID_None, mAttribute, mValue, true);
63 }
64 
65 NS_IMETHODIMP
UndoTransaction()66 ChangeAttributeTransaction::UndoTransaction()
67 {
68   if (mAttributeWasSet) {
69     return mElement->SetAttr(kNameSpaceID_None, mAttribute, mUndoValue, true);
70   }
71   return mElement->UnsetAttr(kNameSpaceID_None, mAttribute, true);
72 }
73 
74 NS_IMETHODIMP
RedoTransaction()75 ChangeAttributeTransaction::RedoTransaction()
76 {
77   if (mRemoveAttribute) {
78     return mElement->UnsetAttr(kNameSpaceID_None, mAttribute, true);
79   }
80 
81   return mElement->SetAttr(kNameSpaceID_None, mAttribute, mValue, true);
82 }
83 
84 NS_IMETHODIMP
GetTxnDescription(nsAString & aString)85 ChangeAttributeTransaction::GetTxnDescription(nsAString& aString)
86 {
87   aString.AssignLiteral("ChangeAttributeTransaction: [mRemoveAttribute == ");
88 
89   if (mRemoveAttribute) {
90     aString.AppendLiteral("true] ");
91   } else {
92     aString.AppendLiteral("false] ");
93   }
94   aString += nsDependentAtomString(mAttribute);
95   return NS_OK;
96 }
97 
98 } // namespace mozilla
99