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(EditorBase& aEditorBase,
|
|
|
|
nsINode& aNodeToDelete)
|
|
|
|
{
|
|
|
|
RefPtr<DeleteNodeTransaction> transaction =
|
|
|
|
new DeleteNodeTransaction(aEditorBase, aNodeToDelete);
|
|
|
|
if (NS_WARN_IF(!transaction->CanDoIt())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return transaction.forget();
|
|
|
|
}
|
|
|
|
|
2017-03-10 07:23:40 +03:00
|
|
|
DeleteNodeTransaction::DeleteNodeTransaction(EditorBase& aEditorBase,
|
2017-12-15 15:24:33 +03:00
|
|
|
nsINode& aNodeToDelete)
|
2017-03-21 13:00:36 +03:00
|
|
|
: mEditorBase(&aEditorBase)
|
2017-03-10 07:23:40 +03:00
|
|
|
, mNodeToDelete(&aNodeToDelete)
|
|
|
|
, mParentNode(aNodeToDelete.GetParentNode())
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
|
|
|
}
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2016-07-07 09:08:33 +03:00
|
|
|
DeleteNodeTransaction::~DeleteNodeTransaction()
|
2014-07-09 01:23:18 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteNodeTransaction, EditTransactionBase,
|
2017-03-21 13:00:36 +03:00
|
|
|
mEditorBase,
|
2017-03-10 07:23:40 +03:00
|
|
|
mNodeToDelete,
|
|
|
|
mParentNode,
|
2014-04-25 20:49:00 +04:00
|
|
|
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
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
2017-03-21 13:00:36 +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
|
|
|
}
|
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 09:08:33 +03:00
|
|
|
DeleteNodeTransaction::DoTransaction()
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
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
|
|
|
|
2017-03-10 07:23:40 +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.
|
2017-12-15 15:24:33 +03:00
|
|
|
mEditorBase->RangeUpdaterRef().SelAdjDeleteNode(mNodeToDelete);
|
2002-11-10 18:11:08 +03:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
ErrorResult error;
|
2017-03-10 07:23:40 +03:00
|
|
|
mParentNode->RemoveChild(*mNodeToDelete, error);
|
2015-04-27 16:18:51 +03:00
|
|
|
return error.StealNSResult();
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 09:08:33 +03:00
|
|
|
DeleteNodeTransaction::UndoTransaction()
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
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;
|
2016-10-14 15:33:42 +03:00
|
|
|
nsCOMPtr<nsIContent> refNode = mRefNode;
|
2017-03-10 07:23:40 +03:00
|
|
|
mParentNode->InsertBefore(*mNodeToDelete, refNode, error);
|
2015-04-27 16:18:51 +03:00
|
|
|
return error.StealNSResult();
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 09:08:33 +03:00
|
|
|
DeleteNodeTransaction::RedoTransaction()
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
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
|
|
|
|
2017-12-15 15:24:33 +03:00
|
|
|
mEditorBase->RangeUpdaterRef().SelAdjDeleteNode(mNodeToDelete);
|
2002-11-10 18:11:08 +03:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
ErrorResult error;
|
2017-03-10 07:23:40 +03:00
|
|
|
mParentNode->RemoveChild(*mNodeToDelete, error);
|
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
|