1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "ChangeAttributeTransaction.h"
#include "mozilla/dom/Element.h" // for Element
#include "nsAString.h"
#include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc.
namespace mozilla {
using namespace dom;
// 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();
}
ChangeAttributeTransaction::ChangeAttributeTransaction(Element& aElement,
nsAtom& aAttribute,
const nsAString* aValue)
: EditTransactionBase(),
mElement(&aElement),
mAttribute(&aAttribute),
mValue(aValue ? *aValue : u""_ns),
mRemoveAttribute(!aValue),
mAttributeWasSet(false) {}
NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTransaction,
EditTransactionBase, mElement)
NS_IMPL_ADDREF_INHERITED(ChangeAttributeTransaction, EditTransactionBase)
NS_IMPL_RELEASE_INHERITED(ChangeAttributeTransaction, EditTransactionBase)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ChangeAttributeTransaction)
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
NS_IMETHODIMP ChangeAttributeTransaction::DoTransaction() {
// Need to get the current value of the attribute and save it, and set
// mAttributeWasSet
mAttributeWasSet =
mElement->GetAttr(kNameSpaceID_None, mAttribute, mUndoValue);
// XXX: hack until attribute-was-set code is implemented
if (!mUndoValue.IsEmpty()) {
mAttributeWasSet = true;
}
// XXX: end hack
// Now set the attribute to the new value
if (mRemoveAttribute) {
OwningNonNull<Element> element = *mElement;
nsresult rv = element->UnsetAttr(kNameSpaceID_None, mAttribute, true);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::UnsetAttr() failed");
return rv;
}
OwningNonNull<Element> element = *mElement;
nsresult rv = element->SetAttr(kNameSpaceID_None, mAttribute, mValue, true);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::SetAttr() failed");
return rv;
}
NS_IMETHODIMP ChangeAttributeTransaction::UndoTransaction() {
if (NS_WARN_IF(!mElement)) {
return NS_ERROR_NOT_AVAILABLE;
}
if (mAttributeWasSet) {
OwningNonNull<Element> element = *mElement;
nsresult rv =
element->SetAttr(kNameSpaceID_None, mAttribute, mUndoValue, true);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::SetAttr() failed");
return rv;
}
OwningNonNull<Element> element = *mElement;
nsresult rv = element->UnsetAttr(kNameSpaceID_None, mAttribute, true);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::UnsetAttr() failed");
return rv;
}
NS_IMETHODIMP ChangeAttributeTransaction::RedoTransaction() {
if (NS_WARN_IF(!mElement)) {
return NS_ERROR_NOT_AVAILABLE;
}
if (mRemoveAttribute) {
OwningNonNull<Element> element = *mElement;
nsresult rv = element->UnsetAttr(kNameSpaceID_None, mAttribute, true);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::UnsetAttr() failed");
return rv;
}
OwningNonNull<Element> element = *mElement;
nsresult rv = element->SetAttr(kNameSpaceID_None, mAttribute, mValue, true);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Element::SetAttr() failed");
return rv;
}
} // namespace mozilla
|