Skip to content

Fix SelectableTextBlock selection offset with InlineUIContainer - #21646

Merged
MrJul merged 2 commits into
AvaloniaUI:masterfrom
JoshuaRowePhantom:fix/selectabletextblock-inlineuicontainer-selection
Jul 6, 2026
Merged

MrJul merged 2 commits into
AvaloniaUI:masterfrom
JoshuaRowePhantom:fix/selectabletextblock-inlineuicontainer-selection

Conversation

@JoshuaRowePhantom

Copy link
Copy Markdown
Contributor

Fixes #15880

Problem

EmbeddedControlRun (created by InlineUIContainer.BuildTextRun) inherits TextRun.DefaultTextSourceLength = 1, so each embedded control occupies one position in the TextLayout. However, InlineUIContainer.AppendText was a no-op, meaning Inlines.Text had zero characters for each embedded control.

This caused TextLayout.HitTestPoint to return positions that were ahead of Inlines.Text indices by one for every InlineUIContainer before the cursor, breaking SelectedText for any text after an embedded control.

Fix

Append the Unicode Object Replacement Character (U+FFFC) in InlineUIContainer.AppendText — the standard placeholder for embedded objects (used by WPF for the same purpose) — so that Inlines.Text character offsets stay in sync with TextLayout positions.

Testing

Added a unit test to SelectableTextBlockTests that verifies SelectedText returns the correct value when the selection range spans text after an InlineUIContainer.

JoshuaRowePhantom and others added 2 commits June 24, 2026 14:00
…oniaUI#15880)

EmbeddedControlRun inherits TextRun.DefaultTextSourceLength = 1, so each
InlineUIContainer occupies one position in TextLayout. However,
InlineUIContainer.AppendText was a no-op, causing Inlines.Text to have
zero characters for each embedded control. This made TextLayout.HitTestPoint
return positions that were ahead of Inlines.Text indices by one per
InlineUIContainer, breaking SelectedText for any text after an embedded
control.

Fix: append the Unicode Object Replacement Character (U+FFFC) — the
standard placeholder for embedded objects — in AppendText so that
Inlines.Text character offsets stay in sync with the TextLayout positions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace single assertion with a [Theory] covering all boundary positions
around the embedded control in 'foo\uFFFCbar':
- entirely before the container
- exactly the container character
- up to and including the container
- starting exactly after the container
- container through end
- one character either side of the container
- entire content

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@JoshuaRowePhantom
JoshuaRowePhantom marked this pull request as ready for review June 24, 2026 21:44
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.1.999-cibuild0066912-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@cla-avalonia

cla-avalonia commented Jun 24, 2026 •

Copy link
Copy Markdown
Collaborator
  • All contributors have signed the CLA.

@MrJul MrJul added bug backport-candidate-12.0.x Consider this PR for backporting to 12.0 branch labels Jun 25, 2026
@JoshuaRowePhantom

Copy link
Copy Markdown
Contributor Author

@cla-avalonia agree

@MrJul
MrJul requested a review from Gillibald July 6, 2026 09:27

@Gillibald Gillibald left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

RichTextEditor(FlowDocument) does the same

LGTM

@MrJul
MrJul added this pull request to the merge queue Jul 6, 2026
Merged via the queue into AvaloniaUI:master with commit 9becf5d Jul 6, 2026
12 checks passed
@MrJul MrJul removed the backport-candidate-12.0.x Consider this PR for backporting to 12.0 branch label Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InlineUIContainer breaks SelectableTextBlock selection

5 participants