7
7
#include < vector>
8
8
9
9
#include " atom/common/native_mate_converters/string16_converter.h"
10
+ #include " base/logging.h"
10
11
#include " native_mate/converter.h"
12
+ #include " native_mate/dictionary.h"
13
+ #include " third_party/icu/source/common/unicode/uscript.h"
11
14
#include " third_party/WebKit/public/web/WebTextCheckingCompletion.h"
15
+ #include " third_party/WebKit/public/web/WebTextCheckingResult.h"
12
16
13
17
#include " atom/common/node_includes.h"
14
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
+
15
35
namespace atom {
16
36
17
37
namespace api {
18
38
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
+
19
56
SpellCheckClient::SpellCheckClient (v8::Isolate* isolate,
20
57
v8::Handle<v8::Object> provider)
21
58
: isolate_(isolate), provider_(isolate, provider) {}
@@ -27,30 +64,53 @@ void SpellCheckClient::spellCheck(
27
64
int & misspelledOffset,
28
65
int & misspelledLength,
29
66
blink::WebVector<blink::WebString>* optionalSuggestions) {
30
- std::vector< int > result;
67
+ blink::WebTextCheckingResult result;
31
68
if (!CallProviderMethod (" spellCheck" , text, &result))
32
69
return ;
33
70
34
- if (result.size () != 2 )
35
- return ;
36
-
37
- misspelledOffset = result[0 ];
38
- misspelledLength = result[1 ];
71
+ misspelledOffset = result.location ;
72
+ misspelledLength = result.length ;
39
73
}
40
74
41
75
void SpellCheckClient::checkTextOfParagraph (
42
76
const blink::WebString& text,
43
77
blink::WebTextCheckingTypeMask mask,
44
78
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" ;
45
86
}
46
87
47
88
void SpellCheckClient::requestCheckingOfText (
48
89
const blink::WebString& textToCheck,
49
90
const blink::WebVector<uint32_t >& markersInText,
50
91
const blink::WebVector<unsigned >& markerOffsets,
51
92
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 )) {
53
102
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 ;
54
114
}
55
115
56
116
blink::WebString SpellCheckClient::autoCorrectWord (
0 commit comments