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),
|
2020-09-23 18:17:15 +03:00
|
|
|
mValue(aValue ? *aValue : u""_ns),
|
2014-08-29 15:43:23 +04:00
|
|
|
mRemoveAttribute(!aValue),
|
|
|
|
mAttributeWasSet(false) {}
|
1999-01-06 23:30:13 +03:00
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTransaction,
|
|
|
|
EditTransactionBase, 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
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_IMETHODIMP ChangeAttributeTransaction::DoTransaction() {
|
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) {
|
2020-04-03 11:30:37 +03:00
|
|
|
OwningNonNull<Element> element = *mElement;
|
|
|
|
nsresult rv = element->UnsetAttr(kNameSpaceID_None, mAttribute, true);
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::UnsetAttr() failed");
|
|
|
|
return rv;
|
2014-08-29 15:43:23 +04:00
|
|
|
}
|
1999-01-06 23:30:13 +03:00
|
|
|
|
2020-04-03 11:30:37 +03:00
|
|
|
OwningNonNull<Element> element = *mElement;
|
|
|
|
nsresult rv = element->SetAttr(kNameSpaceID_None, mAttribute, mValue, true);
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::SetAttr() failed");
|
|
|
|
return rv;
|
1999-01-06 23:30:13 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_IMETHODIMP ChangeAttributeTransaction::UndoTransaction() {
|
2020-04-03 11:30:37 +03:00
|
|
|
if (NS_WARN_IF(!mElement)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2014-08-29 15:43:23 +04:00
|
|
|
if (mAttributeWasSet) {
|
2020-04-03 11:30:37 +03:00
|
|
|
OwningNonNull<Element> element = *mElement;
|
2020-03-09 17:57:23 +03:00
|
|
|
nsresult rv =
|
2020-04-03 11:30:37 +03:00
|
|
|
element->SetAttr(kNameSpaceID_None, mAttribute, mUndoValue, true);
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::SetAttr() failed");
|
|
|
|
return rv;
|
2014-08-29 15:43:23 +04:00
|
|
|
}
|
2020-04-03 11:30:37 +03:00
|
|
|
OwningNonNull<Element> element = *mElement;
|
|
|
|
nsresult rv = element->UnsetAttr(kNameSpaceID_None, mAttribute, true);
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::UnsetAttr() failed");
|
|
|
|
return rv;
|
2014-08-29 15:43:23 +04:00
|
|
|
}
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_IMETHODIMP ChangeAttributeTransaction::RedoTransaction() {
|
2020-04-03 11:30:37 +03:00
|
|
|
if (NS_WARN_IF(!mElement)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2014-08-29 15:43:23 +04:00
|
|
|
if (mRemoveAttribute) {
|
2020-04-03 11:30:37 +03:00
|
|
|
OwningNonNull<Element> element = *mElement;
|
|
|
|
nsresult rv = element->UnsetAttr(kNameSpaceID_None, mAttribute, true);
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::UnsetAttr() failed");
|
|
|
|
return rv;
|
2014-08-29 15:43:23 +04:00
|
|
|
}
|
1999-01-07 04:02:16 +03:00
|
|
|
|
2020-04-03 11:30:37 +03:00
|
|
|
OwningNonNull<Element> element = *mElement;
|
|
|
|
nsresult rv = element->SetAttr(kNameSpaceID_None, mAttribute, mValue, true);
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::SetAttr() failed");
|
|
|
|
return rv;
|
1999-01-06 23:30:13 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 08:12:30 +03:00
|
|
|
} // namespace mozilla
|