Skip to content

Fix screen readers reading the wrong line on blank lines in textboxes (closes #7190)#11509

Open
trypsynth wants to merge 1 commit intodotnet:mainfrom
trypsynth:fix/screen-reader-blank-line-7190
Open

Fix screen readers reading the wrong line on blank lines in textboxes (closes #7190)#11509
trypsynth wants to merge 1 commit intodotnet:mainfrom
trypsynth:fix/screen-reader-blank-line-7190

Conversation

@trypsynth
Copy link

@trypsynth trypsynth commented Mar 7, 2026

Fixes #7190

Description

When navigating a TextBox or RichTextBox line-by-line with a screen reader (e.g. Narrator, NVDA), blank lines caused the next line's content to be announced instead of "blank", and that next line would then be announced again on the following navigation.

Root cause: In TextRangeAdaptor.ExpandToEnclosingUnit(TextUnit.Line), blank lines produce a degenerate lineRange where Start == End (ContentLength == 0). The condition lineRange.Contains(_end) fails when _end has already advanced past the blank line, so snapEndPosition stays true and the expandEnd block runs, bleeding _end into the next non-blank line. The resulting range spans both the blank line and the next line, so GetText() returns the next line's content.

Fix: add an additional guard clause for the degenerate case: When lineRange.Start == lineRange.End (blank line), snapEndPosition is set to false and _end is snapped to lineRange.End (same position), preventing expandEnd from running. GetText() returns "" and screen readers correctly announce "blank".

Covers TextBox (TextBoxView.GetLineRange returns [offset, offset] for blank lines) and RichTextBox (TextDocumentView returns a degenerate segment for empty paragraphs).

Customer Impact

Incredibly inconsistent and confusing behavior for screen reader users in WPF text boxes.

Regression

No, this has existed since at least 2011, when the issue was first raised against NVDA.

Testing

Built WPF, coppied in the required DLLs into a new wpf app created with dotnet new, added a text box with blank lines and content, and confirmed it reads correctly with NVDA, my screen reader.

Risk

None

Microsoft Reviewers: Open in CodeFlow

When navigating a TextBox or RichTextBox line-by-line with a screen
reader (Narrator, NVDA), blank lines caused the next line's content to
be announced instead of "blank", and that next line would then be
announced again on the following navigation.

Root cause: In TextRangeAdaptor.ExpandToEnclosingUnit(TextUnit.Line),
blank lines produce a degenerate lineRange where Start == End
(ContentLength == 0). The condition lineRange.Contains(_end) fails
when _end has already advanced past the blank line, so snapEndPosition
stays true and the expandEnd block runs, bleeding _end into the next
non-blank line. The resulting range spans both the blank line and the
next line, so GetText() returns the next line's content.

Fix: add an additional guard clause for the degenerate case:

  if (lineRange.Contains(_end) || lineRange.Start.CompareTo(lineRange.End) == 0)

When lineRange.Start == lineRange.End (blank line), snapEndPosition is
set to false and _end is snapped to lineRange.End (same position),
preventing expandEnd from running. GetText() returns "" and screen
readers correctly announce "blank".

Covers TextBox (TextBoxView.GetLineRange returns [offset, offset] for
blank lines) and RichTextBox (TextDocumentView returns a degenerate
segment for empty paragraphs).

Fixes dotnet#7190
@trypsynth trypsynth requested a review from a team March 7, 2026 19:16
@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 Mar 7, 2026
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Screen-reader reads lines twice in a TextBox / TextBlock / RichTextBox

1 participant