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/. */
6 #include "ChangeAttributeTxn.h"
8 #include "mozilla/dom/Element.h" // for Element
10 #include "nsAString.h"
11 #include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc
13 using namespace mozilla
;
14 using namespace mozilla::dom
;
16 ChangeAttributeTxn::ChangeAttributeTxn(Element
& aElement
, nsIAtom
& aAttribute
,
17 const nsAString
* aValue
)
20 , mAttribute(&aAttribute
)
21 , mValue(aValue
? *aValue
: EmptyString())
22 , mRemoveAttribute(!aValue
)
23 , mAttributeWasSet(false)
28 ChangeAttributeTxn::~ChangeAttributeTxn()
32 NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTxn
, EditTxn
,
35 NS_IMPL_ADDREF_INHERITED(ChangeAttributeTxn
, EditTxn
)
36 NS_IMPL_RELEASE_INHERITED(ChangeAttributeTxn
, EditTxn
)
37 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ChangeAttributeTxn
)
38 NS_INTERFACE_MAP_END_INHERITING(EditTxn
)
41 ChangeAttributeTxn::DoTransaction()
43 // Need to get the current value of the attribute and save it, and set
45 mAttributeWasSet
= mElement
->GetAttr(kNameSpaceID_None
, mAttribute
,
48 // XXX: hack until attribute-was-set code is implemented
49 if (!mUndoValue
.IsEmpty()) {
50 mAttributeWasSet
= true;
54 // Now set the attribute to the new value
55 if (mRemoveAttribute
) {
56 return mElement
->UnsetAttr(kNameSpaceID_None
, mAttribute
, true);
59 return mElement
->SetAttr(kNameSpaceID_None
, mAttribute
, mValue
, true);
63 ChangeAttributeTxn::UndoTransaction()
65 if (mAttributeWasSet
) {
66 return mElement
->SetAttr(kNameSpaceID_None
, mAttribute
, mUndoValue
, true);
68 return mElement
->UnsetAttr(kNameSpaceID_None
, mAttribute
, true);
72 ChangeAttributeTxn::RedoTransaction()
74 if (mRemoveAttribute
) {
75 return mElement
->UnsetAttr(kNameSpaceID_None
, mAttribute
, true);
78 return mElement
->SetAttr(kNameSpaceID_None
, mAttribute
, mValue
, true);
82 ChangeAttributeTxn::GetTxnDescription(nsAString
& aString
)
84 aString
.AssignLiteral("ChangeAttributeTxn: [mRemoveAttribute == ");
86 if (mRemoveAttribute
) {
87 aString
.AppendLiteral("true] ");
89 aString
.AppendLiteral("false] ");
91 aString
+= nsDependentAtomString(mAttribute
);