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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/* -*- 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 "EditorDOMAPIWrapper.h"
#include "mozilla/Logging.h"
#include "mozilla/ToString.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(
EditorBase& aEditorBase, Element& aElement, nsAtom& aAttribute,
const nsAString& aValue) {
RefPtr<ChangeAttributeTransaction> transaction =
new ChangeAttributeTransaction(aEditorBase, aElement, aAttribute,
&aValue);
return transaction.forget();
}
// static
already_AddRefed<ChangeAttributeTransaction>
ChangeAttributeTransaction::CreateToRemove(EditorBase& aEditorBase,
Element& aElement,
nsAtom& aAttribute) {
RefPtr<ChangeAttributeTransaction> transaction =
new ChangeAttributeTransaction(aEditorBase, aElement, aAttribute,
nullptr);
return transaction.forget();
}
ChangeAttributeTransaction::ChangeAttributeTransaction(EditorBase& aEditorBase,
Element& aElement,
nsAtom& aAttribute,
const nsAString* aValue)
: EditTransactionBase(),
mEditorBase(&aEditorBase),
mElement(&aElement),
mAttribute(&aAttribute),
mValue(aValue ? *aValue : u""_ns),
mRemoveAttribute(!aValue),
mAttributeWasSet(false) {}
std::ostream& operator<<(std::ostream& aStream,
const ChangeAttributeTransaction& aTransaction) {
aStream << "{ mElement=" << aTransaction.mElement.get();
if (aTransaction.mElement) {
aStream << " (" << *aTransaction.mElement << ")";
}
aStream << ", mAttribute=" << nsAtomCString(aTransaction.mAttribute).get()
<< ", mValue=\"" << NS_ConvertUTF16toUTF8(aTransaction.mValue).get()
<< "\", mUndoValue=\""
<< NS_ConvertUTF16toUTF8(aTransaction.mUndoValue).get()
<< "\", mRemoveAttribute="
<< (aTransaction.mRemoveAttribute ? "true" : "false")
<< ", mAttributeWasSet="
<< (aTransaction.mAttributeWasSet ? "true" : "false")
<< ", mEditorBase=" << aTransaction.mEditorBase.get() << " }";
return aStream;
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(ChangeAttributeTransaction,
EditTransactionBase, mEditorBase, 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() {
MOZ_ASSERT(mEditorBase);
// Need to get the current value of the attribute and save it, and set
// mAttributeWasSet
mAttributeWasSet = mElement->GetAttr(mAttribute, mUndoValue);
// XXX: hack until attribute-was-set code is implemented
if (!mUndoValue.IsEmpty()) {
mAttributeWasSet = true;
}
// XXX: end hack
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p ChangeAttributeTransaction::%s this=%s", this, __FUNCTION__,
ToString(*this).c_str()));
const OwningNonNull<EditorBase> editorBase = *mEditorBase;
const OwningNonNull<Element> element = *mElement;
// Now set the attribute to the new value
if (mRemoveAttribute) {
AutoElementAttrAPIWrapper elementWrapper(editorBase, element);
nsresult rv = elementWrapper.UnsetAttr(MOZ_KnownLive(mAttribute), true);
if (NS_FAILED(rv)) {
NS_WARNING("AutoElementAttrAPIWrapper::UnsetAttr() failed");
return rv;
}
NS_WARNING_ASSERTION(
elementWrapper.IsExpectedResult(EmptyString()),
"Removing attribute caused other mutations, but ignored");
return NS_OK;
}
AutoElementAttrAPIWrapper elementWrapper(editorBase, element);
nsresult rv = elementWrapper.SetAttr(MOZ_KnownLive(mAttribute), mValue, true);
if (NS_FAILED(rv)) {
NS_WARNING("AutoElementAttrAPIWrapper::SetAttr() failed");
return rv;
}
NS_WARNING_ASSERTION(elementWrapper.IsExpectedResult(mValue),
"Setting attribute caused other mutations, but ignored");
return rv;
}
NS_IMETHODIMP ChangeAttributeTransaction::UndoTransaction() {
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p ChangeAttributeTransaction::%s this=%s", this, __FUNCTION__,
ToString(*this).c_str()));
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mElement)) {
return NS_ERROR_NOT_AVAILABLE;
}
const OwningNonNull<EditorBase> editorBase = *mEditorBase;
const OwningNonNull<Element> element = *mElement;
if (mAttributeWasSet) {
AutoElementAttrAPIWrapper elementWrapper(editorBase, element);
nsresult rv =
elementWrapper.SetAttr(MOZ_KnownLive(mAttribute), mUndoValue, true);
if (NS_FAILED(rv)) {
NS_WARNING("AutoElementAttrAPIWrapper::SetAttr() failed");
return rv;
}
NS_WARNING_ASSERTION(
elementWrapper.IsExpectedResult(mUndoValue),
"Setting attribute caused other mutations, but ignored");
return NS_OK;
}
AutoElementAttrAPIWrapper elementWrapper(editorBase, element);
nsresult rv = elementWrapper.UnsetAttr(MOZ_KnownLive(mAttribute), true);
if (NS_FAILED(rv)) {
NS_WARNING("AutoElementAttrAPIWrapper::UnsetAttr() failed");
return rv;
}
NS_WARNING_ASSERTION(
elementWrapper.IsExpectedResult(EmptyString()),
"Removing attribute caused other mutations, but ignored");
return NS_OK;
}
NS_IMETHODIMP ChangeAttributeTransaction::RedoTransaction() {
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p ChangeAttributeTransaction::%s this=%s", this, __FUNCTION__,
ToString(*this).c_str()));
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mElement)) {
return NS_ERROR_NOT_AVAILABLE;
}
const OwningNonNull<EditorBase> editorBase = *mEditorBase;
const OwningNonNull<Element> element = *mElement;
if (mRemoveAttribute) {
AutoElementAttrAPIWrapper elementWrapper(editorBase, element);
nsresult rv = elementWrapper.UnsetAttr(MOZ_KnownLive(mAttribute), true);
if (NS_FAILED(rv)) {
NS_WARNING("AutoElementAttrAPIWrapper::UnsetAttr() failed");
return rv;
}
NS_WARNING_ASSERTION(
elementWrapper.IsExpectedResult(EmptyString()),
"Removing attribute caused other mutations, but ignored");
return NS_OK;
}
AutoElementAttrAPIWrapper elementWrapper(editorBase, element);
nsresult rv = elementWrapper.SetAttr(MOZ_KnownLive(mAttribute), mValue, true);
if (NS_FAILED(rv)) {
NS_WARNING("AutoElementAttrAPIWrapper::SetAttr() failed");
return rv;
}
NS_WARNING_ASSERTION(elementWrapper.IsExpectedResult(mValue),
"Setting attribute caused other mutations, but ignored");
return NS_OK;
}
} // namespace mozilla
|