Skip to content

Commit 7e23c54

Browse files
[SuperEditor] - Fix crash with selected color strategy in an empty paragraph (Resolves #2253) (#2539)
1 parent 6461b7d commit 7e23c54

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

super_editor/lib/src/default_editor/layout_single_column/_styler_user_selection.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,22 @@ class SingleColumnLayoutSelectionStyler extends SingleColumnLayoutStylePhase {
134134
if (viewModel is TextComponentViewModel) {
135135
final componentTextColor = viewModel.textStyleBuilder({}).color;
136136

137-
final textWithSelectionAttributions =
138-
textSelection != null && _selectedTextColorStrategy != null && componentTextColor != null
139-
? (viewModel.text.copyText(0)
140-
..addAttribution(
141-
ColorAttribution(_selectedTextColorStrategy!(
142-
originalTextColor: componentTextColor,
143-
selectionHighlightColor: _selectionStyles.selectionColor,
144-
)),
145-
SpanRange(textSelection.start, textSelection.end - 1),
146-
// The selected range might already have a color attribution. We want to override it
147-
// with the selected text color.
148-
overwriteConflictingSpans: true,
149-
))
150-
: viewModel.text;
137+
final textWithSelectionAttributions = textSelection != null &&
138+
!textSelection.isCollapsed &&
139+
_selectedTextColorStrategy != null &&
140+
componentTextColor != null
141+
? (viewModel.text.copyText(0)
142+
..addAttribution(
143+
ColorAttribution(_selectedTextColorStrategy!(
144+
originalTextColor: componentTextColor,
145+
selectionHighlightColor: _selectionStyles.selectionColor,
146+
)),
147+
SpanRange(textSelection.start, textSelection.end - 1),
148+
// The selected range might already have a color attribution. We want to override it
149+
// with the selected text color.
150+
overwriteConflictingSpans: true,
151+
))
152+
: viewModel.text;
151153

152154
viewModel
153155
..text = textWithSelectionAttributions

super_editor/test/super_editor/supereditor_selection_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@ void main() {
8484
expect(richText.getSpanForPosition(const TextPosition(offset: 5))!.style!.color, Colors.green);
8585
expect(richText.getSpanForPosition(const TextPosition(offset: 16))!.style!.color, Colors.green);
8686
});
87+
88+
testWidgetsOnAllPlatforms("does not crash when triple tapping on an empty paragraph", (tester) async {
89+
// This test is to ensure that the selection color strategy doesn't crash when the paragraph is empty.
90+
// See https://github.com/superlistapp/super_editor/issues/2253 for more context.
91+
final stylesheet = defaultStylesheet.copyWith(
92+
selectedTextColorStrategy: ({required Color originalTextColor, required Color selectionHighlightColor}) {
93+
return Colors.white;
94+
},
95+
);
96+
97+
await tester //
98+
.createDocument()
99+
.withSingleEmptyParagraph()
100+
.useStylesheet(stylesheet)
101+
.pump();
102+
103+
// Tripple tap on the empty paragraph.
104+
await tester.tripleTapInParagraph("1", 0);
105+
106+
// Reaching this point means the editor didn't crash.
107+
});
87108
});
88109

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

0 commit comments

Comments
 (0)