/src/mozilla-central/editor/libeditor/ChangeAttributeTransaction.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
5 | | |
6 | | #include "ChangeAttributeTransaction.h" |
7 | | |
8 | | #include "mozilla/dom/Element.h" // for Element |
9 | | |
10 | | #include "nsAString.h" |
11 | | #include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc. |
12 | | |
13 | | namespace mozilla { |
14 | | |
15 | | using namespace dom; |
16 | | |
17 | | // static |
18 | | already_AddRefed<ChangeAttributeTransaction> |
19 | | ChangeAttributeTransaction::Create(Element& aElement, |
20 | | nsAtom& aAttribute, |
21 | | const nsAString& aValue) |
22 | 0 | { |
23 | 0 | RefPtr<ChangeAttributeTransaction> transaction = |
24 | 0 | new ChangeAttributeTransaction(aElement, aAttribute, &aValue); |
25 | 0 | return transaction.forget(); |
26 | 0 | } |
27 | | |
28 | | // static |
29 | | already_AddRefed<ChangeAttributeTransaction> |
30 | | ChangeAttributeTransaction::CreateToRemove(Element& aElement, |
31 | | nsAtom& aAttribute) |
32 | 0 | { |
33 | 0 | RefPtr<ChangeAttributeTransaction> transaction = |
34 | 0 | new ChangeAttributeTransaction(aElement, aAttribute, nullptr); |
35 | 0 | return transaction.forget(); |
36 | 0 | } |
37 | | |
38 | | ChangeAttributeTransaction::ChangeAttributeTransaction(Element& aElement, |
39 | | nsAtom& aAttribute, |
40 | | const nsAString* aValue) |
41 | | : EditTransactionBase() |
42 | | , mElement(&aElement) |
43 | | , mAttribute(&aAttribute) |
44 | | , mValue(aValue ? *aValue : EmptyString()) |
45 | | , mRemoveAttribute(!aValue) |
46 | | , mAttributeWasSet(false) |
47 | 0 | { |
48 | 0 | } |
49 | | |
50 | | ChangeAttributeTransaction::~ChangeAttributeTransaction() |
51 | 0 | { |
52 | 0 | } |
53 | | |
54 | | NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTransaction, |
55 | | EditTransactionBase, |
56 | | mElement) |
57 | | |
58 | | NS_IMPL_ADDREF_INHERITED(ChangeAttributeTransaction, EditTransactionBase) |
59 | | NS_IMPL_RELEASE_INHERITED(ChangeAttributeTransaction, EditTransactionBase) |
60 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ChangeAttributeTransaction) |
61 | 0 | NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase) |
62 | | |
63 | | NS_IMETHODIMP |
64 | | ChangeAttributeTransaction::DoTransaction() |
65 | 0 | { |
66 | 0 | // Need to get the current value of the attribute and save it, and set |
67 | 0 | // mAttributeWasSet |
68 | 0 | mAttributeWasSet = mElement->GetAttr(kNameSpaceID_None, mAttribute, |
69 | 0 | mUndoValue); |
70 | 0 |
|
71 | 0 | // XXX: hack until attribute-was-set code is implemented |
72 | 0 | if (!mUndoValue.IsEmpty()) { |
73 | 0 | mAttributeWasSet = true; |
74 | 0 | } |
75 | 0 | // XXX: end hack |
76 | 0 |
|
77 | 0 | // Now set the attribute to the new value |
78 | 0 | if (mRemoveAttribute) { |
79 | 0 | return mElement->UnsetAttr(kNameSpaceID_None, mAttribute, true); |
80 | 0 | } |
81 | 0 | |
82 | 0 | return mElement->SetAttr(kNameSpaceID_None, mAttribute, mValue, true); |
83 | 0 | } |
84 | | |
85 | | NS_IMETHODIMP |
86 | | ChangeAttributeTransaction::UndoTransaction() |
87 | 0 | { |
88 | 0 | if (mAttributeWasSet) { |
89 | 0 | return mElement->SetAttr(kNameSpaceID_None, mAttribute, mUndoValue, true); |
90 | 0 | } |
91 | 0 | return mElement->UnsetAttr(kNameSpaceID_None, mAttribute, true); |
92 | 0 | } |
93 | | |
94 | | NS_IMETHODIMP |
95 | | ChangeAttributeTransaction::RedoTransaction() |
96 | 0 | { |
97 | 0 | if (mRemoveAttribute) { |
98 | 0 | return mElement->UnsetAttr(kNameSpaceID_None, mAttribute, true); |
99 | 0 | } |
100 | 0 | |
101 | 0 | return mElement->SetAttr(kNameSpaceID_None, mAttribute, mValue, true); |
102 | 0 | } |
103 | | |
104 | | } // namespace mozilla |