michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ChangeAttributeTxn.h" michael@0: #include "nsAString.h" michael@0: #include "nsDebug.h" // for NS_ASSERTION michael@0: #include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc michael@0: #include "nsIDOMElement.h" // for nsIDOMElement michael@0: #include "nsIEditor.h" // for nsIEditor michael@0: #include "nsString.h" // for nsString michael@0: michael@0: ChangeAttributeTxn::ChangeAttributeTxn() michael@0: : EditTxn() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTxn, EditTxn, michael@0: mElement) michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(ChangeAttributeTxn, EditTxn) michael@0: NS_IMPL_RELEASE_INHERITED(ChangeAttributeTxn, EditTxn) michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ChangeAttributeTxn) michael@0: NS_INTERFACE_MAP_END_INHERITING(EditTxn) michael@0: michael@0: NS_IMETHODIMP ChangeAttributeTxn::Init(nsIEditor *aEditor, michael@0: nsIDOMElement *aElement, michael@0: const nsAString& aAttribute, michael@0: const nsAString& aValue, michael@0: bool aRemoveAttribute) michael@0: { michael@0: NS_ASSERTION(aEditor && aElement, "bad arg"); michael@0: if (!aEditor || !aElement) { return NS_ERROR_NULL_POINTER; } michael@0: michael@0: mEditor = aEditor; michael@0: mElement = do_QueryInterface(aElement); michael@0: mAttribute = aAttribute; michael@0: mValue = aValue; michael@0: mRemoveAttribute = aRemoveAttribute; michael@0: mAttributeWasSet=false; michael@0: mUndoValue.Truncate(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP ChangeAttributeTxn::DoTransaction(void) michael@0: { michael@0: NS_ASSERTION(mEditor && mElement, "bad state"); michael@0: if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; } michael@0: michael@0: // need to get the current value of the attribute and save it, and set mAttributeWasSet michael@0: nsresult result = mEditor->GetAttributeValue(mElement, mAttribute, mUndoValue, &mAttributeWasSet); michael@0: // XXX: hack until attribute-was-set code is implemented michael@0: if (!mUndoValue.IsEmpty()) michael@0: mAttributeWasSet = true; michael@0: // XXX: end hack michael@0: michael@0: // now set the attribute to the new value michael@0: if (!mRemoveAttribute) michael@0: result = mElement->SetAttribute(mAttribute, mValue); michael@0: else michael@0: result = mElement->RemoveAttribute(mAttribute); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: NS_IMETHODIMP ChangeAttributeTxn::UndoTransaction(void) michael@0: { michael@0: NS_ASSERTION(mEditor && mElement, "bad state"); michael@0: if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; } michael@0: michael@0: nsresult result; michael@0: if (mAttributeWasSet) michael@0: result = mElement->SetAttribute(mAttribute, mUndoValue); michael@0: else michael@0: result = mElement->RemoveAttribute(mAttribute); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: NS_IMETHODIMP ChangeAttributeTxn::RedoTransaction(void) michael@0: { michael@0: NS_ASSERTION(mEditor && mElement, "bad state"); michael@0: if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; } michael@0: michael@0: nsresult result; michael@0: if (!mRemoveAttribute) michael@0: result = mElement->SetAttribute(mAttribute, mValue); michael@0: else michael@0: result = mElement->RemoveAttribute(mAttribute); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: NS_IMETHODIMP ChangeAttributeTxn::GetTxnDescription(nsAString& aString) michael@0: { michael@0: aString.AssignLiteral("ChangeAttributeTxn: [mRemoveAttribute == "); michael@0: michael@0: if (!mRemoveAttribute) michael@0: aString.AppendLiteral("false] "); michael@0: else michael@0: aString.AppendLiteral("true] "); michael@0: aString += mAttribute; michael@0: return NS_OK; michael@0: }