2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
#include "DeleteNodeTxn.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsDebug.h"
|
2012-06-19 06:23:36 -07:00
|
|
|
#include "nsEditor.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsSelectionState.h" // nsRangeUpdater
|
|
|
|
#include "nsAString.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
DeleteNodeTxn::DeleteNodeTxn()
|
2012-06-19 06:23:36 -07:00
|
|
|
: EditTxn(), mNode(), mParent(), mRefNode(), mRangeUpdater(nsnull)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(DeleteNodeTxn)
|
2009-05-08 21:59:25 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DeleteNodeTxn, EditTxn)
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mNode)
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mRefNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DeleteNodeTxn, EditTxn)
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mNode)
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mRefNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(DeleteNodeTxn, EditTxn)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(DeleteNodeTxn, EditTxn)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteNodeTxn)
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
nsresult
|
2012-06-19 06:23:36 -07:00
|
|
|
DeleteNodeTxn::Init(nsEditor* aEditor, nsINode* aNode,
|
|
|
|
nsRangeUpdater* aRangeUpdater)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_ENSURE_TRUE(aEditor && aNode, NS_ERROR_NULL_POINTER);
|
2007-06-27 19:48:16 -07:00
|
|
|
mEditor = aEditor;
|
2012-06-19 06:23:36 -07:00
|
|
|
mNode = aNode;
|
|
|
|
mParent = aNode->GetNodeParent();
|
2007-06-27 19:48:16 -07:00
|
|
|
|
2012-06-19 06:23:36 -07: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-27 19:48:16 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mRangeUpdater = aRangeUpdater;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_IMETHODIMP
|
2012-06-19 06:23:36 -07:00
|
|
|
DeleteNodeTxn::DoTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-19 06:23:36 -07: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;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
// remember which child mNode was (by remembering which child was next);
|
|
|
|
// mRefNode can be null
|
|
|
|
mRefNode = mNode->GetNextSibling();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07: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());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
return mParent->RemoveChild(mNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_IMETHODIMP
|
2012-06-19 06:23:36 -07:00
|
|
|
DeleteNodeTxn::UndoTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-19 06:23:36 -07:00
|
|
|
if (!mParent) {
|
|
|
|
// this is a legal state, the txn is a no-op
|
|
|
|
return NS_OK;
|
2010-01-17 15:11:04 -08:00
|
|
|
}
|
2012-06-19 06:23:36 -07:00
|
|
|
if (!mNode) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
nsresult res;
|
|
|
|
mParent->InsertBefore(mNode, mRefNode, &res);
|
|
|
|
return res;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_IMETHODIMP
|
2012-06-19 06:23:36 -07:00
|
|
|
DeleteNodeTxn::RedoTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-19 06:23:36 -07: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-17 15:11:04 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
if (mRangeUpdater) {
|
|
|
|
mRangeUpdater->SelAdjDeleteNode(mNode->AsDOMNode());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
return mParent->RemoveChild(mNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
NS_IMETHODIMP
|
2012-06-19 06:23:36 -07:00
|
|
|
DeleteNodeTxn::GetTxnDescription(nsAString& aString)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-19 06:23:36 -07:00
|
|
|
aString.AssignLiteral("DeleteNodeTxn");
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|