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-08-29 11:43:24 +00:00
|
|
|
|
|
|
|
#include "mozilla/dom/Selection.h" // local var
|
|
|
|
#include "mozilla/dom/Text.h" // mTextNode
|
|
|
|
#include "nsAString.h" // params
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsDebug.h" // for NS_ASSERTION, etc
|
2014-08-29 11:43:24 +00:00
|
|
|
#include "nsEditor.h" // mEditor
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsError.h" // for NS_SUCCEEDED, NS_FAILED, etc
|
2014-08-29 11:43:24 +00:00
|
|
|
#include "nsIPresShell.h" // nsISelectionController constants
|
|
|
|
#include "nsRange.h" // local var
|
1999-12-01 00:35:31 +00:00
|
|
|
|
2013-10-01 07:22:59 +00:00
|
|
|
using namespace mozilla;
|
2014-08-29 11:43:24 +00:00
|
|
|
using namespace mozilla::dom;
|
2013-10-01 07:22:59 +00:00
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
IMETextTxn::IMETextTxn(Text& aTextNode, uint32_t aOffset,
|
|
|
|
uint32_t aReplaceLength,
|
|
|
|
TextRangeArray* aTextRangeArray,
|
|
|
|
const nsAString& aStringToInsert,
|
|
|
|
nsEditor& aEditor)
|
1999-06-29 20:31:22 +00:00
|
|
|
: EditTxn()
|
2014-08-29 11:43:24 +00:00
|
|
|
, mTextNode(&aTextNode)
|
|
|
|
, mOffset(aOffset)
|
|
|
|
, mReplaceLength(aReplaceLength)
|
|
|
|
, mRanges(aTextRangeArray)
|
|
|
|
, mStringToInsert(aStringToInsert)
|
|
|
|
, mEditor(aEditor)
|
|
|
|
, mFixed(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IMETextTxn::~IMETextTxn()
|
1999-06-29 20:31:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-25 16:49:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(IMETextTxn, EditTxn,
|
2014-08-29 11:43:24 +00:00
|
|
|
mTextNode)
|
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)
|
2014-12-30 15:55:42 +00:00
|
|
|
if (aIID.Equals(NS_GET_IID(IMETextTxn))) {
|
|
|
|
foundInterface = static_cast<nsITransaction*>(this);
|
|
|
|
} else
|
2009-05-09 04:59:25 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(IMETextTxn, EditTxn)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IMETextTxn, EditTxn)
|
1999-06-29 20:31:22 +00:00
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IMETextTxn::DoTransaction()
|
1999-06-29 20:31:22 +00:00
|
|
|
{
|
2014-08-29 11:43:24 +00:00
|
|
|
// Fail before making any changes if there's no selection controller
|
2011-03-11 05:40:30 +00:00
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
2014-08-29 11:43:24 +00:00
|
|
|
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
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
// Advance caret: This requires the presentation shell to get the selection.
|
|
|
|
nsresult res;
|
2003-06-30 14:31:48 +00:00
|
|
|
if (mReplaceLength == 0) {
|
2014-08-29 11:43:24 +00:00
|
|
|
res = mTextNode->InsertData(mOffset, mStringToInsert);
|
1999-10-27 00:10:03 +00:00
|
|
|
} else {
|
2014-08-29 11:43:24 +00:00
|
|
|
res = mTextNode->ReplaceData(mOffset, mReplaceLength, mStringToInsert);
|
1999-08-25 10:51:55 +00:00
|
|
|
}
|
2014-08-29 11:43:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
res = SetSelectionForRanges();
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
1999-08-25 10:51:55 +00:00
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
return NS_OK;
|
1999-06-29 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IMETextTxn::UndoTransaction()
|
1999-06-29 20:31:22 +00:00
|
|
|
{
|
2014-08-29 11:43:24 +00:00
|
|
|
// Get the selection first so we'll fail before making any changes if we
|
|
|
|
// can't get it
|
|
|
|
nsRefPtr<Selection> selection = mEditor.GetSelection();
|
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NOT_INITIALIZED);
|
2000-10-28 22:17:53 +00:00
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
nsresult res = mTextNode->DeleteData(mOffset, mStringToInsert.Length());
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
1999-08-25 10:51:55 +00:00
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
// set the selection to the insertion point where the string was removed
|
|
|
|
res = selection->Collapse(mTextNode, mOffset);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(res),
|
|
|
|
"Selection could not be collapsed after undo of IME insert.");
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-06-29 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IMETextTxn::Merge(nsITransaction* aTransaction, bool* aDidMerge)
|
1999-06-29 20:31:22 +00:00
|
|
|
{
|
2014-08-29 11:43:24 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aTransaction && aDidMerge);
|
1999-06-29 20:31:22 +00:00
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
// Check to make sure we aren't fixed, if we are then nothing gets absorbed
|
1999-08-25 10:51:55 +00:00
|
|
|
if (mFixed) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*aDidMerge = false;
|
1999-08-25 10:51:55 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
// If aTransaction is another IMETextTxn then absorb it
|
|
|
|
nsRefPtr<IMETextTxn> otherTxn = do_QueryObject(aTransaction);
|
|
|
|
if (otherTxn) {
|
|
|
|
// We absorb the next IME transaction by adopting its insert string
|
2014-03-04 13:48:26 +00:00
|
|
|
mStringToInsert = otherTxn->mStringToInsert;
|
|
|
|
mRanges = otherTxn->mRanges;
|
2011-10-17 14:59:28 +00:00
|
|
|
*aDidMerge = true;
|
1999-08-25 10:51:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
void
|
|
|
|
IMETextTxn::MarkFixed()
|
2000-07-11 19:51:36 +00:00
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mFixed = true;
|
2000-07-11 19:51:36 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 11:43:24 +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;
|
|
|
|
}
|
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
/* ============ private 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-08-29 11:43:24 +00:00
|
|
|
nsRefPtr<Selection> selection = mEditor.GetSelection();
|
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NOT_INITIALIZED);
|
1999-06-29 20:31:22 +00:00
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
nsresult rv = selection->StartBatchChanges();
|
2014-03-04 13:48:26 +00:00
|
|
|
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
|
|
|
|
};
|
2014-08-29 11:43:24 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
mEditor.GetSelectionController(getter_AddRefs(selCon));
|
|
|
|
NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2014-03-04 13:48:26 +00:00
|
|
|
for (uint32_t i = 0; i < ArrayLength(kIMESelections); ++i) {
|
|
|
|
nsCOMPtr<nsISelection> selectionOfIME;
|
|
|
|
if (NS_FAILED(selCon->GetSelection(kIMESelections[i],
|
|
|
|
getter_AddRefs(selectionOfIME)))) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-29 11:43:24 +00:00
|
|
|
rv = selectionOfIME->RemoveAllRanges();
|
2014-03-04 13:48:26 +00:00
|
|
|
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-08-29 11:43:24 +00:00
|
|
|
|
2014-06-24 01:06:20 +00:00
|
|
|
#ifdef DEBUG
|
2014-08-29 11:43:24 +00:00
|
|
|
// Bounds-checking on debug builds
|
|
|
|
uint32_t maxOffset = mTextNode->Length();
|
2014-06-24 01:06:20 +00:00
|
|
|
#endif
|
2014-08-29 11:43:24 +00:00
|
|
|
|
2014-06-24 01:06:20 +00:00
|
|
|
// The mStringToInsert may be truncated if maxlength attribute value doesn't
|
2014-08-29 11:43:24 +00:00
|
|
|
// allow input of all text of this composition. So, we can get actual length
|
2014-06-24 01:06:20 +00:00
|
|
|
// 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);
|
2014-08-29 11:43:24 +00:00
|
|
|
rv = selection->Collapse(mTextNode, 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-08-29 11:43:24 +00:00
|
|
|
// If the clause length is 0, it should be a bug.
|
2014-03-04 13:48:26 +00:00
|
|
|
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);
|
2014-08-29 11:43:24 +00:00
|
|
|
rv = nsRange::CreateRange(mTextNode, startOffset,
|
|
|
|
mTextNode, 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);
|
2014-08-29 11:43:24 +00:00
|
|
|
rv = selection->Collapse(mTextNode, caretOffset);
|
2014-03-04 13:48:26 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Failed to set caret at the end of composition string");
|
|
|
|
}
|
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
rv = selection->EndBatchChanges();
|
2014-03-04 13:48:26 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to end batch changes");
|
|
|
|
|
|
|
|
return rv;
|
1999-06-29 20:31:22 +00:00
|
|
|
}
|