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.

     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"
     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
    14 ChangeAttributeTxn::ChangeAttributeTxn()
    15   : EditTxn()
    16 {
    17 }
    19 NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTxn, EditTxn,
    20                                    mElement)
    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)
    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; }
    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 }
    46 NS_IMETHODIMP ChangeAttributeTxn::DoTransaction(void)
    47 {
    48   NS_ASSERTION(mEditor && mElement, "bad state");
    49   if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
    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
    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);
    64   return result;
    65 }
    67 NS_IMETHODIMP ChangeAttributeTxn::UndoTransaction(void)
    68 {
    69   NS_ASSERTION(mEditor && mElement, "bad state");
    70   if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
    72   nsresult result;
    73   if (mAttributeWasSet)
    74     result = mElement->SetAttribute(mAttribute, mUndoValue);
    75   else
    76     result = mElement->RemoveAttribute(mAttribute);
    78   return result;
    79 }
    81 NS_IMETHODIMP ChangeAttributeTxn::RedoTransaction(void)
    82 {
    83   NS_ASSERTION(mEditor && mElement, "bad state");
    84   if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
    86   nsresult result;
    87   if (!mRemoveAttribute)
    88     result = mElement->SetAttribute(mAttribute, mValue);
    89   else
    90     result = mElement->RemoveAttribute(mAttribute);
    92   return result;
    93 }
    95 NS_IMETHODIMP ChangeAttributeTxn::GetTxnDescription(nsAString& aString)
    96 {
    97   aString.AssignLiteral("ChangeAttributeTxn: [mRemoveAttribute == ");
    99   if (!mRemoveAttribute)
   100     aString.AppendLiteral("false] ");
   101   else
   102     aString.AppendLiteral("true] ");
   103   aString += mAttribute;
   104   return NS_OK;
   105 }

mercurial