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-07 20:56:20 +03:00
|
|
|
|
2016-07-07 09:20:22 +03:00
|
|
|
#include "DeleteTextTransaction.h"
|
2016-07-08 07:10:13 +03:00
|
|
|
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/Assertions.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"
|
2014-04-10 20:09:40 +04:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsAString.h"
|
1999-01-07 20:56:20 +03:00
|
|
|
|
2016-07-07 09:20:22 +03:00
|
|
|
namespace mozilla {
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2016-07-07 09:20:22 +03:00
|
|
|
using namespace dom;
|
|
|
|
|
|
|
|
DeleteTextTransaction::DeleteTextTransaction(
|
2016-07-08 07:10:13 +03:00
|
|
|
EditorBase& aEditorBase,
|
2016-07-07 09:20:22 +03:00
|
|
|
nsGenericDOMDataNode& aCharData,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aNumCharsToDelete,
|
2016-06-24 09:01:40 +03:00
|
|
|
RangeUpdater* aRangeUpdater)
|
2017-03-21 13:00:36 +03:00
|
|
|
: mEditorBase(&aEditorBase)
|
2014-08-20 16:25:16 +04:00
|
|
|
, mCharData(&aCharData)
|
|
|
|
, mOffset(aOffset)
|
|
|
|
, mNumCharsToDelete(aNumCharsToDelete)
|
|
|
|
, mRangeUpdater(aRangeUpdater)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
2014-08-20 16:25:16 +04:00
|
|
|
NS_ASSERTION(mCharData->Length() >= aOffset + aNumCharsToDelete,
|
|
|
|
"Trying to delete more characters than in node");
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteTextTransaction, EditTransactionBase,
|
2017-03-21 13:00:36 +03:00
|
|
|
mEditorBase,
|
2014-04-25 20:49:00 +04:00
|
|
|
mCharData)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2016-07-07 09:20:22 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteTextTransaction)
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2017-03-10 07:38:28 +03:00
|
|
|
bool
|
|
|
|
DeleteTextTransaction::CanDoIt() const
|
1999-01-07 20:56:20 +03:00
|
|
|
{
|
2017-03-21 13:00:36 +03:00
|
|
|
if (NS_WARN_IF(!mCharData) || NS_WARN_IF(!mEditorBase)) {
|
2017-03-10 07:38:28 +03:00
|
|
|
return false;
|
2007-06-28 06:48:16 +04:00
|
|
|
}
|
2017-03-21 13:00:36 +03:00
|
|
|
return mEditorBase->IsModifiableNode(mCharData);
|
1999-01-07 20:56:20 +03:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:58:29 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 09:20:22 +03:00
|
|
|
DeleteTextTransaction::DoTransaction()
|
1999-01-07 20:56:20 +03:00
|
|
|
{
|
2017-03-21 13:00:36 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mCharData)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2003-05-24 01:43:10 +04:00
|
|
|
|
2014-08-20 16:25:16 +04:00
|
|
|
// Get the text that we're about to delete
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = mCharData->SubstringData(mOffset, mNumCharsToDelete,
|
|
|
|
mDeletedText);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
rv = mCharData->DeleteData(mOffset, mNumCharsToDelete);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-06-08 13:58:29 +04:00
|
|
|
|
|
|
|
if (mRangeUpdater) {
|
|
|
|
mRangeUpdater->SelAdjDeleteText(mCharData, mOffset, mNumCharsToDelete);
|
|
|
|
}
|
2002-11-10 18:11:08 +03:00
|
|
|
|
2014-08-20 16:25:16 +04:00
|
|
|
// Only set selection to deletion point if editor gives permission
|
2017-03-21 13:00:36 +03:00
|
|
|
if (mEditorBase->GetShouldTxnSetSelection()) {
|
|
|
|
RefPtr<Selection> selection = mEditorBase->GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2016-10-19 12:09:33 +03:00
|
|
|
rv = selection->Collapse(mCharData, mOffset);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
2014-08-20 16:25:16 +04:00
|
|
|
"Selection could not be collapsed after undo of deletetext");
|
2016-10-19 12:09:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
1999-02-22 18:53:31 +03:00
|
|
|
}
|
2014-08-20 16:25:16 +04:00
|
|
|
// Else do nothing - DOM Range gravity will adjust selection
|
2012-06-08 13:58:29 +04:00
|
|
|
return NS_OK;
|
1999-01-07 20:56:20 +03:00
|
|
|
}
|
|
|
|
|
2014-08-20 16:25:16 +04: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 13:58:29 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 09:20:22 +03:00
|
|
|
DeleteTextTransaction::UndoTransaction()
|
1999-01-07 20:56:20 +03:00
|
|
|
{
|
2017-03-21 13:00:36 +03:00
|
|
|
if (NS_WARN_IF(!mCharData)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2012-06-08 13:58:29 +04:00
|
|
|
return mCharData->InsertData(mOffset, mDeletedText);
|
1999-01-07 20:56:20 +03:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:58:29 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 09:20:22 +03:00
|
|
|
DeleteTextTransaction::GetTxnDescription(nsAString& aString)
|
1999-01-07 20:56:20 +03:00
|
|
|
{
|
2016-07-07 09:20:22 +03:00
|
|
|
aString.AssignLiteral("DeleteTextTransaction: ");
|
2001-03-09 17:23:59 +03:00
|
|
|
aString += mDeletedText;
|
1999-01-07 20:56:20 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-07-07 09:20:22 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|