Skip to content

fix(textblock): Return CRLF for explicit line breaks - #26706

Open
ZainnQureshii wants to merge 1 commit into
unoplatform:masterfrom
ZainnQureshii:fix/textblock-linebreak-crlf
Open

ZainnQureshii wants to merge 1 commit into
unoplatform:masterfrom
ZainnQureshii:fix/textblock-linebreak-crlf

Conversation

@ZainnQureshii

Copy link
Copy Markdown

GitHub Issue: closes #26417

PR Type:

🐞 Bugfix

What changed? 🚀

For <TextBlock>Line1<LineBreak/>Line2<LineBreak/><LineBreak/>Line4</TextBlock>, Uno returned "Line1\nLine2\n\nLine4". Code that splits the string on CRLF missed the explicit breaks. TextBlock.Text now returns "Line1\r\nLine2\r\n\r\nLine4", matching the WinUI output reported in #26417 (src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.cs:201). Literal line endings in a Run retain their original characters.

TextBlock.SelectedText, clipboard copy, and UI Automation ITextRangeProvider.GetText now also return CRLF for each explicit break. They previously returned LF. The selection and automation offsets still count each explicit break as one position, so selecting the word after a break still returns the whole word. RichTextBlock uses the same inline walk and continues to include its CRLF paragraph terminators (src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.cs:1994, src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.cs:2059, src/Uno.UI/DirectUI/TextRangeAdapter.cs:1031, src/Uno.UI/UI/Xaml/Controls/RichTextBlock/RichTextBlock.mux.cs:587, src/Uno.UI/UI/Xaml/Documents/InlineExtensions.cs:36).

UI Automation text search, movement, and endpoint calculations still use the LF offset space. TextBox, PasswordBox, and RichEditBox use their existing text paths. RichEditBox's internal display block uses runs and hyperlinks, so its known-text update path has no explicit LineBreak to expand (src/Uno.UI/DirectUI/TextAdapter.cs:46, src/Uno.UI/DirectUI/TextRangeAdapter.cs:627, src/Uno.UI/UI/Xaml/Controls/RichEditBox/RichEditBox.rendering.cs:695).

Layout and RichTextBlock overflow still read the flat inline text, so a CRLF export cannot shift their offsets (src/Uno.UI/UI/Xaml/Documents/ParsedText.cs:48, src/Uno.UI/UI/Xaml/Controls/RichTextBlock/RichTextBlockOverflow.mux.cs:436).

On macOS arm64 with .NET SDK 11.0.100, the two CRLF assertions failed independently on 3938657de (both exit 2). The final Release unit build had zero warnings and errors. The focused test passed with both assertions, the Range(0, 18) check, and the word range after the break. The related filter passed all 18 tests. The whitespace check reported zero changes across seven files. All final checks below exited 0.

dotnet build src/Uno.UI.UnitTests/Uno.UI.UnitTests.csproj -c Release -p:UnoTargetFrameworkOverride=net11.0 -p:GeneratePackageOnBuild=false -p:UnoFastDevBuild=false
dotnet test --project src/Uno.UI.UnitTests/Uno.UI.UnitTests.csproj -c Release -p:UnoTargetFrameworkOverride=net11.0 --no-build --filter 'FullyQualifiedName~Given_TextBlock.When_LineBreak_Is_Read_Through_Text'
dotnet test --project src/Uno.UI.UnitTests/Uno.UI.UnitTests.csproj -c Release -p:UnoTargetFrameworkOverride=net11.0 --no-build --filter 'FullyQualifiedName~.TextBlockTests.Given_TextBlock|FullyQualifiedName~.Given_InlineCollection_RunModel'
dotnet format whitespace --folder --verify-no-changes --verbosity diagnostic --include \
  src/Uno.UI.UnitTests/Windows_UI_XAML_Controls/TextBlockTests/Given_TextBlock.cs \
  src/Uno.UI/DirectUI/TextAdapter.cs \
  src/Uno.UI/DirectUI/TextRangeAdapter.cs \
  src/Uno.UI/UI/Xaml/Controls/RichTextBlock/RichTextBlock.cs \
  src/Uno.UI/UI/Xaml/Controls/RichTextBlock/RichTextBlock.mux.cs \
  src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.cs \
  src/Uno.UI/UI/Xaml/Documents/InlineExtensions.cs

The macOS unit test host used the native dylibs from Uno.icu-macos 77.4.1 in src/Uno.UI.UnitTests/bin/AnyCPU/Release/net11.0/.

Both Skia Desktop builds completed with the macOS native host compiled using Apple's Command Line Tools. On this host, the desktop app stalled at startup and produced no test report. A separate scratch probe passed before and after the change for RichTextBlock selection after and across an explicit break, paragraph endings, and a nested inline edit.

dotnet build src/SamplesApp/SamplesApp/SamplesApp.csproj -c Release -f net11.0-desktop -p:UnoFastDevBuild=true -p:UnoTargetFrameworkOverride=net11.0-desktop

PR Checklist ✅

Prepared with AI assistance (Codex and Claude Code) and reviewed with GitHub Copilot.

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings November 25, 2026 00:26
@CLAassistant

CLAassistant commented Nov 25, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@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 the area/automation Categorizes an issue or PR as relevant to project automation label Nov 25, 2026
@mergify

mergify Bot commented Nov 25, 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 once it meets the queue conditions

Copilot AI 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.

🟡 Changes recommended

Text searches spanning explicit line breaks can fail, and replacing equivalent exported text with different inline structures can leave selection offsets and SelectedText stale.

2 open issues
What changed in this PR

This PR addresses #26417 by exporting explicit LineBreak elements as CRLF from TextBlock, matching WinUI while retaining single-position breaks for layout and selection offsets.

Changes:

  • Separates exported text from the flat text used for offset calculations.
  • Shares inline text extraction with RichTextBlock and updates selection and UI Automation exports.
  • Adds regression coverage for literal line endings, nested breaks, selection, and automation ranges.
File Description
src/​Uno.UI/​UI/​Xaml/​Documents/​InlineExtensions.cs Adds shared CRLF export and selection extraction helpers.
src/​Uno.UI/​UI/​Xaml/​Controls/​TextBlock/​TextBlock.cs Exports CRLF while maintaining flat selection offsets.
src/​Uno.UI/​UI/​Xaml/​Controls/​RichTextBlock/​RichTextBlock.mux.cs Reuses the shared selection extraction helper.
src/​Uno.UI/​UI/​Xaml/​Controls/​RichTextBlock/​RichTextBlock.cs Reuses shared plain-text export logic.
src/​Uno.UI/​DirectUI/​TextRangeAdapter.cs Exports TextBlock automation ranges with CRLF breaks.
src/​Uno.UI/​DirectUI/​TextAdapter.cs Uses flat text and lengths for automation offsets.
src/​Uno.UI.UnitTests/​Windows_UI_XAML_Controls/​TextBlockTests/​Given_TextBlock.cs Adds CRLF, selection, and automation regression assertions.

Review effort: Balanced

PasswordBox passwordBox => new string('•', passwordBox.Password?.Length ?? 0),
TextBox textBox => textBox.Text ?? string.Empty,
TextBlock textBlock => textBlock.Text ?? string.Empty,
TextBlock textBlock => textBlock.TextForSelection ?? string.Empty,
Comment on lines +202 to +203
_inlineLineBreakCount = inlines.Count(static inline => inline is LineBreak);
_inlinesText = string.Concat(inlines.Select(InlineExtensions.GetPlainText));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/automation Categorizes an issue or PR as relevant to project automation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextBlock.Text reports a LineBreak as "\n" instead of "\r\n" like WinUI

3 participants