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
|
|
|
|
|
|
|
#include "DeleteTextTxn.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-06-08 02:58:29 -07:00
|
|
|
#include "mozilla/Selection.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsDebug.h"
|
2012-06-08 02:58:29 -07:00
|
|
|
#include "nsEditor.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsISelection.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsSelectionState.h"
|
|
|
|
#include "nsAString.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-08 02:58:29 -07:00
|
|
|
using namespace mozilla;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-08 02:58:29 -07:00
|
|
|
DeleteTextTxn::DeleteTextTxn() :
|
|
|
|
EditTxn(),
|
2012-07-30 07:20:58 -07:00
|
|
|
mEditor(nullptr),
|
2012-06-08 02:58:29 -07:00
|
|
|
mCharData(),
|
|
|
|
mOffset(0),
|
|
|
|
mNumCharsToDelete(0),
|
2012-07-30 07:20:58 -07:00
|
|
|
mRangeUpdater(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(DeleteTextTxn)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DeleteTextTxn, EditTxn)
|
2012-06-08 02:58:29 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mCharData)
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DeleteTextTxn, EditTxn)
|
2012-06-08 02:58:29 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mCharData)
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteTextTxn)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
|
|
|
|
2012-06-08 02:58:29 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DeleteTextTxn::Init(nsEditor* aEditor,
|
|
|
|
nsIDOMCharacterData* aCharData,
|
|
|
|
PRUint32 aOffset,
|
|
|
|
PRUint32 aNumCharsToDelete,
|
|
|
|
nsRangeUpdater* aRangeUpdater)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-19 06:21:48 -07:00
|
|
|
MOZ_ASSERT(aEditor && aCharData);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mEditor = aEditor;
|
2012-06-08 02:58:29 -07:00
|
|
|
mCharData = aCharData;
|
|
|
|
|
2007-06-27 19:48:16 -07:00
|
|
|
// do nothing if the node is read-only
|
2012-06-08 02:58:29 -07:00
|
|
|
if (!mEditor->IsModifiableNode(mCharData)) {
|
2007-06-27 19:48:16 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mOffset = aOffset;
|
|
|
|
mNumCharsToDelete = aNumCharsToDelete;
|
2012-06-08 02:58:29 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
PRUint32 length;
|
|
|
|
mCharData->GetLength(&length);
|
|
|
|
NS_ASSERTION(length >= aOffset + aNumCharsToDelete,
|
|
|
|
"Trying to delete more characters than in node");
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
mDeletedText.Truncate();
|
|
|
|
mRangeUpdater = aRangeUpdater;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-08 02:58:29 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DeleteTextTxn::DoTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-08 02:58:29 -07:00
|
|
|
MOZ_ASSERT(mEditor && mCharData);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// get the text that we're about to delete
|
2012-06-08 02:58:29 -07:00
|
|
|
nsresult res = mCharData->SubstringData(mOffset, mNumCharsToDelete,
|
|
|
|
mDeletedText);
|
2012-06-23 23:54:42 -07:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(res));
|
2012-06-08 02:58:29 -07:00
|
|
|
res = mCharData->DeleteData(mOffset, mNumCharsToDelete);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
if (mRangeUpdater) {
|
|
|
|
mRangeUpdater->SelAdjDeleteText(mCharData, mOffset, mNumCharsToDelete);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// only set selection to deletion point if editor gives permission
|
2011-09-28 23:19:26 -07:00
|
|
|
bool bAdjustSelection;
|
2007-03-22 10:30:00 -07:00
|
|
|
mEditor->ShouldTxnSetSelection(&bAdjustSelection);
|
2012-06-08 02:58:29 -07:00
|
|
|
if (bAdjustSelection) {
|
|
|
|
nsRefPtr<Selection> selection = mEditor->GetSelection();
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2012-06-08 02:58:29 -07:00
|
|
|
res = selection->Collapse(mCharData, mOffset);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(res),
|
|
|
|
"selection could not be collapsed after undo of deletetext.");
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-08 02:58:29 -07:00
|
|
|
// else do nothing - dom range gravity will adjust selection
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//XXX: we may want to store the selection state and restore it properly
|
|
|
|
// was it an insertion point or an extended selection?
|
2012-06-08 02:58:29 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DeleteTextTxn::UndoTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-08 02:58:29 -07:00
|
|
|
MOZ_ASSERT(mEditor && mCharData);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-08 02:58:29 -07:00
|
|
|
return mCharData->InsertData(mOffset, mDeletedText);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-08 02:58:29 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DeleteTextTxn::GetTxnDescription(nsAString& aString)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aString.AssignLiteral("DeleteTextTxn: ");
|
|
|
|
aString += mDeletedText;
|
|
|
|
return NS_OK;
|
|
|
|
}
|