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
|
|
|
|
|
|
|
#include "IMETextTxn.h"
|
2014-08-29 04:43:24 -07:00
|
|
|
|
|
|
|
#include "mozilla/dom/Selection.h" // local var
|
|
|
|
#include "mozilla/dom/Text.h" // mTextNode
|
|
|
|
#include "nsAString.h" // params
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsDebug.h" // for NS_ASSERTION, etc
|
2014-08-29 04:43:24 -07:00
|
|
|
#include "nsEditor.h" // mEditor
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsError.h" // for NS_SUCCEEDED, NS_FAILED, etc
|
2014-08-29 04:43:24 -07:00
|
|
|
#include "nsIPresShell.h" // nsISelectionController constants
|
|
|
|
#include "nsRange.h" // local var
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-01 00:22:59 -07:00
|
|
|
using namespace mozilla;
|
2014-08-29 04:43:24 -07:00
|
|
|
using namespace mozilla::dom;
|
2013-10-01 00:22:59 -07:00
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
IMETextTxn::IMETextTxn(Text& aTextNode, uint32_t aOffset,
|
|
|
|
uint32_t aReplaceLength,
|
|
|
|
TextRangeArray* aTextRangeArray,
|
|
|
|
const nsAString& aStringToInsert,
|
|
|
|
nsEditor& aEditor)
|
2007-03-22 10:30:00 -07:00
|
|
|
: EditTxn()
|
2014-08-29 04:43:24 -07:00
|
|
|
, mTextNode(&aTextNode)
|
|
|
|
, mOffset(aOffset)
|
|
|
|
, mReplaceLength(aReplaceLength)
|
|
|
|
, mRanges(aTextRangeArray)
|
|
|
|
, mStringToInsert(aStringToInsert)
|
|
|
|
, mEditor(aEditor)
|
|
|
|
, mFixed(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IMETextTxn::~IMETextTxn()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(IMETextTxn, EditTxn,
|
2014-08-29 04:43:24 -07:00
|
|
|
mTextNode)
|
2013-05-21 16:23:53 -07:00
|
|
|
// mRangeList can't lead to cycles
|
2009-05-08 21:59:25 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IMETextTxn)
|
2014-08-29 04:43:24 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsITransaction, IMETextTxn)
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(IMETextTxn, EditTxn)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IMETextTxn, EditTxn)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IMETextTxn::DoTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-29 04:43:24 -07:00
|
|
|
// Fail before making any changes if there's no selection controller
|
2011-03-10 21:40:30 -08:00
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
2014-08-29 04:43:24 -07:00
|
|
|
mEditor.GetSelectionController(getter_AddRefs(selCon));
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
// Advance caret: This requires the presentation shell to get the selection.
|
|
|
|
nsresult res;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mReplaceLength == 0) {
|
2014-08-29 04:43:24 -07:00
|
|
|
res = mTextNode->InsertData(mOffset, mStringToInsert);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2014-08-29 04:43:24 -07:00
|
|
|
res = mTextNode->ReplaceData(mOffset, mReplaceLength, mStringToInsert);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-08-29 04:43:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
res = SetSelectionForRanges();
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IMETextTxn::UndoTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-29 04:43:24 -07: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);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
nsresult res = mTextNode->DeleteData(mOffset, mStringToInsert.Length());
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-29 04:43:24 -07: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;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IMETextTxn::Merge(nsITransaction* aTransaction, bool* aDidMerge)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-29 04:43:24 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aTransaction && aDidMerge);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
// Check to make sure we aren't fixed, if we are then nothing gets absorbed
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mFixed) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aDidMerge = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-29 04:43:24 -07: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 05:48:26 -08:00
|
|
|
mStringToInsert = otherTxn->mStringToInsert;
|
|
|
|
mRanges = otherTxn->mRanges;
|
2011-10-17 07:59:28 -07:00
|
|
|
*aDidMerge = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aDidMerge = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
void
|
|
|
|
IMETextTxn::MarkFixed()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mFixed = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IMETextTxn::GetTxnDescription(nsAString& aString)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aString.AssignLiteral("IMETextTxn: ");
|
|
|
|
aString += mStringToInsert;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
/* ============ private methods ================== */
|
2014-03-04 05:48:26 -08:00
|
|
|
static SelectionType
|
|
|
|
ToSelectionType(uint32_t aTextRangeType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-03-04 05:48:26 -08: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;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-04 05:48:26 -08:00
|
|
|
nsresult
|
|
|
|
IMETextTxn::SetSelectionForRanges()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-29 04:43:24 -07:00
|
|
|
nsRefPtr<Selection> selection = mEditor.GetSelection();
|
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
nsresult rv = selection->StartBatchChanges();
|
2014-03-04 05:48:26 -08: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 04:43:24 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
mEditor.GetSelectionController(getter_AddRefs(selCon));
|
|
|
|
NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2014-03-04 05:48:26 -08: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 04:43:24 -07:00
|
|
|
rv = selectionOfIME->RemoveAllRanges();
|
2014-03-04 05:48:26 -08:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Failed to remove all ranges of IME selection");
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-04 05:48:26 -08:00
|
|
|
// Set caret position and selection of IME composition with TextRangeArray.
|
|
|
|
bool setCaret = false;
|
|
|
|
uint32_t countOfRanges = mRanges ? mRanges->Length() : 0;
|
2014-08-29 04:43:24 -07:00
|
|
|
|
2014-06-23 18:06:20 -07:00
|
|
|
#ifdef DEBUG
|
2014-08-29 04:43:24 -07:00
|
|
|
// Bounds-checking on debug builds
|
|
|
|
uint32_t maxOffset = mTextNode->Length();
|
2014-06-23 18:06:20 -07:00
|
|
|
#endif
|
2014-08-29 04:43:24 -07:00
|
|
|
|
2014-06-23 18:06:20 -07:00
|
|
|
// The mStringToInsert may be truncated if maxlength attribute value doesn't
|
2014-08-29 04:43:24 -07:00
|
|
|
// allow input of all text of this composition. So, we can get actual length
|
2014-06-23 18:06:20 -07:00
|
|
|
// of the inserted string from it.
|
|
|
|
uint32_t insertedLength = mStringToInsert.Length();
|
2014-03-04 05:48:26 -08: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-23 18:06:20 -07: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 04:43:24 -07:00
|
|
|
rv = selection->Collapse(mTextNode, caretOffset);
|
2014-03-04 05:48:26 -08:00
|
|
|
setCaret = setCaret || NS_SUCCEEDED(rv);
|
|
|
|
NS_ASSERTION(setCaret, "Failed to collapse normal selection");
|
|
|
|
continue;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
// If the clause length is 0, it should be a bug.
|
2014-03-04 05:48:26 -08:00
|
|
|
if (!textRange.Length()) {
|
|
|
|
NS_WARNING("Any clauses must not be empty");
|
|
|
|
continue;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-04 05:48:26 -08:00
|
|
|
nsRefPtr<nsRange> clauseRange;
|
2014-06-23 18:06:20 -07: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 04:43:24 -07:00
|
|
|
rv = nsRange::CreateRange(mTextNode, startOffset,
|
|
|
|
mTextNode, endOffset,
|
2014-03-04 05:48:26 -08: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-23 18:06:20 -07:00
|
|
|
int32_t caretOffset = static_cast<int32_t>(mOffset + insertedLength);
|
|
|
|
MOZ_ASSERT(caretOffset >= 0 &&
|
|
|
|
static_cast<uint32_t>(caretOffset) <= maxOffset);
|
2014-08-29 04:43:24 -07:00
|
|
|
rv = selection->Collapse(mTextNode, caretOffset);
|
2014-03-04 05:48:26 -08:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Failed to set caret at the end of composition string");
|
|
|
|
}
|
|
|
|
|
2014-08-29 04:43:24 -07:00
|
|
|
rv = selection->EndBatchChanges();
|
2014-03-04 05:48:26 -08:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to end batch changes");
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|