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"
|
Bug 1275906 part.2 TextComposition should use IMEContentObserver for sending NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED if the editor which has the composition is in the active IMEContentObserver r=smaug
For sending NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED after the other change notifications which was caused by the user input, we need to use IMEContentObserver::IMENotificationSender because it sends the notifications when it's safe to do it.
This patch makes TextComposition use IMEContentObserver to send the notification. However, if there is no active IMEContentObserver, e.g., composition events are fired on unfocused window, TextComposition sends it by itself (same as current implementation).
If IMEContentObserver stops observing when it has pending NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED, it cannot send the notification (i.e., it is discarded completely in such case). However, in such case, IMEContentObserver sends NOTIFY_IME_OF_BLUR. So, anyway, native IME handler should treat the blur notification as it including NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED.
On the other hand, we're buggy if composition events are fired in non-active window. Even in such case, IMEContentObserver should be created for active editor in each document and it notifies IME of the changes. But this is out of the scope of this bug.
MozReview-Commit-ID: 7Q0ZsJTh4hX
--HG--
extra : rebase_source : 6417f991fa8c0fbe3f25b27bacf4257e5485aecc
2016-06-01 16:14:41 +03:00
|
|
|
#include "IMEContentObserver.h"
|
|
|
|
#include "IMEStateManager.h"
|
2012-09-26 09:47:45 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-03-22 04:05:20 +04:00
|
|
|
#include "nsIContent.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"
|
2017-06-20 13:23:44 +03:00
|
|
|
#include "mozilla/EditorBase.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"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-07-03 05:49:36 +03:00
|
|
|
#include "mozilla/dom/TabParent.h"
|
2012-09-26 09:47:45 +04:00
|
|
|
|
2015-12-29 16:57:38 +03:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// Some defiens will be conflict with OSX SDK
|
|
|
|
#define TextRange _TextRange
|
|
|
|
#define TextRangeArray _TextRangeArray
|
|
|
|
#define Comment _Comment
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "nsPluginInstanceOwner.h"
|
|
|
|
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#undef TextRange
|
|
|
|
#undef TextRangeArray
|
|
|
|
#undef Comment
|
|
|
|
#endif
|
|
|
|
|
2014-02-18 04:00:15 +04:00
|
|
|
using namespace mozilla::widget;
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2016-07-20 07:07:53 +03:00
|
|
|
#define IDEOGRAPHIC_SPACE (NS_LITERAL_STRING(u"\x3000"))
|
2014-09-26 04:05:11 +04:00
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* TextComposition
|
|
|
|
******************************************************************************/
|
|
|
|
|
2015-07-17 05:25:00 +03:00
|
|
|
bool TextComposition::sHandlingSelectionEvent = false;
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
TextComposition::TextComposition(nsPresContext* aPresContext,
|
|
|
|
nsINode* aNode,
|
2015-07-03 05:49:36 +03:00
|
|
|
TabParent* aTabParent,
|
2014-10-07 14:01:49 +04:00
|
|
|
WidgetCompositionEvent* aCompositionEvent)
|
2014-02-26 04:48:02 +04:00
|
|
|
: mPresContext(aPresContext)
|
|
|
|
, mNode(aNode)
|
2015-07-03 05:49:36 +03:00
|
|
|
, mTabParent(aTabParent)
|
2015-12-11 09:15:57 +03:00
|
|
|
, mNativeContext(aCompositionEvent->mNativeIMEContext)
|
2014-02-26 04:48:02 +04:00
|
|
|
, mCompositionStartOffset(0)
|
2016-06-02 08:26:47 +03:00
|
|
|
, mTargetClauseOffsetInComposition(0)
|
2017-12-15 07:14:51 +03:00
|
|
|
, mCompositionStartOffsetInTextNode(UINT32_MAX)
|
|
|
|
, mCompositionLengthInTextNode(UINT32_MAX)
|
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)
|
2017-11-20 16:59:04 +03:00
|
|
|
, mHasReceivedCommitEvent(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))
|
2016-06-10 13:32:49 +03:00
|
|
|
, mWasCompositionStringEmpty(true)
|
2012-09-26 09:47:45 +04:00
|
|
|
{
|
2015-12-11 09:15:57 +03:00
|
|
|
MOZ_ASSERT(aCompositionEvent->mNativeIMEContext.IsValid());
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
|
2014-02-25 10:59:28 +04:00
|
|
|
void
|
|
|
|
TextComposition::Destroy()
|
|
|
|
{
|
|
|
|
mPresContext = nullptr;
|
|
|
|
mNode = nullptr;
|
2017-06-27 16:02:07 +03:00
|
|
|
mTabParent = nullptr;
|
2017-12-15 07:14:51 +03:00
|
|
|
mContainerTextNode = nullptr;
|
|
|
|
mCompositionStartOffsetInTextNode = UINT32_MAX;
|
|
|
|
mCompositionLengthInTextNode = UINT32_MAX;
|
2014-02-25 10:59:28 +04:00
|
|
|
// TODO: If the editor is still alive and this is held by it, we should tell
|
|
|
|
// this being destroyed for cleaning up the stuff.
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-07-03 05:49:36 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mTabParent);
|
|
|
|
|
2016-04-14 11:03:14 +03:00
|
|
|
if (!IsValidStateForComposition(aCompositionEvent->mWidget)) {
|
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;
|
|
|
|
}
|
2015-09-11 15:21:27 +03:00
|
|
|
CloneAndDispatchAs(aCompositionEvent, eCompositionUpdate);
|
2016-04-14 11:03:14 +03:00
|
|
|
return IsValidStateForComposition(aCompositionEvent->mWidget);
|
2014-11-25 08:02:30 +03:00
|
|
|
}
|
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,
|
2015-08-26 15:56:59 +03:00
|
|
|
EventMessage aMessage,
|
2014-11-25 08:02:31 +03:00
|
|
|
nsEventStatus* aStatus,
|
|
|
|
EventDispatchingCallback* aCallBack)
|
2014-11-25 08:02:30 +03:00
|
|
|
{
|
2015-07-03 05:49:36 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mTabParent);
|
|
|
|
|
2016-04-14 11:03:14 +03:00
|
|
|
MOZ_ASSERT(IsValidStateForComposition(aCompositionEvent->mWidget),
|
2014-11-25 08:02:30 +03:00
|
|
|
"Should be called only when it's safe to dispatch an event");
|
|
|
|
|
2016-03-17 10:01:30 +03:00
|
|
|
WidgetCompositionEvent compositionEvent(aCompositionEvent->IsTrusted(),
|
2016-04-14 11:03:14 +03:00
|
|
|
aMessage, aCompositionEvent->mWidget);
|
2016-03-28 07:29:42 +03:00
|
|
|
compositionEvent.mTime = aCompositionEvent->mTime;
|
2016-03-28 07:49:02 +03:00
|
|
|
compositionEvent.mTimeStamp = aCompositionEvent->mTimeStamp;
|
2014-11-25 08:02:30 +03:00
|
|
|
compositionEvent.mData = aCompositionEvent->mData;
|
2015-12-11 09:15:57 +03:00
|
|
|
compositionEvent.mNativeIMEContext = aCompositionEvent->mNativeIMEContext;
|
2015-09-08 06:54:14 +03:00
|
|
|
compositionEvent.mOriginalMessage = aCompositionEvent->mMessage;
|
2014-11-25 08:02:30 +03:00
|
|
|
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;
|
2015-09-11 15:21:27 +03:00
|
|
|
if (aMessage == eCompositionUpdate) {
|
2014-11-25 08:02:30 +03:00
|
|
|
mLastData = compositionEvent.mData;
|
2015-12-29 16:57:38 +03:00
|
|
|
mLastRanges = aCompositionEvent->mRanges;
|
2014-11-25 08:02:30 +03:00
|
|
|
}
|
2015-12-29 16:57:37 +03:00
|
|
|
|
|
|
|
DispatchEvent(&compositionEvent, status, aCallBack, aCompositionEvent);
|
2015-12-29 19:42:02 +03:00
|
|
|
return compositionEvent.mFlags;
|
2015-12-29 16:57:37 +03:00
|
|
|
}
|
|
|
|
|
2015-12-29 16:57:37 +03:00
|
|
|
void
|
|
|
|
TextComposition::DispatchEvent(WidgetCompositionEvent* aDispatchEvent,
|
|
|
|
nsEventStatus* aStatus,
|
|
|
|
EventDispatchingCallback* aCallBack,
|
|
|
|
const WidgetCompositionEvent *aOriginalEvent)
|
|
|
|
{
|
|
|
|
nsPluginInstanceOwner::GeneratePluginEvent(aOriginalEvent,
|
|
|
|
aDispatchEvent);
|
|
|
|
|
|
|
|
EventDispatcher::Dispatch(mNode, mPresContext,
|
|
|
|
aDispatchEvent, nullptr, aStatus, aCallBack);
|
2016-06-10 13:32:49 +03:00
|
|
|
|
|
|
|
OnCompositionEventDispatched(aDispatchEvent);
|
2015-12-29 16:57:37 +03:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:05:12 +04:00
|
|
|
void
|
2014-10-07 14:01:49 +04:00
|
|
|
TextComposition::OnCompositionEventDiscarded(
|
2015-07-03 05:49:36 +03:00
|
|
|
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.
|
|
|
|
|
2016-03-17 10:01:30 +03:00
|
|
|
MOZ_ASSERT(aCompositionEvent->IsTrusted(),
|
2014-09-26 04:05:12 +04:00
|
|
|
"Shouldn't be called with untrusted event");
|
|
|
|
|
2015-07-03 05:49:36 +03:00
|
|
|
if (mTabParent) {
|
|
|
|
// The composition event should be discarded in the child process too.
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << mTabParent->SendCompositionEvent(*aCompositionEvent);
|
2015-07-03 05:49:36 +03:00
|
|
|
}
|
|
|
|
|
2014-09-26 04:05:12 +04:00
|
|
|
// 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) {
|
2017-03-16 10:26:43 +03:00
|
|
|
if (*source == '\t' || *source == '\n' || !IsControlChar(*source)) {
|
2015-05-01 07:49:29 +03:00
|
|
|
*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
|
|
|
{
|
2016-06-10 13:32:49 +03:00
|
|
|
mWasCompositionStringEmpty = mString.IsEmpty();
|
|
|
|
|
2017-11-20 16:59:04 +03:00
|
|
|
if (aCompositionEvent->IsFollowedByCompositionEnd()) {
|
|
|
|
mHasReceivedCommitEvent = true;
|
|
|
|
}
|
|
|
|
|
2017-07-26 17:23:38 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-07-03 05:49:36 +03:00
|
|
|
// If the content is a container of TabParent, composition should be in the
|
|
|
|
// remote process.
|
|
|
|
if (mTabParent) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << mTabParent->SendCompositionEvent(*aCompositionEvent);
|
2016-03-17 05:17:42 +03:00
|
|
|
aCompositionEvent->StopPropagation();
|
2015-07-03 05:49:36 +03:00
|
|
|
if (aCompositionEvent->CausesDOMTextEvent()) {
|
|
|
|
mLastData = aCompositionEvent->mData;
|
2015-12-29 16:57:38 +03:00
|
|
|
mLastRanges = aCompositionEvent->mRanges;
|
2015-07-03 05:49:36 +03:00
|
|
|
// Although, the composition event hasn't been actually handled yet,
|
|
|
|
// emulate an editor to be handling the composition event.
|
|
|
|
EditorWillHandleCompositionChangeEvent(aCompositionEvent);
|
|
|
|
EditorDidHandleCompositionChangeEvent();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-01 07:49:29 +03:00
|
|
|
if (!mAllowControlCharacters) {
|
|
|
|
RemoveControlCharactersFrom(aCompositionEvent->mData,
|
|
|
|
aCompositionEvent->mRanges);
|
|
|
|
}
|
2015-09-11 15:21:26 +03:00
|
|
|
if (aCompositionEvent->mMessage == eCompositionCommitAsIs) {
|
2014-11-25 08:02:31 +03:00
|
|
|
NS_ASSERTION(!aCompositionEvent->mRanges,
|
2015-09-11 15:21:26 +03:00
|
|
|
"mRanges of eCompositionCommitAsIs should be null");
|
2014-11-25 08:02:31 +03:00
|
|
|
aCompositionEvent->mRanges = nullptr;
|
|
|
|
NS_ASSERTION(aCompositionEvent->mData.IsEmpty(),
|
2015-09-11 15:21:26 +03:00
|
|
|
"mData of eCompositionCommitAsIs should be empty string");
|
Bug 1278084 part.3 TextComposition shouldn't decide composition string which is only an ideographic space as a placeholder r=m_kato
Currently, when TextComposition tries to forcibly commit composition synchronously, it cancels the composition if there is only an ideographic space since legacy Chinese IMEs for Windows were used an ideographic space as a placeholder and shows actual composition string in its owning window (called reading window).
However, Japanese TIPs basically use composition to input an ideographic space. Unfortunately, this intentional input of an ideographic space is always canceled if an editor commits to composition at every input event in TSF mode because TSF cannot commit during a call of ITextStore::RequestLock(). Additionally, we will enable e10s mode, then, on all platforms, requesting commit composition is handled asynchronously.
Therefore, we should make the hack disabled in default settings now. If we'll find a way to distinguish if an ideographic space is a placeholder, we should recover this hack. Note that such input fields cannot handle such legacy IMEs, so, disabling the hack in default settings must be fine.
MozReview-Commit-ID: IdBcfBxeJum
--HG--
extra : rebase_source : 18ca5cd1083ade8813703cec05c020dc03f09f63
2016-06-07 15:25:24 +03:00
|
|
|
bool removePlaceholderCharacter =
|
|
|
|
Preferences::GetBool("intl.ime.remove_placeholder_character_at_commit",
|
|
|
|
false);
|
|
|
|
if (removePlaceholderCharacter && mLastData == IDEOGRAPHIC_SPACE) {
|
|
|
|
// If the last data is an ideographic space (FullWidth space), it might be
|
2014-11-25 08:02:31 +03:00
|
|
|
// a placeholder character of some Chinese IME. So, committing with
|
Bug 1278084 part.3 TextComposition shouldn't decide composition string which is only an ideographic space as a placeholder r=m_kato
Currently, when TextComposition tries to forcibly commit composition synchronously, it cancels the composition if there is only an ideographic space since legacy Chinese IMEs for Windows were used an ideographic space as a placeholder and shows actual composition string in its owning window (called reading window).
However, Japanese TIPs basically use composition to input an ideographic space. Unfortunately, this intentional input of an ideographic space is always canceled if an editor commits to composition at every input event in TSF mode because TSF cannot commit during a call of ITextStore::RequestLock(). Additionally, we will enable e10s mode, then, on all platforms, requesting commit composition is handled asynchronously.
Therefore, we should make the hack disabled in default settings now. If we'll find a way to distinguish if an ideographic space is a placeholder, we should recover this hack. Note that such input fields cannot handle such legacy IMEs, so, disabling the hack in default settings must be fine.
MozReview-Commit-ID: IdBcfBxeJum
--HG--
extra : rebase_source : 18ca5cd1083ade8813703cec05c020dc03f09f63
2016-06-07 15:25:24 +03:00
|
|
|
// this data might not be expected by users. Let's use empty string.
|
2014-11-25 08:02:31 +03:00
|
|
|
aCompositionEvent->mData.Truncate();
|
|
|
|
} else {
|
|
|
|
aCompositionEvent->mData = mLastData;
|
|
|
|
}
|
2015-09-11 15:21:27 +03:00
|
|
|
} else if (aCompositionEvent->mMessage == eCompositionCommit) {
|
2014-11-25 08:02:32 +03:00
|
|
|
NS_ASSERTION(!aCompositionEvent->mRanges,
|
2015-09-11 15:21:27 +03:00
|
|
|
"mRanges of eCompositionCommit should be null");
|
2014-11-25 08:02:32 +03:00
|
|
|
aCompositionEvent->mRanges = nullptr;
|
2014-11-25 08:02:31 +03:00
|
|
|
}
|
|
|
|
|
2016-04-14 11:03:14 +03:00
|
|
|
if (!IsValidStateForComposition(aCompositionEvent->mWidget)) {
|
2014-09-26 04:05:11 +04:00
|
|
|
*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;
|
2015-08-22 04:34:51 +03:00
|
|
|
switch (aCompositionEvent->mMessage) {
|
2015-09-11 15:21:27 +03:00
|
|
|
case eCompositionEnd:
|
2015-09-11 15:21:27 +03:00
|
|
|
case eCompositionChange:
|
2015-09-11 15:21:26 +03:00
|
|
|
case eCompositionCommitAsIs:
|
2015-09-11 15:21:27 +03:00
|
|
|
case eCompositionCommit:
|
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
|
2015-09-11 15:21:27 +03:00
|
|
|
// the last data (E.g., previous eCompositionChange event made the
|
2014-11-25 08:02:31 +03:00
|
|
|
// composition string empty or didn't have clause information), we don't
|
|
|
|
// need to dispatch redundant DOM text event.
|
|
|
|
if (dispatchDOMTextEvent &&
|
2015-09-11 15:21:27 +03:00
|
|
|
aCompositionEvent->mMessage != eCompositionChange &&
|
2014-11-25 08:02:31 +03:00
|
|
|
!mIsComposing && mLastData == aCompositionEvent->mData) {
|
|
|
|
dispatchEvent = dispatchDOMTextEvent = false;
|
|
|
|
}
|
|
|
|
|
2015-09-11 15:21:27 +03:00
|
|
|
// widget may dispatch redundant eCompositionChange event
|
2015-02-11 06:20:02 +03:00
|
|
|
// which modifies neither composition string, clauses nor caret
|
|
|
|
// position. In such case, we shouldn't dispatch DOM events.
|
|
|
|
if (dispatchDOMTextEvent &&
|
2015-09-11 15:21:27 +03:00
|
|
|
aCompositionEvent->mMessage == eCompositionChange &&
|
2015-02-11 06:20:02 +03:00
|
|
|
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
|
2015-09-11 15:21:27 +03:00
|
|
|
// overwrite the event message as eCompositionChange because due to
|
2014-11-25 08:02:31 +03:00
|
|
|
// the limitation of mapping between event messages and DOM event types,
|
|
|
|
// we cannot map multiple event messages to a DOM event type.
|
|
|
|
if (dispatchDOMTextEvent &&
|
2015-09-11 15:21:27 +03:00
|
|
|
aCompositionEvent->mMessage != eCompositionChange) {
|
2014-11-25 08:02:31 +03:00
|
|
|
aCompositionEvent->mFlags =
|
2015-09-11 15:21:27 +03:00
|
|
|
CloneAndDispatchAs(aCompositionEvent, eCompositionChange,
|
2014-11-25 08:02:31 +03:00
|
|
|
aStatus, aCallBack);
|
|
|
|
} else {
|
2015-12-29 16:57:37 +03:00
|
|
|
DispatchEvent(aCompositionEvent, aStatus, aCallBack);
|
2014-11-25 08:02:31 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*aStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2013-11-07 04:11:11 +04:00
|
|
|
|
2016-04-14 11:03:14 +03:00
|
|
|
if (!IsValidStateForComposition(aCompositionEvent->mWidget)) {
|
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.
|
2015-09-11 15:21:27 +03:00
|
|
|
if (aCompositionEvent->mMessage != eCompositionEnd) {
|
|
|
|
CloneAndDispatchAs(aCompositionEvent, eCompositionEnd);
|
2014-11-25 08:02:31 +03:00
|
|
|
}
|
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
|
|
|
|
2016-06-10 13:32:49 +03:00
|
|
|
MaybeNotifyIMEOfCompositionEventHandled(aCompositionEvent);
|
2013-11-07 04:11:11 +04:00
|
|
|
}
|
|
|
|
|
2015-07-17 05:25:00 +03:00
|
|
|
// static
|
|
|
|
void
|
|
|
|
TextComposition::HandleSelectionEvent(nsPresContext* aPresContext,
|
|
|
|
TabParent* aTabParent,
|
|
|
|
WidgetSelectionEvent* aSelectionEvent)
|
|
|
|
{
|
|
|
|
// If the content is a container of TabParent, composition should be in the
|
|
|
|
// remote process.
|
|
|
|
if (aTabParent) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << aTabParent->SendSelectionEvent(*aSelectionEvent);
|
2016-03-17 05:17:42 +03:00
|
|
|
aSelectionEvent->StopPropagation();
|
2015-07-17 05:25:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentEventHandler handler(aPresContext);
|
2015-07-17 05:25:00 +03:00
|
|
|
AutoRestore<bool> saveHandlingSelectionEvent(sHandlingSelectionEvent);
|
|
|
|
sHandlingSelectionEvent = true;
|
|
|
|
// XXX During setting selection, a selection listener may change selection
|
|
|
|
// again. In such case, sHandlingSelectionEvent doesn't indicate if
|
|
|
|
// the selection change is caused by a selection event. However, it
|
|
|
|
// must be non-realistic scenario.
|
2015-07-17 05:25:00 +03:00
|
|
|
handler.OnSelectionEvent(aSelectionEvent);
|
|
|
|
}
|
|
|
|
|
2016-06-10 13:45:21 +03:00
|
|
|
uint32_t
|
|
|
|
TextComposition::GetSelectionStartOffset()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIWidget> widget = mPresContext->GetRootWidget();
|
|
|
|
WidgetQueryContentEvent selectedTextEvent(true, eQuerySelectedText, widget);
|
2016-09-26 15:06:26 +03:00
|
|
|
// Due to a bug of widget, mRanges may not be nullptr even though composition
|
|
|
|
// string is empty. So, we need to check it here for avoiding to return
|
|
|
|
// odd start offset.
|
|
|
|
if (!mLastData.IsEmpty() && mRanges && mRanges->HasClauses()) {
|
2016-06-20 10:34:40 +03:00
|
|
|
selectedTextEvent.InitForQuerySelectedText(
|
|
|
|
ToSelectionType(mRanges->GetFirstClause()->mRangeType));
|
|
|
|
} else {
|
2016-09-26 15:06:26 +03:00
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
!mLastData.IsEmpty() || !mRanges || !mRanges->HasClauses(),
|
|
|
|
"Shouldn't have empty clause info when composition string is empty");
|
2016-06-20 10:34:40 +03:00
|
|
|
selectedTextEvent.InitForQuerySelectedText(SelectionType::eNormal);
|
|
|
|
}
|
2016-06-10 13:45:21 +03:00
|
|
|
|
|
|
|
// The editor which has this composition is observed by active
|
|
|
|
// IMEContentObserver, we can use the cache of it.
|
|
|
|
RefPtr<IMEContentObserver> contentObserver =
|
|
|
|
IMEStateManager::GetActiveContentObserver();
|
|
|
|
bool doQuerySelection = true;
|
|
|
|
if (contentObserver) {
|
|
|
|
if (contentObserver->IsManaging(this)) {
|
|
|
|
doQuerySelection = false;
|
|
|
|
contentObserver->HandleQueryContentEvent(&selectedTextEvent);
|
|
|
|
}
|
|
|
|
// If another editor already has focus, we cannot retrieve selection
|
|
|
|
// in the editor which has this composition...
|
|
|
|
else if (NS_WARN_IF(contentObserver->GetPresContext() == mPresContext)) {
|
|
|
|
return 0; // XXX Is this okay?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, using slow path (i.e., compute every time with
|
|
|
|
// ContentEventHandler)
|
|
|
|
if (doQuerySelection) {
|
|
|
|
ContentEventHandler handler(mPresContext);
|
|
|
|
handler.HandleQueryContentEvent(&selectedTextEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!selectedTextEvent.mSucceeded)) {
|
|
|
|
return 0; // XXX Is this okay?
|
|
|
|
}
|
|
|
|
return selectedTextEvent.mReply.mOffset;
|
|
|
|
}
|
|
|
|
|
2013-11-07 04:11:11 +04:00
|
|
|
void
|
2016-06-10 13:32:49 +03:00
|
|
|
TextComposition::OnCompositionEventDispatched(
|
2014-10-07 14:01:49 +04:00
|
|
|
const WidgetCompositionEvent* aCompositionEvent)
|
2013-11-07 04:11:11 +04:00
|
|
|
{
|
2015-07-03 05:49:36 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mTabParent);
|
|
|
|
|
2016-06-10 13:32:49 +03:00
|
|
|
if (!IsValidStateForComposition(aCompositionEvent->mWidget)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Every composition event may cause changing composition start offset,
|
|
|
|
// especially when there is no composition string. Therefore, we need to
|
|
|
|
// update mCompositionStartOffset with the latest offset.
|
|
|
|
|
|
|
|
MOZ_ASSERT(aCompositionEvent->mMessage != eCompositionStart ||
|
|
|
|
mWasCompositionStringEmpty,
|
|
|
|
"mWasCompositionStringEmpty should be true if the dispatched "
|
|
|
|
"event is eCompositionStart");
|
2013-11-07 04:11:11 +04:00
|
|
|
|
2016-06-10 13:32:49 +03:00
|
|
|
if (mWasCompositionStringEmpty &&
|
|
|
|
!aCompositionEvent->CausesDOMCompositionEndEvent()) {
|
|
|
|
// If there was no composition string, current selection start may be the
|
|
|
|
// offset for inserting composition string.
|
2016-06-10 13:45:21 +03:00
|
|
|
// Update composition start offset with current selection start.
|
|
|
|
mCompositionStartOffset = GetSelectionStartOffset();
|
2016-06-02 08:26:47 +03:00
|
|
|
mTargetClauseOffsetInComposition = 0;
|
2016-06-10 13:32:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aCompositionEvent->CausesDOMTextEvent()) {
|
2016-06-02 08:26:47 +03:00
|
|
|
mTargetClauseOffsetInComposition = aCompositionEvent->TargetClauseOffset();
|
2016-06-10 13:32:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 15:06:34 +03:00
|
|
|
void
|
|
|
|
TextComposition::OnStartOffsetUpdatedInChild(uint32_t aStartOffset)
|
|
|
|
{
|
|
|
|
mCompositionStartOffset = aStartOffset;
|
|
|
|
}
|
|
|
|
|
2016-06-10 13:32:49 +03:00
|
|
|
void
|
|
|
|
TextComposition::MaybeNotifyIMEOfCompositionEventHandled(
|
|
|
|
const WidgetCompositionEvent* aCompositionEvent)
|
|
|
|
{
|
|
|
|
if (aCompositionEvent->mMessage != eCompositionStart &&
|
|
|
|
!aCompositionEvent->CausesDOMTextEvent()) {
|
2014-10-07 14:01:48 +04:00
|
|
|
return;
|
2013-11-07 04:11:11 +04:00
|
|
|
}
|
|
|
|
|
Bug 1275906 part.2 TextComposition should use IMEContentObserver for sending NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED if the editor which has the composition is in the active IMEContentObserver r=smaug
For sending NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED after the other change notifications which was caused by the user input, we need to use IMEContentObserver::IMENotificationSender because it sends the notifications when it's safe to do it.
This patch makes TextComposition use IMEContentObserver to send the notification. However, if there is no active IMEContentObserver, e.g., composition events are fired on unfocused window, TextComposition sends it by itself (same as current implementation).
If IMEContentObserver stops observing when it has pending NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED, it cannot send the notification (i.e., it is discarded completely in such case). However, in such case, IMEContentObserver sends NOTIFY_IME_OF_BLUR. So, anyway, native IME handler should treat the blur notification as it including NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED.
On the other hand, we're buggy if composition events are fired in non-active window. Even in such case, IMEContentObserver should be created for active editor in each document and it notifies IME of the changes. But this is out of the scope of this bug.
MozReview-Commit-ID: 7Q0ZsJTh4hX
--HG--
extra : rebase_source : 6417f991fa8c0fbe3f25b27bacf4257e5485aecc
2016-06-01 16:14:41 +03:00
|
|
|
RefPtr<IMEContentObserver> contentObserver =
|
|
|
|
IMEStateManager::GetActiveContentObserver();
|
|
|
|
// When IMEContentObserver is managing the editor which has this composition,
|
|
|
|
// composition event handled notification should be sent after the observer
|
|
|
|
// notifies all pending notifications. Therefore, we should use it.
|
|
|
|
// XXX If IMEContentObserver suddenly loses focus after here and notifying
|
|
|
|
// widget of pending notifications, we won't notify widget of composition
|
|
|
|
// event handled. Although, this is a bug but it should be okay since
|
|
|
|
// destroying IMEContentObserver notifies IME of blur. So, native IME
|
|
|
|
// handler can treat it as this notification too.
|
|
|
|
if (contentObserver && contentObserver->IsManaging(this)) {
|
|
|
|
contentObserver->MaybeNotifyCompositionEventHandled();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Otherwise, e.g., this composition is in non-active window, we should
|
|
|
|
// notify widget directly.
|
2016-05-31 05:39:15 +03:00
|
|
|
NotifyIME(NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED);
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:51 +04:00
|
|
|
void
|
2015-08-26 15:56:59 +03:00
|
|
|
TextComposition::DispatchCompositionEventRunnable(EventMessage 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,
|
2017-11-20 16:59:04 +03:00
|
|
|
// or has already finished in IME, we don't need to request it again because
|
|
|
|
// request from this instance shouldn't cause committing nor canceling current
|
|
|
|
// composition in IME, and 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 (!CanRequsetIMEToCommitOrCancelComposition()) {
|
2014-09-26 04:05:11 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<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) {
|
2015-09-11 15:21:26 +03:00
|
|
|
DispatchCompositionEventRunnable(eCompositionCommitAsIs, EmptyString(),
|
2014-11-25 08:02:31 +03:00
|
|
|
true);
|
2014-11-25 08:02:32 +03:00
|
|
|
} else {
|
2015-09-11 15:21:27 +03:00
|
|
|
DispatchCompositionEventRunnable(eCompositionCommit, 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);
|
2017-07-05 18:47:40 +03:00
|
|
|
return IMEStateManager::NotifyIME(aMessage, mPresContext, mTabParent);
|
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()
|
|
|
|
{
|
2015-07-03 05:49:36 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mTabParent);
|
|
|
|
|
2015-06-04 20:06:09 +03:00
|
|
|
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
|
2017-06-20 13:23:44 +03:00
|
|
|
TextComposition::StartHandlingComposition(EditorBase* aEditorBase)
|
2014-02-12 17:02:56 +04:00
|
|
|
{
|
2015-07-03 05:49:36 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mTabParent);
|
|
|
|
|
2014-02-12 17:02:56 +04:00
|
|
|
MOZ_ASSERT(!HasEditor(), "There is a handling editor already");
|
2017-06-20 13:23:44 +03:00
|
|
|
mEditorBaseWeak = do_GetWeakReference(static_cast<nsIEditor*>(aEditorBase));
|
2014-02-12 17:02:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-06-20 13:23:44 +03:00
|
|
|
TextComposition::EndHandlingComposition(EditorBase* aEditorBase)
|
2014-02-12 17:02:56 +04:00
|
|
|
{
|
2015-07-03 05:49:36 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mTabParent);
|
|
|
|
|
2014-02-12 17:02:56 +04:00
|
|
|
#ifdef DEBUG
|
2017-06-20 13:23:44 +03:00
|
|
|
RefPtr<EditorBase> editorBase = GetEditorBase();
|
|
|
|
MOZ_ASSERT(editorBase == aEditorBase,
|
|
|
|
"Another editor handled the composition?");
|
2014-02-12 17:02:56 +04:00
|
|
|
#endif // #ifdef DEBUG
|
2017-06-20 13:23:44 +03:00
|
|
|
mEditorBaseWeak = nullptr;
|
2014-02-12 17:02:56 +04:00
|
|
|
}
|
|
|
|
|
2017-06-20 13:23:44 +03:00
|
|
|
already_AddRefed<EditorBase>
|
|
|
|
TextComposition::GetEditorBase() const
|
2014-02-12 17:02:56 +04:00
|
|
|
{
|
2017-06-20 13:23:44 +03:00
|
|
|
nsCOMPtr<nsIEditor> editor = do_QueryReferent(mEditorBaseWeak);
|
|
|
|
RefPtr<EditorBase> editorBase = static_cast<EditorBase*>(editor.get());
|
|
|
|
return editorBase.forget();
|
2014-02-12 17:02:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TextComposition::HasEditor() const
|
|
|
|
{
|
2017-06-20 13:23:44 +03:00
|
|
|
return mEditorBaseWeak && mEditorBaseWeak->IsAlive();
|
2014-02-12 17:02:56 +04:00
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:51 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* TextComposition::CompositionEventDispatcher
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
TextComposition::CompositionEventDispatcher::CompositionEventDispatcher(
|
2017-06-12 22:34:10 +03:00
|
|
|
TextComposition* aComposition,
|
|
|
|
nsINode* aEventTarget,
|
|
|
|
EventMessage aEventMessage,
|
|
|
|
const nsAString& aData,
|
|
|
|
bool aIsSynthesizedEvent)
|
|
|
|
: Runnable("TextComposition::CompositionEventDispatcher")
|
|
|
|
, mTextComposition(aComposition)
|
2014-09-26 04:05:11 +04:00
|
|
|
, mEventTarget(aEventTarget)
|
|
|
|
, mData(aData)
|
2015-08-26 15:56:59 +03:00
|
|
|
, mEventMessage(aEventMessage)
|
2014-09-26 04:05:11 +04:00
|
|
|
, 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
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPresContext> presContext = mTextComposition->mPresContext;
|
2012-09-26 09:47:51 +04:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
switch (mEventMessage) {
|
2015-09-11 15:21:27 +03:00
|
|
|
case eCompositionStart: {
|
|
|
|
WidgetCompositionEvent compStart(true, eCompositionStart, widget);
|
2015-12-11 09:15:57 +03:00
|
|
|
compStart.mNativeIMEContext = mTextComposition->mNativeContext;
|
2015-09-10 04:40:05 +03:00
|
|
|
WidgetQueryContentEvent selectedText(true, eQuerySelectedText, widget);
|
2014-09-26 04:05:11 +04:00
|
|
|
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;
|
|
|
|
}
|
2015-09-11 15:21:27 +03:00
|
|
|
case eCompositionChange:
|
2015-09-11 15:21:26 +03:00
|
|
|
case eCompositionCommitAsIs:
|
2015-09-11 15:21:27 +03:00
|
|
|
case eCompositionCommit: {
|
2014-09-26 04:05:11 +04:00
|
|
|
WidgetCompositionEvent compEvent(true, mEventMessage, widget);
|
2015-12-11 09:15:57 +03:00
|
|
|
compEvent.mNativeIMEContext = mTextComposition->mNativeContext;
|
2015-09-11 15:21:26 +03:00
|
|
|
if (mEventMessage != eCompositionCommitAsIs) {
|
2014-11-25 08:02:31 +03:00
|
|
|
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
|
2015-12-11 09:15:57 +03:00
|
|
|
TextCompositionArray::IndexOf(const NativeIMEContext& aNativeIMEContext)
|
2012-09-26 09:47:45 +04:00
|
|
|
{
|
2015-12-11 09:15:57 +03:00
|
|
|
if (!aNativeIMEContext.IsValid()) {
|
|
|
|
return NoIndex;
|
|
|
|
}
|
2012-09-26 09:47:45 +04:00
|
|
|
for (index_type i = Length(); i > 0; --i) {
|
2015-12-11 09:15:57 +03:00
|
|
|
if (ElementAt(i - 1)->GetNativeIMEContext() == aNativeIMEContext) {
|
2012-09-26 09:47:45 +04:00
|
|
|
return i - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoIndex;
|
|
|
|
}
|
|
|
|
|
2015-12-11 09:15:57 +03:00
|
|
|
TextCompositionArray::index_type
|
|
|
|
TextCompositionArray::IndexOf(nsIWidget* aWidget)
|
|
|
|
{
|
|
|
|
return IndexOf(aWidget->GetNativeIMEContext());
|
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
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);
|
2015-08-13 15:22:48 +03:00
|
|
|
if (i == NoIndex) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ElementAt(i);
|
2012-09-26 09:47:45 +04:00
|
|
|
}
|
|
|
|
|
2015-12-04 12:50:34 +03:00
|
|
|
TextComposition*
|
|
|
|
TextCompositionArray::GetCompositionFor(
|
|
|
|
const WidgetCompositionEvent* aCompositionEvent)
|
|
|
|
{
|
|
|
|
index_type i = IndexOf(aCompositionEvent->mNativeIMEContext);
|
|
|
|
if (i == NoIndex) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ElementAt(i);
|
|
|
|
}
|
|
|
|
|
2015-12-04 12:50:43 +03:00
|
|
|
TextComposition*
|
|
|
|
TextCompositionArray::GetCompositionFor(nsPresContext* aPresContext)
|
|
|
|
{
|
|
|
|
index_type i = IndexOf(aPresContext);
|
|
|
|
if (i == NoIndex) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ElementAt(i);
|
|
|
|
}
|
|
|
|
|
2012-09-26 09:47:45 +04:00
|
|
|
TextComposition*
|
|
|
|
TextCompositionArray::GetCompositionFor(nsPresContext* aPresContext,
|
|
|
|
nsINode* aNode)
|
|
|
|
{
|
|
|
|
index_type i = IndexOf(aPresContext, aNode);
|
2015-08-13 15:22:48 +03:00
|
|
|
if (i == NoIndex) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ElementAt(i);
|
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
|