Skip to content

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

Open

Description

@mikernet

Current behavior 🐛

On Skia WebAssembly (and partly Skia Android), where the platform applies IME composition text itself and the TextBox follows its hidden input:

  1. TextCompositionChanged is raised before its text is in Text. TextBox and RichEditBox raise it when the platform reports the composition, before the text sync that follows. Its StartIndex/Length point into text that isn't there yet: a handler reading Text.Substring(e.StartIndex, e.Length) throws ArgumentOutOfRangeException, and a handler that moves focus to another TextBox makes the rest of that keystroke land in the other TextBox. Skia Android has the same order (the composition is reported in EndBatchEdit before the text sync). On WebAssembly, a TextCompositionStarted or TextCompositionEnded handler that moves focus has the same effect.
  2. The composition's range can lie outside the text after a coercion or a deletion during the composition (a TextChanging handler that shortens the text), in TextCompositionChanged and in TextCompositionEnded for a composition that ends without a commit.
  3. The composition never ends when the hidden input is replaced or removed (the same TextBox re-entered with another focus state, as a tap does after keyboard focus; accessibility detaching the input): IsComposing stays true and TextCompositionEnded is never raised.
  4. An edit a BeforeTextChanging handler cancels during a composition stays in the hidden input until the composition ends. The keyboard keeps composing on the refused character, so later characters are refused too: typing a, a rejected x, then b leaves Text at a.
  5. A read-only TextBox still opens the soft keyboard on mobile browsers and lets the user type, each edit being reverted. WinUI doesn't show the touch keyboard for a read-only TextBox.
  6. After a refused plain (non-composition) edit, the caret goes back only a dispatcher tick later, so a fast next key lands where the refused edit left it ("abc", caret after "a", refused "x" then "y" gives "abyc").

Expected behavior 🎯

  • TextCompositionChanged is raised once its text is in Text, after TextChanging, and its range fits the text, as does that of a composition ending without a commit. TextCompositionStarted stays before the text, as on WinUI.
  • The browser's text doesn't land in a TextBox that a composition event handler moved focus to.
  • A composition ends (IsComposing false, TextCompositionEnded raised) when its input goes away.
  • An edit BeforeTextChanging cancels is written back to the hidden input right away, so the keyboard continues from the TextBox's text. (Edits MaxLength or a TextChanging handler limit can stay reconciled when the composition ends, so a conversion can bring the composition back within the limit.)
  • A read-only TextBox doesn't bring up the soft keyboard; keyboard navigation, selection and copy keep working.
  • The caret is back where a refused edit started before the next key.

Out of scope here: on Skia Android, TextCompositionEnded for a commit is raised before the committed text is synced, with the committed text's own range (not clamped, since clamping it to the stale text would cut a longer commit short).

How to reproduce it (as minimally and precisely as possible) 🔬

Skia WebAssembly, Chrome on Android (Gboard or Samsung Keyboard), or the runtime tests that replay the browser's event sequences on the hidden input:

<TextBox x:Name="tb" />
tb.TextCompositionChanged += (s, e) => Debug.WriteLine(tb.Text.Substring(e.StartIndex, e.Length)); // throws

Type a word with the soft keyboard. For 4: tb.BeforeTextChanging += (s, e) => e.Cancel = e.NewText.Contains('x'); then type axb. For 5: set IsReadOnly="True" and tap the TextBox.

Workaround 🛠️

Read the composed text in TextChanged instead of TextCompositionChanged; avoid cancelling edits in BeforeTextChanging during a composition.

Affected platforms 📱💻🖥️

WebAssembly, Android (Skia)

Uno.Sdk version (and other relevant versions) 📦

master after #25804 (Uno 7.0 dev)

IDE version 🧑‍💻

n/a

Anything else we need to know? 💬

#25804 lists "TextCompositionStarted/Changed are raised before Text contains the composition, on WebAssembly and on Skia Android" as a known follow-up. Analysis of the remaining cases: #24502 (comment)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions