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 04:02:32 +03:00
|
|
|
|
2016-07-07 10:06:41 +03:00
|
|
|
#include "InsertTextTransaction.h"
|
2014-08-29 15:43:23 +04:00
|
|
|
|
2016-07-08 07:10:13 +03:00
|
|
|
#include "mozilla/EditorBase.h" // mEditorBase
|
2016-11-12 08:56:33 +03:00
|
|
|
#include "mozilla/SelectionState.h" // RangeUpdater
|
2014-08-29 15:43:23 +04:00
|
|
|
#include "mozilla/dom/Selection.h" // Selection local var
|
|
|
|
#include "mozilla/dom/Text.h" // mTextNode
|
|
|
|
#include "nsAString.h" // nsAString parameter
|
2016-07-08 08:15:21 +03:00
|
|
|
#include "nsDebug.h" // for NS_ASSERTION, etc.
|
|
|
|
#include "nsError.h" // for NS_OK, etc.
|
2015-04-15 19:47:03 +03:00
|
|
|
#include "nsQueryObject.h" // for do_QueryObject
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2016-07-07 10:06:41 +03:00
|
|
|
namespace mozilla {
|
2014-08-29 15:43:23 +04:00
|
|
|
|
2016-07-07 10:06:41 +03:00
|
|
|
using namespace dom;
|
2014-08-29 15:43:23 +04:00
|
|
|
|
2016-07-07 10:06:41 +03:00
|
|
|
InsertTextTransaction::InsertTextTransaction(Text& aTextNode,
|
|
|
|
uint32_t aOffset,
|
|
|
|
const nsAString& aStringToInsert,
|
2016-11-12 08:56:33 +03:00
|
|
|
EditorBase& aEditorBase,
|
|
|
|
RangeUpdater* aRangeUpdater)
|
2016-07-07 10:06:41 +03:00
|
|
|
: mTextNode(&aTextNode)
|
2014-08-29 15:43:23 +04:00
|
|
|
, mOffset(aOffset)
|
|
|
|
, mStringToInsert(aStringToInsert)
|
2017-03-21 13:00:36 +03:00
|
|
|
, mEditorBase(&aEditorBase)
|
2016-11-12 08:56:33 +03:00
|
|
|
, mRangeUpdater(aRangeUpdater)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-07 10:06:41 +03:00
|
|
|
InsertTextTransaction::~InsertTextTransaction()
|
2014-07-09 01:23:18 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(InsertTextTransaction, EditTransactionBase,
|
2017-03-21 13:00:36 +03:00
|
|
|
mEditorBase,
|
2014-08-29 15:43:23 +04:00
|
|
|
mTextNode)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(InsertTextTransaction, EditTransactionBase)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(InsertTextTransaction, EditTransactionBase)
|
2016-07-07 10:06:41 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertTextTransaction)
|
|
|
|
if (aIID.Equals(NS_GET_IID(InsertTextTransaction))) {
|
2014-12-30 18:55:42 +03:00
|
|
|
foundInterface = static_cast<nsITransaction*>(this);
|
|
|
|
} else
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
1999-01-07 04:02:32 +03:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 10:06:41 +03:00
|
|
|
InsertTextTransaction::DoTransaction()
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
2017-03-21 13:00:36 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mTextNode)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = mTextNode->InsertData(mOffset, mStringToInsert);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-08-29 15:43:23 +04:00
|
|
|
|
|
|
|
// Only set selection to insertion 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
|
|
|
DebugOnly<nsresult> rv =
|
|
|
|
selection->Collapse(mTextNode, mOffset + mStringToInsert.Length());
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
2014-08-29 15:43:23 +04:00
|
|
|
"Selection could not be collapsed after insert");
|
|
|
|
} else {
|
|
|
|
// Do nothing - DOM Range gravity will adjust selection
|
2000-01-04 06:09:41 +03:00
|
|
|
}
|
2016-11-12 08:56:33 +03:00
|
|
|
mRangeUpdater->SelAdjInsertText(*mTextNode, mOffset, mStringToInsert);
|
2000-01-04 06:09:41 +03:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
return NS_OK;
|
1999-01-07 04:02:32 +03:00
|
|
|
}
|
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 10:06:41 +03:00
|
|
|
InsertTextTransaction::UndoTransaction()
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
2014-08-29 15:43:23 +04:00
|
|
|
return mTextNode->DeleteData(mOffset, mStringToInsert.Length());
|
1999-01-07 04:02:32 +03:00
|
|
|
}
|
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 10:06:41 +03:00
|
|
|
InsertTextTransaction::Merge(nsITransaction* aTransaction,
|
|
|
|
bool* aDidMerge)
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
2014-08-29 15:43:23 +04:00
|
|
|
if (!aTransaction || !aDidMerge) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// Set out param default value
|
|
|
|
*aDidMerge = false;
|
|
|
|
|
2016-07-07 10:06:41 +03:00
|
|
|
// If aTransaction is a InsertTextTransaction, and if the selection hasn't
|
|
|
|
// changed, then absorb it.
|
|
|
|
RefPtr<InsertTextTransaction> otherTransaction = do_QueryObject(aTransaction);
|
|
|
|
if (otherTransaction && IsSequentialInsert(*otherTransaction)) {
|
2014-08-29 15:43:23 +04:00
|
|
|
nsAutoString otherData;
|
2016-07-07 10:06:41 +03:00
|
|
|
otherTransaction->GetData(otherData);
|
2014-08-29 15:43:23 +04:00
|
|
|
mStringToInsert += otherData;
|
|
|
|
*aDidMerge = true;
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
2014-08-29 15:43:23 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-01-07 04:02:32 +03:00
|
|
|
}
|
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 10:06:41 +03:00
|
|
|
InsertTextTransaction::GetTxnDescription(nsAString& aString)
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
2016-07-07 10:06:41 +03:00
|
|
|
aString.AssignLiteral("InsertTextTransaction: ");
|
2001-03-09 17:23:59 +03:00
|
|
|
aString += mStringToInsert;
|
1999-01-07 04:02:32 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
/* ============ private methods ================== */
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
void
|
2016-07-07 10:06:41 +03:00
|
|
|
InsertTextTransaction::GetData(nsString& aResult)
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
|
|
|
aResult = mStringToInsert;
|
|
|
|
}
|
1999-01-28 22:00:14 +03:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
bool
|
2016-07-07 10:06:41 +03:00
|
|
|
InsertTextTransaction::IsSequentialInsert(
|
|
|
|
InsertTextTransaction& aOtherTransaction)
|
1999-01-28 22:00:14 +03:00
|
|
|
{
|
2016-07-07 10:06:41 +03:00
|
|
|
return aOtherTransaction.mTextNode == mTextNode &&
|
|
|
|
aOtherTransaction.mOffset == mOffset + mStringToInsert.Length();
|
1999-01-28 22:00:14 +03:00
|
|
|
}
|
2016-07-07 10:06:41 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|