@@ -54,22 +54,53 @@ bool HasWordCharacters(const base::string16& text, int index) {
54
54
} // namespace
55
55
56
56
SpellCheckClient::SpellCheckClient (v8::Isolate* isolate,
57
+ const std::string& language,
57
58
v8::Handle<v8::Object> provider)
58
- : isolate_(isolate), provider_(isolate, provider) {}
59
+ : isolate_(isolate), provider_(isolate, provider) {
60
+ character_attributes_.SetDefaultLanguage (language);
61
+
62
+ // Persistent the method.
63
+ mate::Dictionary dict (isolate, provider);
64
+ dict.Get (" spellCheck" , &spell_check_);
65
+ }
59
66
60
67
SpellCheckClient::~SpellCheckClient () {}
61
68
62
69
void SpellCheckClient::spellCheck (
63
70
const blink::WebString& text,
64
- int & misspelledOffset,
65
- int & misspelledLength,
66
- blink::WebVector<blink::WebString>* optionalSuggestions) {
67
- blink::WebTextCheckingResult result;
68
- if (!CallProviderMethod (" spellCheck" , text, &result))
71
+ int & misspelling_start,
72
+ int & misspelling_len,
73
+ blink::WebVector<blink::WebString>* optional_suggestions) {
74
+ if (text.length () == 0 || spell_check_.IsEmpty ())
69
75
return ;
70
76
71
- misspelledOffset = result.location ;
72
- misspelledLength = result.length ;
77
+ base::string16 word;
78
+ int word_start;
79
+ int word_length;
80
+ if (!text_iterator_.IsInitialized () &&
81
+ !text_iterator_.Initialize (&character_attributes_, true )) {
82
+ // We failed to initialize text_iterator_, return as spelled correctly.
83
+ VLOG (1 ) << " Failed to initialize SpellcheckWordIterator" ;
84
+ return ;
85
+ }
86
+
87
+ base::string16 in_word (text);
88
+ text_iterator_.SetText (in_word.c_str (), in_word.size ());
89
+ while (text_iterator_.GetNextWord (&word, &word_start, &word_length)) {
90
+ // Found a word (or a contraction) that the spellchecker can check the
91
+ // spelling of.
92
+ if (CheckSpelling (word))
93
+ continue ;
94
+
95
+ // If the given word is a concatenated word of two or more valid words
96
+ // (e.g. "hello:hello"), we should treat it as a valid word.
97
+ if (IsValidContraction (word))
98
+ continue ;
99
+
100
+ misspelling_start = word_start;
101
+ misspelling_len = word_length;
102
+ return ;
103
+ }
73
104
}
74
105
75
106
void SpellCheckClient::checkTextOfParagraph (
@@ -90,13 +121,6 @@ void SpellCheckClient::requestCheckingOfText(
90
121
const blink::WebVector<uint32_t >& markersInText,
91
122
const blink::WebVector<unsigned >& markerOffsets,
92
123
blink::WebTextCheckingCompletion* completionCallback) {
93
- v8::HandleScope handle_scope (isolate_);
94
- v8::Handle<v8::Object> provider = provider_.NewHandle ();
95
- if (!provider->Has (mate::StringToV8 (isolate_, " requestCheckingOfText" ))) {
96
- completionCallback->didCancelCheckingText ();
97
- return ;
98
- }
99
-
100
124
base::string16 text (textToCheck);
101
125
if (text.empty () || !HasWordCharacters (text, 0 )) {
102
126
completionCallback->didCancelCheckingText ();
@@ -151,6 +175,46 @@ bool SpellCheckClient::CallProviderMethod(const char* method,
151
175
return mate::ConvertFromV8 (isolate_, v8_result, result);;
152
176
}
153
177
178
+ bool SpellCheckClient::CheckSpelling (const base::string16& word_to_check) {
179
+ if (spell_check_.IsEmpty ())
180
+ return true ;
181
+
182
+ v8::HandleScope handle_scope (isolate_);
183
+ v8::Handle<v8::Value> word = mate::ConvertToV8 (isolate_, word_to_check);
184
+ v8::Handle<v8::Value> result = spell_check_.NewHandle ()->Call (
185
+ provider_.NewHandle (), 1 , &word);
186
+
187
+ if (result->IsBoolean ())
188
+ return result->BooleanValue ();
189
+ else
190
+ return true ;
191
+ }
192
+
193
+ // Returns whether or not the given string is a valid contraction.
194
+ // This function is a fall-back when the SpellcheckWordIterator class
195
+ // returns a concatenated word which is not in the selected dictionary
196
+ // (e.g. "in'n'out") but each word is valid.
197
+ bool SpellCheckClient::IsValidContraction (const base::string16& contraction) {
198
+ if (!contraction_iterator_.IsInitialized () &&
199
+ !contraction_iterator_.Initialize (&character_attributes_, false )) {
200
+ // We failed to initialize the word iterator, return as spelled correctly.
201
+ VLOG (1 ) << " Failed to initialize contraction_iterator_" ;
202
+ return true ;
203
+ }
204
+
205
+ contraction_iterator_.SetText (contraction.c_str (), contraction.length ());
206
+
207
+ base::string16 word;
208
+ int word_start;
209
+ int word_length;
210
+
211
+ while (contraction_iterator_.GetNextWord (&word, &word_start, &word_length)) {
212
+ if (!CheckSpelling (word))
213
+ return false ;
214
+ }
215
+ return true ;
216
+ }
217
+
154
218
} // namespace api
155
219
156
220
} // namespace atom
0 commit comments