Skip to content

Commit 2e6aac6

Browse files
authored
Fix spell check throws when text contains regex reserved characters (#140384)
## Description This PR fixes an issue related to the spell check implementation usage of Regex (searched text should be escaped). ## Related Issue Fixes flutter/flutter#136032. ## Tests Adds 1 test.
1 parent b2ef280 commit 2e6aac6

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

packages/flutter/lib/src/widgets/spell_check.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ List<SuggestionSpan> _correctSpellCheckResults(
134134
final int spanLength = currentSpan.range.end - currentSpan.range.start;
135135

136136
// Try finding SuggestionSpan from resultsText in new text.
137-
final RegExp currentSpanTextRegexp = RegExp('\\b$currentSpanText\\b');
137+
final String escapedText = RegExp.escape(currentSpanText);
138+
final RegExp currentSpanTextRegexp = RegExp('\\b$escapedText\\b');
138139
final int foundIndex = newText.substring(searchStart).indexOf(currentSpanTextRegexp);
139140

140141
// Check whether word was found exactly where expected or elsewhere in the newText.

packages/flutter/test/widgets/spell_check_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,35 @@ void main() {
353353

354354
expect(textSpanTree, equals(expectedTextSpanTree));
355355
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android, TargetPlatform.iOS }));
356+
357+
testWidgets(
358+
'buildTextSpanWithSpellCheckSuggestions does not throw when text contains regex reserved characters',
359+
(WidgetTester tester) async {
360+
// Regression test for https://github.com/flutter/flutter/issues/136032.
361+
const String text = 'Hello, *ãresaa';
362+
const String resultsText = 'Hello, *ãresa';
363+
const TextEditingValue value = TextEditingValue(text: text);
364+
const bool composingRegionOutOfRange = false;
365+
const SpellCheckResults spellCheckResults = SpellCheckResults(
366+
resultsText,
367+
<SuggestionSpan>[
368+
SuggestionSpan(TextRange(start: 7, end: 12), <String>['*rangesa']),
369+
],
370+
);
371+
372+
const TextSpan expectedTextSpanTree = TextSpan(children: <TextSpan>[
373+
TextSpan(text: 'Hello, *ãresaa'),
374+
]);
375+
final TextSpan textSpanTree = buildTextSpanWithSpellCheckSuggestions(
376+
value,
377+
composingRegionOutOfRange,
378+
null,
379+
misspelledTextStyle,
380+
spellCheckResults,
381+
);
382+
383+
expect(tester.takeException(), null);
384+
expect(textSpanTree, equals(expectedTextSpanTree));
385+
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android, TargetPlatform.iOS }),
386+
);
356387
}

0 commit comments

Comments
 (0)