2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-01-06 23:30:13 +03:00
|
|
|
|
2016-07-07 08:12:30 +03:00
|
|
|
#include "ChangeAttributeTransaction.h"
|
2014-12-02 08:07:42 +03:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
#include "mozilla/dom/Element.h" // for Element
|
|
|
|
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsAString.h"
|
2016-07-08 08:15:21 +03:00
|
|
|
#include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc.
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2016-07-07 08:12:30 +03:00
|
|
|
namespace mozilla {
|
2014-08-29 15:43:23 +04:00
|
|
|
|
2016-07-07 08:12:30 +03:00
|
|
|
using namespace dom;
|
|
|
|
|
2017-12-18 11:07:52 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<ChangeAttributeTransaction>
|
|
|
|
ChangeAttributeTransaction::Create(Element& aElement,
|
|
|
|
nsAtom& aAttribute,
|
|
|
|
const nsAString& aValue)
|
|
|
|
{
|
|
|
|
RefPtr<ChangeAttributeTransaction> transaction =
|
|
|
|
new ChangeAttributeTransaction(aElement, aAttribute, &aValue);
|
|
|
|
return transaction.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<ChangeAttributeTransaction>
|
|
|
|
ChangeAttributeTransaction::CreateToRemove(Element& aElement,
|
|
|
|
nsAtom& aAttribute)
|
|
|
|
{
|
|
|
|
RefPtr<ChangeAttributeTransaction> transaction =
|
|
|
|
new ChangeAttributeTransaction(aElement, aAttribute, nullptr);
|
|
|
|
return transaction.forget();
|
|
|
|
}
|
|
|
|
|
2016-07-07 08:12:30 +03:00
|
|
|
ChangeAttributeTransaction::ChangeAttributeTransaction(Element& aElement,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom& aAttribute,
|
2016-07-07 08:12:30 +03:00
|
|
|
const nsAString* aValue)
|
2016-07-08 03:48:34 +03:00
|
|
|
: EditTransactionBase()
|
2014-08-29 15:43:23 +04:00
|
|
|
, mElement(&aElement)
|
|
|
|
, mAttribute(&aAttribute)
|
|
|
|
, mValue(aValue ? *aValue : EmptyString())
|
|
|
|
, mRemoveAttribute(!aValue)
|
|
|
|
, mAttributeWasSet(false)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
|
|
|
}
|
1999-01-06 23:30:13 +03:00
|
|
|
|
2016-07-07 08:12:30 +03:00
|
|
|
ChangeAttributeTransaction::~ChangeAttributeTransaction()
|
2014-07-09 01:23:18 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTransaction,
|
|
|
|
EditTransactionBase,
|
2014-04-25 20:49:00 +04:00
|
|
|
mElement)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(ChangeAttributeTransaction, EditTransactionBase)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ChangeAttributeTransaction, EditTransactionBase)
|
2016-07-07 08:12:30 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ChangeAttributeTransaction)
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 08:12:30 +03:00
|
|
|
ChangeAttributeTransaction::DoTransaction()
|
1999-01-06 23:30:13 +03:00
|
|
|
{
|
2014-08-29 15:43:23 +04:00
|
|
|
// Need to get the current value of the attribute and save it, and set
|
|
|
|
// mAttributeWasSet
|
|
|
|
mAttributeWasSet = mElement->GetAttr(kNameSpaceID_None, mAttribute,
|
|
|
|
mUndoValue);
|
1999-01-06 23:30:13 +03:00
|
|
|
|
1999-01-07 04:02:16 +03:00
|
|
|
// XXX: hack until attribute-was-set code is implemented
|
2014-08-29 15:43:23 +04:00
|
|
|
if (!mUndoValue.IsEmpty()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mAttributeWasSet = true;
|
2014-08-29 15:43:23 +04:00
|
|
|
}
|
1999-01-07 04:02:16 +03:00
|
|
|
// XXX: end hack
|
1999-01-06 23:30:13 +03:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
// Now set the attribute to the new value
|
|
|
|
if (mRemoveAttribute) {
|
|
|
|
return mElement->UnsetAttr(kNameSpaceID_None, mAttribute, true);
|
|
|
|
}
|
1999-01-06 23:30:13 +03:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
return mElement->SetAttr(kNameSpaceID_None, mAttribute, mValue, true);
|
1999-01-06 23:30:13 +03:00
|
|
|
}
|
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 08:12:30 +03:00
|
|
|
ChangeAttributeTransaction::UndoTransaction()
|
1999-01-06 23:30:13 +03:00
|
|
|
{
|
2014-08-29 15:43:23 +04:00
|
|
|
if (mAttributeWasSet) {
|
|
|
|
return mElement->SetAttr(kNameSpaceID_None, mAttribute, mUndoValue, true);
|
|
|
|
}
|
|
|
|
return mElement->UnsetAttr(kNameSpaceID_None, mAttribute, true);
|
|
|
|
}
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 08:12:30 +03:00
|
|
|
ChangeAttributeTransaction::RedoTransaction()
|
2014-08-29 15:43:23 +04:00
|
|
|
{
|
|
|
|
if (mRemoveAttribute) {
|
|
|
|
return mElement->UnsetAttr(kNameSpaceID_None, mAttribute, true);
|
|
|
|
}
|
1999-01-07 04:02:16 +03:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
return mElement->SetAttr(kNameSpaceID_None, mAttribute, mValue, true);
|
1999-01-06 23:30:13 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 08:12:30 +03:00
|
|
|
} // namespace mozilla
|