Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SuperEditor] Fix crash with selected color strategy in an empty paragraph (Resolves #2253) #2539

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,22 @@ class SingleColumnLayoutSelectionStyler extends SingleColumnLayoutStylePhase {
if (viewModel is TextComponentViewModel) {
final componentTextColor = viewModel.textStyleBuilder({}).color;

final textWithSelectionAttributions =
textSelection != null && _selectedTextColorStrategy != null && componentTextColor != null
? (viewModel.text.copyText(0)
..addAttribution(
ColorAttribution(_selectedTextColorStrategy!(
originalTextColor: componentTextColor,
selectionHighlightColor: _selectionStyles.selectionColor,
)),
SpanRange(textSelection.start, textSelection.end - 1),
// The selected range might already have a color attribution. We want to override it
// with the selected text color.
overwriteConflictingSpans: true,
))
: viewModel.text;
final textWithSelectionAttributions = textSelection != null &&
!textSelection.isCollapsed &&
_selectedTextColorStrategy != null &&
componentTextColor != null
? (viewModel.text.copyText(0)
..addAttribution(
ColorAttribution(_selectedTextColorStrategy!(
originalTextColor: componentTextColor,
selectionHighlightColor: _selectionStyles.selectionColor,
)),
SpanRange(textSelection.start, textSelection.end - 1),
// The selected range might already have a color attribution. We want to override it
// with the selected text color.
overwriteConflictingSpans: true,
))
: viewModel.text;

viewModel
..text = textWithSelectionAttributions
Expand Down
21 changes: 21 additions & 0 deletions super_editor/test/super_editor/supereditor_selection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ void main() {
expect(richText.getSpanForPosition(const TextPosition(offset: 5))!.style!.color, Colors.green);
expect(richText.getSpanForPosition(const TextPosition(offset: 16))!.style!.color, Colors.green);
});

testWidgetsOnAllPlatforms("does not crash when triple tapping on an empty paragraph", (tester) async {
// This test is to ensure that the selection color strategy doesn't crash when the paragraph is empty.
// See https://github.com/superlistapp/super_editor/issues/2253 for more context.
final stylesheet = defaultStylesheet.copyWith(
selectedTextColorStrategy: ({required Color originalTextColor, required Color selectionHighlightColor}) {
return Colors.white;
},
);

await tester //
.createDocument()
.withSingleEmptyParagraph()
.useStylesheet(stylesheet)
.pump();

// Tripple tap on the empty paragraph.
await tester.tripleTapInParagraph("1", 0);

// Reaching this point means the editor didn't crash.
});
});

testWidgetsOnArbitraryDesktop("calculates upstream document selection within a single node", (tester) async {
Expand Down
Loading