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 https://github.com/flutter/flutter/issues/136032. ## Tests Adds 1 test.
This commit is contained in:
@@ -134,7 +134,8 @@ List<SuggestionSpan> _correctSpellCheckResults(
|
||||
final int spanLength = currentSpan.range.end - currentSpan.range.start;
|
||||
|
||||
// Try finding SuggestionSpan from resultsText in new text.
|
||||
final RegExp currentSpanTextRegexp = RegExp('\\b$currentSpanText\\b');
|
||||
final String escapedText = RegExp.escape(currentSpanText);
|
||||
final RegExp currentSpanTextRegexp = RegExp('\\b$escapedText\\b');
|
||||
final int foundIndex = newText.substring(searchStart).indexOf(currentSpanTextRegexp);
|
||||
|
||||
// Check whether word was found exactly where expected or elsewhere in the newText.
|
||||
|
||||
@@ -353,4 +353,35 @@ void main() {
|
||||
|
||||
expect(textSpanTree, equals(expectedTextSpanTree));
|
||||
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android, TargetPlatform.iOS }));
|
||||
|
||||
testWidgets(
|
||||
'buildTextSpanWithSpellCheckSuggestions does not throw when text contains regex reserved characters',
|
||||
(WidgetTester tester) async {
|
||||
// Regression test for https://github.com/flutter/flutter/issues/136032.
|
||||
const String text = 'Hello, *ãresaa';
|
||||
const String resultsText = 'Hello, *ãresa';
|
||||
const TextEditingValue value = TextEditingValue(text: text);
|
||||
const bool composingRegionOutOfRange = false;
|
||||
const SpellCheckResults spellCheckResults = SpellCheckResults(
|
||||
resultsText,
|
||||
<SuggestionSpan>[
|
||||
SuggestionSpan(TextRange(start: 7, end: 12), <String>['*rangesa']),
|
||||
],
|
||||
);
|
||||
|
||||
const TextSpan expectedTextSpanTree = TextSpan(children: <TextSpan>[
|
||||
TextSpan(text: 'Hello, *ãresaa'),
|
||||
]);
|
||||
final TextSpan textSpanTree = buildTextSpanWithSpellCheckSuggestions(
|
||||
value,
|
||||
composingRegionOutOfRange,
|
||||
null,
|
||||
misspelledTextStyle,
|
||||
spellCheckResults,
|
||||
);
|
||||
|
||||
expect(tester.takeException(), null);
|
||||
expect(textSpanTree, equals(expectedTextSpanTree));
|
||||
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android, TargetPlatform.iOS }),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user