@@ -38,6 +38,8 @@ namespace api {
38
38
39
39
namespace {
40
40
41
+ const int kMaxAutoCorrectWordSize = 8 ;
42
+
41
43
bool HasWordCharacters (const base::string16& text, int index) {
42
44
const base::char16* data = text.data ();
43
45
int length = text.length ();
@@ -139,11 +141,7 @@ void SpellCheckClient::requestCheckingOfText(
139
141
140
142
blink::WebString SpellCheckClient::autoCorrectWord (
141
143
const blink::WebString& misspelledWord) {
142
- base::string16 result;
143
- if (!CallProviderMethod (" autoCorrectWord" , misspelledWord, &result))
144
- return blink::WebString ();
145
-
146
- return result;
144
+ return GetAutoCorrectionWord (base::string16 (misspelledWord));
147
145
}
148
146
149
147
void SpellCheckClient::showSpellingUI (bool show) {
@@ -190,6 +188,53 @@ bool SpellCheckClient::CheckSpelling(const base::string16& word_to_check) {
190
188
return true ;
191
189
}
192
190
191
+ base::string16 SpellCheckClient::GetAutoCorrectionWord (
192
+ const base::string16& word) {
193
+ base::string16 autocorrect_word;
194
+
195
+ int word_length = static_cast <int >(word.size ());
196
+ if (word_length < 2 || word_length > kMaxAutoCorrectWordSize )
197
+ return autocorrect_word;
198
+
199
+ base::char16 misspelled_word[kMaxAutoCorrectWordSize + 1 ];
200
+ const base::char16* word_char = word.c_str ();
201
+ for (int i = 0 ; i <= kMaxAutoCorrectWordSize ; ++i) {
202
+ if (i >= word_length)
203
+ misspelled_word[i] = 0 ;
204
+ else
205
+ misspelled_word[i] = word_char[i];
206
+ }
207
+
208
+ // Swap adjacent characters and spellcheck.
209
+ int misspelling_start, misspelling_len;
210
+ for (int i = 0 ; i < word_length - 1 ; i++) {
211
+ // Swap.
212
+ std::swap (misspelled_word[i], misspelled_word[i + 1 ]);
213
+
214
+ // Check spelling.
215
+ misspelling_start = misspelling_len = 0 ;
216
+ spellCheck (blink::WebString (misspelled_word, word_length),
217
+ misspelling_start,
218
+ misspelling_len,
219
+ NULL );
220
+
221
+ // Make decision: if only one swap produced a valid word, then we want to
222
+ // return it. If we found two or more, we don't do autocorrection.
223
+ if (misspelling_len == 0 ) {
224
+ if (autocorrect_word.empty ()) {
225
+ autocorrect_word.assign (misspelled_word);
226
+ } else {
227
+ autocorrect_word.clear ();
228
+ break ;
229
+ }
230
+ }
231
+
232
+ // Restore the swapped characters.
233
+ std::swap (misspelled_word[i], misspelled_word[i + 1 ]);
234
+ }
235
+ return autocorrect_word;
236
+ }
237
+
193
238
// Returns whether or not the given string is a valid contraction.
194
239
// This function is a fall-back when the SpellcheckWordIterator class
195
240
// returns a concatenated word which is not in the selected dictionary
0 commit comments