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/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" |
| 12 |
| 13 using namespace blink; |
| 14 using namespace std; |
| 15 |
| 16 namespace WebTestRunner { |
| 17 |
| 18 namespace { |
| 19 |
| 20 class HostMethodTask : public WebMethodTask<SpellCheckClient> { |
| 21 public: |
| 22 typedef void (SpellCheckClient::*CallbackMethodType)(); |
| 23 HostMethodTask(SpellCheckClient* object, CallbackMethodType callback) |
| 24 : WebMethodTask<SpellCheckClient>(object) |
| 25 , m_callback(callback) |
| 26 { } |
| 27 |
| 28 virtual void runIfValid() { (m_object->*m_callback)(); } |
| 29 |
| 30 private: |
| 31 CallbackMethodType m_callback; |
| 32 }; |
| 33 |
| 34 } |
| 35 |
| 36 SpellCheckClient::SpellCheckClient(WebTestProxyBase* webTestProxy) |
| 37 : m_lastRequestedTextCheckingCompletion(0) |
| 38 , m_webTestProxy(webTestProxy) |
| 39 { |
| 40 } |
| 41 |
| 42 SpellCheckClient::~SpellCheckClient() |
| 43 { |
| 44 } |
| 45 |
| 46 void SpellCheckClient::setDelegate(WebTestDelegate* delegate) |
| 47 { |
| 48 m_delegate = delegate; |
| 49 } |
| 50 |
| 51 // blink::WebSpellCheckClient |
| 52 void SpellCheckClient::spellCheck(const WebString& text, int& misspelledOffset,
int& misspelledLength, WebVector<WebString>* optionalSuggestions) |
| 53 { |
| 54 // Check the spelling of the given text. |
| 55 m_spellcheck.spellCheckWord(text, &misspelledOffset, &misspelledLength); |
| 56 } |
| 57 |
| 58 void SpellCheckClient::checkTextOfParagraph(const WebString& text, WebTextChecki
ngTypeMask mask, WebVector<WebTextCheckingResult>* webResults) |
| 59 { |
| 60 vector<WebTextCheckingResult> results; |
| 61 if (mask & WebTextCheckingTypeSpelling) { |
| 62 size_t offset = 0; |
| 63 string16 data = text; |
| 64 while (offset < data.length()) { |
| 65 int misspelledPosition = 0; |
| 66 int misspelledLength = 0; |
| 67 m_spellcheck.spellCheckWord(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 vector<WebTextCheckingResult> results; |
| 111 int offset = 0; |
| 112 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(WebString(text), &misspelledPosition, &m
isspelledLength); |
| 118 if (!misspelledLength) |
| 119 break; |
| 120 WebVector<WebString> suggestions; |
| 121 m_spellcheck.fillSuggestionList(WebString(text.substr(misspelledPosi
tion, misspelledLength)), &suggestions); |
| 122 results.push_back(WebTextCheckingResult(WebTextDecorationTypeSpellin
g, offset + misspelledPosition, misspelledLength, suggestions.isEmpty() ? WebStr
ing() : suggestions[0])); |
| 123 text = text.substr(misspelledPosition + misspelledLength); |
| 124 offset += misspelledPosition + misspelledLength; |
| 125 } |
| 126 MockGrammarCheck::checkGrammarOfString(m_lastRequestedTextCheckString, &
results); |
| 127 } |
| 128 m_lastRequestedTextCheckingCompletion->didFinishCheckingText(results); |
| 129 m_lastRequestedTextCheckingCompletion = 0; |
| 130 |
| 131 m_webTestProxy->postSpellCheckEvent(WebString("finishLastTextCheck")); |
| 132 } |
| 133 |
| 134 WebString SpellCheckClient::autoCorrectWord(const WebString&) |
| 135 { |
| 136 // Returns an empty string as Mac WebKit ('WebKitSupport/WebEditorClient.mm'
) |
| 137 // does. (If this function returns a non-empty string, WebKit replaces the |
| 138 // given misspelled string with the result one. This process executes some |
| 139 // editor commands and causes layout-test failures.) |
| 140 return WebString(); |
| 141 } |
| 142 |
| 143 } |
OLD | NEW |