Bug 1735777 [wpt PR 31236] - webrtc wpt: fix invalid unicode indexOf method call...
[gecko.git] / editor / libeditor / ChangeAttributeTransaction.cpp
blob1e57c94267ea617c898094a1f209f48dc75ab16d
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 "ChangeAttributeTransaction.h"
8 #include "mozilla/Logging.h"
9 #include "mozilla/ToString.h"
10 #include "mozilla/dom/Element.h" // for Element
12 #include "nsAString.h"
13 #include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc.
15 namespace mozilla {
17 using namespace dom;
19 // static
20 already_AddRefed<ChangeAttributeTransaction> ChangeAttributeTransaction::Create(
21 Element& aElement, nsAtom& aAttribute, const nsAString& aValue) {
22 RefPtr<ChangeAttributeTransaction> transaction =
23 new ChangeAttributeTransaction(aElement, aAttribute, &aValue);
24 return transaction.forget();
27 // static
28 already_AddRefed<ChangeAttributeTransaction>
29 ChangeAttributeTransaction::CreateToRemove(Element& aElement,
30 nsAtom& aAttribute) {
31 RefPtr<ChangeAttributeTransaction> transaction =
32 new ChangeAttributeTransaction(aElement, aAttribute, nullptr);
33 return transaction.forget();
36 ChangeAttributeTransaction::ChangeAttributeTransaction(Element& aElement,
37 nsAtom& aAttribute,
38 const nsAString* aValue)
39 : EditTransactionBase(),
40 mElement(&aElement),
41 mAttribute(&aAttribute),
42 mValue(aValue ? *aValue : u""_ns),
43 mRemoveAttribute(!aValue),
44 mAttributeWasSet(false) {}
46 std::ostream& operator<<(std::ostream& aStream,
47 const ChangeAttributeTransaction& aTransaction) {
48 aStream << "{ mElement=" << aTransaction.mElement.get();
49 if (aTransaction.mElement) {
50 aStream << " (" << *aTransaction.mElement << ")";
52 aStream << ", mAttribute=" << nsAtomCString(aTransaction.mAttribute).get()
53 << ", mValue=\"" << NS_ConvertUTF16toUTF8(aTransaction.mValue).get()
54 << "\", mUndoValue=\""
55 << NS_ConvertUTF16toUTF8(aTransaction.mUndoValue).get()
56 << "\", mRemoveAttribute="
57 << (aTransaction.mRemoveAttribute ? "true" : "false")
58 << ", mAttributeWasSet="
59 << (aTransaction.mAttributeWasSet ? "true" : "false") << " }";
60 return aStream;
63 NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTransaction,
64 EditTransactionBase, mElement)
66 NS_IMPL_ADDREF_INHERITED(ChangeAttributeTransaction, EditTransactionBase)
67 NS_IMPL_RELEASE_INHERITED(ChangeAttributeTransaction, EditTransactionBase)
68 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ChangeAttributeTransaction)
69 NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
71 NS_IMETHODIMP ChangeAttributeTransaction::DoTransaction() {
72 // Need to get the current value of the attribute and save it, and set
73 // mAttributeWasSet
74 mAttributeWasSet =
75 mElement->GetAttr(kNameSpaceID_None, mAttribute, mUndoValue);
77 // XXX: hack until attribute-was-set code is implemented
78 if (!mUndoValue.IsEmpty()) {
79 mAttributeWasSet = true;
81 // XXX: end hack
83 MOZ_LOG(GetLogModule(), LogLevel::Info,
84 ("%p ChangeAttributeTransaction::%s this=%s", this, __FUNCTION__,
85 ToString(*this).c_str()));
87 // Now set the attribute to the new value
88 if (mRemoveAttribute) {
89 OwningNonNull<Element> element = *mElement;
90 nsresult rv = element->UnsetAttr(kNameSpaceID_None, mAttribute, true);
91 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::UnsetAttr() failed");
92 return rv;
95 OwningNonNull<Element> element = *mElement;
96 nsresult rv = element->SetAttr(kNameSpaceID_None, mAttribute, mValue, true);
97 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::SetAttr() failed");
98 return rv;
101 NS_IMETHODIMP ChangeAttributeTransaction::UndoTransaction() {
102 MOZ_LOG(GetLogModule(), LogLevel::Info,
103 ("%p ChangeAttributeTransaction::%s this=%s", this, __FUNCTION__,
104 ToString(*this).c_str()));
106 if (NS_WARN_IF(!mElement)) {
107 return NS_ERROR_NOT_AVAILABLE;
109 if (mAttributeWasSet) {
110 OwningNonNull<Element> element = *mElement;
111 nsresult rv =
112 element->SetAttr(kNameSpaceID_None, mAttribute, mUndoValue, true);
113 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::SetAttr() failed");
114 return rv;
116 OwningNonNull<Element> element = *mElement;
117 nsresult rv = element->UnsetAttr(kNameSpaceID_None, mAttribute, true);
118 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::UnsetAttr() failed");
119 return rv;
122 NS_IMETHODIMP ChangeAttributeTransaction::RedoTransaction() {
123 MOZ_LOG(GetLogModule(), LogLevel::Info,
124 ("%p ChangeAttributeTransaction::%s this=%s", this, __FUNCTION__,
125 ToString(*this).c_str()));
127 if (NS_WARN_IF(!mElement)) {
128 return NS_ERROR_NOT_AVAILABLE;
130 if (mRemoveAttribute) {
131 OwningNonNull<Element> element = *mElement;
132 nsresult rv = element->UnsetAttr(kNameSpaceID_None, mAttribute, true);
133 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::UnsetAttr() failed");
134 return rv;
137 OwningNonNull<Element> element = *mElement;
138 nsresult rv = element->SetAttr(kNameSpaceID_None, mAttribute, mValue, true);
139 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::SetAttr() failed");
140 return rv;
143 } // namespace mozilla