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
|
|
|
|
|
|
|
#include "InsertTextTxn.h"
|
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
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsDebug.h" // for NS_ASSERTION, etc
|
2014-08-29 15:43:23 +04:00
|
|
|
#include "nsEditor.h" // mEditor
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsError.h" // for NS_OK, etc
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
class nsITransaction;
|
|
|
|
|
|
|
|
InsertTextTxn::InsertTextTxn(Text& aTextNode, uint32_t aOffset,
|
|
|
|
const nsAString& aStringToInsert,
|
|
|
|
nsEditor& aEditor)
|
1999-01-21 04:51:09 +03:00
|
|
|
: EditTxn()
|
2014-08-29 15:43:23 +04:00
|
|
|
, mTextNode(&aTextNode)
|
|
|
|
, mOffset(aOffset)
|
|
|
|
, mStringToInsert(aStringToInsert)
|
|
|
|
, mEditor(aEditor)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-09 01:23:18 +04:00
|
|
|
InsertTextTxn::~InsertTextTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(InsertTextTxn, EditTxn,
|
2014-08-29 15:43:23 +04:00
|
|
|
mTextNode)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2011-02-20 01:18:37 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(InsertTextTxn, EditTxn)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(InsertTextTxn, EditTxn)
|
2009-05-09 08:59:25 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertTextTxn)
|
2014-12-30 18:55:42 +03:00
|
|
|
if (aIID.Equals(NS_GET_IID(InsertTextTxn))) {
|
|
|
|
foundInterface = static_cast<nsITransaction*>(this);
|
|
|
|
} else
|
2009-05-09 08:59:25 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
|
|
|
|
1999-01-07 04:02:32 +03:00
|
|
|
|
2014-08-29 15:43:23 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
InsertTextTxn::DoTransaction()
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
2014-08-29 15:43:23 +04:00
|
|
|
nsresult res = mTextNode->InsertData(mOffset, mStringToInsert);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
// Only set selection to insertion point if editor gives permission
|
|
|
|
if (mEditor.GetShouldTxnSetSelection()) {
|
|
|
|
nsRefPtr<Selection> selection = mEditor.GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2014-08-29 15:43:23 +04:00
|
|
|
res = selection->Collapse(mTextNode,
|
|
|
|
mOffset + mStringToInsert.Length());
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(res),
|
|
|
|
"Selection could not be collapsed after insert");
|
|
|
|
} else {
|
|
|
|
// Do nothing - DOM Range gravity will adjust selection
|
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
|
|
|
|
InsertTextTxn::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
|
|
|
|
InsertTextTxn::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;
|
|
|
|
|
|
|
|
// If aTransaction is a InsertTextTxn, and if the selection hasn't changed,
|
|
|
|
// then absorb it
|
|
|
|
nsRefPtr<InsertTextTxn> otherInsTxn = do_QueryObject(aTransaction);
|
|
|
|
if (otherInsTxn && IsSequentialInsert(*otherInsTxn)) {
|
|
|
|
nsAutoString otherData;
|
|
|
|
otherInsTxn->GetData(otherData);
|
|
|
|
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
|
|
|
|
InsertTextTxn::GetTxnDescription(nsAString& aString)
|
1999-01-07 04:02:32 +03:00
|
|
|
{
|
2004-06-17 04:13:25 +04:00
|
|
|
aString.AssignLiteral("InsertTextTxn: ");
|
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
|
|
|
|
InsertTextTxn::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
|
|
|
|
InsertTextTxn::IsSequentialInsert(InsertTextTxn& aOtherTxn)
|
1999-01-28 22:00:14 +03:00
|
|
|
{
|
2014-08-29 15:43:23 +04:00
|
|
|
return aOtherTxn.mTextNode == mTextNode &&
|
|
|
|
aOtherTxn.mOffset == mOffset + mStringToInsert.Length();
|
1999-01-28 22:00:14 +03:00
|
|
|
}
|