editor/libeditor/base/ChangeAttributeTxn.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:5400a095f28a
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 "ChangeAttributeTxn.h"
7 #include "nsAString.h"
8 #include "nsDebug.h" // for NS_ASSERTION
9 #include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc
10 #include "nsIDOMElement.h" // for nsIDOMElement
11 #include "nsIEditor.h" // for nsIEditor
12 #include "nsString.h" // for nsString
13
14 ChangeAttributeTxn::ChangeAttributeTxn()
15 : EditTxn()
16 {
17 }
18
19 NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTxn, EditTxn,
20 mElement)
21
22 NS_IMPL_ADDREF_INHERITED(ChangeAttributeTxn, EditTxn)
23 NS_IMPL_RELEASE_INHERITED(ChangeAttributeTxn, EditTxn)
24 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ChangeAttributeTxn)
25 NS_INTERFACE_MAP_END_INHERITING(EditTxn)
26
27 NS_IMETHODIMP ChangeAttributeTxn::Init(nsIEditor *aEditor,
28 nsIDOMElement *aElement,
29 const nsAString& aAttribute,
30 const nsAString& aValue,
31 bool aRemoveAttribute)
32 {
33 NS_ASSERTION(aEditor && aElement, "bad arg");
34 if (!aEditor || !aElement) { return NS_ERROR_NULL_POINTER; }
35
36 mEditor = aEditor;
37 mElement = do_QueryInterface(aElement);
38 mAttribute = aAttribute;
39 mValue = aValue;
40 mRemoveAttribute = aRemoveAttribute;
41 mAttributeWasSet=false;
42 mUndoValue.Truncate();
43 return NS_OK;
44 }
45
46 NS_IMETHODIMP ChangeAttributeTxn::DoTransaction(void)
47 {
48 NS_ASSERTION(mEditor && mElement, "bad state");
49 if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
50
51 // need to get the current value of the attribute and save it, and set mAttributeWasSet
52 nsresult result = mEditor->GetAttributeValue(mElement, mAttribute, mUndoValue, &mAttributeWasSet);
53 // XXX: hack until attribute-was-set code is implemented
54 if (!mUndoValue.IsEmpty())
55 mAttributeWasSet = true;
56 // XXX: end hack
57
58 // now set the attribute to the new value
59 if (!mRemoveAttribute)
60 result = mElement->SetAttribute(mAttribute, mValue);
61 else
62 result = mElement->RemoveAttribute(mAttribute);
63
64 return result;
65 }
66
67 NS_IMETHODIMP ChangeAttributeTxn::UndoTransaction(void)
68 {
69 NS_ASSERTION(mEditor && mElement, "bad state");
70 if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
71
72 nsresult result;
73 if (mAttributeWasSet)
74 result = mElement->SetAttribute(mAttribute, mUndoValue);
75 else
76 result = mElement->RemoveAttribute(mAttribute);
77
78 return result;
79 }
80
81 NS_IMETHODIMP ChangeAttributeTxn::RedoTransaction(void)
82 {
83 NS_ASSERTION(mEditor && mElement, "bad state");
84 if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
85
86 nsresult result;
87 if (!mRemoveAttribute)
88 result = mElement->SetAttribute(mAttribute, mValue);
89 else
90 result = mElement->RemoveAttribute(mAttribute);
91
92 return result;
93 }
94
95 NS_IMETHODIMP ChangeAttributeTxn::GetTxnDescription(nsAString& aString)
96 {
97 aString.AssignLiteral("ChangeAttributeTxn: [mRemoveAttribute == ");
98
99 if (!mRemoveAttribute)
100 aString.AppendLiteral("false] ");
101 else
102 aString.AppendLiteral("true] ");
103 aString += mAttribute;
104 return NS_OK;
105 }

mercurial