OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/shell/renderer/test_runner/SpellCheckClient.h" | |
6 | |
7 #include "content/shell/renderer/test_runner/WebTestDelegate.h" | |
8 #include "content/shell/renderer/test_runner/mock_grammar_check.h" | |
9 #include "content/shell/renderer/test_runner/web_test_proxy.h" | |
10 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" | |
11 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" | |
12 | |
13 using namespace blink; | |
14 | |
15 namespace content { | |
16 | |
17 namespace { | |
18 | |
19 class HostMethodTask : public WebMethodTask<SpellCheckClient> { | |
20 public: | |
21 typedef void (SpellCheckClient::*CallbackMethodType)(); | |
22 HostMethodTask(SpellCheckClient* object, CallbackMethodType callback) | |
23 : WebMethodTask<SpellCheckClient>(object) | |
24 , m_callback(callback) | |
25 { } | |
26 | |
27 virtual void runIfValid() OVERRIDE { (m_object->*m_callback)(); } | |
28 | |
29 private: | |
30 CallbackMethodType m_callback; | |
31 }; | |
32 | |
33 } // namespace | |
34 | |
35 SpellCheckClient::SpellCheckClient(WebTestProxyBase* webTestProxy) | |
36 : m_lastRequestedTextCheckingCompletion(0) | |
37 , m_webTestProxy(webTestProxy) | |
38 { | |
39 } | |
40 | |
41 SpellCheckClient::~SpellCheckClient() | |
42 { | |
43 } | |
44 | |
45 void SpellCheckClient::setDelegate(WebTestDelegate* delegate) | |
46 { | |
47 m_delegate = delegate; | |
48 } | |
49 | |
50 // blink::WebSpellCheckClient | |
51 void SpellCheckClient::spellCheck(const WebString& text, int& misspelledOffset,
int& misspelledLength, WebVector<WebString>* optionalSuggestions) | |
52 { | |
53 // Check the spelling of the given text. | |
54 m_spellcheck.SpellCheckWord(text, &misspelledOffset, &misspelledLength); | |
55 } | |
56 | |
57 void SpellCheckClient::checkTextOfParagraph(const WebString& text, WebTextChecki
ngTypeMask mask, WebVector<WebTextCheckingResult>* webResults) | |
58 { | |
59 std::vector<WebTextCheckingResult> results; | |
60 if (mask & WebTextCheckingTypeSpelling) { | |
61 size_t offset = 0; | |
62 base::string16 data = text; | |
63 while (offset < data.length()) { | |
64 int misspelledPosition = 0; | |
65 int misspelledLength = 0; | |
66 m_spellcheck.SpellCheckWord( | |
67 data.substr(offset), &misspelledPosition, &misspelledLength); | |
68 if (!misspelledLength) | |
69 break; | |
70 WebTextCheckingResult result; | |
71 result.decoration = WebTextDecorationTypeSpelling; | |
72 result.location = offset + misspelledPosition; | |
73 result.length = misspelledLength; | |
74 results.push_back(result); | |
75 offset += misspelledPosition + misspelledLength; | |
76 } | |
77 } | |
78 if (mask & WebTextCheckingTypeGrammar) | |
79 MockGrammarCheck::CheckGrammarOfString(text, &results); | |
80 webResults->assign(results); | |
81 } | |
82 | |
83 void SpellCheckClient::requestCheckingOfText( | |
84 const WebString& text, | |
85 const WebVector<uint32_t>& markers, | |
86 const WebVector<unsigned>& markerOffsets, | |
87 WebTextCheckingCompletion* completion) | |
88 { | |
89 if (text.isEmpty()) { | |
90 if (completion) | |
91 completion->didCancelCheckingText(); | |
92 return; | |
93 } | |
94 | |
95 if (m_lastRequestedTextCheckingCompletion) | |
96 m_lastRequestedTextCheckingCompletion->didCancelCheckingText(); | |
97 | |
98 m_lastRequestedTextCheckingCompletion = completion; | |
99 m_lastRequestedTextCheckString = text; | |
100 if (m_spellcheck.HasInCache(text)) | |
101 finishLastTextCheck(); | |
102 else | |
103 m_delegate->postDelayedTask(new HostMethodTask(this, &SpellCheckClient::
finishLastTextCheck), 0); | |
104 } | |
105 | |
106 void SpellCheckClient::finishLastTextCheck() | |
107 { | |
108 if (!m_lastRequestedTextCheckingCompletion) | |
109 return; | |
110 std::vector<WebTextCheckingResult> results; | |
111 int offset = 0; | |
112 base::string16 text = m_lastRequestedTextCheckString; | |
113 if (!m_spellcheck.IsMultiWordMisspelling(WebString(text), &results)) { | |
114 while (text.length()) { | |
115 int misspelledPosition = 0; | |
116 int misspelledLength = 0; | |
117 m_spellcheck.SpellCheckWord( | |
118 WebString(text), &misspelledPosition, &misspelledLength); | |
119 if (!misspelledLength) | |
120 break; | |
121 WebVector<WebString> suggestions; | |
122 m_spellcheck.FillSuggestionList( | |
123 WebString(text.substr(misspelledPosition, misspelledLength)), | |
124 &suggestions); | |
125 results.push_back(WebTextCheckingResult(WebTextDecorationTypeSpellin
g, offset + misspelledPosition, misspelledLength, suggestions.isEmpty() ? WebStr
ing() : suggestions[0])); | |
126 text = text.substr(misspelledPosition + misspelledLength); | |
127 offset += misspelledPosition + misspelledLength; | |
128 } | |
129 MockGrammarCheck::CheckGrammarOfString(m_lastRequestedTextCheckString, &
results); | |
130 } | |
131 m_lastRequestedTextCheckingCompletion->didFinishCheckingText(results); | |
132 m_lastRequestedTextCheckingCompletion = 0; | |
133 | |
134 m_webTestProxy->PostSpellCheckEvent(WebString("finishLastTextCheck")); | |
135 } | |
136 | |
137 WebString SpellCheckClient::autoCorrectWord(const WebString&) | |
138 { | |
139 // Returns an empty string as Mac WebKit ('WebKitSupport/WebEditorClient.mm'
) | |
140 // does. (If this function returns a non-empty string, WebKit replaces the | |
141 // given misspelled string with the result one. This process executes some | |
142 // editor commands and causes layout-test failures.) | |
143 return WebString(); | |
144 } | |
145 | |
146 } // namespace content | |
OLD | NEW |