2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2016-07-07 09:08:33 +03:00
|
|
|
#include "DeleteNodeTransaction.h"
|
2016-07-08 07:10:13 +03:00
|
|
|
#include "mozilla/EditorBase.h"
|
2016-07-09 05:34:41 +03:00
|
|
|
#include "mozilla/SelectionState.h" // RangeUpdater
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsAString.h"
|
1999-01-21 22:44:26 +03:00
|
|
|
|
2016-07-07 09:08:33 +03:00
|
|
|
namespace mozilla {
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2017-12-15 15:24:33 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<DeleteNodeTransaction> DeleteNodeTransaction::MaybeCreate(
|
2020-04-03 07:57:06 +03:00
|
|
|
EditorBase& aEditorBase, nsINode& aNodeToDelete) {
|
2017-12-15 15:24:33 +03:00
|
|
|
RefPtr<DeleteNodeTransaction> transaction =
|
2020-04-03 07:57:06 +03:00
|
|
|
new DeleteNodeTransaction(aEditorBase, aNodeToDelete);
|
2017-12-15 15:24:33 +03:00
|
|
|
if (NS_WARN_IF(!transaction->CanDoIt())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return transaction.forget();
|
|
|
|
}
|
|
|
|
|
2017-03-10 07:23:40 +03:00
|
|
|
DeleteNodeTransaction::DeleteNodeTransaction(EditorBase& aEditorBase,
|
2020-04-03 07:57:06 +03:00
|
|
|
nsINode& aNodeToDelete)
|
2017-03-21 13:00:36 +03:00
|
|
|
: mEditorBase(&aEditorBase),
|
2020-04-03 07:57:06 +03:00
|
|
|
mNodeToDelete(&aNodeToDelete),
|
|
|
|
mParentNode(aNodeToDelete.GetParentNode()) {}
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteNodeTransaction, EditTransactionBase,
|
2020-04-03 07:57:06 +03:00
|
|
|
mEditorBase, mNodeToDelete, mParentNode,
|
|
|
|
mRefNode)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(DeleteNodeTransaction, EditTransactionBase)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(DeleteNodeTransaction, EditTransactionBase)
|
2016-07-07 09:08:33 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteNodeTransaction)
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2017-03-10 07:23:40 +03:00
|
|
|
bool DeleteNodeTransaction::CanDoIt() const {
|
2020-04-03 07:57:06 +03:00
|
|
|
if (NS_WARN_IF(!mNodeToDelete) || NS_WARN_IF(!mEditorBase) || !mParentNode ||
|
|
|
|
!mEditorBase->IsModifiableNode(*mParentNode)) {
|
2017-03-10 07:23:40 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_IMETHODIMP DeleteNodeTransaction::DoTransaction() {
|
2017-03-10 07:23:40 +03:00
|
|
|
if (NS_WARN_IF(!CanDoIt())) {
|
2012-06-19 17:23:36 +04:00
|
|
|
return NS_OK;
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2020-04-03 07:57:06 +03:00
|
|
|
if (!mEditorBase->AsHTMLEditor() && mNodeToDelete->IsText()) {
|
|
|
|
uint32_t length = mNodeToDelete->AsText()->TextLength();
|
2019-07-22 06:53:36 +03:00
|
|
|
if (length > 0) {
|
|
|
|
mEditorBase->AsTextEditor()->WillDeleteText(length, 0, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 07:57:06 +03:00
|
|
|
// Remember which child mNodeToDelete was (by remembering which child was
|
|
|
|
// next). Note that mRefNode can be nullptr.
|
|
|
|
mRefNode = mNodeToDelete->GetNextSibling();
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
// give range updater a chance. SelAdjDeleteNode() needs to be called
|
2016-06-24 08:44:14 +03:00
|
|
|
// *before* we do the action, unlike some of the other RangeItem update
|
2012-06-19 17:23:36 +04:00
|
|
|
// methods.
|
2020-04-03 07:57:06 +03:00
|
|
|
mEditorBase->RangeUpdaterRef().SelAdjDeleteNode(*mNodeToDelete);
|
2002-11-10 18:11:08 +03:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
ErrorResult error;
|
2020-04-03 07:57:06 +03:00
|
|
|
mParentNode->RemoveChild(*mNodeToDelete, error);
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_WARNING_ASSERTION(!error.Failed(), "nsINode::RemoveChild() failed");
|
2015-04-27 16:18:51 +03:00
|
|
|
return error.StealNSResult();
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHODIMP
|
2016-07-07 09:08:33 +03:00
|
|
|
DeleteNodeTransaction::UndoTransaction() {
|
2017-03-10 07:23:40 +03:00
|
|
|
if (NS_WARN_IF(!CanDoIt())) {
|
|
|
|
// This is a legal state, the transaction is a no-op.
|
2012-06-19 17:23:36 +04:00
|
|
|
return NS_OK;
|
2010-01-18 02:11:04 +03:00
|
|
|
}
|
2012-10-09 16:31:24 +04:00
|
|
|
ErrorResult error;
|
2020-04-03 07:57:06 +03:00
|
|
|
RefPtr<EditorBase> editorBase = mEditorBase;
|
|
|
|
nsCOMPtr<nsINode> parent = mParentNode;
|
|
|
|
nsCOMPtr<nsINode> nodeToDelete = mNodeToDelete;
|
|
|
|
nsCOMPtr<nsIContent> refNode = mRefNode;
|
|
|
|
parent->InsertBefore(*nodeToDelete, refNode, error);
|
2020-03-09 17:57:23 +03:00
|
|
|
if (error.Failed()) {
|
|
|
|
NS_WARNING("nsINode::InsertBefore() failed");
|
2019-07-22 06:53:36 +03:00
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
2020-04-03 07:57:06 +03:00
|
|
|
if (!editorBase->AsHTMLEditor() && nodeToDelete->IsText()) {
|
|
|
|
uint32_t length = nodeToDelete->AsText()->TextLength();
|
2019-07-22 06:53:36 +03:00
|
|
|
if (length > 0) {
|
2020-03-09 17:57:23 +03:00
|
|
|
nsresult rv = MOZ_KnownLive(editorBase->AsTextEditor())
|
|
|
|
->DidInsertText(length, 0, length);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("TextEditor::DidInsertText() failed");
|
|
|
|
return rv;
|
2019-07-22 06:53:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_IMETHODIMP DeleteNodeTransaction::RedoTransaction() {
|
2017-03-10 07:23:40 +03:00
|
|
|
if (NS_WARN_IF(!CanDoIt())) {
|
|
|
|
// This is a legal state, the transaction is a no-op.
|
2012-06-19 17:23:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2020-04-03 07:57:06 +03:00
|
|
|
if (!mEditorBase->AsHTMLEditor() && mNodeToDelete->IsText()) {
|
|
|
|
uint32_t length = mNodeToDelete->AsText()->TextLength();
|
2019-07-22 06:53:36 +03:00
|
|
|
if (length > 0) {
|
|
|
|
mEditorBase->AsTextEditor()->WillDeleteText(length, 0, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 07:57:06 +03:00
|
|
|
mEditorBase->RangeUpdaterRef().SelAdjDeleteNode(*mNodeToDelete);
|
2002-11-10 18:11:08 +03:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
ErrorResult error;
|
2020-04-03 07:57:06 +03:00
|
|
|
mParentNode->RemoveChild(*mNodeToDelete, error);
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_WARNING_ASSERTION(!error.Failed(), "nsINode::RemoveChild() failed");
|
2015-04-27 16:18:51 +03:00
|
|
|
return error.StealNSResult();
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 09:08:33 +03:00
|
|
|
} // namespace mozilla
|