Skip to content

Commit e7dfd48

Browse files
committed
Spell check pasted text
1 parent 90b2d12 commit e7dfd48

File tree

2 files changed

+53
-75
lines changed

2 files changed

+53
-75
lines changed

atom/renderer/api/atom_api_spell_check_client.cc

Lines changed: 46 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "atom/renderer/api/atom_api_spell_check_client.h"
66

7+
#include <algorithm>
78
#include <vector>
89

910
#include "atom/common/native_mate_converters/string16_converter.h"
@@ -14,24 +15,6 @@
1415
#include "third_party/WebKit/public/web/WebTextCheckingCompletion.h"
1516
#include "third_party/WebKit/public/web/WebTextCheckingResult.h"
1617

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-
3518
namespace atom {
3619

3720
namespace api {
@@ -76,35 +59,11 @@ void SpellCheckClient::spellCheck(
7659
int& misspelling_start,
7760
int& misspelling_len,
7861
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;
10867
}
10968
}
11069

@@ -132,14 +91,9 @@ void SpellCheckClient::requestCheckingOfText(
13291
return;
13392
}
13493

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);
14397
}
14498

14599
blink::WebString SpellCheckClient::autoCorrectWord(
@@ -161,25 +115,47 @@ void SpellCheckClient::updateSpellingUIWithMisspelledWord(
161115
const blink::WebString& word) {
162116
}
163117

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;
169124

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+
}
173134

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;
178142

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+
}
180156
}
181157

182-
bool SpellCheckClient::CheckSpelling(const base::string16& word_to_check) {
158+
bool SpellCheckClient::SpellCheckWord(const base::string16& word_to_check) {
183159
if (spell_check_.IsEmpty())
184160
return true;
185161

@@ -260,7 +236,7 @@ bool SpellCheckClient::IsValidContraction(const base::string16& contraction) {
260236
int word_length;
261237

262238
while (contraction_iterator_.GetNextWord(&word, &word_start, &word_length)) {
263-
if (!CheckSpelling(word))
239+
if (!SpellCheckWord(word))
264240
return false;
265241
}
266242
return true;

atom/renderer/api/atom_api_spell_check_client.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define ATOM_RENDERER_API_ATOM_API_SPELL_CHECK_CLIENT_H_
77

88
#include <string>
9+
#include <vector>
910

1011
#include "base/callback.h"
1112
#include "chrome/renderer/spellchecker/spellcheck_worditerator.h"
@@ -47,12 +48,13 @@ class SpellCheckClient : public blink::WebSpellCheckClient {
4748
void updateSpellingUIWithMisspelledWord(
4849
const blink::WebString& word) override;
4950

50-
template<class T>
51-
bool CallProviderMethod(const char* method, const blink::WebString& text,
52-
T* result);
51+
// Check the spelling of text.
52+
void SpellCheckText(const base::string16& text,
53+
bool stop_at_first_result,
54+
std::vector<blink::WebTextCheckingResult>* results);
5355

54-
// Call JavaScript to check spelling.
55-
bool CheckSpelling(const base::string16& word_to_check);
56+
// Call JavaScript to check spelling a word.
57+
bool SpellCheckWord(const base::string16& word_to_check);
5658

5759
// Find a possible correctly spelled word for a misspelled word. Computes an
5860
// empty string if input misspelled word is too long, there is ambiguity, or

0 commit comments

Comments
 (0)