Skip to content

Commit

Permalink
Bug 1520983 - part 2: Make `IMEInputHandler::InsertTextAsComittingCom…
Browse files Browse the repository at this point in the history
…position()` use `eContentCommandInsertText` event if there is no composition r=m_kato

Like the other browsers, we should stop dispatching a set of composition
events when `insertText:` is called by Emoji picker, accent character
picker of some Western keyboard layouts.  Then, corresponding
`beforeinput` event becomes cancelable like the other browsers'.

Differential Revision: https://phabricator.services.mozilla.com/D114827
  • Loading branch information
masayuki-nakano committed May 17, 2021
1 parent 1bb2df9 commit 684add0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions modules/libpref/init/StaticPrefList.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5562,6 +5562,20 @@
value: false
mirror: always

#ifdef XP_MACOSX
# Whether text input without keyboard press nor IME composition should cause a
# set of composition events or not. E.g., when you use Emoji picker on macOS,
# it inserts an Emoji character directly. If set to true, `compositionstart`,
# `compositionupdate` and `compositionend` events will be fired, and the
# correspnding `beforeinput` events are not cancelable. Otherwise, if set to
# false, any user input events are not exposed to web apps but only
# `beforeinput` event is fired as "insert text" as a cancelable event.
- name: intl.ime.use_composition_events_for_insert_text
type: bool
value: false
mirror: always
#endif

#---------------------------------------------------------------------------
# Prefs starting with "javascript."
#
Expand Down
18 changes: 18 additions & 0 deletions widget/cocoa/TextInputHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "mozilla/AutoRestore.h"
#include "mozilla/MiscEvents.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/StaticPrefs_intl.h"
#include "mozilla/Telemetry.h"
#include "mozilla/TextEventDispatcher.h"
#include "mozilla/TextEvents.h"
Expand Down Expand Up @@ -3789,13 +3790,30 @@ inline NSRange MakeNSRangeFrom(const Maybe<OffsetAndData<uint32_t>>& aOffsetAndD
nsCocoaUtils::GetStringForNSString([aAttrString string], str);

if (!IsIMEComposing()) {
MOZ_DIAGNOSTIC_ASSERT(!str.IsEmpty());

// If there is no selection and replacement range is specified, set the
// range as selection.
if (aReplacementRange && aReplacementRange->location != NSNotFound &&
!NSEqualRanges(SelectedRange(), *aReplacementRange)) {
NS_ENSURE_TRUE_VOID(SetSelection(*aReplacementRange));
}

if (!StaticPrefs::intl_ime_use_composition_events_for_insert_text()) {
// In the default settings, we should not use composition events for
// inserting text without key press nor IME composition because the
// other browsers do so. This will cause only a cancelable `beforeinput`
// event whose `inputType` is `insertText`.
WidgetContentCommandEvent insertTextEvent(true, eContentCommandInsertText, mWidget);
insertTextEvent.mString = Some(str);
DispatchEvent(insertTextEvent);
return;
}

// Otherise, emulate an IME composition. This is our traditional behavior,
// but `beforeinput` events are not cancelable since they should be so for
// native IME limitation. So, this is now seriously imcompatible with the
// other browsers.
if (!DispatchCompositionStartEvent()) {
MOZ_LOG(gLog, LogLevel::Info,
("%p IMEInputHandler::InsertTextAsCommittingComposition, "
Expand Down

0 comments on commit 684add0

Please sign in to comment.