2 * Copyright (C) 2006, 2007 Apple, Inc. All rights reserved.
3 * Copyright (C) 2012 Google, Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "web/SpellCheckerClientImpl.h"
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"
41 using namespace WebCore;
45 SpellCheckerClientImpl::SpellCheckerClientImpl(WebViewImpl* webview)
47 , m_spellCheckThisFieldStatus(SpellCheckAutomatic)
51 SpellCheckerClientImpl::~SpellCheckerClientImpl()
55 bool SpellCheckerClientImpl::shouldSpellcheckByDefault()
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())
61 const LocalFrame* frame = toLocalFrame(m_webView->focusedWebCoreFrame());
64 if (frame->spellChecker().isSpellCheckingEnabledInFocusedNode())
66 const Document* document = frame->document();
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.
78 const RenderObject* renderer = element->renderer();
85 bool SpellCheckerClientImpl::isContinuousSpellCheckingEnabled()
87 if (m_spellCheckThisFieldStatus == SpellCheckForcedOff)
89 if (m_spellCheckThisFieldStatus == SpellCheckForcedOn)
91 return shouldSpellcheckByDefault();
94 void SpellCheckerClientImpl::toggleContinuousSpellChecking()
96 if (isContinuousSpellCheckingEnabled()) {
97 m_spellCheckThisFieldStatus = SpellCheckForcedOff;
98 if (Page* page = m_webView->page()) {
99 for (LocalFrame* frame = page->mainFrame(); frame && frame->document(); frame = frame->tree().traverseNext()) {
100 frame->document()->markers().removeMarkers(DocumentMarker::MisspellingMarkers());
104 m_spellCheckThisFieldStatus = SpellCheckForcedOn;
105 if (m_webView->focusedWebCoreFrame()->isLocalFrame()) {
106 if (LocalFrame* frame = toLocalFrame(m_webView->focusedWebCoreFrame())) {
107 VisibleSelection frameSelection = frame->selection().selection();
108 // If a selection is in an editable element spell check its content.
109 if (Element* rootEditableElement = frameSelection.rootEditableElement()) {
110 frame->spellChecker().didBeginEditing(rootEditableElement);
117 bool SpellCheckerClientImpl::isGrammarCheckingEnabled()
119 const LocalFrame* frame = toLocalFrame(m_webView->focusedWebCoreFrame());
120 return frame && frame->settings() && (frame->settings()->asynchronousSpellCheckingEnabled() || frame->settings()->unifiedTextCheckerEnabled());
123 bool SpellCheckerClientImpl::shouldEraseMarkersAfterChangeSelection(TextCheckingType type) const
125 const Frame* frame = m_webView->focusedWebCoreFrame();
126 return !frame || !frame->settings() || (!frame->settings()->asynchronousSpellCheckingEnabled() && !frame->settings()->unifiedTextCheckerEnabled());
129 void SpellCheckerClientImpl::checkSpellingOfString(const String& text, int* misspellingLocation, int* misspellingLength)
131 // SpellCheckWord will write (0, 0) into the output vars, which is what our
132 // caller expects if the word is spelled correctly.
133 int spellLocation = -1;
136 // Check to see if the provided text is spelled correctly.
137 if (m_webView->spellCheckClient()) {
138 m_webView->spellCheckClient()->spellCheck(text, spellLocation, spellLength, 0);
144 // Note: the Mac code checks if the pointers are null before writing to them,
146 if (misspellingLocation)
147 *misspellingLocation = spellLocation;
148 if (misspellingLength)
149 *misspellingLength = spellLength;
152 void SpellCheckerClientImpl::requestCheckingOfString(WTF::PassRefPtr<WebCore::TextCheckingRequest> request)
154 if (m_webView->spellCheckClient()) {
155 const String& text = request->data().text();
156 const Vector<uint32_t>& markers = request->data().markers();
157 const Vector<unsigned>& markerOffsets = request->data().offsets();
158 m_webView->spellCheckClient()->requestCheckingOfText(text, markers, markerOffsets, new WebTextCheckingCompletionImpl(request));
162 String SpellCheckerClientImpl::getAutoCorrectSuggestionForMisspelledWord(const String& misspelledWord)
164 if (!(isContinuousSpellCheckingEnabled() && m_webView->client()))
167 // Do not autocorrect words with capital letters in it except the
168 // first letter. This will remove cases changing "IMB" to "IBM".
169 for (size_t i = 1; i < misspelledWord.length(); i++) {
170 if (u_isupper(static_cast<UChar32>(misspelledWord[i])))
174 if (m_webView->spellCheckClient())
175 return m_webView->spellCheckClient()->autoCorrectWord(WebString(misspelledWord));
179 void SpellCheckerClientImpl::checkGrammarOfString(const String& text, WTF::Vector<GrammarDetail>& details, int* badGrammarLocation, int* badGrammarLength)
181 if (badGrammarLocation)
182 *badGrammarLocation = -1;
183 if (badGrammarLength)
184 *badGrammarLength = 0;
186 if (!m_webView->spellCheckClient())
188 WebVector<WebTextCheckingResult> webResults;
189 m_webView->spellCheckClient()->checkTextOfParagraph(text, WebTextCheckingTypeGrammar, &webResults);
190 if (!webResults.size())
193 // Convert a list of WebTextCheckingResults to a list of GrammarDetails. If
194 // the converted vector of GrammarDetails has grammar errors, we set
195 // badGrammarLocation and badGrammarLength to tell WebKit that the input
196 // text has grammar errors.
197 for (size_t i = 0; i < webResults.size(); ++i) {
198 if (webResults[i].decoration == WebTextDecorationTypeGrammar) {
199 GrammarDetail detail;
200 detail.location = webResults[i].location;
201 detail.length = webResults[i].length;
202 detail.userDescription = webResults[i].replacement;
203 details.append(detail);
208 if (badGrammarLocation)
209 *badGrammarLocation = 0;
210 if (badGrammarLength)
211 *badGrammarLength = text.length();
214 void SpellCheckerClientImpl::updateSpellingUIWithMisspelledWord(const String& misspelledWord)
216 if (m_webView->spellCheckClient())
217 m_webView->spellCheckClient()->updateSpellingUIWithMisspelledWord(WebString(misspelledWord));
220 void SpellCheckerClientImpl::showSpellingUI(bool show)
222 if (m_webView->spellCheckClient())
223 m_webView->spellCheckClient()->showSpellingUI(show);
226 bool SpellCheckerClientImpl::spellingUIIsShowing()
228 if (m_webView->spellCheckClient())
229 return m_webView->spellCheckClient()->isShowingSpellingUI();