editor/libeditor/base/ChangeAttributeTxn.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "ChangeAttributeTxn.h"
michael@0 7 #include "nsAString.h"
michael@0 8 #include "nsDebug.h" // for NS_ASSERTION
michael@0 9 #include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc
michael@0 10 #include "nsIDOMElement.h" // for nsIDOMElement
michael@0 11 #include "nsIEditor.h" // for nsIEditor
michael@0 12 #include "nsString.h" // for nsString
michael@0 13
michael@0 14 ChangeAttributeTxn::ChangeAttributeTxn()
michael@0 15 : EditTxn()
michael@0 16 {
michael@0 17 }
michael@0 18
michael@0 19 NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTxn, EditTxn,
michael@0 20 mElement)
michael@0 21
michael@0 22 NS_IMPL_ADDREF_INHERITED(ChangeAttributeTxn, EditTxn)
michael@0 23 NS_IMPL_RELEASE_INHERITED(ChangeAttributeTxn, EditTxn)
michael@0 24 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ChangeAttributeTxn)
michael@0 25 NS_INTERFACE_MAP_END_INHERITING(EditTxn)
michael@0 26
michael@0 27 NS_IMETHODIMP ChangeAttributeTxn::Init(nsIEditor *aEditor,
michael@0 28 nsIDOMElement *aElement,
michael@0 29 const nsAString& aAttribute,
michael@0 30 const nsAString& aValue,
michael@0 31 bool aRemoveAttribute)
michael@0 32 {
michael@0 33 NS_ASSERTION(aEditor && aElement, "bad arg");
michael@0 34 if (!aEditor || !aElement) { return NS_ERROR_NULL_POINTER; }
michael@0 35
michael@0 36 mEditor = aEditor;
michael@0 37 mElement = do_QueryInterface(aElement);
michael@0 38 mAttribute = aAttribute;
michael@0 39 mValue = aValue;
michael@0 40 mRemoveAttribute = aRemoveAttribute;
michael@0 41 mAttributeWasSet=false;
michael@0 42 mUndoValue.Truncate();
michael@0 43 return NS_OK;
michael@0 44 }
michael@0 45
michael@0 46 NS_IMETHODIMP ChangeAttributeTxn::DoTransaction(void)
michael@0 47 {
michael@0 48 NS_ASSERTION(mEditor && mElement, "bad state");
michael@0 49 if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
michael@0 50
michael@0 51 // need to get the current value of the attribute and save it, and set mAttributeWasSet
michael@0 52 nsresult result = mEditor->GetAttributeValue(mElement, mAttribute, mUndoValue, &mAttributeWasSet);
michael@0 53 // XXX: hack until attribute-was-set code is implemented
michael@0 54 if (!mUndoValue.IsEmpty())
michael@0 55 mAttributeWasSet = true;
michael@0 56 // XXX: end hack
michael@0 57
michael@0 58 // now set the attribute to the new value
michael@0 59 if (!mRemoveAttribute)
michael@0 60 result = mElement->SetAttribute(mAttribute, mValue);
michael@0 61 else
michael@0 62 result = mElement->RemoveAttribute(mAttribute);
michael@0 63
michael@0 64 return result;
michael@0 65 }
michael@0 66
michael@0 67 NS_IMETHODIMP ChangeAttributeTxn::UndoTransaction(void)
michael@0 68 {
michael@0 69 NS_ASSERTION(mEditor && mElement, "bad state");
michael@0 70 if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
michael@0 71
michael@0 72 nsresult result;
michael@0 73 if (mAttributeWasSet)
michael@0 74 result = mElement->SetAttribute(mAttribute, mUndoValue);
michael@0 75 else
michael@0 76 result = mElement->RemoveAttribute(mAttribute);
michael@0 77
michael@0 78 return result;
michael@0 79 }
michael@0 80
michael@0 81 NS_IMETHODIMP ChangeAttributeTxn::RedoTransaction(void)
michael@0 82 {
michael@0 83 NS_ASSERTION(mEditor && mElement, "bad state");
michael@0 84 if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
michael@0 85
michael@0 86 nsresult result;
michael@0 87 if (!mRemoveAttribute)
michael@0 88 result = mElement->SetAttribute(mAttribute, mValue);
michael@0 89 else
michael@0 90 result = mElement->RemoveAttribute(mAttribute);
michael@0 91
michael@0 92 return result;
michael@0 93 }
michael@0 94
michael@0 95 NS_IMETHODIMP ChangeAttributeTxn::GetTxnDescription(nsAString& aString)
michael@0 96 {
michael@0 97 aString.AssignLiteral("ChangeAttributeTxn: [mRemoveAttribute == ");
michael@0 98
michael@0 99 if (!mRemoveAttribute)
michael@0 100 aString.AppendLiteral("false] ");
michael@0 101 else
michael@0 102 aString.AppendLiteral("true] ");
michael@0 103 aString += mAttribute;
michael@0 104 return NS_OK;
michael@0 105 }

mercurial