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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/* -*- 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 "MoveNodeTransaction.h"
#include "EditorBase.h" // for EditorBase
#include "EditorDOMAPIWrapper.h" // for AutoNodeAPIWrapper
#include "EditorDOMPoint.h" // for EditorDOMPoint
#include "HTMLEditor.h" // for HTMLEditor
#include "HTMLEditUtils.h" // for HTMLEditUtils
#include "mozilla/Likely.h"
#include "mozilla/Logging.h"
#include "mozilla/ToString.h"
#include "nsDebug.h" // for NS_WARNING, etc.
#include "nsError.h" // for NS_ERROR_NULL_POINTER, etc.
#include "nsIContent.h" // for nsIContent
#include "nsString.h" // for nsString
namespace mozilla {
using namespace dom;
/******************************************************************************
* mozilla::MoveNodeTransactionBase
******************************************************************************/
MoveNodeTransactionBase::MoveNodeTransactionBase(
HTMLEditor& aHTMLEditor, nsIContent& aLastContentToMove,
const EditorRawDOMPoint& aPointToInsert)
: mContainer(aPointToInsert.GetContainer()),
mReference(aPointToInsert.GetChild()),
mOldContainer(aLastContentToMove.GetParentNode()),
mOldNextSibling(aLastContentToMove.GetNextSibling()),
mHTMLEditor(&aHTMLEditor) {
MOZ_ASSERT(mContainer);
MOZ_ASSERT(mOldContainer);
MOZ_ASSERT_IF(mReference, mReference->GetParentNode() == mContainer);
MOZ_ASSERT_IF(mOldNextSibling,
mOldNextSibling->GetParentNode() == mOldContainer);
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(MoveNodeTransactionBase, EditTransactionBase,
mHTMLEditor, mContainer, mReference,
mOldContainer, mOldNextSibling)
NS_IMPL_ADDREF_INHERITED(MoveNodeTransactionBase, EditTransactionBase)
NS_IMPL_RELEASE_INHERITED(MoveNodeTransactionBase, EditTransactionBase)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MoveNodeTransactionBase)
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
/******************************************************************************
* mozilla::MoveNodeTransaction
******************************************************************************/
template already_AddRefed<MoveNodeTransaction> MoveNodeTransaction::MaybeCreate(
HTMLEditor& aHTMLEditor, nsIContent& aContentToMove,
const EditorDOMPoint& aPointToInsert);
template already_AddRefed<MoveNodeTransaction> MoveNodeTransaction::MaybeCreate(
HTMLEditor& aHTMLEditor, nsIContent& aContentToMove,
const EditorRawDOMPoint& aPointToInsert);
// static
template <typename PT, typename CT>
already_AddRefed<MoveNodeTransaction> MoveNodeTransaction::MaybeCreate(
HTMLEditor& aHTMLEditor, nsIContent& aContentToMove,
const EditorDOMPointBase<PT, CT>& aPointToInsert) {
if (NS_WARN_IF(!aContentToMove.GetParentNode()) ||
NS_WARN_IF(!aPointToInsert.IsSet())) {
return nullptr;
}
// TODO: We should not allow to move a node to improper container element.
// However, this is currently used to move invalid parent while
// processing the nodes. Therefore, treating the case as error breaks
// a lot.
if (NS_WARN_IF(aContentToMove.IsInComposedDoc() &&
!HTMLEditUtils::IsRemovableNode(aContentToMove)) ||
// The destination should be editable, but it may be in an orphan node or
// sub-tree to reduce number of DOM mutation events. In such case, we're
// okay to move a node into the non-editable content because we can assume
// that the caller will insert it into an editable element.
NS_WARN_IF(aPointToInsert.IsInComposedDoc() &&
!HTMLEditUtils::IsSimplyEditableNode(
*aPointToInsert.GetContainer()))) {
return nullptr;
}
RefPtr<MoveNodeTransaction> transaction =
new MoveNodeTransaction(aHTMLEditor, aContentToMove, aPointToInsert);
return transaction.forget();
}
template <typename PT, typename CT>
MoveNodeTransaction::MoveNodeTransaction(
HTMLEditor& aHTMLEditor, nsIContent& aContentToMove,
const EditorDOMPointBase<PT, CT>& aPointToInsert)
: MoveNodeTransactionBase(aHTMLEditor, aContentToMove,
aPointToInsert.template To<EditorRawDOMPoint>()),
mContentToMove(&aContentToMove) {}
std::ostream& operator<<(std::ostream& aStream,
const MoveNodeTransaction& aTransaction) {
auto DumpNodeDetails = [&](const nsINode* aNode) {
if (aNode) {
if (aNode->IsText()) {
nsAutoString data;
aNode->AsText()->GetData(data);
aStream << " (#text \"" << NS_ConvertUTF16toUTF8(data).get() << "\")";
} else {
aStream << " (" << *aNode << ")";
}
}
};
aStream << "{ mContentToMove=" << aTransaction.mContentToMove.get();
DumpNodeDetails(aTransaction.mContentToMove);
aStream << ", mContainer=" << aTransaction.mContainer.get();
DumpNodeDetails(aTransaction.mContainer);
aStream << ", mReference=" << aTransaction.mReference.get();
DumpNodeDetails(aTransaction.mReference);
aStream << ", mOldContainer=" << aTransaction.mOldContainer.get();
DumpNodeDetails(aTransaction.mOldContainer);
aStream << ", mOldNextSibling=" << aTransaction.mOldNextSibling.get();
DumpNodeDetails(aTransaction.mOldNextSibling);
aStream << ", mHTMLEditor=" << aTransaction.mHTMLEditor.get() << " }";
return aStream;
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(MoveNodeTransaction, MoveNodeTransactionBase,
mContentToMove)
NS_IMPL_ADDREF_INHERITED(MoveNodeTransaction, MoveNodeTransactionBase)
NS_IMPL_RELEASE_INHERITED(MoveNodeTransaction, MoveNodeTransactionBase)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MoveNodeTransaction)
NS_INTERFACE_MAP_END_INHERITING(MoveNodeTransactionBase)
NS_IMETHODIMP MoveNodeTransaction::DoTransaction() {
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p MoveNodeTransaction::%s this=%s", this, __FUNCTION__,
ToString(*this).c_str()));
return DoTransactionInternal();
}
nsresult MoveNodeTransaction::DoTransactionInternal() {
MOZ_DIAGNOSTIC_ASSERT(mHTMLEditor);
MOZ_DIAGNOSTIC_ASSERT(mContentToMove);
MOZ_DIAGNOSTIC_ASSERT(mContainer);
MOZ_DIAGNOSTIC_ASSERT(mOldContainer);
const OwningNonNull<HTMLEditor> htmlEditor = *mHTMLEditor;
const OwningNonNull<nsIContent> contentToMove = *mContentToMove;
const OwningNonNull<nsINode> container = *mContainer;
const nsCOMPtr<nsIContent> newNextSibling = mReference;
if (contentToMove->IsElement()) {
nsresult rv = htmlEditor->MarkElementDirty(
MOZ_KnownLive(*contentToMove->AsElement()));
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
return EditorBase::ToGenericNSResult(rv);
}
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"EditorBase::MarkElementDirty() failed, but ignored");
}
{
AutoMoveNodeSelNotify notifyStoredRanges(
htmlEditor->RangeUpdaterRef(), contentToMove,
newNextSibling ? EditorRawDOMPoint(newNextSibling)
: EditorRawDOMPoint::AtEndOf(*container));
AutoNodeAPIWrapper nodeWrapper(htmlEditor, container);
nsresult rv = nodeWrapper.InsertBefore(contentToMove, newNextSibling);
if (NS_FAILED(rv)) {
NS_WARNING("AutoNodeAPIWrapper::InsertBefore() failed");
return rv;
}
NS_WARNING_ASSERTION(nodeWrapper.IsExpectedResult(),
"Moving node caused other mutations, but ignored");
if (MOZ_LIKELY(contentToMove->GetParentNode() &&
contentToMove->OwnerDoc() == htmlEditor->GetDocument())) {
notifyStoredRanges.DidMoveContent(contentToMove);
}
}
return NS_OK;
}
NS_IMETHODIMP MoveNodeTransaction::UndoTransaction() {
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p MoveNodeTransaction::%s this=%s", this, __FUNCTION__,
ToString(*this).c_str()));
if (NS_WARN_IF(!mHTMLEditor) || NS_WARN_IF(!mContentToMove) ||
NS_WARN_IF(!mOldContainer)) {
// Perhaps, nulled-out by the cycle collector.
return NS_ERROR_FAILURE;
}
// If the original point has been changed, refer mOldNextSibling if it's
// reasonable. Otherwise, use end of the old container.
if (mOldNextSibling && mOldContainer != mOldNextSibling->GetParentNode()) {
// TODO: Check whether the new container is proper one for containing
// mContentToMove. However, there are few testcases so that we
// shouldn't change here without creating a lot of undo tests.
if (mOldNextSibling->GetParentNode() &&
(mOldNextSibling->IsInComposedDoc() ||
!mOldContainer->IsInComposedDoc())) {
mOldContainer = mOldNextSibling->GetParentNode();
} else {
mOldNextSibling = nullptr; // end of mOldContainer
}
}
if (MOZ_UNLIKELY(mOldContainer->IsInComposedDoc() &&
!HTMLEditUtils::IsSimplyEditableNode(*mOldContainer))) {
NS_WARNING(
"MoveNodeTransaction::UndoTransaction() couldn't move the "
"content into the old container due to non-editable one");
return NS_ERROR_FAILURE;
}
if (MOZ_UNLIKELY(mContentToMove->IsInComposedDoc() &&
!HTMLEditUtils::IsRemovableNode(*mContentToMove))) {
// For the consistency with MoveSiblingsTransaction::UndoTransaction(), we
// should return "OK" even if we cannot move the node from non-editable
// node.
return NS_OK;
}
// And store the latest node which should be referred at redoing.
mContainer = mContentToMove->GetParentNode();
mReference = mContentToMove->GetNextSibling();
const OwningNonNull<HTMLEditor> htmlEditor = *mHTMLEditor;
const OwningNonNull<nsINode> oldContainer = *mOldContainer;
const OwningNonNull<nsIContent> contentToMove = *mContentToMove;
const nsCOMPtr<nsIContent> oldNextSibling = mOldNextSibling;
if (contentToMove->IsElement()) {
nsresult rv = htmlEditor->MarkElementDirty(
MOZ_KnownLive(*contentToMove->AsElement()));
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
return EditorBase::ToGenericNSResult(rv);
}
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"EditorBase::MarkElementDirty() failed, but ignored");
}
{
AutoMoveNodeSelNotify notifyStoredRanges(
htmlEditor->RangeUpdaterRef(), contentToMove,
oldNextSibling ? EditorRawDOMPoint(oldNextSibling)
: EditorRawDOMPoint::AtEndOf(*oldContainer));
AutoNodeAPIWrapper nodeWrapper(htmlEditor, oldContainer);
nsresult rv = nodeWrapper.InsertBefore(contentToMove, oldNextSibling);
if (NS_FAILED(rv)) {
NS_WARNING("AutoNodeAPIWrapper::InsertBefore() failed");
return rv;
}
NS_WARNING_ASSERTION(nodeWrapper.IsExpectedResult(),
"Moving node caused other mutations, but ignored");
if (MOZ_LIKELY(contentToMove->GetParentNode() &&
contentToMove->OwnerDoc() == htmlEditor->GetDocument())) {
notifyStoredRanges.DidMoveContent(contentToMove);
}
}
return NS_OK;
}
NS_IMETHODIMP MoveNodeTransaction::RedoTransaction() {
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p MoveNodeTransaction::%s this=%s", this, __FUNCTION__,
ToString(*this).c_str()));
if (NS_WARN_IF(!mHTMLEditor) || NS_WARN_IF(!mContentToMove) ||
NS_WARN_IF(!mContainer)) {
// Perhaps, nulled-out by the cycle collector.
return NS_ERROR_FAILURE;
}
// If the inserting point has been changed, refer mReference if it's
// reasonable. Otherwise, use end of the container.
if (mReference && mContainer != mReference->GetParentNode()) {
// TODO: Check whether the new container is proper one for containing
// mContentToMove. However, there are few testcases so that we
// shouldn't change here without creating a lot of redo tests.
if (mReference->GetParentNode() &&
(mReference->IsInComposedDoc() || !mContainer->IsInComposedDoc())) {
mContainer = mReference->GetParentNode();
} else {
mReference = nullptr; // end of mContainer
}
}
if (MOZ_UNLIKELY(mContainer->IsInComposedDoc() &&
!HTMLEditUtils::IsSimplyEditableNode(*mContainer))) {
NS_WARNING(
"MoveNodeTransaction::RedoTransaction() couldn't move the "
"content into the new container due to non-editable one");
return NS_ERROR_FAILURE;
}
if (NS_WARN_IF(mContentToMove->IsInComposedDoc() &&
!HTMLEditUtils::IsRemovableNode(*mContentToMove))) {
// For the consistency with MoveSiblingsTransaction::RedoTransaction(), we
// should return "OK" even if we cannot move the node from non-editable
// node.
return NS_OK;
}
// And store the latest node which should be back.
mOldContainer = mContentToMove->GetParentNode();
mOldNextSibling = mContentToMove->GetNextSibling();
nsresult rv = DoTransactionInternal();
if (NS_FAILED(rv)) {
NS_WARNING("MoveNodeTransaction::DoTransactionInternal() failed");
return rv;
}
return NS_OK;
}
} // namespace mozilla
|