| 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 /* |
| 6 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 7 * |
| 8 * Redistribution and use in source and binary forms, with or without |
| 9 * modification, are permitted provided that the following conditions are |
| 10 * met: |
| 11 * |
| 12 * * Redistributions of source code must retain the above copyright |
| 13 * notice, this list of conditions and the following disclaimer. |
| 14 * * Redistributions in binary form must reproduce the above |
| 15 * copyright notice, this list of conditions and the following disclaimer |
| 16 * in the documentation and/or other materials provided with the |
| 17 * distribution. |
| 18 * * Neither the name of Google Inc. nor the names of its |
| 19 * contributors may be used to endorse or promote products derived from |
| 20 * this software without specific prior written permission. |
| 21 * |
| 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 */ |
| 34 |
| 35 #include "content/test/layout_tests/runner/SpellCheckClient.h" |
| 36 |
| 37 #include "content/public/test/layout_tests/WebTestDelegate.h" |
| 38 #include "content/public/test/layout_tests/WebTestProxy.h" |
| 39 #include "content/test/layout_tests/runner/MockGrammarCheck.h" |
| 40 #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" |
| 41 #include "third_party/WebKit/public/web/WebTextCheckingResult.h" |
| 42 |
| 43 using namespace blink; |
| 44 using namespace std; |
| 45 |
| 46 namespace WebTestRunner { |
| 47 |
| 48 namespace { |
| 49 |
| 50 class HostMethodTask : public WebMethodTask<SpellCheckClient> { |
| 51 public: |
| 52 typedef void (SpellCheckClient::*CallbackMethodType)(); |
| 53 HostMethodTask(SpellCheckClient* object, CallbackMethodType callback) |
| 54 : WebMethodTask<SpellCheckClient>(object) |
| 55 , m_callback(callback) |
| 56 { } |
| 57 |
| 58 virtual void runIfValid() { (m_object->*m_callback)(); } |
| 59 |
| 60 private: |
| 61 CallbackMethodType m_callback; |
| 62 }; |
| 63 |
| 64 } |
| 65 |
| 66 SpellCheckClient::SpellCheckClient(WebTestProxyBase* webTestProxy) |
| 67 : m_lastRequestedTextCheckingCompletion(0) |
| 68 , m_webTestProxy(webTestProxy) |
| 69 { |
| 70 } |
| 71 |
| 72 SpellCheckClient::~SpellCheckClient() |
| 73 { |
| 74 } |
| 75 |
| 76 void SpellCheckClient::setDelegate(WebTestDelegate* delegate) |
| 77 { |
| 78 m_delegate = delegate; |
| 79 } |
| 80 |
| 81 // blink::WebSpellCheckClient |
| 82 void SpellCheckClient::spellCheck(const WebString& text, int& misspelledOffset,
int& misspelledLength, WebVector<WebString>* optionalSuggestions) |
| 83 { |
| 84 // Check the spelling of the given text. |
| 85 m_spellcheck.spellCheckWord(text, &misspelledOffset, &misspelledLength); |
| 86 } |
| 87 |
| 88 void SpellCheckClient::checkTextOfParagraph(const WebString& text, WebTextChecki
ngTypeMask mask, WebVector<WebTextCheckingResult>* webResults) |
| 89 { |
| 90 vector<WebTextCheckingResult> results; |
| 91 if (mask & WebTextCheckingTypeSpelling) { |
| 92 size_t offset = 0; |
| 93 string16 data = text; |
| 94 while (offset < data.length()) { |
| 95 int misspelledPosition = 0; |
| 96 int misspelledLength = 0; |
| 97 m_spellcheck.spellCheckWord(data.substr(offset), &misspelledPosition
, &misspelledLength); |
| 98 if (!misspelledLength) |
| 99 break; |
| 100 WebTextCheckingResult result; |
| 101 result.decoration = WebTextDecorationTypeSpelling; |
| 102 result.location = offset + misspelledPosition; |
| 103 result.length = misspelledLength; |
| 104 results.push_back(result); |
| 105 offset += misspelledPosition + misspelledLength; |
| 106 } |
| 107 } |
| 108 if (mask & WebTextCheckingTypeGrammar) |
| 109 MockGrammarCheck::checkGrammarOfString(text, &results); |
| 110 webResults->assign(results); |
| 111 } |
| 112 |
| 113 void SpellCheckClient::requestCheckingOfText( |
| 114 const WebString& text, |
| 115 const WebVector<uint32_t>& markers, |
| 116 const WebVector<unsigned>& markerOffsets, |
| 117 WebTextCheckingCompletion* completion) |
| 118 { |
| 119 if (text.isEmpty()) { |
| 120 if (completion) |
| 121 completion->didCancelCheckingText(); |
| 122 return; |
| 123 } |
| 124 |
| 125 if (m_lastRequestedTextCheckingCompletion) |
| 126 m_lastRequestedTextCheckingCompletion->didCancelCheckingText(); |
| 127 |
| 128 m_lastRequestedTextCheckingCompletion = completion; |
| 129 m_lastRequestedTextCheckString = text; |
| 130 if (m_spellcheck.hasInCache(text)) |
| 131 finishLastTextCheck(); |
| 132 else |
| 133 m_delegate->postDelayedTask(new HostMethodTask(this, &SpellCheckClient::
finishLastTextCheck), 0); |
| 134 } |
| 135 |
| 136 void SpellCheckClient::finishLastTextCheck() |
| 137 { |
| 138 if (!m_lastRequestedTextCheckingCompletion) |
| 139 return; |
| 140 vector<WebTextCheckingResult> results; |
| 141 int offset = 0; |
| 142 string16 text = m_lastRequestedTextCheckString; |
| 143 if (!m_spellcheck.isMultiWordMisspelling(WebString(text), &results)) { |
| 144 while (text.length()) { |
| 145 int misspelledPosition = 0; |
| 146 int misspelledLength = 0; |
| 147 m_spellcheck.spellCheckWord(WebString(text), &misspelledPosition, &m
isspelledLength); |
| 148 if (!misspelledLength) |
| 149 break; |
| 150 WebVector<WebString> suggestions; |
| 151 m_spellcheck.fillSuggestionList(WebString(text.substr(misspelledPosi
tion, misspelledLength)), &suggestions); |
| 152 results.push_back(WebTextCheckingResult(WebTextDecorationTypeSpellin
g, offset + misspelledPosition, misspelledLength, suggestions.isEmpty() ? WebStr
ing() : suggestions[0])); |
| 153 text = text.substr(misspelledPosition + misspelledLength); |
| 154 offset += misspelledPosition + misspelledLength; |
| 155 } |
| 156 MockGrammarCheck::checkGrammarOfString(m_lastRequestedTextCheckString, &
results); |
| 157 } |
| 158 m_lastRequestedTextCheckingCompletion->didFinishCheckingText(results); |
| 159 m_lastRequestedTextCheckingCompletion = 0; |
| 160 |
| 161 m_webTestProxy->postSpellCheckEvent(WebString("finishLastTextCheck")); |
| 162 } |
| 163 |
| 164 WebString SpellCheckClient::autoCorrectWord(const WebString&) |
| 165 { |
| 166 // Returns an empty string as Mac WebKit ('WebKitSupport/WebEditorClient.mm'
) |
| 167 // does. (If this function returns a non-empty string, WebKit replaces the |
| 168 // given misspelled string with the result one. This process executes some |
| 169 // editor commands and causes layout-test failures.) |
| 170 return WebString(); |
| 171 } |
| 172 |
| 173 } |
| OLD | NEW |