Also expose requestCheckingOfText
This commit is contained in:
parent
a61331a083
commit
b801a93dc5
2 changed files with 68 additions and 9 deletions
|
@ -7,15 +7,52 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||||
|
#include "base/logging.h"
|
||||||
#include "native_mate/converter.h"
|
#include "native_mate/converter.h"
|
||||||
|
#include "native_mate/dictionary.h"
|
||||||
|
#include "third_party/icu/source/common/unicode/uscript.h"
|
||||||
#include "third_party/WebKit/public/web/WebTextCheckingCompletion.h"
|
#include "third_party/WebKit/public/web/WebTextCheckingCompletion.h"
|
||||||
|
#include "third_party/WebKit/public/web/WebTextCheckingResult.h"
|
||||||
|
|
||||||
#include "atom/common/node_includes.h"
|
#include "atom/common/node_includes.h"
|
||||||
|
|
||||||
|
namespace mate {
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct Converter<blink::WebTextCheckingResult> {
|
||||||
|
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
|
||||||
|
blink::WebTextCheckingResult* out) {
|
||||||
|
mate::Dictionary dict;
|
||||||
|
if (!ConvertFromV8(isolate, val, &dict))
|
||||||
|
return false;
|
||||||
|
return dict.Get("location", &(out->location)) &&
|
||||||
|
dict.Get("length", &(out->length));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace mate
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
bool HasWordCharacters(const base::string16& text, int index) {
|
||||||
|
const base::char16* data = text.data();
|
||||||
|
int length = text.length();
|
||||||
|
while (index < length) {
|
||||||
|
uint32 code = 0;
|
||||||
|
U16_NEXT(data, index, length, code);
|
||||||
|
UErrorCode error = U_ZERO_ERROR;
|
||||||
|
if (uscript_getScript(code, &error) != USCRIPT_COMMON)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
SpellCheckClient::SpellCheckClient(v8::Isolate* isolate,
|
SpellCheckClient::SpellCheckClient(v8::Isolate* isolate,
|
||||||
v8::Handle<v8::Object> provider)
|
v8::Handle<v8::Object> provider)
|
||||||
: isolate_(isolate), provider_(isolate, provider) {}
|
: isolate_(isolate), provider_(isolate, provider) {}
|
||||||
|
@ -27,21 +64,25 @@ void SpellCheckClient::spellCheck(
|
||||||
int& misspelledOffset,
|
int& misspelledOffset,
|
||||||
int& misspelledLength,
|
int& misspelledLength,
|
||||||
blink::WebVector<blink::WebString>* optionalSuggestions) {
|
blink::WebVector<blink::WebString>* optionalSuggestions) {
|
||||||
std::vector<int> result;
|
blink::WebTextCheckingResult result;
|
||||||
if (!CallProviderMethod("spellCheck", text, &result))
|
if (!CallProviderMethod("spellCheck", text, &result))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (result.size() != 2)
|
misspelledOffset = result.location;
|
||||||
return;
|
misspelledLength = result.length;
|
||||||
|
|
||||||
misspelledOffset = result[0];
|
|
||||||
misspelledLength = result[1];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpellCheckClient::checkTextOfParagraph(
|
void SpellCheckClient::checkTextOfParagraph(
|
||||||
const blink::WebString& text,
|
const blink::WebString& text,
|
||||||
blink::WebTextCheckingTypeMask mask,
|
blink::WebTextCheckingTypeMask mask,
|
||||||
blink::WebVector<blink::WebTextCheckingResult>* results) {
|
blink::WebVector<blink::WebTextCheckingResult>* results) {
|
||||||
|
if (!results)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!(mask & blink::WebTextCheckingTypeSpelling))
|
||||||
|
return;
|
||||||
|
|
||||||
|
NOTREACHED() << "checkTextOfParagraph should never be called";
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpellCheckClient::requestCheckingOfText(
|
void SpellCheckClient::requestCheckingOfText(
|
||||||
|
@ -49,8 +90,27 @@ void SpellCheckClient::requestCheckingOfText(
|
||||||
const blink::WebVector<uint32_t>& markersInText,
|
const blink::WebVector<uint32_t>& markersInText,
|
||||||
const blink::WebVector<unsigned>& markerOffsets,
|
const blink::WebVector<unsigned>& markerOffsets,
|
||||||
blink::WebTextCheckingCompletion* completionCallback) {
|
blink::WebTextCheckingCompletion* completionCallback) {
|
||||||
if (completionCallback)
|
v8::HandleScope handle_scope(isolate_);
|
||||||
|
v8::Handle<v8::Object> provider = provider_.NewHandle();
|
||||||
|
if (!provider->Has(mate::StringToV8(isolate_, "requestCheckingOfText"))) {
|
||||||
completionCallback->didCancelCheckingText();
|
completionCallback->didCancelCheckingText();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
base::string16 text(textToCheck);
|
||||||
|
if (text.empty() || !HasWordCharacters(text, 0)) {
|
||||||
|
completionCallback->didCancelCheckingText();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<blink::WebTextCheckingResult> result;
|
||||||
|
if (!CallProviderMethod("requestCheckingOfText", textToCheck, &result)) {
|
||||||
|
completionCallback->didCancelCheckingText();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
completionCallback->didFinishCheckingText(result);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
blink::WebString SpellCheckClient::autoCorrectWord(
|
blink::WebString SpellCheckClient::autoCorrectWord(
|
||||||
|
|
|
@ -41,8 +41,7 @@ class SpellCheckClient : public blink::WebSpellCheckClient {
|
||||||
const blink::WebString& word) override;
|
const blink::WebString& word) override;
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
bool CallProviderMethod(const char* method,
|
bool CallProviderMethod(const char* method, const blink::WebString& text,
|
||||||
const blink::WebString& text,
|
|
||||||
T* result);
|
T* result);
|
||||||
|
|
||||||
v8::Isolate* isolate_;
|
v8::Isolate* isolate_;
|
||||||
|
|
Loading…
Reference in a new issue