editor/libeditor/base/ChangeAttributeTxn.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/libeditor/base/ChangeAttributeTxn.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,105 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "ChangeAttributeTxn.h"
    1.10 +#include "nsAString.h"
    1.11 +#include "nsDebug.h"                    // for NS_ASSERTION
    1.12 +#include "nsError.h"                    // for NS_ERROR_NOT_INITIALIZED, etc
    1.13 +#include "nsIDOMElement.h"              // for nsIDOMElement
    1.14 +#include "nsIEditor.h"                  // for nsIEditor
    1.15 +#include "nsString.h"                   // for nsString
    1.16 +
    1.17 +ChangeAttributeTxn::ChangeAttributeTxn()
    1.18 +  : EditTxn()
    1.19 +{
    1.20 +}
    1.21 +
    1.22 +NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTxn, EditTxn,
    1.23 +                                   mElement)
    1.24 +
    1.25 +NS_IMPL_ADDREF_INHERITED(ChangeAttributeTxn, EditTxn)
    1.26 +NS_IMPL_RELEASE_INHERITED(ChangeAttributeTxn, EditTxn)
    1.27 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ChangeAttributeTxn)
    1.28 +NS_INTERFACE_MAP_END_INHERITING(EditTxn)
    1.29 +
    1.30 +NS_IMETHODIMP ChangeAttributeTxn::Init(nsIEditor      *aEditor,
    1.31 +                                       nsIDOMElement  *aElement,
    1.32 +                                       const nsAString& aAttribute,
    1.33 +                                       const nsAString& aValue,
    1.34 +                                       bool aRemoveAttribute)
    1.35 +{
    1.36 +  NS_ASSERTION(aEditor && aElement, "bad arg");
    1.37 +  if (!aEditor || !aElement) { return NS_ERROR_NULL_POINTER; }
    1.38 +
    1.39 +  mEditor = aEditor;
    1.40 +  mElement = do_QueryInterface(aElement);
    1.41 +  mAttribute = aAttribute;
    1.42 +  mValue = aValue;
    1.43 +  mRemoveAttribute = aRemoveAttribute;
    1.44 +  mAttributeWasSet=false;
    1.45 +  mUndoValue.Truncate();
    1.46 +  return NS_OK;
    1.47 +}
    1.48 +
    1.49 +NS_IMETHODIMP ChangeAttributeTxn::DoTransaction(void)
    1.50 +{
    1.51 +  NS_ASSERTION(mEditor && mElement, "bad state");
    1.52 +  if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
    1.53 +
    1.54 +  // need to get the current value of the attribute and save it, and set mAttributeWasSet
    1.55 +  nsresult result = mEditor->GetAttributeValue(mElement, mAttribute, mUndoValue, &mAttributeWasSet);
    1.56 +  // XXX: hack until attribute-was-set code is implemented
    1.57 +  if (!mUndoValue.IsEmpty())
    1.58 +    mAttributeWasSet = true;
    1.59 +  // XXX: end hack
    1.60 +  
    1.61 +  // now set the attribute to the new value
    1.62 +  if (!mRemoveAttribute)
    1.63 +    result = mElement->SetAttribute(mAttribute, mValue);
    1.64 +  else
    1.65 +    result = mElement->RemoveAttribute(mAttribute);
    1.66 +
    1.67 +  return result;
    1.68 +}
    1.69 +
    1.70 +NS_IMETHODIMP ChangeAttributeTxn::UndoTransaction(void)
    1.71 +{
    1.72 +  NS_ASSERTION(mEditor && mElement, "bad state");
    1.73 +  if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
    1.74 +
    1.75 +  nsresult result;
    1.76 +  if (mAttributeWasSet)
    1.77 +    result = mElement->SetAttribute(mAttribute, mUndoValue);
    1.78 +  else
    1.79 +    result = mElement->RemoveAttribute(mAttribute);
    1.80 +
    1.81 +  return result;
    1.82 +}
    1.83 +
    1.84 +NS_IMETHODIMP ChangeAttributeTxn::RedoTransaction(void)
    1.85 +{
    1.86 +  NS_ASSERTION(mEditor && mElement, "bad state");
    1.87 +  if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
    1.88 +
    1.89 +  nsresult result;
    1.90 +  if (!mRemoveAttribute)
    1.91 +    result = mElement->SetAttribute(mAttribute, mValue);
    1.92 +  else
    1.93 +    result = mElement->RemoveAttribute(mAttribute);
    1.94 +
    1.95 +  return result;
    1.96 +}
    1.97 +
    1.98 +NS_IMETHODIMP ChangeAttributeTxn::GetTxnDescription(nsAString& aString)
    1.99 +{
   1.100 +  aString.AssignLiteral("ChangeAttributeTxn: [mRemoveAttribute == ");
   1.101 +
   1.102 +  if (!mRemoveAttribute)
   1.103 +    aString.AppendLiteral("false] ");
   1.104 +  else
   1.105 +    aString.AppendLiteral("true] ");
   1.106 +  aString += mAttribute;
   1.107 +  return NS_OK;
   1.108 +}

mercurial