Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / SpellCheckerClientImpl.cpp
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 (LocalFrame* frame = page->mainFrame(); frame && frame->document(); frame = frame->tree().traverseNext()) {
100                 frame->document()->markers().removeMarkers(DocumentMarker::MisspellingMarkers());
101             }
102         }
103     } else {
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);
111                 }
112             }
113         }
114     }
115 }
116
117 bool SpellCheckerClientImpl::isGrammarCheckingEnabled()
118 {
119     const LocalFrame* frame = toLocalFrame(m_webView->focusedWebCoreFrame());
120     return frame && frame->settings() && (frame->settings()->asynchronousSpellCheckingEnabled() || frame->settings()->unifiedTextCheckerEnabled());
121 }
122
123 bool SpellCheckerClientImpl::shouldEraseMarkersAfterChangeSelection(TextCheckingType type) const
124 {
125     const Frame* frame = m_webView->focusedWebCoreFrame();
126     return !frame || !frame->settings() || (!frame->settings()->asynchronousSpellCheckingEnabled() && !frame->settings()->unifiedTextCheckerEnabled());
127 }
128
129 void SpellCheckerClientImpl::checkSpellingOfString(const String& text, int* misspellingLocation, int* misspellingLength)
130 {
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;
134     int spellLength = 0;
135
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);
139     } else {
140         spellLocation = 0;
141         spellLength = 0;
142     }
143
144     // Note: the Mac code checks if the pointers are null before writing to them,
145     // so we do too.
146     if (misspellingLocation)
147         *misspellingLocation = spellLocation;
148     if (misspellingLength)
149         *misspellingLength = spellLength;
150 }
151
152 void SpellCheckerClientImpl::requestCheckingOfString(WTF::PassRefPtr<WebCore::TextCheckingRequest> request)
153 {
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));
159     }
160 }
161
162 String SpellCheckerClientImpl::getAutoCorrectSuggestionForMisspelledWord(const String& misspelledWord)
163 {
164     if (!(isContinuousSpellCheckingEnabled() && m_webView->client()))
165         return String();
166
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])))
171             return String();
172     }
173
174     if (m_webView->spellCheckClient())
175         return m_webView->spellCheckClient()->autoCorrectWord(WebString(misspelledWord));
176     return String();
177 }
178
179 void SpellCheckerClientImpl::checkGrammarOfString(const String& text, WTF::Vector<GrammarDetail>& details, int* badGrammarLocation, int* badGrammarLength)
180 {
181     if (badGrammarLocation)
182         *badGrammarLocation = -1;
183     if (badGrammarLength)
184         *badGrammarLength = 0;
185
186     if (!m_webView->spellCheckClient())
187         return;
188     WebVector<WebTextCheckingResult> webResults;
189     m_webView->spellCheckClient()->checkTextOfParagraph(text, WebTextCheckingTypeGrammar, &webResults);
190     if (!webResults.size())
191         return;
192
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);
204         }
205     }
206     if (!details.size())
207         return;
208     if (badGrammarLocation)
209         *badGrammarLocation = 0;
210     if (badGrammarLength)
211         *badGrammarLength = text.length();
212 }
213
214 void SpellCheckerClientImpl::updateSpellingUIWithMisspelledWord(const String& misspelledWord)
215 {
216     if (m_webView->spellCheckClient())
217         m_webView->spellCheckClient()->updateSpellingUIWithMisspelledWord(WebString(misspelledWord));
218 }
219
220 void SpellCheckerClientImpl::showSpellingUI(bool show)
221 {
222     if (m_webView->spellCheckClient())
223         m_webView->spellCheckClient()->showSpellingUI(show);
224 }
225
226 bool SpellCheckerClientImpl::spellingUIIsShowing()
227 {
228     if (m_webView->spellCheckClient())
229         return m_webView->spellCheckClient()->isShowingSpellingUI();
230     return false;
231 }
232
233 } // namesace WebKit