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-10-09 05:31:24 -07:00
|
|
|
using namespace mozilla;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
DeleteNodeTxn::DeleteNodeTxn()
|
2012-07-30 07:20:58 -07:00
|
|
|
: EditTxn(), mNode(), mParent(), mRefNode(), mRangeUpdater(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-08 14:23:18 -07:00
|
|
|
DeleteNodeTxn::~DeleteNodeTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteNodeTxn, EditTxn,
|
|
|
|
mNode,
|
|
|
|
mParent,
|
|
|
|
mRefNode)
|
2009-05-08 21:59:25 -07:00
|
|
|
|
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;
|
2012-10-09 05:31:24 -07:00
|
|
|
mParent = aNode->GetParentNode();
|
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-10-09 05:31:24 -07:00
|
|
|
ErrorResult error;
|
|
|
|
mParent->RemoveChild(*mNode, error);
|
|
|
|
return error.ErrorCode();
|
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-10-09 05:31:24 -07:00
|
|
|
ErrorResult error;
|
|
|
|
mParent->InsertBefore(*mNode, mRefNode, error);
|
|
|
|
return error.ErrorCode();
|
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-10-09 05:31:24 -07:00
|
|
|
ErrorResult error;
|
|
|
|
mParent->RemoveChild(*mNode, error);
|
|
|
|
return error.ErrorCode();
|
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;
|
|
|
|
}
|