1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "ChangeAttributeTxn.h"
39 : #include "nsIDOMElement.h"
40 :
41 0 : ChangeAttributeTxn::ChangeAttributeTxn()
42 0 : : EditTxn()
43 : {
44 0 : }
45 :
46 1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(ChangeAttributeTxn)
47 :
48 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(ChangeAttributeTxn, EditTxn)
49 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mElement)
50 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
51 :
52 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ChangeAttributeTxn, EditTxn)
53 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mElement)
54 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
55 :
56 0 : NS_IMPL_ADDREF_INHERITED(ChangeAttributeTxn, EditTxn)
57 0 : NS_IMPL_RELEASE_INHERITED(ChangeAttributeTxn, EditTxn)
58 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ChangeAttributeTxn)
59 0 : NS_INTERFACE_MAP_END_INHERITING(EditTxn)
60 :
61 0 : NS_IMETHODIMP ChangeAttributeTxn::Init(nsIEditor *aEditor,
62 : nsIDOMElement *aElement,
63 : const nsAString& aAttribute,
64 : const nsAString& aValue,
65 : bool aRemoveAttribute)
66 : {
67 0 : NS_ASSERTION(aEditor && aElement, "bad arg");
68 0 : if (!aEditor || !aElement) { return NS_ERROR_NULL_POINTER; }
69 :
70 0 : mEditor = aEditor;
71 0 : mElement = do_QueryInterface(aElement);
72 0 : mAttribute = aAttribute;
73 0 : mValue = aValue;
74 0 : mRemoveAttribute = aRemoveAttribute;
75 0 : mAttributeWasSet=false;
76 0 : mUndoValue.Truncate();
77 0 : return NS_OK;
78 : }
79 :
80 0 : NS_IMETHODIMP ChangeAttributeTxn::DoTransaction(void)
81 : {
82 0 : NS_ASSERTION(mEditor && mElement, "bad state");
83 0 : if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
84 :
85 : // need to get the current value of the attribute and save it, and set mAttributeWasSet
86 0 : nsresult result = mEditor->GetAttributeValue(mElement, mAttribute, mUndoValue, &mAttributeWasSet);
87 : // XXX: hack until attribute-was-set code is implemented
88 0 : if (!mUndoValue.IsEmpty())
89 0 : mAttributeWasSet = true;
90 : // XXX: end hack
91 :
92 : // now set the attribute to the new value
93 0 : if (!mRemoveAttribute)
94 0 : result = mElement->SetAttribute(mAttribute, mValue);
95 : else
96 0 : result = mElement->RemoveAttribute(mAttribute);
97 :
98 0 : return result;
99 : }
100 :
101 0 : NS_IMETHODIMP ChangeAttributeTxn::UndoTransaction(void)
102 : {
103 0 : NS_ASSERTION(mEditor && mElement, "bad state");
104 0 : if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
105 :
106 : nsresult result;
107 0 : if (mAttributeWasSet)
108 0 : result = mElement->SetAttribute(mAttribute, mUndoValue);
109 : else
110 0 : result = mElement->RemoveAttribute(mAttribute);
111 :
112 0 : return result;
113 : }
114 :
115 0 : NS_IMETHODIMP ChangeAttributeTxn::RedoTransaction(void)
116 : {
117 0 : NS_ASSERTION(mEditor && mElement, "bad state");
118 0 : if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
119 :
120 : nsresult result;
121 0 : if (!mRemoveAttribute)
122 0 : result = mElement->SetAttribute(mAttribute, mValue);
123 : else
124 0 : result = mElement->RemoveAttribute(mAttribute);
125 :
126 0 : return result;
127 : }
128 :
129 0 : NS_IMETHODIMP ChangeAttributeTxn::GetTxnDescription(nsAString& aString)
130 : {
131 0 : aString.AssignLiteral("ChangeAttributeTxn: [mRemoveAttribute == ");
132 :
133 0 : if (!mRemoveAttribute)
134 0 : aString.AppendLiteral("false] ");
135 : else
136 0 : aString.AppendLiteral("true] ");
137 0 : aString += mAttribute;
138 0 : return NS_OK;
139 4392 : }
|