1 /* 2 * Copyright (C) 2006, 2007 Apple, Inc. All rights reserved. 3 * Copyright (C) 2012 Google, Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "config.h" 28 #include "web/SpellCheckerClientImpl.h" 29 30 #include "core/dom/DocumentMarkerController.h" 31 #include "core/editing/Editor.h" 32 #include "core/editing/SpellChecker.h" 33 #include "core/frame/LocalFrame.h" 34 #include "core/frame/Settings.h" 35 #include "core/page/Page.h" 36 #include "public/web/WebSpellCheckClient.h" 37 #include "public/web/WebTextCheckingResult.h" 38 #include "web/WebTextCheckingCompletionImpl.h" 39 #include "web/WebViewImpl.h" 40 41 using namespace WebCore; 42 43 namespace blink { 44 45 SpellCheckerClientImpl::SpellCheckerClientImpl(WebViewImpl* webview) 46 : m_webView(webview) 47 , m_spellCheckThisFieldStatus(SpellCheckAutomatic) 48 { 49 } 50 51 SpellCheckerClientImpl::~SpellCheckerClientImpl() 52 { 53 } 54 55 bool SpellCheckerClientImpl::shouldSpellcheckByDefault() 56 { 57 // Spellcheck should be enabled for all editable areas (such as textareas, 58 // contentEditable regions, designMode docs and inputs). 59 if (!m_webView->focusedWebCoreFrame()->isLocalFrame()) 60 return false; 61 const LocalFrame* frame = toLocalFrame(m_webView->focusedWebCoreFrame()); 62 if (!frame) 63 return false; 64 if (frame->spellChecker().isSpellCheckingEnabledInFocusedNode()) 65 return true; 66 const Document* document = frame->document(); 67 if (!document) 68 return false; 69 const Element* element = document->focusedElement(); 70 // If |element| is null, we default to allowing spellchecking. This is done 71 // in order to mitigate the issue when the user clicks outside the textbox, 72 // as a result of which |element| becomes null, resulting in all the spell 73 // check markers being deleted. Also, the LocalFrame will decide not to do 74 // spellchecking if the user can't edit - so returning true here will not 75 // cause any problems to the LocalFrame's behavior. 76 if (!element) 77 return true; 78 const RenderObject* renderer = element->renderer(); 79 if (!renderer) 80 return false; 81 82 return true; 83 } 84 85 bool SpellCheckerClientImpl::isContinuousSpellCheckingEnabled() 86 { 87 if (m_spellCheckThisFieldStatus == SpellCheckForcedOff) 88 return false; 89 if (m_spellCheckThisFieldStatus == SpellCheckForcedOn) 90 return true; 91 return shouldSpellcheckByDefault(); 92 } 93 94 void SpellCheckerClientImpl::toggleContinuousSpellChecking() 95 { 96 if (isContinuousSpellCheckingEnabled()) { 97 m_spellCheckThisFieldStatus = SpellCheckForcedOff; 98 if (Page* page = m_webView->page()) { 99 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree().traverseNext()) { 100 if (!frame->isLocalFrame()) 101 continue; 102 toLocalFrame(frame)->document()->markers().removeMarkers(DocumentMarker::MisspellingMarkers()); 103 } 104 } 105 } else { 106 m_spellCheckThisFieldStatus = SpellCheckForcedOn; 107 if (m_webView->focusedWebCoreFrame()->isLocalFrame()) { 108 if (LocalFrame* frame = toLocalFrame(m_webView->focusedWebCoreFrame())) { 109 VisibleSelection frameSelection = frame->selection().selection(); 110 // If a selection is in an editable element spell check its content. 111 if (Element* rootEditableElement = frameSelection.rootEditableElement()) { 112 frame->spellChecker().didBeginEditing(rootEditableElement); 113 } 114 } 115 } 116 } 117 } 118 119 bool SpellCheckerClientImpl::isGrammarCheckingEnabled() 120 { 121 const LocalFrame* frame = toLocalFrame(m_webView->focusedWebCoreFrame()); 122 return frame && frame->settings() && (frame->settings()->asynchronousSpellCheckingEnabled() || frame->settings()->unifiedTextCheckerEnabled()); 123 } 124 125 bool SpellCheckerClientImpl::shouldEraseMarkersAfterChangeSelection(TextCheckingType type) const 126 { 127 const Frame* frame = m_webView->focusedWebCoreFrame(); 128 return !frame || !frame->settings() || (!frame->settings()->asynchronousSpellCheckingEnabled() && !frame->settings()->unifiedTextCheckerEnabled()); 129 } 130 131 void SpellCheckerClientImpl::checkSpellingOfString(const String& text, int* misspellingLocation, int* misspellingLength) 132 { 133 // SpellCheckWord will write (0, 0) into the output vars, which is what our 134 // caller expects if the word is spelled correctly. 135 int spellLocation = -1; 136 int spellLength = 0; 137 138 // Check to see if the provided text is spelled correctly. 139 if (m_webView->spellCheckClient()) { 140 m_webView->spellCheckClient()->spellCheck(text, spellLocation, spellLength, 0); 141 } else { 142 spellLocation = 0; 143 spellLength = 0; 144 } 145 146 // Note: the Mac code checks if the pointers are null before writing to them, 147 // so we do too. 148 if (misspellingLocation) 149 *misspellingLocation = spellLocation; 150 if (misspellingLength) 151 *misspellingLength = spellLength; 152 } 153 154 void SpellCheckerClientImpl::requestCheckingOfString(WTF::PassRefPtr<WebCore::TextCheckingRequest> request) 155 { 156 if (m_webView->spellCheckClient()) { 157 const String& text = request->data().text(); 158 const Vector<uint32_t>& markers = request->data().markers(); 159 const Vector<unsigned>& markerOffsets = request->data().offsets(); 160 m_webView->spellCheckClient()->requestCheckingOfText(text, markers, markerOffsets, new WebTextCheckingCompletionImpl(request)); 161 } 162 } 163 164 String SpellCheckerClientImpl::getAutoCorrectSuggestionForMisspelledWord(const String& misspelledWord) 165 { 166 if (!(isContinuousSpellCheckingEnabled() && m_webView->client())) 167 return String(); 168 169 // Do not autocorrect words with capital letters in it except the 170 // first letter. This will remove cases changing "IMB" to "IBM". 171 for (size_t i = 1; i < misspelledWord.length(); i++) { 172 if (u_isupper(static_cast<UChar32>(misspelledWord[i]))) 173 return String(); 174 } 175 176 if (m_webView->spellCheckClient()) 177 return m_webView->spellCheckClient()->autoCorrectWord(WebString(misspelledWord)); 178 return String(); 179 } 180 181 void SpellCheckerClientImpl::checkGrammarOfString(const String& text, WTF::Vector<GrammarDetail>& details, int* badGrammarLocation, int* badGrammarLength) 182 { 183 if (badGrammarLocation) 184 *badGrammarLocation = -1; 185 if (badGrammarLength) 186 *badGrammarLength = 0; 187 188 if (!m_webView->spellCheckClient()) 189 return; 190 WebVector<WebTextCheckingResult> webResults; 191 m_webView->spellCheckClient()->checkTextOfParagraph(text, WebTextCheckingTypeGrammar, &webResults); 192 if (!webResults.size()) 193 return; 194 195 // Convert a list of WebTextCheckingResults to a list of GrammarDetails. If 196 // the converted vector of GrammarDetails has grammar errors, we set 197 // badGrammarLocation and badGrammarLength to tell WebKit that the input 198 // text has grammar errors. 199 for (size_t i = 0; i < webResults.size(); ++i) { 200 if (webResults[i].decoration == WebTextDecorationTypeGrammar) { 201 GrammarDetail detail; 202 detail.location = webResults[i].location; 203 detail.length = webResults[i].length; 204 detail.userDescription = webResults[i].replacement; 205 details.append(detail); 206 } 207 } 208 if (!details.size()) 209 return; 210 if (badGrammarLocation) 211 *badGrammarLocation = 0; 212 if (badGrammarLength) 213 *badGrammarLength = text.length(); 214 } 215 216 void SpellCheckerClientImpl::updateSpellingUIWithMisspelledWord(const String& misspelledWord) 217 { 218 if (m_webView->spellCheckClient()) 219 m_webView->spellCheckClient()->updateSpellingUIWithMisspelledWord(WebString(misspelledWord)); 220 } 221 222 void SpellCheckerClientImpl::showSpellingUI(bool show) 223 { 224 if (m_webView->spellCheckClient()) 225 m_webView->spellCheckClient()->showSpellingUI(show); 226 } 227 228 bool SpellCheckerClientImpl::spellingUIIsShowing() 229 { 230 if (m_webView->spellCheckClient()) 231 return m_webView->spellCheckClient()->isShowingSpellingUI(); 232 return false; 233 } 234 235 } // namesace WebKit 236