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
|
|
|
|
2016-07-07 09:08:33 +03:00
|
|
|
DeleteNodeTransaction::DeleteNodeTransaction()
|
2016-07-08 07:10:13 +03:00
|
|
|
: mEditorBase(nullptr)
|
2016-07-07 09:08:33 +03:00
|
|
|
, mRangeUpdater(nullptr)
|
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,
|
2014-04-25 20:49:00 +04:00
|
|
|
mNode,
|
|
|
|
mParent,
|
|
|
|
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
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
nsresult
|
2016-07-08 07:10:13 +03:00
|
|
|
DeleteNodeTransaction::Init(EditorBase* aEditorBase,
|
2016-07-07 09:08:33 +03:00
|
|
|
nsINode* aNode,
|
2016-06-24 09:01:40 +03:00
|
|
|
RangeUpdater* aRangeUpdater)
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
2016-07-08 07:10:13 +03:00
|
|
|
NS_ENSURE_TRUE(aEditorBase && aNode, NS_ERROR_NULL_POINTER);
|
|
|
|
mEditorBase = aEditorBase;
|
2012-06-19 17:23:36 +04:00
|
|
|
mNode = aNode;
|
2012-10-09 16:31:24 +04:00
|
|
|
mParent = aNode->GetParentNode();
|
2007-06-28 06:48:16 +04:00
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
// do nothing if the node has a parent and it's read-only
|
2016-07-08 07:10:13 +03:00
|
|
|
NS_ENSURE_TRUE(!mParent || mEditorBase->IsModifiableNode(mParent),
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_ERROR_FAILURE);
|
2007-06-28 06:48:16 +04:00
|
|
|
|
2002-11-10 18:11:08 +03:00
|
|
|
mRangeUpdater = aRangeUpdater;
|
1999-02-15 21:25:30 +03:00
|
|
|
return NS_OK;
|
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
|
|
|
{
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_ENSURE_TRUE(mNode, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
if (!mParent) {
|
|
|
|
// this is a no-op, there's no parent to delete mNode from
|
|
|
|
return NS_OK;
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
// remember which child mNode was (by remembering which child was next);
|
|
|
|
// mRefNode can be null
|
|
|
|
mRefNode = mNode->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.
|
|
|
|
if (mRangeUpdater) {
|
|
|
|
mRangeUpdater->SelAdjDeleteNode(mNode->AsDOMNode());
|
|
|
|
}
|
2002-11-10 18:11:08 +03:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
ErrorResult error;
|
|
|
|
mParent->RemoveChild(*mNode, 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
|
|
|
{
|
2012-06-19 17:23:36 +04:00
|
|
|
if (!mParent) {
|
|
|
|
// this is a legal state, the txn is a no-op
|
|
|
|
return NS_OK;
|
2010-01-18 02:11:04 +03:00
|
|
|
}
|
2012-06-19 17:23:36 +04:00
|
|
|
if (!mNode) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-04-04 22:01:35 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
ErrorResult error;
|
2016-10-14 15:33:42 +03:00
|
|
|
nsCOMPtr<nsIContent> refNode = mRefNode;
|
|
|
|
mParent->InsertBefore(*mNode, 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
|
|
|
{
|
2012-06-19 17:23:36 +04:00
|
|
|
if (!mParent) {
|
|
|
|
// this is a legal state, the txn is a no-op
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!mNode) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2010-01-18 02:11:04 +03:00
|
|
|
}
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
if (mRangeUpdater) {
|
|
|
|
mRangeUpdater->SelAdjDeleteNode(mNode->AsDOMNode());
|
|
|
|
}
|
2002-11-10 18:11:08 +03:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
ErrorResult error;
|
|
|
|
mParent->RemoveChild(*mNode, 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::GetTxnDescription(nsAString& aString)
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
2016-07-07 09:08:33 +03:00
|
|
|
aString.AssignLiteral("DeleteNodeTransaction");
|
1999-01-14 21:02:45 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-07-07 09:08:33 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|