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-07-12 23:33:42 -07:00
|
|
|
#include <stdio.h> // for printf
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "InsertTextTxn.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsDebug.h" // for NS_ASSERTION, etc
|
|
|
|
#include "nsError.h" // for NS_OK, etc
|
|
|
|
#include "nsIDOMCharacterData.h" // for nsIDOMCharacterData
|
|
|
|
#include "nsIEditor.h" // for nsIEditor
|
|
|
|
#include "nsISelection.h" // for nsISelection
|
|
|
|
#include "nsISupportsUtils.h" // for NS_ADDREF_THIS, NS_RELEASE
|
|
|
|
#include "nsITransaction.h" // for nsITransaction
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
InsertTextTxn::InsertTextTxn()
|
|
|
|
: EditTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:23:53 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(InsertTextTxn, EditTxn,
|
|
|
|
mElement)
|
2009-05-08 21:59:25 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
NS_IMPL_ADDREF_INHERITED(InsertTextTxn, EditTxn)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(InsertTextTxn, EditTxn)
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertTextTxn)
|
|
|
|
if (aIID.Equals(InsertTextTxn::GetCID())) {
|
|
|
|
*aInstancePtr = (void*)(InsertTextTxn*)this;
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
2009-07-22 15:31:02 -07:00
|
|
|
} else
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aOffset,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsAString &aStringToInsert,
|
|
|
|
nsIEditor *aEditor)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
nsAutoString text;
|
|
|
|
aElement->GetData(text);
|
|
|
|
printf("InsertTextTxn: Offset to insert at = %d. Text of the node to insert into:\n", aOffset);
|
|
|
|
wprintf(text.get());
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_ASSERTION(aElement && aEditor, "bad args");
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(aElement && aEditor, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mElement = do_QueryInterface(aElement);
|
|
|
|
mOffset = aOffset;
|
|
|
|
mStringToInsert = aStringToInsert;
|
|
|
|
mEditor = aEditor;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP InsertTextTxn::DoTransaction(void)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mElement && mEditor, "bad state");
|
|
|
|
if (!mElement || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
|
|
|
nsresult result = mElement->InsertData(mOffset, mStringToInsert);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// only set selection to insertion 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);
|
|
|
|
if (bAdjustSelection)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
result = mEditor->GetSelection(getter_AddRefs(selection));
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
result = selection->Collapse(mElement, mOffset+mStringToInsert.Length());
|
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// do nothing - dom range gravity will adjust selection
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP InsertTextTxn::UndoTransaction(void)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mElement && mEditor, "bad state");
|
|
|
|
if (!mElement || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length = mStringToInsert.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
return mElement->DeleteData(mOffset, length);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP InsertTextTxn::Merge(nsITransaction *aTransaction, bool *aDidMerge)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// set out param default value
|
|
|
|
if (aDidMerge)
|
2011-10-17 07:59:28 -07:00
|
|
|
*aDidMerge = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
if (aDidMerge && aTransaction)
|
|
|
|
{
|
|
|
|
// if aTransaction is a InsertTextTxn, and if the selection hasn't changed,
|
|
|
|
// then absorb it
|
2012-07-30 07:20:58 -07:00
|
|
|
InsertTextTxn *otherInsTxn = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
aTransaction->QueryInterface(InsertTextTxn::GetCID(), (void **)&otherInsTxn);
|
|
|
|
if (otherInsTxn)
|
|
|
|
{
|
|
|
|
if (IsSequentialInsert(otherInsTxn))
|
|
|
|
{
|
|
|
|
nsAutoString otherData;
|
|
|
|
otherInsTxn->GetData(otherData);
|
|
|
|
mStringToInsert += otherData;
|
2011-10-17 07:59:28 -07:00
|
|
|
*aDidMerge = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
NS_RELEASE(otherInsTxn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP InsertTextTxn::GetTxnDescription(nsAString& aString)
|
|
|
|
{
|
|
|
|
aString.AssignLiteral("InsertTextTxn: ");
|
|
|
|
aString += mStringToInsert;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ============ protected methods ================== */
|
|
|
|
|
|
|
|
NS_IMETHODIMP InsertTextTxn::GetData(nsString& aResult)
|
|
|
|
{
|
|
|
|
aResult = mStringToInsert;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool InsertTextTxn::IsSequentialInsert(InsertTextTxn *aOtherTxn)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aOtherTxn, "null param");
|
|
|
|
if (aOtherTxn && aOtherTxn->mElement == mElement)
|
|
|
|
{
|
|
|
|
// here, we need to compare offsets.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t length = mStringToInsert.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aOtherTxn->mOffset == (mOffset + length))
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|