2001-09-25 22:53:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00: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-06-29 20:31:22 +00:00
|
|
|
|
|
|
|
#include "IMETextTxn.h"
|
2014-03-04 23:39:42 +00:00
|
|
|
#include "mozilla/DebugOnly.h" // for DebugOnly
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "mozilla/mozalloc.h" // for operator new
|
2013-10-01 07:22:59 +00:00
|
|
|
#include "mozilla/TextEvents.h" // for TextRangeStyle
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsAString.h" // for nsAString_internal::Length, etc
|
|
|
|
#include "nsAutoPtr.h" // for nsRefPtr
|
|
|
|
#include "nsDebug.h" // for NS_ASSERTION, etc
|
|
|
|
#include "nsError.h" // for NS_SUCCEEDED, NS_FAILED, etc
|
|
|
|
#include "nsIDOMCharacterData.h" // for nsIDOMCharacterData
|
|
|
|
#include "nsIDOMRange.h" // for nsRange::SetEnd, etc
|
2013-08-06 20:19:11 +00:00
|
|
|
#include "nsIContent.h" // for nsIContent
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsIEditor.h" // for nsIEditor
|
|
|
|
#include "nsIPresShell.h" // for SelectionType
|
|
|
|
#include "nsISelection.h" // for nsISelection
|
|
|
|
#include "nsISelectionController.h" // for nsISelectionController, etc
|
|
|
|
#include "nsISelectionPrivate.h" // for nsISelectionPrivate
|
|
|
|
#include "nsISupportsImpl.h" // for nsRange::AddRef, etc
|
|
|
|
#include "nsISupportsUtils.h" // for NS_ADDREF_THIS, NS_RELEASE
|
|
|
|
#include "nsITransaction.h" // for nsITransaction
|
|
|
|
#include "nsRange.h" // for nsRange
|
|
|
|
#include "nsString.h" // for nsString
|
1999-12-01 00:35:31 +00:00
|
|
|
|
2013-10-01 07:22:59 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
1999-12-01 00:35:31 +00:00
|
|
|
// #define DEBUG_IMETXN
|
1999-06-29 20:31:22 +00:00
|
|
|
|
|
|
|
IMETextTxn::IMETextTxn()
|
|
|
|
: EditTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-25 16:49:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(IMETextTxn, EditTxn,
|
|
|
|
mElement)
|
2013-05-21 23:23:53 +00:00
|
|
|
// mRangeList can't lead to cycles
|
2009-05-09 04:59:25 +00:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IMETextTxn)
|
|
|
|
if (aIID.Equals(IMETextTxn::GetCID())) {
|
|
|
|
*aInstancePtr = (void*)(IMETextTxn*)this;
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
2009-07-22 22:31:02 +00:00
|
|
|
} else
|
2009-05-09 04:59:25 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
|
|
|
|
1999-08-25 10:51:55 +00:00
|
|
|
NS_IMETHODIMP IMETextTxn::Init(nsIDOMCharacterData *aElement,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aReplaceLength,
|
2014-03-04 13:48:26 +00:00
|
|
|
TextRangeArray *aTextRangeArray,
|
2002-03-23 22:16:54 +00:00
|
|
|
const nsAString &aStringToInsert,
|
2011-03-11 05:40:30 +00:00
|
|
|
nsIEditor *aEditor)
|
1999-06-29 20:31:22 +00:00
|
|
|
{
|
2014-03-04 13:48:26 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aElement);
|
|
|
|
mElement = aElement;
|
1999-06-29 20:31:22 +00:00
|
|
|
mOffset = aOffset;
|
|
|
|
mReplaceLength = aReplaceLength;
|
|
|
|
mStringToInsert = aStringToInsert;
|
2011-03-11 05:40:30 +00:00
|
|
|
mEditor = aEditor;
|
2014-03-04 13:48:26 +00:00
|
|
|
mRanges = aTextRangeArray;
|
2011-10-17 14:59:28 +00:00
|
|
|
mFixed = false;
|
1999-06-29 20:31:22 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-03-09 14:23:59 +00:00
|
|
|
NS_IMETHODIMP IMETextTxn::DoTransaction(void)
|
1999-06-29 20:31:22 +00:00
|
|
|
{
|
2000-10-28 22:17:53 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_IMETXN
|
|
|
|
printf("Do IME Text element = %p replace = %d len = %d\n", mElement.get(), mReplaceLength, mStringToInsert.Length());
|
|
|
|
#endif
|
|
|
|
|
2011-03-11 05:40:30 +00:00
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
mEditor->GetSelectionController(getter_AddRefs(selCon));
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED);
|
1999-08-25 10:51:55 +00:00
|
|
|
|
|
|
|
// advance caret: This requires the presentation shell to get the selection.
|
2003-06-30 14:31:48 +00:00
|
|
|
nsresult result;
|
|
|
|
if (mReplaceLength == 0) {
|
|
|
|
result = mElement->InsertData(mOffset, mStringToInsert);
|
1999-10-27 00:10:03 +00:00
|
|
|
} else {
|
2003-06-30 14:31:48 +00:00
|
|
|
result = mElement->ReplaceData(mOffset, mReplaceLength, mStringToInsert);
|
1999-10-27 00:10:03 +00:00
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
2014-03-04 13:48:26 +00:00
|
|
|
result = SetSelectionForRanges();
|
1999-08-25 10:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
1999-06-29 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
2001-03-09 14:23:59 +00:00
|
|
|
NS_IMETHODIMP IMETextTxn::UndoTransaction(void)
|
1999-06-29 20:31:22 +00:00
|
|
|
{
|
2000-10-28 22:17:53 +00:00
|
|
|
#ifdef DEBUG_IMETXN
|
|
|
|
printf("Undo IME Text element = %p\n", mElement.get());
|
|
|
|
#endif
|
|
|
|
|
2011-03-11 05:40:30 +00:00
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
mEditor->GetSelectionController(getter_AddRefs(selCon));
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED);
|
1999-08-25 10:51:55 +00:00
|
|
|
|
2003-06-30 14:31:48 +00:00
|
|
|
nsresult result = mElement->DeleteData(mOffset, mStringToInsert.Length());
|
1999-06-29 20:31:22 +00:00
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{ // set the selection to the insertion point where the string was removed
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2000-04-27 07:37:12 +00:00
|
|
|
result = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection));
|
1999-06-29 20:31:22 +00:00
|
|
|
if (NS_SUCCEEDED(result) && selection) {
|
1999-07-18 02:27:19 +00:00
|
|
|
result = selection->Collapse(mElement, mOffset);
|
1999-06-29 20:31:22 +00:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of IME insert.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHODIMP IMETextTxn::Merge(nsITransaction *aTransaction, bool *aDidMerge)
|
1999-06-29 20:31:22 +00:00
|
|
|
{
|
1999-10-18 14:48:41 +00:00
|
|
|
NS_ASSERTION(aDidMerge, "illegal vaule- null ptr- aDidMerge");
|
|
|
|
NS_ASSERTION(aTransaction, "illegal vaule- null ptr- aTransaction");
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(aDidMerge && aTransaction, NS_ERROR_NULL_POINTER);
|
1999-10-18 14:48:41 +00:00
|
|
|
|
2000-10-28 22:17:53 +00:00
|
|
|
#ifdef DEBUG_IMETXN
|
|
|
|
printf("Merge IME Text element = %p\n", mElement.get());
|
|
|
|
#endif
|
1999-06-29 20:31:22 +00:00
|
|
|
|
1999-08-25 10:51:55 +00:00
|
|
|
//
|
|
|
|
// check to make sure we aren't fixed, if we are then nothing get's absorbed
|
|
|
|
//
|
|
|
|
if (mFixed) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*aDidMerge = false;
|
1999-08-25 10:51:55 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2003-06-30 14:31:48 +00:00
|
|
|
// if aTransaction is another IMETextTxn then absorb it
|
1999-08-25 10:51:55 +00:00
|
|
|
//
|
2012-07-30 14:20:58 +00:00
|
|
|
IMETextTxn* otherTxn = nullptr;
|
2003-06-30 14:31:48 +00:00
|
|
|
nsresult result = aTransaction->QueryInterface(IMETextTxn::GetCID(),(void**)&otherTxn);
|
2000-06-08 23:40:31 +00:00
|
|
|
if (otherTxn && NS_SUCCEEDED(result))
|
1999-08-25 10:51:55 +00:00
|
|
|
{
|
|
|
|
//
|
2003-06-30 14:31:48 +00:00
|
|
|
// we absorb the next IME transaction by adopting its insert string as our own
|
1999-08-25 10:51:55 +00:00
|
|
|
//
|
2014-03-04 13:48:26 +00:00
|
|
|
mStringToInsert = otherTxn->mStringToInsert;
|
|
|
|
mRanges = otherTxn->mRanges;
|
2011-10-17 14:59:28 +00:00
|
|
|
*aDidMerge = true;
|
2000-10-28 22:17:53 +00:00
|
|
|
#ifdef DEBUG_IMETXN
|
|
|
|
printf("IMETextTxn assimilated IMETextTxn:%p\n", aTransaction);
|
|
|
|
#endif
|
1999-08-25 10:51:55 +00:00
|
|
|
NS_RELEASE(otherTxn);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
*aDidMerge = false;
|
1999-08-25 10:51:55 +00:00
|
|
|
return NS_OK;
|
1999-06-29 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
2000-07-11 19:51:36 +00:00
|
|
|
NS_IMETHODIMP IMETextTxn::MarkFixed(void)
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mFixed = true;
|
2000-07-11 19:51:36 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-03-23 22:16:54 +00:00
|
|
|
NS_IMETHODIMP IMETextTxn::GetTxnDescription(nsAString& aString)
|
1999-06-29 20:31:22 +00:00
|
|
|
{
|
2004-06-17 00:13:25 +00:00
|
|
|
aString.AssignLiteral("IMETextTxn: ");
|
2001-03-09 14:23:59 +00:00
|
|
|
aString += mStringToInsert;
|
1999-06-29 20:31:22 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ============ protected methods ================== */
|
2014-03-04 13:48:26 +00:00
|
|
|
static SelectionType
|
|
|
|
ToSelectionType(uint32_t aTextRangeType)
|
1999-10-27 00:10:03 +00:00
|
|
|
{
|
2014-03-04 13:48:26 +00:00
|
|
|
switch(aTextRangeType) {
|
|
|
|
case NS_TEXTRANGE_RAWINPUT:
|
|
|
|
return nsISelectionController::SELECTION_IME_RAWINPUT;
|
|
|
|
case NS_TEXTRANGE_SELECTEDRAWTEXT:
|
|
|
|
return nsISelectionController::SELECTION_IME_SELECTEDRAWTEXT;
|
|
|
|
case NS_TEXTRANGE_CONVERTEDTEXT:
|
|
|
|
return nsISelectionController::SELECTION_IME_CONVERTEDTEXT;
|
|
|
|
case NS_TEXTRANGE_SELECTEDCONVERTEDTEXT:
|
|
|
|
return nsISelectionController::SELECTION_IME_SELECTEDCONVERTEDTEXT;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Selection type is invalid");
|
|
|
|
return nsISelectionController::SELECTION_NORMAL;
|
|
|
|
}
|
1999-10-27 00:10:03 +00:00
|
|
|
}
|
1999-06-29 20:31:22 +00:00
|
|
|
|
2014-03-04 13:48:26 +00:00
|
|
|
nsresult
|
|
|
|
IMETextTxn::SetSelectionForRanges()
|
1999-06-29 20:31:22 +00:00
|
|
|
{
|
2014-03-04 13:48:26 +00:00
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
mEditor->GetSelectionController(getter_AddRefs(selCon));
|
|
|
|
NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED);
|
1999-06-29 20:31:22 +00:00
|
|
|
|
2014-03-04 13:48:26 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult rv =
|
|
|
|
selCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(selection));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
|
|
|
rv = selPriv->StartBatchChanges();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// First, remove all selections of IME composition.
|
|
|
|
static const SelectionType kIMESelections[] = {
|
|
|
|
nsISelectionController::SELECTION_IME_RAWINPUT,
|
|
|
|
nsISelectionController::SELECTION_IME_SELECTEDRAWTEXT,
|
|
|
|
nsISelectionController::SELECTION_IME_CONVERTEDTEXT,
|
|
|
|
nsISelectionController::SELECTION_IME_SELECTEDCONVERTEDTEXT
|
|
|
|
};
|
|
|
|
for (uint32_t i = 0; i < ArrayLength(kIMESelections); ++i) {
|
|
|
|
nsCOMPtr<nsISelection> selectionOfIME;
|
|
|
|
if (NS_FAILED(selCon->GetSelection(kIMESelections[i],
|
|
|
|
getter_AddRefs(selectionOfIME)))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
DebugOnly<nsresult> rv = selectionOfIME->RemoveAllRanges();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Failed to remove all ranges of IME selection");
|
|
|
|
}
|
1999-10-27 00:10:03 +00:00
|
|
|
|
2014-03-04 13:48:26 +00:00
|
|
|
// Set caret position and selection of IME composition with TextRangeArray.
|
|
|
|
bool setCaret = false;
|
|
|
|
uint32_t countOfRanges = mRanges ? mRanges->Length() : 0;
|
2014-06-24 01:06:20 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// When this sets selection (caret) offset to out of the content of
|
|
|
|
// the editor, let's crash the process only on debug build. That makes such
|
|
|
|
// bugs detectable with automated tests.
|
|
|
|
uint32_t maxOffset = UINT32_MAX;
|
|
|
|
mElement->GetLength(&maxOffset);
|
|
|
|
#endif
|
|
|
|
// The mStringToInsert may be truncated if maxlength attribute value doesn't
|
|
|
|
// allow to input all text of this composition. So, we can get actual length
|
|
|
|
// of the inserted string from it.
|
|
|
|
uint32_t insertedLength = mStringToInsert.Length();
|
2014-03-04 13:48:26 +00:00
|
|
|
for (uint32_t i = 0; i < countOfRanges; ++i) {
|
|
|
|
const TextRange& textRange = mRanges->ElementAt(i);
|
|
|
|
|
|
|
|
// Caret needs special handling since its length may be 0 and if it's not
|
|
|
|
// specified explicitly, we need to handle it ourselves later.
|
|
|
|
if (textRange.mRangeType == NS_TEXTRANGE_CARETPOSITION) {
|
|
|
|
NS_ASSERTION(!setCaret, "The ranges already has caret position");
|
|
|
|
NS_ASSERTION(!textRange.Length(), "nsEditor doesn't support wide caret");
|
2014-06-24 01:06:20 +00:00
|
|
|
int32_t caretOffset = static_cast<int32_t>(
|
|
|
|
mOffset + std::min(textRange.mStartOffset, insertedLength));
|
|
|
|
MOZ_ASSERT(caretOffset >= 0 &&
|
|
|
|
static_cast<uint32_t>(caretOffset) <= maxOffset);
|
|
|
|
rv = selection->Collapse(mElement, caretOffset);
|
2014-03-04 13:48:26 +00:00
|
|
|
setCaret = setCaret || NS_SUCCEEDED(rv);
|
|
|
|
NS_ASSERTION(setCaret, "Failed to collapse normal selection");
|
|
|
|
continue;
|
|
|
|
}
|
1999-06-29 20:31:22 +00:00
|
|
|
|
2014-03-04 13:48:26 +00:00
|
|
|
// If the clause length is 0, it's should be a bug.
|
|
|
|
if (!textRange.Length()) {
|
|
|
|
NS_WARNING("Any clauses must not be empty");
|
|
|
|
continue;
|
1999-08-25 10:51:55 +00:00
|
|
|
}
|
2000-04-27 07:37:12 +00:00
|
|
|
|
2014-03-04 13:48:26 +00:00
|
|
|
nsRefPtr<nsRange> clauseRange;
|
2014-06-24 01:06:20 +00:00
|
|
|
int32_t startOffset = static_cast<int32_t>(
|
|
|
|
mOffset + std::min(textRange.mStartOffset, insertedLength));
|
|
|
|
MOZ_ASSERT(startOffset >= 0 &&
|
|
|
|
static_cast<uint32_t>(startOffset) <= maxOffset);
|
|
|
|
int32_t endOffset = static_cast<int32_t>(
|
|
|
|
mOffset + std::min(textRange.mEndOffset, insertedLength));
|
|
|
|
MOZ_ASSERT(endOffset >= startOffset &&
|
|
|
|
static_cast<uint32_t>(endOffset) <= maxOffset);
|
|
|
|
rv = nsRange::CreateRange(mElement, startOffset,
|
|
|
|
mElement, endOffset,
|
2014-03-04 13:48:26 +00:00
|
|
|
getter_AddRefs(clauseRange));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to create a DOM range for a clause of composition");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the range of the clause to selection.
|
|
|
|
nsCOMPtr<nsISelection> selectionOfIME;
|
|
|
|
rv = selCon->GetSelection(ToSelectionType(textRange.mRangeType),
|
|
|
|
getter_AddRefs(selectionOfIME));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to get IME selection");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = selectionOfIME->AddRange(clauseRange);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to add selection range for a clause of composition");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the style of the clause.
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selectionOfIMEPriv =
|
|
|
|
do_QueryInterface(selectionOfIME);
|
|
|
|
if (!selectionOfIMEPriv) {
|
|
|
|
NS_WARNING("Failed to get nsISelectionPrivate interface from selection");
|
|
|
|
continue; // Since this is additional feature, we can continue this job.
|
|
|
|
}
|
|
|
|
rv = selectionOfIMEPriv->SetTextRangeStyle(clauseRange,
|
|
|
|
textRange.mRangeStyle);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to set selection style");
|
|
|
|
break; // but this is unexpected...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the ranges doesn't include explicit caret position, let's set the
|
|
|
|
// caret to the end of composition string.
|
|
|
|
if (!setCaret) {
|
2014-06-24 01:06:20 +00:00
|
|
|
int32_t caretOffset = static_cast<int32_t>(mOffset + insertedLength);
|
|
|
|
MOZ_ASSERT(caretOffset >= 0 &&
|
|
|
|
static_cast<uint32_t>(caretOffset) <= maxOffset);
|
|
|
|
rv = selection->Collapse(mElement, caretOffset);
|
2014-03-04 13:48:26 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Failed to set caret at the end of composition string");
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = selPriv->EndBatchChanges();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to end batch changes");
|
|
|
|
|
|
|
|
return rv;
|
1999-06-29 20:31:22 +00:00
|
|
|
}
|
|
|
|
|