Skip to content

Hide edges from IME during SetText - #12148

Open
miloush wants to merge 1 commit into
dotnet:mainfrom
miloush:InlineBlockRtbCrash
Open

miloush wants to merge 1 commit into
dotnet:mainfrom
miloush:InlineBlockRtbCrash

Conversation

@miloush

@miloush miloush commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3630

/cc @pranav-gupta-msft @amarinov-msft

Description

The issue is that some edits require changes to the document structure. In #3630, such edit is inserting text in front of the first BlockUIContainer, which needs to insert a Paragraph there to hold the text. When the edit comes from an IME, we are under lock in which we are not allowed to announce changes to the document. IME has two ways to request edit: InsertTextAtSelection and SetText. The former API has a structure allowing us to communicate back to the IME the changes we had to do. SetText does not. The current behavior then is we do the necessary changes, cannot tell anyone, character offsets get out of sync and we fail fast when we find out. "Characters" here includes special characters representing a start of an element in a document ("left edge").

The fix is we hide the newly created elements from the IME (defer their edges) until we are allowed to announce changes, at which point we raise the normal events about document changes. A tree keeps a track of list of elements that have an deferred edge.

All new code reachable from SetText has previously caused crash, no existing behavior should be changed.

Now InsertTextAtSelection includes change in behavior that is strictly not required to fix the crash. When the fix is taken, non-IME typing and SetText behave the same - when you type over a selection, the selection is replaced as expected. That is not currently the case with InsertTextAtSelection which collapses the selection first (or in same cases rejects the edit). That seems wrong, so I aligned the behavior with the other two methods, but again all unaffected scenarios should go through existing code path.

Finally, when user types something through IME and we have to create a Paragraph for it, cancelling the composition does not remove the inserted Paragraph. This is similar to typing without IME where backspace also does not remove the inserted Paragraph. It is possible to keep track of the implicit Paragraph and automatically remove it when composition is cancelled, but it was too much extra code for minimal benefit.

This is one of the more complex parts of WPF and I am not married to the solution. If anyone has better suggestions how to address this (I tried several), chime in.

Customer Impact

Without this fix, customer applications will crash / fail fast with ExecutionEngineException whenever typing using IME when the cursor is at a BlockUIContainer boundary.

Regression

No. Repros in .NET Framework.

Testing

Tested few scenarios using Microsoft rule-based TSF (Tamil Anjali) including the case in #3630 which crashes without the fix and does not crash with the fix. I also tested it with voice input, as some code paths do not execute when typing on keyboard (namely if there is a preexisting selection, TSF first calls TestKeyDown, which translates into our OnPreviewKeyDown, where we find out we have a selection, we delete it, tell the IME, which sees it as a conflict and does not start a composition - voice does not send key downs).

Tested using locally built WPF x64 using 12.0.100-alpha.1.26569.103 SDK.

It needs to be said that there is a lot of test cases and it is very likely I missed some. I think typing over pre-selected Tables still causes a crash, and I think IME trying to insert new lines into Hyperlnk will cause crash, but I haven't tested those as that is not related to the reported issue.

Risk

All new and changed code should execute only in a specific case - the caret or selection touches a BlockUIContainer edge. Most of the new code only runs in scenarios where we currently crash. Slight change of behavior when IME types over selection using InsertTextAtSelection to align it with other methods of input (and the expectation that typing over a selection replaces it).

Microsoft Reviewers: Open in CodeFlow

@miloush
miloush requested a review from a team November 19, 2026 23:02
@miloush
miloush requested a review from a team as a code owner November 19, 2026 23:02
@dotnet-policy-service dotnet-policy-service Bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Nov 19, 2026
@lindexi

lindexi commented Nov 24, 2026

Copy link
Copy Markdown
Member

I cherry-picked your changes into my WpfRuntime repo, the GitHub Actions CI build passed and produced a NuGet package. You can now install the following package in your project to test out / use your fix:

<PackageReference Include="WpfLab.WpfRuntime" Version="1.1.0.1-test.20261123132506.55ca5e" />

See: WpfLab/WpfRuntime#46

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

Labels

Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage Queue for test pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RichTextBox insert text application will crash

3 participants