Skip to content

fix(ime): Raise composition events after their text on browser soft keyboards - #26434

Open
mikernet wants to merge 13 commits into
unoplatform:masterfrom
mikernet:dev/mikernet/ime-composition-order
Open

mikernet wants to merge 13 commits into
unoplatform:masterfrom
mikernet:dev/mikernet/ime-composition-order

Conversation

@mikernet

@mikernet mikernet commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #26432

PR Type:

🐞 Bugfix

What changed? 🚀

This builds on #25804's IME design (the hidden input is the source of truth for composition text, and the TextBox follows it with textAlreadyApplied) and fixes what is left of the bugs #24502 fixed under its earlier design. It supersedes #24502, which I'll close. #25804's own body lists the first one as a known follow-up.

Each fix is its own commit, with its tests:

1. TextCompositionChanged is raised once its text is in Text (TextBox and RichEditBox, WebAssembly and Skia Android)
The platform applies the composition itself and reports it before the text sync that follows it (the browser's input event, Android's EndBatchEdit). The event was raised on that report, so its range pointed into text that wasn't there yet: a handler reading Text.Substring(e.StartIndex, e.Length) threw, and one moving focus sent the rest of the keystroke to the next TextBox. The event now waits for the sync, after TextChanging, as for text the TextBox applies itself. RichEditBox holds its Composition and ConversionTargetChanged automation events with it. An update whose text the control refused, and so isn't in Text, isn't raised. This changes the order of the events on every platform-applied update, so on Skia Android too, not only on WebAssembly.
On WebAssembly, a TextCompositionStarted or TextCompositionEnded handler that moves focus no longer gets the browser's text synced into the TextBox that took focus.
The ranges of TextCompositionChanged and of a composition ending without a commit (cancel, coercion, external change) are clamped to the text. A commit's TextCompositionEnded keeps the committed text's own range, since Skia Android raises it before syncing that text (the point morning4coffe-dev made on #24502).
Skia Android now passes where its composing region starts, as the browser does, so a composition opened on an existing word is placed there.
Tests: When_IME_Platform_Applied_Composition_Changed_After_Its_Text (TextBox) and When_Platform_Applied_Composition_Changed_After_Its_Text (RichEditBox), which replay Android's order with a fake extension on every Skia target; on WebAssembly When_Browser_Ime_Changed_Handler_Reads_Composition, When_Browser_Ime_Composition_Repeats_Existing_Character, When_Browser_Ime_Composition_Extends_Existing_Text, When_Browser_Ime_Caret_Moved_Inside_Preedit_Repeating_Text, When_Browser_Ime_Composition_Preedit_Emptied, When_Browser_Ime_Composition_Coerced_Shorter_Than_Its_Start, When_Browser_Ime_Changed_Handler_Moves_Focus, When_Browser_Ime_Started_Handler_Moves_Focus, When_Browser_Ime_Ended_Handler_Moves_Focus (each with a single-line and a multi-line target), two WebKit commit orders this already fixes (When_Browser_Ime_WebKit_Commit_Equal_To_Preedit_Ends_At_Once, When_Browser_Ime_WebKit_Preedit_Removed_Before_Cancel) and Given_RichEditBox.When_Wasm_Composition_Reopened_On_Existing_Text_Changed_Sees_The_Preedit.

2. A composition ends when the hidden input is replaced or removed (WebAssembly)
The browser's compositionend for a replaced or removed input is ignored on purpose, so when the same TextBox was re-entered with another focus state (a tap after keyboard focus) or accessibility detached the input while the TextBox kept focus, IsComposing stayed true and TextCompositionEnded never came. focus() and detachCore() now end it.
Tests: When_Browser_Ime_Same_TextBox_Refocused_During_Composition, When_Browser_Ime_Input_Detached_During_Composition.

3. A composition edit the control refuses is written back at once (TextBox and RichEditBox, WebAssembly and Skia Android)
#25804 reconciles a control's edit made during a composition when the composition ends. Until then refused text stayed in the hidden input, and the keyboard kept composing on it: typing a, then an x a BeforeTextChanging handler rejects, then b left Text at a.
Now, when the control refuses the composition's edit outright (a BeforeTextChanging cancel, a read-only RichEditBox or a document refusing protected text, and in the browser a read-only TextBox), or keeps text shorter than where the composition starts (a TextChanging handler clearing the text), its text goes back to the input at once. The composition ends for the control too, and the caret is pushed back to the input, so the keyboard builds its next keystrokes and suggestions on the text the control kept. IImeSessionHost.UpdateTextFromNative and TextBoxCore.ApplyNativeInput report the refusal: MaxLength rejects the whole text in Uno, so the text alone can't tell it from a BeforeTextChanging cancel.
Any other difference (MaxLength, a TextChanging limit or filter, CharacterCasing) keeps #25804's reconciliation at the composition's end. Meanwhile the input's selection changes don't reach the control, since they index the composition's text, not the control's. A conversion IME's reading is longer than the text it converts to: with MaxLength="4", Microsoft Pinyin's zhangsan still converts to 张三, and kana past MaxLength still converts to kanji that fit. The trade-off is Gboard's Latin words: the letters typed past MaxLength stay in the input until the word ends, as on master.
On Skia Android, EndBatchEdit left a refused edit in the native editable, since a refusal raises no text change to write back, and nothing reconciled the editable when a composition ended. The editable now takes the host's text, as one change, after a refusal or a cut before the composition (and loses its composing region, which ends the composition there too), when the composing region goes away, and outside a composition. A refused sync there also left the host's caret where the IME put it in the refused text, so the next key landed one place late; it now stays where the edit started.
Tests: When_Browser_Ime_Composition_Rejected_By_BeforeTextChanging, When_Browser_Ime_Same_Length_Replacement_Is_Refused, When_Browser_Ime_Composition_Coerced_Shorter_Than_Its_Start, When_Browser_Ime_Filtered_Composition_Reported_Again_Stays_Open, When_Browser_Ime_Pinyin_Reading_Past_MaxLength_Converts_Within_It, When_Browser_Ime_Kana_Composes_Past_MaxLength and When_Browser_Ime_Kana_Composes_Past_TextChanging_Limit (the reading stays in the input and converts within the limit), When_Browser_Ime_Composes_Past_MaxLength (now checks the input during and after the composition), When_Browser_Ime_Composes_Past_MaxLength_In_The_Middle_Keeps_The_Caret (the TextBox keeps its caret while the input holds the composition's longer text), Given_RichEditBox.When_Wasm_Composition_Refused_By_Protected_Text_Is_Written_Back (a composition starting right after protected text), When_Wasm_Composition_Past_MaxLength_Applies_At_Its_End, When_Wasm_Composition_Past_MaxLength_In_The_Middle_Keeps_The_Caret and When_Wasm_Composition_With_Upper_Casing_Stays_Open. On every Skia target there are When_IME_Platform_Sync_Refused_Then_Caret_Stays and When_Native_Text_Is_Compared_To_The_Kept_Text, a table of the rule's cases (refusal, cut before the start, limit, filter, line breaks, outside a composition). #25804's When_Browser_Ime_Composes_With_TextChanging_Filter still passes unchanged.
I checked Skia Android with Gboard on an emulator (Google APIs API 31 image, Gboard 10.8), driven by taps, with the MaxLength and BeforeTextChanging cases above:

  • Latin past MaxLength="4": the word ends on space, the editable takes abcd back, and one Backspace deletes the d.
  • Latin x refused by BeforeTextChanging: the composition ends at once, and Gboard's next composition builds on the kept text (it doesn't re-send ax after updateSelection(..., -1, -1)).
  • Japanese romaji past MaxLength="4": the reading stays in Gboard's composition, and committing it writes the kept text back.
    The kana-to-kanji conversion itself couldn't be tried there: that Gboard has no Japanese conversion model.
    On Skia Android a read-only TextBox doesn't refuse an IME edit (UpdateTextFromNative has no IsReadOnly check), as on master; that's left for a follow-up.

4. The hidden input is read-only for a read-only TextBox (WebAssembly)
It stayed editable, so a mobile browser opened the soft keyboard and let the user type into it until each edit was reverted; WinUI doesn't show the touch keyboard for a read-only TextBox. The input now gets readOnly and follows IsReadOnly. A read-only RichEditBox already suppressed the soft keyboard (inputmode="none"); it gets readOnly too, for consistency.
Mobile browsers show or hide the keyboard when an element takes focus, not when readOnly flips on the focused one. So focus moving between a read-only and an editable TextBox now focuses a new input instead of reusing the focused one, and IsReadOnly changing on the focused TextBox enters a new input as focus does (except during a composition, which keeps its input).
A read-only input still takes focus, a caret and a selection, and still gets key events and copy; the browser only refuses edits to it. I checked that in Chrome 157 with trusted CDP input on a plain read-only input: focus, ArrowLeft and Shift+ArrowLeft move the caret and the selection, keydown/keyup and copy fire, and typing fires beforeinput but leaves the value unchanged. The runtime test uses synthetic key events, so it shows that the TextBox still handles the keys and the input follows its selection, not native copy.
#25804's When_Browser_Input_Edits_Read_Only_TextBox said the input stays editable under a read-only TextBox; it now checks the input is read-only, and still that an edit reaching it anyway is refused.
Re-applying the properties when IsReadOnly changes also re-applies the keyboard type, autocorrect and accessory view on Apple UIKit, which is harmless.
Tests: When_Browser_Read_Only_TextBox_Keeps_Keyboard_And_Selection, When_Browser_Focus_Moves_From_Read_Only_TextBox, When_Browser_Read_Only_Toggled_On_Focused_TextBox, When_Browser_Input_Edits_Read_Only_TextBox (updated), Given_RichEditBox.When_Wasm_Read_Only_Hidden_Input_Follows_IsReadOnly, and When_Browser_Ime_TextBox_Made_Read_Only_During_Composition (the composition still ends; that part already worked on master).

5. The caret is restored at once after a refused input (WebAssembly)
After the TextBox refused a plain edit, the caret went back to where the edit started a dispatcher tick later, so a key typed in between landed where the refused edit left it ("abc" with the caret after "a": a refused "x" then "y" gave "abyc"). It is now restored while the refused edit is applied, and the restore a tick later is skipped on this path.
Test: When_Browser_Input_Refused_Then_Next_Key_Follows_At_Once.

6. More soft keyboard sequences as regression guards (test only)
#24502's soft keyboard tests that already pass on master and have no equivalent there, adapted to master's helpers: When_Browser_Soft_Keyboard_Backspace_Has_No_Code_In_PasswordBox, When_Browser_Ime_Caret_Moved_During_Composition, When_Browser_Ime_Composition_Ends_On_Replaced_Input, When_Browser_Ime_Multiline_Line_Break_During_Composition, When_Browser_Multiline_Selection_Follows_Native, When_Browser_Ime_Text_Set_By_TextCompositionEnded_Handler, When_Browser_Ime_Text_Cleared_During_Composition.

Not in this PR: the rest of #24502's WebKit/iOS commit ordering, which I'm proposing separately in #26435 (stacked on this one).

Validation

  • Runtime, Skia WebAssembly (Chrome 157, the browser's event sequences replayed on the hidden input): Given_TextBox, Given_RichEditBox, Given_PasswordBox, Given_BrowserKeyboardInputSource, Given_AccessibleTextBox and Given_ImeSessionCoordinator*: 1392 pass, 30 inconclusive. The one failure, When_Touch_LongPress_Drag_Release_Does_Not_Open_Flyout_iOS, fails the same way without these changes.
  • Runtime, Skia desktop (Windows): Given_TextBox, Given_RichEditBox, Given_PasswordBox and Given_ImeSessionCoordinator*: 1420 pass, one inconclusive; the one failure is the same iOS touch test.
  • Runtime, Skia Android (emulator, Gboard): the manual checks in fix 3.
  • Tests fail before the fix:
    • On master's product code, every new or changed fix test here fails (on WebAssembly, and the three shared-code tests on desktop too), except these:
      • the single-line rows of When_Browser_Ime_Started_Handler_Moves_Focus/When_Browser_Ime_Ended_Handler_Moves_Focus, where the reused input is emptied for the newly focused TextBox before the stale sync, so only the multi-line rows (a new textarea) show the bug;
      • tests that check fix 3 keeps master's reconciliation, which aren't fail-before tests: When_Browser_Ime_Filtered_Composition_Reported_Again_Stays_Open and When_Wasm_Composition_With_Upper_Casing_Stays_Open (both pass on master), and the MaxLength, limit and pinyin tests, each covered by a mutation below;
      • When_Wasm_Composition_Refused_By_Protected_Text_Is_Written_Back, which I checked with the mutation below rather than on master.
    • When_Native_Text_Is_Compared_To_The_Kept_Text calls the rule directly, as a table of its cases, so it has no fail-before (it doesn't compile on master).
    • The 7 regression guards pass on master, and so does When_Browser_Ime_TextBox_Made_Read_Only_During_Composition without its readOnly checks.
    • With ApplyNativeInput counting every edit that left Text unchanged as a refusal (how the rule treated a kept text before the narrowing), When_Browser_Ime_Pinyin_Reading_Past_MaxLength_Converts_Within_It, both When_Browser_Ime_Kana_Composes_* and When_Browser_Ime_Composes_Past_MaxLength fail, and When_Browser_Ime_Composition_Rejected_By_BeforeTextChanging still passes.
    • With a RichEditBox's length difference counted as a refusal, When_Wasm_Composition_Past_MaxLength_Applies_At_Its_End and When_Wasm_Composition_Past_MaxLength_In_The_Middle_Keeps_The_Caret fail (RichEditBox clips with ClampInsertToMaxLength instead of refusing).
    • With the RichEditBox's refusal dropped, When_Wasm_Composition_Refused_By_Protected_Text_Is_Written_Back fails.
    • When_Browser_Ime_Composes_Past_MaxLength_In_The_Middle_Keeps_The_Caret fails on the narrowing alone (308f529): the input's caret moves the TextBox's.
    • Without the caret restore, When_IME_Platform_Sync_Refused_Then_Caret_Stays fails.
    • With the read-only check removed from the input reuse, or the re-entry on an IsReadOnly change, When_Browser_Focus_Moves_From_Read_Only_TextBox and When_Browser_Read_Only_Toggled_On_Focused_TextBox fail.
  • iOS Safari: not checked on a device. By WebKit's source, assigning input.value during a composition doesn't cancel it on iOS (macOS cancels it on the selection change). After fix 3 the value is assigned mid-composition only for a refusal or a cut before the composition.

PR Checklist ✅

On WebAssembly and on Skia Android the platform applies the composition
text itself and reports it before syncing it to the TextBox (the input
event that follows compositionupdate, Android's EndBatchEdit). The
TextBox and the RichEditBox raised TextCompositionChanged on that
report, while Text still held the previous text: a handler reading
Text.Substring(e.StartIndex, e.Length) threw, and one that moved focus
made the rest of the update land in the next TextBox.

Hold the event back until the platform's text sync has landed, after
TextChanging, as for text the TextBox applies itself. RichEditBox holds
its Composition and ConversionTargetChanged automation events with it.
An update whose text the control refused, and so isn't in Text, is not
raised. This changes the order of the events on every platform-applied
update, on Android too.

A TextCompositionStarted or TextCompositionEnded handler can also move
focus before the browser's text is synced; the browser host no longer
syncs the old input's value into the TextBox that took focus.

The composition's range can also outlast its text, after a coercion or
a deletion, so TextCompositionChanged, and TextCompositionEnded for a
composition that ends without a commit, now get the part of the range
that is in the text. A commit's TextCompositionEnded keeps the committed
text's own range: Skia Android raises it before syncing that text. On
WebAssembly, a composition whose committed text the TextBox changed (a
filter, MaxLength) ends without a commit, with its range in the text.

Skia Android now reports where its composing region starts, as the
browser already does, so a composition the keyboard opens on an
existing word is placed there rather than at the caret.
A composition open on the hidden input never ended when that input was
replaced (the same TextBox re-entered with another focus state, as a
tap does after keyboard focus) or removed while the TextBox kept focus
(accessibility handing it over to its semantic element). The browser's
compositionend for the old input is ignored on purpose, so the TextBox
stayed composing and never raised TextCompositionEnded.

End it when the input is replaced or removed.
While a composition edits the hidden input, the browser owns its value,
so a TextBox text that differs from it is written back when the
composition ends. For an edit a BeforeTextChanging handler cancelled,
that left the refused character in the input while the keyboard went
on composing: the next keystrokes were built on it and refused too, so
typing "a", a rejected "x", then "b" left Text at "a".

Write the TextBox's text back at once when the TextBox rejects an edit
without its text changing at all (a BeforeTextChanging cancel, or a
read-only TextBox), which ends the browser's composition, and end it
for the TextBox too. Anything else is still reconciled when the
composition ends, so the keyboard keeps its composition: an edit a
TextChanging handler changes, even back to the previous text (a length
limit), and a refusal past MaxLength. A conversion can then bring the
composition back within the limit (にほん to 日本 with MaxLength 2).
The hidden input under a read-only TextBox stayed editable, so a mobile
browser opened the soft keyboard for it and let the user type until the
TextBox reverted each edit. WinUI doesn't show the touch keyboard for a
read-only TextBox.

Set readOnly on the hidden input when the TextBox or RichEditBox is
read-only, and update it when IsReadOnly changes. (A read-only
RichEditBox already suppressed the soft keyboard; it gets readOnly for
consistency.) A read-only input still takes focus, a caret and a
selection, and still gets key events and copy: the browser only
refuses edits to it. Keyboard navigation, selection and Ctrl+C keep
working, since the TextBox handles the keys and the browser copies the
input's selection.

Mobile browsers show or hide the soft keyboard when an element takes
focus, not when readOnly flips on the focused one. So focus moving
between a read-only and an editable TextBox focuses a new input instead
of reusing the focused one, and IsReadOnly changing on the focused
TextBox enters a new input as focus does, except during a composition.
When the TextBox refuses an edit the browser applied to the hidden
input (MaxLength, BeforeTextChanging, read-only), the caret went back to
where the edit started only a dispatcher tick later. Until then the
TextBox and the input both had it where the refused edit left it, so a
key typed in between landed there: typing "x" (refused) then "y" in the
middle of "abc" gave "abyc" instead of "aybc".

Put the caret back while the refused edit is being applied, and push it
to the input even when it didn't change, since writing the text back
moved the input's caret. The restore a tick later is skipped on this
path: it would now move the caret of a key typed in between.
Soft keyboard sequences from an earlier round of browser IME work that
master already handles, kept as regression guards: a code-less
Backspace in a PasswordBox, a caret moved during a composition, a
composition ending on an input that was replaced, a line break typed
during a composition, a caret move in a textarea, and text set or
cleared from code during a composition.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@github-actions github-actions Bot added platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform area/skia ✏️ Categorizes an issue or PR as relevant to Skia labels Nov 13, 2026
@mergify

mergify Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request

mikernet added a commit to Singulink/uno that referenced this pull request Nov 14, 2026
Port of the TextBox part of master's e86befc: the selectionchange
handler only followed an <input>, so a caret move the keyboard made in
a multi-line TextBox's <textarea> never reached the TextBox.
When_Browser_Multiline_Selection_Follows_Native (unoplatform#26434, next commit)
covers it.
mikernet added a commit to Singulink/uno that referenced this pull request Nov 14, 2026
Port of unoplatform#26434 (for unoplatform#26432), commits
faba3ed, 096daef, 8e2644f, b8d2189, edce6fa and
6939ef4, onto the unoplatform#25804 IME design this branch takes
from servicing/6.8 (unoplatform#25984).

- TextCompositionChanged for text the platform applied (WebAssembly,
  Skia Android) waits for the platform's text sync, and isn't raised
  for an update the TextBox refused. The Changed and no-commit Ended
  ranges are clamped to the text. Skia Android reports where its
  composing region starts.
- A composition ends when the hidden input is replaced or detached.
- An edit a BeforeTextChanging handler or a read-only TextBox refuses
  is written back to the input at once, ending the composition.
  Unlike master, this branch does the same for an edit MaxLength
  refuses or a TextChanging handler changes (see below).
- The hidden input is read-only with the TextBox.
- The caret goes back at once after a refused edit.

6.8 differences:
- Master's TextBoxCore.IME.cs, .Input.cs and .cs changes go to
  TextBox.IME.skia.cs, TextBox.skia.cs and TextBox.cs; events are raised
  on the TextBox itself. There is no RichEditBox IME, partial commit or
  CompositionCanceled on 6.8, so those parts are left out.
- Skia Android syncs through a new TextBox.UpdateTextFromNative, the
  6.8 counterpart of master's IImeSessionHost.UpdateTextFromNative. It
  raises the held TextCompositionChanged, and compares the editable's
  \n line breaks as the TextBox's \r, as master's
  IsNativeEchoOfCurrentText does.
- The read-only input follows TextBox.IsReadOnly through
  OnIsReadonlyChangedPartial; UpdateProperties, which only sets the
  enter key hint on 6.8, now also sets readOnly or re-enters the input.

With this branch's port of unoplatform#26005 (be85699):
- A refused platform sync puts the composition range back on the text
  that is there (unoplatform#26005), and raises TextCompositionChanged
  for that range only when it isn't the last range raised. The refused
  update's own range is never raised (unoplatform#26434).
- While a composition holds text the TextBox refused past MaxLength
  (reconciled when it ends), the hidden input's selectionchange isn't
  reported: its offsets are in that text, not in the TextBox's. Without
  this, the input's caret replaced the one edce6fa restores at once.
- ApplyNativeInput keeps unoplatform#26005's handling of a refused
  edit's pending selection.

Like Android's own LengthFilter (and this branch before the catch-up),
a composition edit the TextBox doesn't keep as the keyboard applied it
(MaxLength, a TextChanging filter, as well as BeforeTextChanging and
read-only) writes the TextBox's text back at once and ends the
composition, on WebAssembly and on Skia Android. Master keeps a
MaxLength or TextChanging refusal in the input until the composition
ends, so a CJK conversion can bring it back within the limit. Gboard
composes every Latin word, though, so the refused letters stayed in the
input for the whole word: its suggestions were built on them, and
Backspace deleted them before it reached the TextBox's text. A
conversion past the limit is lost here, as with Android's LengthFilter.
When_Browser_Ime_Composes_With_TextChanging_Filter (unoplatform#25804)
and the two conversion tests of unoplatform#26434 now expect that,
and When_Browser_Ime_Composes_Word_Past_MaxLength_Then_Input_Keeps_The_Limit
covers the Gboard case.

Adds tests for a refused Skia Android sync, \n line breaks in its sync,
and a refused composition followed by another before the dispatcher
runs.
mikernet added a commit to Singulink/uno that referenced this pull request Nov 14, 2026
Port of unoplatform#26435 (stacked on unoplatform#26434), commit
6d7709a.

WebKit browsers commit a composition through input events of their own:
deleteCompositionText, insertFromComposition, then compositionend. The
removal of the preedit is now reported together with the insertion that
follows it, and a preedit put back unchanged isn't reported again. For
the orders where compositionend comes before the input event that
applies the commit, the composition ends with that event, after the text
change and TextCompositionChanged, or after the task when none comes.

6.8 differences: the browser's composition cancel is OnCompositionEnded
here (master: OnCompositionCanceled), so
When_Browser_Ime_Selected_Composition_Cancelled_Without_Input_Event
expects the composition to end with its range, as every composition
that ends without a commit does on 6.8.
mikernet added a commit to Singulink/uno that referenced this pull request Nov 14, 2026
Port of the review round of unoplatform#26434 (commits 0e475cdb9e
and 73279a2cf1 there) onto this branch's write-back.

- Narrower rule: the input or the native editable takes the TextBox's
  text back at once, ending the composition, only when the TextBox
  refused the edit or kept text of another length (MaxLength, a length
  limit). A change that keeps the length (an upper-case TextChanging
  filter) waits for the end of the composition again, as
  unoplatform#25804 made it, so an IME can still convert it:
  When_Browser_Ime_Composes_With_TextChanging_Filter is back to
  servicing/6.8's version. TextBox.MustReturnTextToNative holds the
  rule, and compares without allocating.
- Skia Android's EndBatchEdit no longer reads ActiveTextBox again after
  the sync, which runs app handlers that may move focus or clear it
  (a null reference in the IME callback): it writes back only to the
  TextBox it synced, and only after the IME changed the text. The
  write-back is one batch for the listeners, so a TextCompositionEnded
  handler that sets the text again is written after it.
- The comments no longer describe this as what Android's LengthFilter
  does, and the remaining ones about the end-of-composition
  reconciliation say which edits it still covers.
- When_Browser_Ime_Composes_Past_MaxLength and
  When_Browser_Ime_Composition_Coerced_Shorter_Than_Its_Start check the
  input right after the update, and that the browser's late
  compositionend changes nothing.

TextBox.UpdateTextFromNative already kept the TextBox's caret on a
refused sync (the applied check of unoplatform#26005), unlike master's;
When_IME_Platform_Sync_Refused_Then_Caret_Stays covers it here too.
6.8 has no RichEditBox IME, so the RichEditBox part doesn't apply.
A composition edit the TextBox refused stayed in the hidden input until
the composition ended, unless a BeforeTextChanging handler or a
read-only TextBox refused it: an edit MaxLength refused, or that a
length limit put back, was left for the end of the composition, so that
a CJK conversion could still bring it back within the limit. Gboard
composes every Latin word, though, so the letters typed past MaxLength
stayed in the input for the whole word: the keyboard built its
suggestions on them, and Backspace deleted them before it reached the
TextBox's text.

This replaces that trade-off. When the TextBox keeps text of another
length than the input's (MaxLength, a length limit), or refuses a change
of the input's text and keeps its own previous text, its text goes back
to the input at once and the composition ends, so the keyboard builds
its next keystrokes and suggestions on the text the TextBox kept. The
input now reports its value at the previous sync with each edit, so the
same text reported again (a caret move inside the preedit) doesn't count
as a refusal while a same-length filter keeps the two texts apart. Kana
typed past a limit is cut at the limit rather than converted later. A
change that keeps the length (an upper-case filter) still waits for the
end of the composition, as unoplatform#25804 made it, so an IME can
still convert it. ImeSessionCoordinator.MustReturnTextToNative holds the
rule.

RichEditBox gets the same rule, so an edit it refuses or cuts to its
text limit is written back, and its CharacterCasing waits for the end of
the composition. ApplyNativeInput no longer has to tell a refusal from a
handler's put-back text, so its result and the text change count go.

Tests: When_Browser_Ime_Converts_Past_MaxLength and
When_Browser_Ime_Converts_Past_TextChanging_Limit become
When_Browser_Ime_Kana_Composes_Past_MaxLength and
When_Browser_Ime_Kana_Composes_Past_TextChanging_Limit;
When_Browser_Ime_Composes_Word_Past_MaxLength_Then_Input_Keeps_The_Limit
covers the Gboard case and
Given_RichEditBox.When_Wasm_Composition_Past_MaxLength_Is_Written_Back
the RichEditBox, with
When_Wasm_Composition_Past_MaxLength_In_The_Middle_Keeps_The_Caret and
When_Wasm_Composition_With_Upper_Casing_Stays_Open.
When_Browser_Ime_Filtered_Composition_Reported_Again_Stays_Open,
When_Browser_Ime_Same_Length_Replacement_Is_Refused and the table test
When_Native_Text_Is_Compared_To_The_Kept_Text cover the rule's clauses.
When_Browser_Ime_Composes_Past_MaxLength and
When_Browser_Ime_Composition_Coerced_Shorter_Than_Its_Start now check
the input right after the update, and that the browser's late
compositionend changes nothing.
Skia Android's EndBatchEdit synced the IME's text to the host, but an
edit the host refused (MaxLength, BeforeTextChanging) stayed in the
editable, as a refusal raises no text change to write back, and a
TextChanging handler's change wasn't written back either while the
batch ended. The keyboard kept composing on text the host doesn't have.

With the WebAssembly host's rule
(ImeSessionCoordinator.MustReturnTextToNative, given the host's text
read before the sync and the editable's text at the start of the batch),
the editable now takes the host's text at once and drops its composing
region, which ends the composition, as one change for the listeners: a
TextCompositionEnded handler that sets the text again is written after
it. The check runs only after the IME changed the text, as a
RichEditBox's text costs the document, and only while the host it synced
is still the active one, as the handlers that ran may have moved focus.
UpdateEditableStateFromHost reads the host once for the same reason.

A refused sync also put the host's caret where the IME left it in the
refused text, which the editable then took back, so the next key landed
one place late. The caret now stays where the edit started
(When_IME_Platform_Sync_Refused_Then_Caret_Stays).
mikernet added a commit to Singulink/uno that referenced this pull request Nov 14, 2026
Port of the second review round of unoplatform#26434 (commits
d6480af and 71c75a7 there).

An upper-case TextChanging filter leaves the TextBox's text ("AB")
different from the composition's ("ab") until the composition ends.
The input reporting its text again unchanged (a caret move inside the
preedit) then looked like a refusal, as the TextBox kept its previous
text, and ended the composition. A refusal now also needs the input's
text to have changed: the browser passes the input's value at the
previous sync with each edit, and Skia Android compares with the
editable's text at the start of the batch and reads the TextBox's text
before the sync, instead of the editable's, for what the TextBox had.

UpdateEditableSelectionAndText reads the active TextBox once, as the
editable's listeners notified by Clear may change it. The selectionchange
comment no longer names MaxLength, which no longer stays in a
composition.

Tests: When_Browser_Ime_Filtered_Composition_Reported_Again_Stays_Open,
When_Browser_Ime_Same_Length_Replacement_Is_Refused and the table test
When_Native_Text_Is_Compared_To_The_Kept_Text.
The browser passed the input's previous value with each edit, so the
whole text crossed to .NET twice per keystroke, while the rule only
needs to know whether the input's text changed. The browser now
compares the input's value with the one at the previous sync or write
(both written by the input, so their line breaks match) and passes the
result, for TextBox and RichEditBox alike. An overload of
MustReturnTextToNative takes it; Android keeps passing the editable's
text from the start of the batch, compared only when the rule needs it.

The table test checks both overloads with the same rows.
mikernet added a commit to Singulink/uno that referenced this pull request Nov 14, 2026
Port of unoplatform#26434's 0effe84.

The browser passed the input's previous value with each edit, so the
whole text crossed to .NET twice per keystroke, while the rule only
needs to know whether the input's text changed. The browser now
compares the input's value with the one at the previous sync or write
(both written by the input, so their line breaks match) and passes the
result. An overload of TextBox.MustReturnTextToNative takes it; Skia
Android keeps passing the editable's text from the start of the batch,
compared only when the rule needs it.

The table test checks both overloads with the same rows.

@MartinZikmund MartinZikmund left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikernet thanks a lot for this one, and for the very thorough write-up and fail-before evidence. Moving TextCompositionChanged after the text sync is right for WinUI parity: in dxaml/xcp/core/native/text/Controls/TextBoxBase.cpp, CTextBoxBase::TxNotify handles EN_CHANGE by running OnContentChanged (sync TextChanging, queued TextChanged) first, and only then SendTextCompositionEvent(CompositionChanged). Uno's async TextChanged gives the same TextChanging, then TextCompositionChanged, then TextChanged order. The focus-move guards, the compositionend on a replaced or detached input, the read-only input and the caret restore all look good to me.

A few things before this goes in:

  1. Ending the composition on a length difference hurts desktop CJK IMEs (inline). On Windows Chrome with Microsoft Pinyin, the preedit in the input is the raw pinyin, so with MaxLength="4" the 5th letter of zhangsan now commits zhan as Latin text, and the user can never type 张三 (2 chars). On master the preedit stays in the input and is reconciled when it ends, so 张三 fits. When_Browser_Ime_Kana_Composes_Past_MaxLength pins the same trade-off for kana. I get why Gboard needs it, but can we keep it from breaking conversion IMEs? Some options: only write back once the text the TextBox kept is shorter than where the composition starts, or keep master's end-of-composition reconciliation when the length difference comes only from MaxLength or a TextChanging limit. If you think the trade-off is right as it is, please say so in the PR body so we decide it on purpose.
  2. Skia Android validation. This changes the event order on Skia Android and makes EndBatchEdit reset the editable and its composing region on a refusal. The only validation listed is the Android compile plus the fake extension on desktop. Could you run Gboard (Latin + Japanese) on a real Skia Android head, at least the MaxLength and BeforeTextChanging cases? I'm especially curious whether Gboard leaves its composition cleanly after the updateSelection(..., -1, -1).
  3. iOS Safari: is there any manual check that assigning input.value mid-composition ends the composition on WebKit? If WebKit keeps composing, the next compositionupdates are dropped as stale (the generation was bumped), so the rest of that word gets no composition events.

Not blocking, FYI: WinUI doesn't raise BeforeTextChanging during a composition at all (CTextBox::UpdateTextProperty, shouldRaiseBeforeTextChanging = !m_compositionInProgress). It defers it to the final EN_CHANGE. Uno raises it per update, which is why the BeforeTextChanging refusal path exists here. That divergence was there before this PR, so no need to fix it here; I just want us to be aware that part of this behavior depends on it.

On other open PRs: #26435 stacks cleanly on top of this one. #25174 (approved) conflicts with this PR in BrowserInvisibleTextBoxViewExtension.ts detachCore(), where both add lines at the top. It's trivial: keep both (the lastKeyDownEnter* resets and wasComposing).

Ending the composition whenever the control kept text of another length
than the input's broke conversion IMEs, whose reading is longer than the
text it converts to: with MaxLength="4", Microsoft Pinyin in Chrome
committed "zhan" as Latin text instead of letting "zhangsan" convert to
two characters, and kana past MaxLength was cut instead of converted.

The host now tells whether it refused the edit (read-only,
BeforeTextChanging) rather than keeping other text through a limit or a
handler (MaxLength, TextChanging). Only a refusal, or a kept text that
no longer reaches where the composition starts, goes back to the input
at once. Any other difference waits for the composition to end, as on
master: the browser reconciles it at compositionend, and Skia Android
now writes it back in the batch edit that removes the composing region,
since nothing synced the editable there. Outside a composition, Android
writes any difference back at once.

This gives up the Gboard MaxLength fix: the letters of a Latin word
typed past MaxLength stay in the input until the word ends, as on
master, since nothing tells them apart from a reading.

The browser no longer passes whether the input's text changed, which
only the old rule needed, and skips the check, and with it reading a
RichEditBox's document, outside a composition.
During a composition that doesn't start the text, the write-back check
read the host's whole text on every input event, which for a
RichEditBox materializes the document. When the host didn't refuse
the edit, only the kept text's length matters: the input's text reaches
where its composition starts, so a kept text shorter than that differs
from it. The host now exposes its text's length for that.

The refusal's doc now says what each host refuses: a read-only TextBox
refuses only in the browser, where ApplyNativeInput checks IsReadOnly.
Nothing covered the browser's write-back of a composition update a
RichEditBox refuses, since the MaxLength cases now wait for the
composition to end. A composition started right after protected text,
which the document refuses, now checks that the input takes the
document's text and caret back at once and the composition ends.
@mikernet

mikernet commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author
  1. Conversion IMEs and length differences: narrowed in e8a8f8b, along the lines you suggested. The text alone can't tell a MaxLength rejection from a BeforeTextChanging cancel: Uno's MaxLength rejects the whole over-long text (CoerceText returns UnsetValue), so in both cases the TextBox keeps its previous text. IImeSessionHost.UpdateTextFromNative and TextBoxCore.ApplyNativeInput now return whether the host refused the edit, as opposed to a limit or a handler keeping other text. A refusal is a BeforeTextChanging cancel, a read-only RichEditBox, a document refusing protected text, or, in the browser, a read-only TextBox.
    During a composition, the input takes the control's text at once only when the control refused the edit, or kept text shorter than where the composition starts (When_Browser_Ime_Composition_Coerced_Shorter_Than_Its_Start). Any other difference (MaxLength, a TextChanging limit or filter) waits for the composition to end, as on master.
    Tests, and the mutation each fails under:

    • When_Browser_Ime_Pinyin_Reading_Past_MaxLength_Converts_Within_It (new): MaxLength="4", zhangsan composed one letter at a time, so the TextBox stays at zhan while the input keeps the reading, and 张三 then fits.
    • When_Browser_Ime_Kana_Composes_Past_MaxLength and ..._Past_TextChanging_Limit (changed): にほん stays in the input and converts to 日本.
    • When_Browser_Ime_Composes_Past_MaxLength (changed).
    • These four fail when ApplyNativeInput counts every edit that left Text unchanged as a refusal, which is how the rule treated a kept text before this change.
    • The RichEditBox MaxLength tests now check the reconciliation at the end (When_Wasm_Composition_Past_MaxLength_Applies_At_Its_End, ..._In_The_Middle_Keeps_The_Caret). RichEditBox clips with ClampInsertToMaxLength instead of reporting a refusal, so both fail under their own mutation: the document's length difference counted as a refusal.
    • When_Native_Text_Is_Compared_To_The_Kept_Text calls the rule directly, as a table of its cases. It has no fail-before.
    • What this gives up: the Gboard MaxLength part of the original fix. Gboard composes every Latin word, so the letters typed past MaxLength stay in the input until the word ends, as on master. Nothing in the events tells them apart from a reading.
    • Skia Android had no end-of-composition reconciliation: the editable kept the refused text after the word ended. EndBatchEdit now writes the host's text back in the batch that removes the composing region, and outside a composition.
    • The browser no longer passes whether the input's text changed (only the old rule needed it).
    • The RichEditBox write-back during a composition lost its coverage with the narrowing. 308f529 adds When_Wasm_Composition_Refused_By_Protected_Text_Is_Written_Back: a composition starting right after protected text is refused, and the input takes the document's text and caret back at once. It fails with the RichEditBox refusal dropped.
    • One thing I found while checking this: on Skia Android a read-only TextBox doesn't refuse an IME edit at all (UpdateTextFromNative goes to ProcessTextInput with no IsReadOnly check), so read-only isn't a refusal there. That was already the case on master; I'd follow up on it separately.
  2. Skia Android with Gboard. I ran a Debug Skia Android build of this branch on an API 31 emulator (Google APIs image, preloaded Gboard 10.8), with a page holding a MaxLength="4" TextBox and one whose BeforeTextChanging refuses x. Gboard was driven by taps.

    • Latin, MaxLength="4", typing abcde: the TextBox keeps abcd while Gboard keeps composing abcde. Space ends the composition and the editable takes abcd back. One Backspace then deletes the d at once, and Gboard re-composes abc, so it builds on the written-back text rather than on abcde.
    • Latin, BeforeTextChanging refusing x, typing a, x, b: the x ends the composition at once (TextCompositionEnded 0+1, Text a), and Gboard's suggestions switch to ones for a. b then starts a new composition and gives ab.
    • So after updateSelection(..., -1, -1) Gboard drops its composing state and suggestions and doesn't re-send ax (your inline question).
    • Japanese QWERTY (romaji to kana), MaxLength="4", typing toukyouto: the TextBox stays at とうきょ while Gboard composes とうきょうと. Enter commits the reading; the editable takes とうきょ back, and Backspace deletes ょ at once.
    • I couldn't test the kana-to-kanji conversion itself: the preloaded Gboard on that image has no Japanese conversion model (変換 offers nothing). That needs a Play Store image with a signed-in account.
    • One thing I noticed: when the host refuses a commit, Android's TextCompositionEnded still reports the commit's own range (0+5 with a 4-character Text), since it is raised before the commit is synced. That was already the case on master, and I'd rather fix it separately.
  3. iOS Safari and assigning input.value mid-composition: not verified on a device yet. The build Mac can run the simulator, but nothing there can type into the simulator's soft keyboard (no idb or XCUITest harness, and AppleScript is off), so I couldn't produce a real marked-text composition. From WebKit's source:

    • Editor::hasComposition() is just m_compositionNode != nullptr, and nothing clears that node when a script replaces the field's text node.
    • The only "abandon the inline input session when the selection changes" path is in WebPage::didChangeSelection, and it sits inside #if HAVE(TOUCH_BAR), so it exists only on macOS. Its FIXME says DOM changes that affect the composition node aren't handled.
    • So macOS Safari cancels the composition, but iOS most likely keeps composing on a stale node, and the next compositionupdates are dropped as stale, as you describe.
    • After the narrowing, the input's value is replaced mid-composition only for a refusal (BeforeTextChanging, which WinUI wouldn't even raise there) or a text cut before the composition. MaxLength and length limits no longer do it.
    • For those two cases on iOS, I'd rather follow up than guess. One option is to blur and refocus the input after such a write-back, which restartComposition already does for input-scope changes.

Nit (IsComposing first): done in e8a8f8b. Outside a composition, the browser path now returns before any of the check. In 74904f5, during a composition the host's text (the whole document, for a RichEditBox) is read only after a refusal. Otherwise only its length counts: the input's text reaches where its composition starts, so a kept text shorter than that start differs from it.

On BeforeTextChanging during a composition: agreed, that's a pre-existing divergence. Raising it once at the end, as WinUI does, would also remove the main reason for the immediate write-back, so it's a good follow-up.

Edit: cc8cec1 fixes a regression the narrowing introduced. While a composition MaxLength refused goes on, the input holds the composition's text and the TextBox its own, so the input's caret no longer indexes the TextBox's text. A key composed in the middle of a full TextBox moved the TextBox's caret onto the input's position (abcd with the caret at 2, composing x: the caret went to 3), and the reconciliation at the composition's end kept it there. While the TextBox holds other text than the composition's, the input's selection changes no longer reach it; the composition's end reconciles both. When_Browser_Ime_Composes_Past_MaxLength_In_The_Middle_Keeps_The_Caret covers it and fails without the fix.

Since a composition MaxLength refuses keeps its text in the input until
it ends, the input and the TextBox hold different text meanwhile. The
input's selection changes still reached the TextBox, so a key composed
in the middle of a full TextBox moved the TextBox's caret onto the
input's positions ("abcd" with the caret at 2, composing "x": the caret
went to 3), and the reconciliation at the end of the composition kept
that caret. While the TextBox holds other text than the composition's,
the input's selection no longer reaches it; the composition's end
reconciles both.
mikernet added a commit to Singulink/uno that referenced this pull request Nov 21, 2026
This reverts a6f4690. unoplatform#26434 no longer ends a browser
composition whose update MaxLength refuses: a conversion IME's reading
can be longer than the text it converts to, so the difference waits for
the composition to end. unoplatform#26005's original expectation
holds again: the composition survives the refused update, and the next
change from code is an outside change that ends it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/skia ✏️ Categorizes an issue or PR as relevant to Skia platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[WASM] TextCompositionChanged raised before its text is applied, and related composition bugs on browser soft keyboards

2 participants