2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-09-26 09:47:45 +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/. */
|
|
|
|
|
2014-03-11 09:08:02 +04:00
|
|
|
#include "ContentEventHandler.h"
|
2012-09-26 09:47:45 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-03-22 04:05:20 +04:00
|
|
|
#include "nsIContent.h"
|
2014-02-12 17:02:56 +04:00
|
|
|
#include "nsIEditor.h"
|
2012-09-26 09:47:45 +04:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsPresContext.h"
|
2014-09-26 04:05:11 +04:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2014-03-08 05:20:07 +04:00
|
|
|
#include "mozilla/IMEStateManager.h"
|
2013-09-25 15:21:19 +04:00
|
|
|
#include "mozilla/MiscEvents.h"
|
2015-05-01 07:49:29 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2014-04-03 08:18:37 +04:00
|
|
|
#include "mozilla/TextComposition.h"
|
2013-09-25 15:21:19 +04:00
|
|
|
#include "mozilla/TextEvents.h"
|
2012-09-26 09:47:45 +04:00
|
|
|
|
2014-02-18 04:00:15 +04:00
|
|
|
using namespace mozilla::widget;
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-09-26 04:05:11 +04:00
|
|
|
#define IDEOGRAPHIC_SPACE (NS_LITERAL_STRING("\x3000"))
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* TextComposition
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
TextComposition::TextComposition(nsPresContext* aPresContext,
|
|
|
|
nsINode* aNode,
|
2014-10-07 14:01:49 +04:00
|
|
|
WidgetCompositionEvent* aCompositionEvent)
|
2014-02-26 04:48:02 +04:00
|
|
|
: mPresContext(aPresContext)
|
|
|
|
, mNode(aNode)
|
2014-10-07 14:01:49 +04:00
|
|
|
, mNativeContext(
|
|
|
|
aCompositionEvent->widget->GetInputContext().mNativeIMEContext)
|
2014-02-26 04:48:02 +04:00
|
|
|
, mCompositionStartOffset(0)
|
|
|
|
, mCompositionTargetOffset(0)
|
2014-10-07 14:01:49 +04:00
|
|
|
, mIsSynthesizedForTests(aCompositionEvent->mFlags.mIsSynthesizedForTests)
|
2014-02-26 04:48:02 +04:00
|
|
|
, mIsComposing(false)
|
|
|
|
, mIsEditorHandlingEvent(false)
|
2014-09-26 04:05:11 +04:00
|
|
|
, mIsRequestingCommit(false)
|
|
|
|
, mIsRequestingCancel(false)
|
|
|
|
, mRequestedToCommitOrCancel(false)
|
2014-09-26 04:05:12 +04:00
|
|
|
, mWasNativeCompositionEndEventDiscarded(false)
|
2015-05-01 07:49:29 +03:00
|
|
|
, mAllowControlCharacters(
|
|
|
|
Preferences::GetBool("dom.compositionevent.allow_control_characters",
|
|
|
|
false))
|
2012-09-26 09:47:45 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-25 10:59:28 +04:00
|
|
|
void
|
|
|
|
TextComposition::Destroy()
|
|
|
|
{
|
|
|
|
mPresContext = nullptr;
|
|
|
|
mNode = nullptr;
|
|
|
|
// TODO: If the editor is still alive and this is held by it, we should tell
|
|
|
|
// this being destroyed for cleaning up the stuff.
|
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
bool
|
|
|
|
TextComposition::MatchesNativeContext(nsIWidget* aWidget) const
|
|
|
|
{
|
2012-10-27 03:35:20 +04:00
|
|
|
return mNativeContext == aWidget->GetInputContext().mNativeIMEContext;
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
|
2014-11-25 08:02:29 +03:00
|
|
|
bool
|
|
|
|
TextComposition::IsValidStateForComposition(nsIWidget* aWidget) const
|
|
|
|
{
|
|
|
|
return !Destroyed() && aWidget && !aWidget->Destroyed() &&
|
|
|
|
mPresContext->GetPresShell() &&
|
|
|
|
!mPresContext->GetPresShell()->IsDestroying();
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:05:12 +04:00
|
|
|
bool
|
2014-10-07 14:01:48 +04:00
|
|
|
TextComposition::MaybeDispatchCompositionUpdate(
|
2014-10-07 14:01:49 +04:00
|
|
|
const WidgetCompositionEvent* aCompositionEvent)
|
2014-09-26 04:05:12 +04:00
|
|
|
{
|
2014-11-25 08:02:29 +03:00
|
|
|
if (!IsValidStateForComposition(aCompositionEvent->widget)) {
|
2014-09-26 04:05:12 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-07 14:01:49 +04:00
|
|
|
if (mLastData == aCompositionEvent->mData) {
|
2014-09-26 04:05:12 +04:00
|
|
|
return true;
|
|
|
|
}
|
2014-11-25 08:02:30 +03:00
|
|
|
CloneAndDispatchAs(aCompositionEvent, NS_COMPOSITION_UPDATE);
|
|
|
|
return IsValidStateForComposition(aCompositionEvent->widget);
|
|
|
|
}
|
2014-09-26 04:05:12 +04:00
|
|
|
|
2014-11-25 08:02:31 +03:00
|
|
|
BaseEventFlags
|
2014-11-25 08:02:30 +03:00
|
|
|
TextComposition::CloneAndDispatchAs(
|
|
|
|
const WidgetCompositionEvent* aCompositionEvent,
|
2014-11-25 08:02:31 +03:00
|
|
|
uint32_t aMessage,
|
|
|
|
nsEventStatus* aStatus,
|
|
|
|
EventDispatchingCallback* aCallBack)
|
2014-11-25 08:02:30 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsValidStateForComposition(aCompositionEvent->widget),
|
|
|
|
"Should be called only when it's safe to dispatch an event");
|
|
|
|
|
|
|
|
WidgetCompositionEvent compositionEvent(aCompositionEvent->mFlags.mIsTrusted,
|
|
|
|
aMessage, aCompositionEvent->widget);
|
|
|
|
compositionEvent.time = aCompositionEvent->time;
|
|
|
|
compositionEvent.timeStamp = aCompositionEvent->timeStamp;
|
|
|
|
compositionEvent.mData = aCompositionEvent->mData;
|
|
|
|
compositionEvent.mFlags.mIsSynthesizedForTests =
|
2014-10-07 14:01:49 +04:00
|
|
|
aCompositionEvent->mFlags.mIsSynthesizedForTests;
|
2014-09-26 04:05:12 +04:00
|
|
|
|
2014-11-25 08:02:31 +03:00
|
|
|
nsEventStatus dummyStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
nsEventStatus* status = aStatus ? aStatus : &dummyStatus;
|
2014-11-25 08:02:30 +03:00
|
|
|
if (aMessage == NS_COMPOSITION_UPDATE) {
|
|
|
|
mLastData = compositionEvent.mData;
|
|
|
|
}
|
2014-10-03 10:33:47 +04:00
|
|
|
EventDispatcher::Dispatch(mNode, mPresContext,
|
2014-11-25 08:02:31 +03:00
|
|
|
&compositionEvent, nullptr, status, aCallBack);
|
|
|
|
return compositionEvent.mFlags;
|
2014-09-26 04:05:12 +04:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:05:12 +04:00
|
|
|
void
|
2014-10-07 14:01:49 +04:00
|
|
|
TextComposition::OnCompositionEventDiscarded(
|
|
|
|
const WidgetCompositionEvent* aCompositionEvent)
|
2014-09-26 04:05:12 +04:00
|
|
|
{
|
|
|
|
// Note that this method is never called for synthesized events for emulating
|
|
|
|
// commit or cancel composition.
|
|
|
|
|
2014-10-07 14:01:49 +04:00
|
|
|
MOZ_ASSERT(aCompositionEvent->mFlags.mIsTrusted,
|
2014-09-26 04:05:12 +04:00
|
|
|
"Shouldn't be called with untrusted event");
|
|
|
|
|
|
|
|
// XXX If composition events are discarded, should we dispatch them with
|
|
|
|
// runnable event? However, even if we do so, it might make native IME
|
|
|
|
// confused due to async modification. Especially when native IME is
|
|
|
|
// TSF.
|
2014-11-25 08:02:30 +03:00
|
|
|
if (!aCompositionEvent->CausesDOMCompositionEndEvent()) {
|
2014-09-26 04:05:12 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWasNativeCompositionEndEventDiscarded = true;
|
|
|
|
}
|
|
|
|
|
2015-05-01 07:49:29 +03:00
|
|
|
static inline bool
|
|
|
|
IsControlChar(uint32_t aCharCode)
|
|
|
|
{
|
|
|
|
return aCharCode < ' ' || aCharCode == 0x7F;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
FindFirstControlCharacter(const nsAString& aStr)
|
|
|
|
{
|
|
|
|
const char16_t* sourceBegin = aStr.BeginReading();
|
|
|
|
const char16_t* sourceEnd = aStr.EndReading();
|
|
|
|
|
|
|
|
for (const char16_t* source = sourceBegin; source < sourceEnd; ++source) {
|
|
|
|
if (*source != '\t' && IsControlChar(*source)) {
|
|
|
|
return source - sourceBegin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
RemoveControlCharactersFrom(nsAString& aStr, TextRangeArray* aRanges)
|
|
|
|
{
|
|
|
|
size_t firstControlCharOffset = FindFirstControlCharacter(aStr);
|
|
|
|
if (firstControlCharOffset == (size_t)-1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString copy(aStr);
|
|
|
|
const char16_t* sourceBegin = copy.BeginReading();
|
|
|
|
const char16_t* sourceEnd = copy.EndReading();
|
|
|
|
|
|
|
|
char16_t* dest = aStr.BeginWriting();
|
|
|
|
if (NS_WARN_IF(!dest)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char16_t* curDest = dest + firstControlCharOffset;
|
|
|
|
size_t i = firstControlCharOffset;
|
|
|
|
for (const char16_t* source = sourceBegin + firstControlCharOffset;
|
|
|
|
source < sourceEnd; ++source) {
|
|
|
|
if (*source == '\t' || !IsControlChar(*source)) {
|
|
|
|
*curDest = *source;
|
|
|
|
++curDest;
|
|
|
|
++i;
|
|
|
|
} else if (aRanges) {
|
|
|
|
aRanges->RemoveCharacter(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aStr.SetLength(curDest - dest);
|
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
void
|
2014-10-07 14:01:49 +04:00
|
|
|
TextComposition::DispatchCompositionEvent(
|
|
|
|
WidgetCompositionEvent* aCompositionEvent,
|
|
|
|
nsEventStatus* aStatus,
|
|
|
|
EventDispatchingCallback* aCallBack,
|
|
|
|
bool aIsSynthesized)
|
2012-09-26 09:47:45 +04:00
|
|
|
{
|
2015-05-01 07:49:29 +03:00
|
|
|
if (!mAllowControlCharacters) {
|
|
|
|
RemoveControlCharactersFrom(aCompositionEvent->mData,
|
|
|
|
aCompositionEvent->mRanges);
|
|
|
|
}
|
2014-11-25 08:02:31 +03:00
|
|
|
if (aCompositionEvent->message == NS_COMPOSITION_COMMIT_AS_IS) {
|
|
|
|
NS_ASSERTION(!aCompositionEvent->mRanges,
|
|
|
|
"mRanges of NS_COMPOSITION_COMMIT_AS_IS should be null");
|
|
|
|
aCompositionEvent->mRanges = nullptr;
|
|
|
|
NS_ASSERTION(aCompositionEvent->mData.IsEmpty(),
|
|
|
|
"mData of NS_COMPOSITION_COMMIT_AS_IS should be empty string");
|
|
|
|
if (mLastData == IDEOGRAPHIC_SPACE) {
|
|
|
|
// If the last data is an ideographic space (FullWidth space), it must be
|
|
|
|
// a placeholder character of some Chinese IME. So, committing with
|
|
|
|
// this data must not be expected by users. Let's use empty string.
|
|
|
|
aCompositionEvent->mData.Truncate();
|
|
|
|
} else {
|
|
|
|
aCompositionEvent->mData = mLastData;
|
|
|
|
}
|
2014-11-25 08:02:32 +03:00
|
|
|
} else if (aCompositionEvent->message == NS_COMPOSITION_COMMIT) {
|
|
|
|
NS_ASSERTION(!aCompositionEvent->mRanges,
|
|
|
|
"mRanges of NS_COMPOSITION_COMMIT should be null");
|
|
|
|
aCompositionEvent->mRanges = nullptr;
|
2014-11-25 08:02:31 +03:00
|
|
|
}
|
|
|
|
|
2014-11-25 08:02:29 +03:00
|
|
|
if (!IsValidStateForComposition(aCompositionEvent->widget)) {
|
2014-09-26 04:05:11 +04:00
|
|
|
*aStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this instance has requested to commit or cancel composition but
|
|
|
|
// is not synthesizing commit event, that means that the IME commits or
|
|
|
|
// cancels the composition asynchronously. Typically, iBus behaves so.
|
|
|
|
// Then, synthesized events which were dispatched immediately after
|
|
|
|
// the request has already committed our editor's composition string and
|
|
|
|
// told it to web apps. Therefore, we should ignore the delayed events.
|
|
|
|
if (mRequestedToCommitOrCancel && !aIsSynthesized) {
|
|
|
|
*aStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-26 04:05:12 +04:00
|
|
|
// IME may commit composition with empty string for a commit request or
|
|
|
|
// with non-empty string for a cancel request. We should prevent such
|
|
|
|
// unexpected result. E.g., web apps may be confused if they implement
|
|
|
|
// autocomplete which attempts to commit composition forcibly when the user
|
|
|
|
// selects one of suggestions but composition string is cleared by IME.
|
|
|
|
// Note that most Chinese IMEs don't expose actual composition string to us.
|
|
|
|
// They typically tell us an IDEOGRAPHIC SPACE or empty string as composition
|
|
|
|
// string. Therefore, we should hack it only when:
|
|
|
|
// 1. committing string is empty string at requesting commit but the last
|
|
|
|
// data isn't IDEOGRAPHIC SPACE.
|
|
|
|
// 2. non-empty string is committed at requesting cancel.
|
|
|
|
if (!aIsSynthesized && (mIsRequestingCommit || mIsRequestingCancel)) {
|
|
|
|
nsString* committingData = nullptr;
|
2014-10-07 14:01:49 +04:00
|
|
|
switch (aCompositionEvent->message) {
|
2014-09-26 04:05:12 +04:00
|
|
|
case NS_COMPOSITION_END:
|
2014-10-07 14:01:47 +04:00
|
|
|
case NS_COMPOSITION_CHANGE:
|
2014-11-25 08:02:31 +03:00
|
|
|
case NS_COMPOSITION_COMMIT_AS_IS:
|
2014-11-25 08:02:32 +03:00
|
|
|
case NS_COMPOSITION_COMMIT:
|
2014-10-07 14:01:49 +04:00
|
|
|
committingData = &aCompositionEvent->mData;
|
2014-09-26 04:05:12 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Unexpected event comes during committing or "
|
|
|
|
"canceling composition");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (committingData) {
|
|
|
|
if (mIsRequestingCommit && committingData->IsEmpty() &&
|
|
|
|
mLastData != IDEOGRAPHIC_SPACE) {
|
|
|
|
committingData->Assign(mLastData);
|
|
|
|
} else if (mIsRequestingCancel && !committingData->IsEmpty()) {
|
|
|
|
committingData->Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 08:02:31 +03:00
|
|
|
bool dispatchEvent = true;
|
|
|
|
bool dispatchDOMTextEvent = aCompositionEvent->CausesDOMTextEvent();
|
|
|
|
|
|
|
|
// When mIsComposing is false but the committing string is different from
|
|
|
|
// the last data (E.g., previous NS_COMPOSITION_CHANGE event made the
|
|
|
|
// composition string empty or didn't have clause information), we don't
|
|
|
|
// need to dispatch redundant DOM text event.
|
|
|
|
if (dispatchDOMTextEvent &&
|
|
|
|
aCompositionEvent->message != NS_COMPOSITION_CHANGE &&
|
|
|
|
!mIsComposing && mLastData == aCompositionEvent->mData) {
|
|
|
|
dispatchEvent = dispatchDOMTextEvent = false;
|
|
|
|
}
|
|
|
|
|
2015-02-11 06:20:02 +03:00
|
|
|
// widget may dispatch redundant NS_COMPOSITION_CHANGE event
|
|
|
|
// which modifies neither composition string, clauses nor caret
|
|
|
|
// position. In such case, we shouldn't dispatch DOM events.
|
|
|
|
if (dispatchDOMTextEvent &&
|
|
|
|
aCompositionEvent->message == NS_COMPOSITION_CHANGE &&
|
|
|
|
mLastData == aCompositionEvent->mData &&
|
|
|
|
mRanges && aCompositionEvent->mRanges &&
|
|
|
|
mRanges->Equals(*aCompositionEvent->mRanges)) {
|
|
|
|
dispatchEvent = dispatchDOMTextEvent = false;
|
|
|
|
}
|
|
|
|
|
2014-11-25 08:02:31 +03:00
|
|
|
if (dispatchDOMTextEvent) {
|
2014-10-07 14:01:49 +04:00
|
|
|
if (!MaybeDispatchCompositionUpdate(aCompositionEvent)) {
|
2014-10-03 10:33:47 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
|
|
|
|
2014-11-25 08:02:31 +03:00
|
|
|
if (dispatchEvent) {
|
|
|
|
// If the composition event should cause a DOM text event, we should
|
|
|
|
// overwrite the event message as NS_COMPOSITION_CHANGE because due to
|
|
|
|
// the limitation of mapping between event messages and DOM event types,
|
|
|
|
// we cannot map multiple event messages to a DOM event type.
|
|
|
|
if (dispatchDOMTextEvent &&
|
|
|
|
aCompositionEvent->message != NS_COMPOSITION_CHANGE) {
|
|
|
|
aCompositionEvent->mFlags =
|
|
|
|
CloneAndDispatchAs(aCompositionEvent, NS_COMPOSITION_CHANGE,
|
|
|
|
aStatus, aCallBack);
|
|
|
|
} else {
|
|
|
|
EventDispatcher::Dispatch(mNode, mPresContext,
|
|
|
|
aCompositionEvent, nullptr, aStatus, aCallBack);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*aStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2013-11-07 04:11:11 +04:00
|
|
|
|
2014-11-25 08:02:29 +03:00
|
|
|
if (!IsValidStateForComposition(aCompositionEvent->widget)) {
|
2014-02-25 10:59:28 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-07 14:01:47 +04:00
|
|
|
// Emulate editor behavior of compositionchange event (DOM text event) handler
|
|
|
|
// if no editor handles composition events.
|
2014-11-25 08:02:31 +03:00
|
|
|
if (dispatchDOMTextEvent && !HasEditor()) {
|
2014-10-07 14:01:49 +04:00
|
|
|
EditorWillHandleCompositionChangeEvent(aCompositionEvent);
|
2014-10-07 14:01:49 +04:00
|
|
|
EditorDidHandleCompositionChangeEvent();
|
2014-02-12 17:02:56 +04:00
|
|
|
}
|
|
|
|
|
2014-11-25 08:02:31 +03:00
|
|
|
if (aCompositionEvent->CausesDOMCompositionEndEvent()) {
|
|
|
|
// Dispatch a compositionend event if it's necessary.
|
|
|
|
if (aCompositionEvent->message != NS_COMPOSITION_END) {
|
|
|
|
CloneAndDispatchAs(aCompositionEvent, NS_COMPOSITION_END);
|
|
|
|
}
|
2014-02-12 17:02:56 +04:00
|
|
|
MOZ_ASSERT(!mIsComposing, "Why is the editor still composing?");
|
|
|
|
MOZ_ASSERT(!HasEditor(), "Why does the editor still keep to hold this?");
|
|
|
|
}
|
2014-02-12 17:02:56 +04:00
|
|
|
|
2013-11-07 04:11:11 +04:00
|
|
|
// Notify composition update to widget if possible
|
2014-10-07 14:01:49 +04:00
|
|
|
NotityUpdateComposition(aCompositionEvent);
|
2013-11-07 04:11:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-10-07 14:01:49 +04:00
|
|
|
TextComposition::NotityUpdateComposition(
|
|
|
|
const WidgetCompositionEvent* aCompositionEvent)
|
2013-11-07 04:11:11 +04:00
|
|
|
{
|
|
|
|
nsEventStatus status;
|
|
|
|
|
|
|
|
// When compositon start, notify the rect of first offset character.
|
|
|
|
// When not compositon start, notify the rect of selected composition
|
2014-10-07 14:01:47 +04:00
|
|
|
// string if compositionchange event.
|
2014-10-07 14:01:49 +04:00
|
|
|
if (aCompositionEvent->message == NS_COMPOSITION_START) {
|
2013-11-07 04:11:11 +04:00
|
|
|
nsCOMPtr<nsIWidget> widget = mPresContext->GetRootWidget();
|
|
|
|
// Update composition start offset
|
|
|
|
WidgetQueryContentEvent selectedTextEvent(true,
|
|
|
|
NS_QUERY_SELECTED_TEXT,
|
|
|
|
widget);
|
|
|
|
widget->DispatchEvent(&selectedTextEvent, status);
|
|
|
|
if (selectedTextEvent.mSucceeded) {
|
|
|
|
mCompositionStartOffset = selectedTextEvent.mReply.mOffset;
|
|
|
|
} else {
|
|
|
|
// Unknown offset
|
|
|
|
NS_WARNING("Cannot get start offset of IME composition");
|
|
|
|
mCompositionStartOffset = 0;
|
|
|
|
}
|
|
|
|
mCompositionTargetOffset = mCompositionStartOffset;
|
2014-11-25 08:02:30 +03:00
|
|
|
} else if (aCompositionEvent->CausesDOMTextEvent()) {
|
2014-03-04 17:48:26 +04:00
|
|
|
mCompositionTargetOffset =
|
2014-10-07 14:01:49 +04:00
|
|
|
mCompositionStartOffset + aCompositionEvent->TargetClauseOffset();
|
2014-10-07 14:01:48 +04:00
|
|
|
} else {
|
|
|
|
return;
|
2013-11-07 04:11:11 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 04:00:15 +04:00
|
|
|
NotifyIME(NOTIFY_IME_OF_COMPOSITION_UPDATE);
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:51 +04:00
|
|
|
void
|
2014-02-18 18:47:55 +04:00
|
|
|
TextComposition::DispatchCompositionEventRunnable(uint32_t aEventMessage,
|
2014-09-26 04:05:11 +04:00
|
|
|
const nsAString& aData,
|
|
|
|
bool aIsSynthesizingCommit)
|
2012-09-26 09:47:51 +04:00
|
|
|
{
|
|
|
|
nsContentUtils::AddScriptRunner(
|
2014-09-26 04:05:11 +04:00
|
|
|
new CompositionEventDispatcher(this, mNode, aEventMessage, aData,
|
|
|
|
aIsSynthesizingCommit));
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:05:11 +04:00
|
|
|
nsresult
|
|
|
|
TextComposition::RequestToCommit(nsIWidget* aWidget, bool aDiscard)
|
2012-09-26 09:47:51 +04:00
|
|
|
{
|
2014-09-26 04:05:11 +04:00
|
|
|
// If this composition is already requested to be committed or canceled,
|
|
|
|
// we don't need to request it again because even if the first request
|
|
|
|
// failed, new request won't success, probably. And we shouldn't synthesize
|
|
|
|
// events for committing or canceling composition twice or more times.
|
|
|
|
if (mRequestedToCommitOrCancel) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-28 12:19:29 +04:00
|
|
|
nsRefPtr<TextComposition> kungFuDeathGrip(this);
|
2014-09-26 04:05:12 +04:00
|
|
|
const nsAutoString lastData(mLastData);
|
2014-09-26 04:05:11 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
AutoRestore<bool> saveRequestingCancel(mIsRequestingCancel);
|
|
|
|
AutoRestore<bool> saveRequestingCommit(mIsRequestingCommit);
|
|
|
|
if (aDiscard) {
|
|
|
|
mIsRequestingCancel = true;
|
|
|
|
mIsRequestingCommit = false;
|
|
|
|
} else {
|
|
|
|
mIsRequestingCancel = false;
|
|
|
|
mIsRequestingCommit = true;
|
|
|
|
}
|
2015-01-28 09:27:31 +03:00
|
|
|
// FYI: CompositionEvents caused by a call of NotifyIME() may be
|
|
|
|
// discarded by PresShell if it's not safe to dispatch the event.
|
|
|
|
nsresult rv =
|
|
|
|
aWidget->NotifyIME(IMENotification(aDiscard ?
|
|
|
|
REQUEST_TO_CANCEL_COMPOSITION :
|
|
|
|
REQUEST_TO_COMMIT_COMPOSITION));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2014-09-26 04:05:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mRequestedToCommitOrCancel = true;
|
|
|
|
|
|
|
|
// If the request is performed synchronously, this must be already destroyed.
|
|
|
|
if (Destroyed()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, synthesize the commit in content.
|
2014-09-26 04:05:12 +04:00
|
|
|
nsAutoString data(aDiscard ? EmptyString() : lastData);
|
2014-11-25 08:02:31 +03:00
|
|
|
if (data == mLastData) {
|
|
|
|
DispatchCompositionEventRunnable(NS_COMPOSITION_COMMIT_AS_IS, EmptyString(),
|
|
|
|
true);
|
2014-11-25 08:02:32 +03:00
|
|
|
} else {
|
|
|
|
DispatchCompositionEventRunnable(NS_COMPOSITION_COMMIT, data, true);
|
2014-09-26 04:05:11 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:51 +04:00
|
|
|
nsresult
|
2014-02-18 04:00:15 +04:00
|
|
|
TextComposition::NotifyIME(IMEMessage aMessage)
|
2012-09-26 09:47:51 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mPresContext, NS_ERROR_NOT_AVAILABLE);
|
2014-03-08 05:20:07 +04:00
|
|
|
return IMEStateManager::NotifyIME(aMessage, mPresContext);
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
|
|
|
|
2014-02-12 17:02:56 +04:00
|
|
|
void
|
2014-10-07 14:01:49 +04:00
|
|
|
TextComposition::EditorWillHandleCompositionChangeEvent(
|
2014-10-07 14:01:49 +04:00
|
|
|
const WidgetCompositionEvent* aCompositionChangeEvent)
|
2014-02-12 17:02:56 +04:00
|
|
|
{
|
2014-10-07 14:01:49 +04:00
|
|
|
mIsComposing = aCompositionChangeEvent->IsComposing();
|
|
|
|
mRanges = aCompositionChangeEvent->mRanges;
|
2014-02-26 04:48:02 +04:00
|
|
|
mIsEditorHandlingEvent = true;
|
2014-02-12 17:02:56 +04:00
|
|
|
|
2014-10-07 14:01:49 +04:00
|
|
|
MOZ_ASSERT(mLastData == aCompositionChangeEvent->mData,
|
2014-10-07 14:01:47 +04:00
|
|
|
"The text of a compositionchange event must be same as previous data "
|
|
|
|
"attribute value of the latest compositionupdate event");
|
2014-02-12 17:02:56 +04:00
|
|
|
}
|
|
|
|
|
2015-06-04 20:06:09 +03:00
|
|
|
void
|
|
|
|
TextComposition::OnEditorDestroyed()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIsEditorHandlingEvent,
|
|
|
|
"The editor should have stopped listening events");
|
|
|
|
nsCOMPtr<nsIWidget> widget = GetWidget();
|
|
|
|
if (NS_WARN_IF(!widget)) {
|
|
|
|
// XXX If this could happen, how do we notify IME of destroying the editor?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to cancel the composition.
|
|
|
|
RequestToCommit(widget, true);
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:02:56 +04:00
|
|
|
void
|
2014-10-07 14:01:49 +04:00
|
|
|
TextComposition::EditorDidHandleCompositionChangeEvent()
|
2014-02-12 17:02:56 +04:00
|
|
|
{
|
|
|
|
mString = mLastData;
|
2014-02-26 04:48:02 +04:00
|
|
|
mIsEditorHandlingEvent = false;
|
2014-02-12 17:02:56 +04:00
|
|
|
}
|
|
|
|
|
2014-02-12 17:02:56 +04:00
|
|
|
void
|
|
|
|
TextComposition::StartHandlingComposition(nsIEditor* aEditor)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!HasEditor(), "There is a handling editor already");
|
|
|
|
mEditorWeak = do_GetWeakReference(aEditor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextComposition::EndHandlingComposition(nsIEditor* aEditor)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCOMPtr<nsIEditor> editor = GetEditor();
|
|
|
|
MOZ_ASSERT(editor == aEditor, "Another editor handled the composition?");
|
|
|
|
#endif // #ifdef DEBUG
|
|
|
|
mEditorWeak = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIEditor>
|
|
|
|
TextComposition::GetEditor() const
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEditor> editor = do_QueryReferent(mEditorWeak);
|
|
|
|
return editor.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TextComposition::HasEditor() const
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEditor> editor = GetEditor();
|
|
|
|
return !!editor;
|
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:51 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* TextComposition::CompositionEventDispatcher
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
TextComposition::CompositionEventDispatcher::CompositionEventDispatcher(
|
2014-09-26 04:05:11 +04:00
|
|
|
TextComposition* aComposition,
|
2012-09-26 09:47:51 +04:00
|
|
|
nsINode* aEventTarget,
|
|
|
|
uint32_t aEventMessage,
|
2014-09-26 04:05:11 +04:00
|
|
|
const nsAString& aData,
|
|
|
|
bool aIsSynthesizedEvent)
|
|
|
|
: mTextComposition(aComposition)
|
|
|
|
, mEventTarget(aEventTarget)
|
|
|
|
, mEventMessage(aEventMessage)
|
|
|
|
, mData(aData)
|
|
|
|
, mIsSynthesizedEvent(aIsSynthesizedEvent)
|
2012-09-26 09:47:51 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TextComposition::CompositionEventDispatcher::Run()
|
|
|
|
{
|
2014-09-26 04:05:11 +04:00
|
|
|
// The widget can be different from the widget which has dispatched
|
|
|
|
// composition events because GetWidget() returns a widget which is proper
|
|
|
|
// for calling NotifyIME(). However, this must no be problem since both
|
|
|
|
// widget should share native IME context. Therefore, even if an event
|
|
|
|
// handler uses the widget for requesting IME to commit or cancel, it works.
|
|
|
|
nsCOMPtr<nsIWidget> widget(mTextComposition->GetWidget());
|
2014-11-25 08:02:29 +03:00
|
|
|
if (!mTextComposition->IsValidStateForComposition(widget)) {
|
2012-09-26 09:47:51 +04:00
|
|
|
return NS_OK; // cannot dispatch any events anymore
|
|
|
|
}
|
|
|
|
|
2014-11-25 08:02:29 +03:00
|
|
|
nsRefPtr<nsPresContext> presContext = mTextComposition->mPresContext;
|
2012-09-26 09:47:51 +04:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
switch (mEventMessage) {
|
|
|
|
case NS_COMPOSITION_START: {
|
2014-09-26 04:05:11 +04:00
|
|
|
WidgetCompositionEvent compStart(true, NS_COMPOSITION_START, widget);
|
2013-10-01 11:23:00 +04:00
|
|
|
WidgetQueryContentEvent selectedText(true, NS_QUERY_SELECTED_TEXT,
|
2014-09-26 04:05:11 +04:00
|
|
|
widget);
|
|
|
|
ContentEventHandler handler(presContext);
|
2012-09-26 09:47:51 +04:00
|
|
|
handler.OnQuerySelectedText(&selectedText);
|
|
|
|
NS_ASSERTION(selectedText.mSucceeded, "Failed to get selected text");
|
2014-10-07 14:01:46 +04:00
|
|
|
compStart.mData = selectedText.mReply.mString;
|
2014-09-26 04:05:12 +04:00
|
|
|
compStart.mFlags.mIsSynthesizedForTests =
|
|
|
|
mTextComposition->IsSynthesizedForTests();
|
2014-09-26 04:05:11 +04:00
|
|
|
IMEStateManager::DispatchCompositionEvent(mEventTarget, presContext,
|
|
|
|
&compStart, &status, nullptr,
|
|
|
|
mIsSynthesizedEvent);
|
2012-09-26 09:47:51 +04:00
|
|
|
break;
|
|
|
|
}
|
2014-11-25 08:02:31 +03:00
|
|
|
case NS_COMPOSITION_CHANGE:
|
2014-11-25 08:02:32 +03:00
|
|
|
case NS_COMPOSITION_COMMIT_AS_IS:
|
|
|
|
case NS_COMPOSITION_COMMIT: {
|
2014-09-26 04:05:11 +04:00
|
|
|
WidgetCompositionEvent compEvent(true, mEventMessage, widget);
|
2014-11-25 08:02:31 +03:00
|
|
|
if (mEventMessage != NS_COMPOSITION_COMMIT_AS_IS) {
|
|
|
|
compEvent.mData = mData;
|
|
|
|
}
|
2014-09-26 04:05:12 +04:00
|
|
|
compEvent.mFlags.mIsSynthesizedForTests =
|
|
|
|
mTextComposition->IsSynthesizedForTests();
|
2014-09-26 04:05:11 +04:00
|
|
|
IMEStateManager::DispatchCompositionEvent(mEventTarget, presContext,
|
|
|
|
&compEvent, &status, nullptr,
|
|
|
|
mIsSynthesizedEvent);
|
2012-09-26 09:47:51 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("Unsupported event");
|
2012-09-26 09:47:51 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* TextCompositionArray
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
TextCompositionArray::index_type
|
|
|
|
TextCompositionArray::IndexOf(nsIWidget* aWidget)
|
|
|
|
{
|
|
|
|
for (index_type i = Length(); i > 0; --i) {
|
2014-01-28 12:19:29 +04:00
|
|
|
if (ElementAt(i - 1)->MatchesNativeContext(aWidget)) {
|
2012-09-26 09:47:45 +04:00
|
|
|
return i - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextCompositionArray::index_type
|
|
|
|
TextCompositionArray::IndexOf(nsPresContext* aPresContext)
|
|
|
|
{
|
|
|
|
for (index_type i = Length(); i > 0; --i) {
|
2014-01-28 12:19:29 +04:00
|
|
|
if (ElementAt(i - 1)->GetPresContext() == aPresContext) {
|
2012-09-26 09:47:45 +04:00
|
|
|
return i - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextCompositionArray::index_type
|
|
|
|
TextCompositionArray::IndexOf(nsPresContext* aPresContext,
|
|
|
|
nsINode* aNode)
|
|
|
|
{
|
|
|
|
index_type index = IndexOf(aPresContext);
|
|
|
|
if (index == NoIndex) {
|
|
|
|
return NoIndex;
|
|
|
|
}
|
2014-01-28 12:19:29 +04:00
|
|
|
nsINode* node = ElementAt(index)->GetEventTargetNode();
|
2012-09-26 09:47:45 +04:00
|
|
|
return node == aNode ? index : NoIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextComposition*
|
|
|
|
TextCompositionArray::GetCompositionFor(nsIWidget* aWidget)
|
|
|
|
{
|
|
|
|
index_type i = IndexOf(aWidget);
|
2014-01-28 12:19:29 +04:00
|
|
|
return i != NoIndex ? ElementAt(i) : nullptr;
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TextComposition*
|
|
|
|
TextCompositionArray::GetCompositionFor(nsPresContext* aPresContext,
|
|
|
|
nsINode* aNode)
|
|
|
|
{
|
|
|
|
index_type i = IndexOf(aPresContext, aNode);
|
2014-01-28 12:19:29 +04:00
|
|
|
return i != NoIndex ? ElementAt(i) : nullptr;
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TextComposition*
|
|
|
|
TextCompositionArray::GetCompositionInContent(nsPresContext* aPresContext,
|
|
|
|
nsIContent* aContent)
|
|
|
|
{
|
|
|
|
// There should be only one composition per content object.
|
|
|
|
for (index_type i = Length(); i > 0; --i) {
|
2014-01-28 12:19:29 +04:00
|
|
|
nsINode* node = ElementAt(i - 1)->GetEventTargetNode();
|
2012-09-26 09:47:45 +04:00
|
|
|
if (node && nsContentUtils::ContentIsDescendantOf(node, aContent)) {
|
2014-01-28 12:19:29 +04:00
|
|
|
return ElementAt(i - 1);
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|