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.
5 #include "content/shell/renderer/test_runner/SpellCheckClient.h"
7 #include "content/shell/renderer/test_runner/MockGrammarCheck.h"
8 #include "content/shell/renderer/test_runner/WebTestDelegate.h"
9 #include "content/shell/renderer/test_runner/WebTestProxy.h"
10 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h"
11 #include "third_party/WebKit/public/web/WebTextCheckingResult.h"
13 using namespace blink
;
14 using namespace content
;
17 namespace WebTestRunner
{
21 class HostMethodTask
: public WebMethodTask
<SpellCheckClient
> {
23 typedef void (SpellCheckClient::*CallbackMethodType
)();
24 HostMethodTask(SpellCheckClient
* object
, CallbackMethodType callback
)
25 : WebMethodTask
<SpellCheckClient
>(object
)
26 , m_callback(callback
)
29 virtual void runIfValid() OVERRIDE
{ (m_object
->*m_callback
)(); }
32 CallbackMethodType m_callback
;
37 SpellCheckClient::SpellCheckClient(WebTestProxyBase
* webTestProxy
)
38 : m_lastRequestedTextCheckingCompletion(0)
39 , m_webTestProxy(webTestProxy
)
43 SpellCheckClient::~SpellCheckClient()
47 void SpellCheckClient::setDelegate(WebTestDelegate
* delegate
)
49 m_delegate
= delegate
;
52 // blink::WebSpellCheckClient
53 void SpellCheckClient::spellCheck(const WebString
& text
, int& misspelledOffset
, int& misspelledLength
, WebVector
<WebString
>* optionalSuggestions
)
55 // Check the spelling of the given text.
56 m_spellcheck
.spellCheckWord(text
, &misspelledOffset
, &misspelledLength
);
59 void SpellCheckClient::checkTextOfParagraph(const WebString
& text
, WebTextCheckingTypeMask mask
, WebVector
<WebTextCheckingResult
>* webResults
)
61 vector
<WebTextCheckingResult
> results
;
62 if (mask
& WebTextCheckingTypeSpelling
) {
64 base::string16 data
= text
;
65 while (offset
< data
.length()) {
66 int misspelledPosition
= 0;
67 int misspelledLength
= 0;
68 m_spellcheck
.spellCheckWord(data
.substr(offset
), &misspelledPosition
, &misspelledLength
);
69 if (!misspelledLength
)
71 WebTextCheckingResult result
;
72 result
.decoration
= WebTextDecorationTypeSpelling
;
73 result
.location
= offset
+ misspelledPosition
;
74 result
.length
= misspelledLength
;
75 results
.push_back(result
);
76 offset
+= misspelledPosition
+ misspelledLength
;
79 if (mask
& WebTextCheckingTypeGrammar
)
80 MockGrammarCheck::checkGrammarOfString(text
, &results
);
81 webResults
->assign(results
);
84 void SpellCheckClient::requestCheckingOfText(
85 const WebString
& text
,
86 const WebVector
<uint32_t>& markers
,
87 const WebVector
<unsigned>& markerOffsets
,
88 WebTextCheckingCompletion
* completion
)
92 completion
->didCancelCheckingText();
96 if (m_lastRequestedTextCheckingCompletion
)
97 m_lastRequestedTextCheckingCompletion
->didCancelCheckingText();
99 m_lastRequestedTextCheckingCompletion
= completion
;
100 m_lastRequestedTextCheckString
= text
;
101 if (m_spellcheck
.hasInCache(text
))
102 finishLastTextCheck();
104 m_delegate
->postDelayedTask(new HostMethodTask(this, &SpellCheckClient::finishLastTextCheck
), 0);
107 void SpellCheckClient::finishLastTextCheck()
109 if (!m_lastRequestedTextCheckingCompletion
)
111 vector
<WebTextCheckingResult
> results
;
113 base::string16 text
= m_lastRequestedTextCheckString
;
114 if (!m_spellcheck
.isMultiWordMisspelling(WebString(text
), &results
)) {
115 while (text
.length()) {
116 int misspelledPosition
= 0;
117 int misspelledLength
= 0;
118 m_spellcheck
.spellCheckWord(WebString(text
), &misspelledPosition
, &misspelledLength
);
119 if (!misspelledLength
)
121 WebVector
<WebString
> suggestions
;
122 m_spellcheck
.fillSuggestionList(WebString(text
.substr(misspelledPosition
, misspelledLength
)), &suggestions
);
123 results
.push_back(WebTextCheckingResult(WebTextDecorationTypeSpelling
, offset
+ misspelledPosition
, misspelledLength
, suggestions
.isEmpty() ? WebString() : suggestions
[0]));
124 text
= text
.substr(misspelledPosition
+ misspelledLength
);
125 offset
+= misspelledPosition
+ misspelledLength
;
127 MockGrammarCheck::checkGrammarOfString(m_lastRequestedTextCheckString
, &results
);
129 m_lastRequestedTextCheckingCompletion
->didFinishCheckingText(results
);
130 m_lastRequestedTextCheckingCompletion
= 0;
132 m_webTestProxy
->postSpellCheckEvent(WebString("finishLastTextCheck"));
135 WebString
SpellCheckClient::autoCorrectWord(const WebString
&)
137 // Returns an empty string as Mac WebKit ('WebKitSupport/WebEditorClient.mm')
138 // does. (If this function returns a non-empty string, WebKit replaces the
139 // given misspelled string with the result one. This process executes some
140 // editor commands and causes layout-test failures.)