Fix screen readers reading the wrong line on blank lines in textboxes (closes #7190)#11509
Open
trypsynth wants to merge 1 commit intodotnet:mainfrom
Open
Fix screen readers reading the wrong line on blank lines in textboxes (closes #7190)#11509trypsynth wants to merge 1 commit intodotnet:mainfrom
trypsynth wants to merge 1 commit intodotnet:mainfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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