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
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
#include "DeleteNodeTxn.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsDebug.h"
|
2012-06-19 17:23:36 +04:00
|
|
|
#include "nsEditor.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsSelectionState.h" // nsRangeUpdater
|
|
|
|
#include "nsAString.h"
|
1999-01-21 22:44:26 +03:00
|
|
|
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
DeleteNodeTxn::DeleteNodeTxn()
|
2012-06-19 17:23:36 +04:00
|
|
|
: EditTxn(), mNode(), mParent(), mRefNode(), mRangeUpdater(nsnull)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
|
|
|
}
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(DeleteNodeTxn)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DeleteNodeTxn, EditTxn)
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mNode)
|
2009-05-09 08:59:25 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mRefNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DeleteNodeTxn, EditTxn)
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mNode)
|
2009-05-09 08:59:25 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mRefNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(DeleteNodeTxn, EditTxn)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(DeleteNodeTxn, EditTxn)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteNodeTxn)
|
2009-05-09 08:59:25 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
nsresult
|
2012-06-19 17:23:36 +04:00
|
|
|
DeleteNodeTxn::Init(nsEditor* aEditor, nsINode* aNode,
|
|
|
|
nsRangeUpdater* aRangeUpdater)
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_ENSURE_TRUE(aEditor && aNode, NS_ERROR_NULL_POINTER);
|
2007-06-28 06:48:16 +04:00
|
|
|
mEditor = aEditor;
|
2012-06-19 17:23:36 +04:00
|
|
|
mNode = aNode;
|
|
|
|
mParent = aNode->GetNodeParent();
|
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
|
|
|
|
NS_ENSURE_TRUE(!mParent || mEditor->IsModifiableNode(mParent),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-19 17:23:36 +04:00
|
|
|
DeleteNodeTxn::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
|
|
|
|
// *before* we do the action, unlike some of the other nsRangeStore update
|
|
|
|
// methods.
|
|
|
|
if (mRangeUpdater) {
|
|
|
|
mRangeUpdater->SelAdjDeleteNode(mNode->AsDOMNode());
|
|
|
|
}
|
2002-11-10 18:11:08 +03:00
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
return mParent->RemoveChild(mNode);
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-19 17:23:36 +04:00
|
|
|
DeleteNodeTxn::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-06-19 17:23:36 +04:00
|
|
|
nsresult res;
|
|
|
|
mParent->InsertBefore(mNode, mRefNode, &res);
|
|
|
|
return res;
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-19 17:23:36 +04:00
|
|
|
DeleteNodeTxn::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-06-19 17:23:36 +04:00
|
|
|
return mParent->RemoveChild(mNode);
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-19 17:23:36 +04:00
|
|
|
DeleteNodeTxn::GetTxnDescription(nsAString& aString)
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
2012-06-19 17:23:36 +04:00
|
|
|
aString.AssignLiteral("DeleteNodeTxn");
|
1999-01-14 21:02:45 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|