Skip to content

Commit b801a93

Browse files
committed
Also expose requestCheckingOfText
1 parent a61331a commit b801a93

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

atom/renderer/api/atom_api_spell_check_client.cc

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,52 @@
77
#include <vector>
88

99
#include "atom/common/native_mate_converters/string16_converter.h"
10+
#include "base/logging.h"
1011
#include "native_mate/converter.h"
12+
#include "native_mate/dictionary.h"
13+
#include "third_party/icu/source/common/unicode/uscript.h"
1114
#include "third_party/WebKit/public/web/WebTextCheckingCompletion.h"
15+
#include "third_party/WebKit/public/web/WebTextCheckingResult.h"
1216

1317
#include "atom/common/node_includes.h"
1418

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+
1535
namespace atom {
1636

1737
namespace api {
1838

39+
namespace {
40+
41+
bool HasWordCharacters(const base::string16& text, int index) {
42+
const base::char16* data = text.data();
43+
int length = text.length();
44+
while (index < length) {
45+
uint32 code = 0;
46+
U16_NEXT(data, index, length, code);
47+
UErrorCode error = U_ZERO_ERROR;
48+
if (uscript_getScript(code, &error) != USCRIPT_COMMON)
49+
return true;
50+
}
51+
return false;
52+
}
53+
54+
} // namespace
55+
1956
SpellCheckClient::SpellCheckClient(v8::Isolate* isolate,
2057
v8::Handle<v8::Object> provider)
2158
: isolate_(isolate), provider_(isolate, provider) {}
@@ -27,30 +64,53 @@ void SpellCheckClient::spellCheck(
2764
int& misspelledOffset,
2865
int& misspelledLength,
2966
blink::WebVector<blink::WebString>* optionalSuggestions) {
30-
std::vector<int> result;
67+
blink::WebTextCheckingResult result;
3168
if (!CallProviderMethod("spellCheck", text, &result))
3269
return;
3370

34-
if (result.size() != 2)
35-
return;
36-
37-
misspelledOffset = result[0];
38-
misspelledLength = result[1];
71+
misspelledOffset = result.location;
72+
misspelledLength = result.length;
3973
}
4074

4175
void SpellCheckClient::checkTextOfParagraph(
4276
const blink::WebString& text,
4377
blink::WebTextCheckingTypeMask mask,
4478
blink::WebVector<blink::WebTextCheckingResult>* results) {
79+
if (!results)
80+
return;
81+
82+
if (!(mask & blink::WebTextCheckingTypeSpelling))
83+
return;
84+
85+
NOTREACHED() << "checkTextOfParagraph should never be called";
4586
}
4687

4788
void SpellCheckClient::requestCheckingOfText(
4889
const blink::WebString& textToCheck,
4990
const blink::WebVector<uint32_t>& markersInText,
5091
const blink::WebVector<unsigned>& markerOffsets,
5192
blink::WebTextCheckingCompletion* completionCallback) {
52-
if (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+
base::string16 text(textToCheck);
101+
if (text.empty() || !HasWordCharacters(text, 0)) {
53102
completionCallback->didCancelCheckingText();
103+
return;
104+
}
105+
106+
std::vector<blink::WebTextCheckingResult> result;
107+
if (!CallProviderMethod("requestCheckingOfText", textToCheck, &result)) {
108+
completionCallback->didCancelCheckingText();
109+
return;
110+
}
111+
112+
completionCallback->didFinishCheckingText(result);
113+
return;
54114
}
55115

56116
blink::WebString SpellCheckClient::autoCorrectWord(

atom/renderer/api/atom_api_spell_check_client.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ class SpellCheckClient : public blink::WebSpellCheckClient {
4141
const blink::WebString& word) override;
4242

4343
template<class T>
44-
bool CallProviderMethod(const char* method,
45-
const blink::WebString& text,
44+
bool CallProviderMethod(const char* method, const blink::WebString& text,
4645
T* result);
4746

4847
v8::Isolate* isolate_;

0 commit comments

Comments
 (0)