4
4
5
5
#include " atom/renderer/api/atom_api_spell_check_client.h"
6
6
7
+ #include < algorithm>
7
8
#include < vector>
8
9
9
10
#include " atom/common/native_mate_converters/string16_converter.h"
14
15
#include " third_party/WebKit/public/web/WebTextCheckingCompletion.h"
15
16
#include " third_party/WebKit/public/web/WebTextCheckingResult.h"
16
17
17
- #include " atom/common/node_includes.h"
18
-
19
- namespace mate {
20
-
21
- template <>
22
- struct Converter <blink::WebTextCheckingResult> {
23
- static bool FromV8 (v8::Isolate* isolate, v8::Handle<v8::Value> val,
24
- blink::WebTextCheckingResult* out) {
25
- mate::Dictionary dict;
26
- if (!ConvertFromV8 (isolate, val, &dict))
27
- return false ;
28
- return dict.Get (" location" , &(out->location )) &&
29
- dict.Get (" length" , &(out->length ));
30
- }
31
- };
32
-
33
- } // namespace mate
34
-
35
18
namespace atom {
36
19
37
20
namespace api {
@@ -76,35 +59,11 @@ void SpellCheckClient::spellCheck(
76
59
int & misspelling_start,
77
60
int & misspelling_len,
78
61
blink::WebVector<blink::WebString>* optional_suggestions) {
79
- if (text.length () == 0 || spell_check_.IsEmpty ())
80
- return ;
81
-
82
- base::string16 word;
83
- int word_start;
84
- int word_length;
85
- if (!text_iterator_.IsInitialized () &&
86
- !text_iterator_.Initialize (&character_attributes_, true )) {
87
- // We failed to initialize text_iterator_, return as spelled correctly.
88
- VLOG (1 ) << " Failed to initialize SpellcheckWordIterator" ;
89
- return ;
90
- }
91
-
92
- base::string16 in_word (text);
93
- text_iterator_.SetText (in_word.c_str (), in_word.size ());
94
- while (text_iterator_.GetNextWord (&word, &word_start, &word_length)) {
95
- // Found a word (or a contraction) that the spellchecker can check the
96
- // spelling of.
97
- if (CheckSpelling (word))
98
- continue ;
99
-
100
- // If the given word is a concatenated word of two or more valid words
101
- // (e.g. "hello:hello"), we should treat it as a valid word.
102
- if (IsValidContraction (word))
103
- continue ;
104
-
105
- misspelling_start = word_start;
106
- misspelling_len = word_length;
107
- return ;
62
+ std::vector<blink::WebTextCheckingResult> results;
63
+ SpellCheckText (base::string16 (text), true , &results);
64
+ if (results.size () == 1 ) {
65
+ misspelling_start = results[0 ].location ;
66
+ misspelling_len = results[0 ].length ;
108
67
}
109
68
}
110
69
@@ -132,14 +91,9 @@ void SpellCheckClient::requestCheckingOfText(
132
91
return ;
133
92
}
134
93
135
- std::vector<blink::WebTextCheckingResult> result;
136
- if (!CallProviderMethod (" requestCheckingOfText" , textToCheck, &result)) {
137
- completionCallback->didCancelCheckingText ();
138
- return ;
139
- }
140
-
141
- completionCallback->didFinishCheckingText (result);
142
- return ;
94
+ std::vector<blink::WebTextCheckingResult> results;
95
+ SpellCheckText (text, false , &results);
96
+ completionCallback->didFinishCheckingText (results);
143
97
}
144
98
145
99
blink::WebString SpellCheckClient::autoCorrectWord (
@@ -161,25 +115,47 @@ void SpellCheckClient::updateSpellingUIWithMisspelledWord(
161
115
const blink::WebString& word) {
162
116
}
163
117
164
- template <class T >
165
- bool SpellCheckClient::CallProviderMethod (const char * method,
166
- const blink::WebString& text,
167
- T* result) {
168
- v8::HandleScope handle_scope (isolate_);
118
+ void SpellCheckClient::SpellCheckText (
119
+ const base::string16& text,
120
+ bool stop_at_first_result,
121
+ std::vector<blink::WebTextCheckingResult>* results) {
122
+ if (text.length () == 0 || spell_check_.IsEmpty ())
123
+ return ;
169
124
170
- v8::Handle<v8::Object> provider = provider_.NewHandle ();
171
- if (!provider->Has (mate::StringToV8 (isolate_, method)))
172
- return false ;
125
+ base::string16 word;
126
+ int word_start;
127
+ int word_length;
128
+ if (!text_iterator_.IsInitialized () &&
129
+ !text_iterator_.Initialize (&character_attributes_, true )) {
130
+ // We failed to initialize text_iterator_, return as spelled correctly.
131
+ VLOG (1 ) << " Failed to initialize SpellcheckWordIterator" ;
132
+ return ;
133
+ }
173
134
174
- v8::Handle<v8::Value> v8_str =
175
- mate::ConvertToV8 (isolate_, base::string16 (text));
176
- v8::Handle<v8::Value> v8_result =
177
- node::MakeCallback (isolate_, provider, method, 1 , &v8_str);
135
+ base::string16 in_word (text);
136
+ text_iterator_.SetText (in_word.c_str (), in_word.size ());
137
+ while (text_iterator_.GetNextWord (&word, &word_start, &word_length)) {
138
+ // Found a word (or a contraction) that the spellchecker can check the
139
+ // spelling of.
140
+ if (SpellCheckWord (word))
141
+ continue ;
178
142
179
- return mate::ConvertFromV8 (isolate_, v8_result, result);;
143
+ // If the given word is a concatenated word of two or more valid words
144
+ // (e.g. "hello:hello"), we should treat it as a valid word.
145
+ if (IsValidContraction (word))
146
+ continue ;
147
+
148
+ blink::WebTextCheckingResult result;
149
+ result.location = word_start;
150
+ result.length = word_length;
151
+ results->push_back (result);
152
+
153
+ if (stop_at_first_result)
154
+ return ;
155
+ }
180
156
}
181
157
182
- bool SpellCheckClient::CheckSpelling (const base::string16& word_to_check) {
158
+ bool SpellCheckClient::SpellCheckWord (const base::string16& word_to_check) {
183
159
if (spell_check_.IsEmpty ())
184
160
return true ;
185
161
@@ -260,7 +236,7 @@ bool SpellCheckClient::IsValidContraction(const base::string16& contraction) {
260
236
int word_length;
261
237
262
238
while (contraction_iterator_.GetNextWord (&word, &word_start, &word_length)) {
263
- if (!CheckSpelling (word))
239
+ if (!SpellCheckWord (word))
264
240
return false ;
265
241
}
266
242
return true ;
0 commit comments