OLD | NEW |
| (Empty) |
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 "sky/engine/web/SpellCheckerClientImpl.h" | |
28 | |
29 #include "sky/engine/core/dom/Document.h" | |
30 #include "sky/engine/core/dom/DocumentMarkerController.h" | |
31 #include "sky/engine/core/dom/Element.h" | |
32 #include "sky/engine/core/editing/Editor.h" | |
33 #include "sky/engine/core/editing/SpellChecker.h" | |
34 #include "sky/engine/core/frame/LocalFrame.h" | |
35 #include "sky/engine/core/frame/Settings.h" | |
36 #include "sky/engine/core/page/Page.h" | |
37 #include "sky/engine/public/web/WebSpellCheckClient.h" | |
38 #include "sky/engine/public/web/WebTextCheckingResult.h" | |
39 #include "sky/engine/web/WebTextCheckingCompletionImpl.h" | |
40 #include "sky/engine/web/WebViewImpl.h" | |
41 | |
42 namespace blink { | |
43 | |
44 SpellCheckerClientImpl::SpellCheckerClientImpl(WebViewImpl* webview) | |
45 : m_webView(webview) | |
46 , m_spellCheckThisFieldStatus(SpellCheckAutomatic) | |
47 { | |
48 } | |
49 | |
50 SpellCheckerClientImpl::~SpellCheckerClientImpl() | |
51 { | |
52 } | |
53 | |
54 bool SpellCheckerClientImpl::shouldSpellcheckByDefault() | |
55 { | |
56 // Spellcheck should be enabled for all editable areas (such as textareas, | |
57 // contentEditable regions, designMode docs and inputs). | |
58 const LocalFrame* frame = m_webView->focusedCoreFrame(); | |
59 if (!frame) | |
60 return false; | |
61 if (frame->spellChecker().isSpellCheckingEnabledInFocusedNode()) | |
62 return true; | |
63 const Document* document = frame->document(); | |
64 if (!document) | |
65 return false; | |
66 const Element* element = document->focusedElement(); | |
67 // If |element| is null, we default to allowing spellchecking. This is done | |
68 // in order to mitigate the issue when the user clicks outside the textbox, | |
69 // as a result of which |element| becomes null, resulting in all the spell | |
70 // check markers being deleted. Also, the LocalFrame will decide not to do | |
71 // spellchecking if the user can't edit - so returning true here will not | |
72 // cause any problems to the LocalFrame's behavior. | |
73 if (!element) | |
74 return true; | |
75 const RenderObject* renderer = element->renderer(); | |
76 if (!renderer) | |
77 return false; | |
78 | |
79 return true; | |
80 } | |
81 | |
82 bool SpellCheckerClientImpl::isContinuousSpellCheckingEnabled() | |
83 { | |
84 if (m_spellCheckThisFieldStatus == SpellCheckForcedOff) | |
85 return false; | |
86 if (m_spellCheckThisFieldStatus == SpellCheckForcedOn) | |
87 return true; | |
88 return shouldSpellcheckByDefault(); | |
89 } | |
90 | |
91 void SpellCheckerClientImpl::toggleContinuousSpellChecking() | |
92 { | |
93 if (isContinuousSpellCheckingEnabled()) { | |
94 m_spellCheckThisFieldStatus = SpellCheckForcedOff; | |
95 if (Page* page = m_webView->page()) { | |
96 LocalFrame* frame = page->mainFrame(); | |
97 frame->document()->markers().removeMarkers(DocumentMarker::Misspelli
ngMarkers()); | |
98 } | |
99 } else { | |
100 m_spellCheckThisFieldStatus = SpellCheckForcedOn; | |
101 if (LocalFrame* frame = m_webView->focusedCoreFrame()) { | |
102 VisibleSelection frameSelection = frame->selection().selection(); | |
103 // If a selection is in an editable element spell check its content. | |
104 if (Element* rootEditableElement = frameSelection.rootEditableElemen
t()) { | |
105 frame->spellChecker().didBeginEditing(rootEditableElement); | |
106 } | |
107 } | |
108 } | |
109 } | |
110 | |
111 bool SpellCheckerClientImpl::isGrammarCheckingEnabled() | |
112 { | |
113 const LocalFrame* frame = m_webView->focusedCoreFrame(); | |
114 return frame && frame->settings() && (frame->settings()->asynchronousSpellCh
eckingEnabled() || frame->settings()->unifiedTextCheckerEnabled()); | |
115 } | |
116 | |
117 bool SpellCheckerClientImpl::shouldEraseMarkersAfterChangeSelection(TextChecking
Type type) const | |
118 { | |
119 const Frame* frame = m_webView->focusedCoreFrame(); | |
120 return !frame || !frame->settings() || (!frame->settings()->asynchronousSpel
lCheckingEnabled() && !frame->settings()->unifiedTextCheckerEnabled()); | |
121 } | |
122 | |
123 void SpellCheckerClientImpl::checkSpellingOfString(const String& text, int* miss
pellingLocation, int* misspellingLength) | |
124 { | |
125 // SpellCheckWord will write (0, 0) into the output vars, which is what our | |
126 // caller expects if the word is spelled correctly. | |
127 int spellLocation = -1; | |
128 int spellLength = 0; | |
129 | |
130 // Check to see if the provided text is spelled correctly. | |
131 if (m_webView->spellCheckClient()) { | |
132 m_webView->spellCheckClient()->spellCheck(text, spellLocation, spellLeng
th, 0); | |
133 } else { | |
134 spellLocation = 0; | |
135 spellLength = 0; | |
136 } | |
137 | |
138 // Note: the Mac code checks if the pointers are null before writing to them
, | |
139 // so we do too. | |
140 if (misspellingLocation) | |
141 *misspellingLocation = spellLocation; | |
142 if (misspellingLength) | |
143 *misspellingLength = spellLength; | |
144 } | |
145 | |
146 void SpellCheckerClientImpl::requestCheckingOfString(WTF::PassRefPtr<TextCheckin
gRequest> request) | |
147 { | |
148 if (m_webView->spellCheckClient()) { | |
149 const String& text = request->data().text(); | |
150 const Vector<uint32_t>& markers = request->data().markers(); | |
151 const Vector<unsigned>& markerOffsets = request->data().offsets(); | |
152 m_webView->spellCheckClient()->requestCheckingOfText(text, markers, mark
erOffsets, new WebTextCheckingCompletionImpl(request)); | |
153 } | |
154 } | |
155 | |
156 String SpellCheckerClientImpl::getAutoCorrectSuggestionForMisspelledWord(const S
tring& misspelledWord) | |
157 { | |
158 if (!(isContinuousSpellCheckingEnabled() && m_webView->client())) | |
159 return String(); | |
160 | |
161 // Do not autocorrect words with capital letters in it except the | |
162 // first letter. This will remove cases changing "IMB" to "IBM". | |
163 for (size_t i = 1; i < misspelledWord.length(); i++) { | |
164 if (u_isupper(static_cast<UChar32>(misspelledWord[i]))) | |
165 return String(); | |
166 } | |
167 | |
168 if (m_webView->spellCheckClient()) | |
169 return m_webView->spellCheckClient()->autoCorrectWord(WebString(misspell
edWord)); | |
170 return String(); | |
171 } | |
172 | |
173 void SpellCheckerClientImpl::checkGrammarOfString(const String& text, WTF::Vecto
r<GrammarDetail>& details, int* badGrammarLocation, int* badGrammarLength) | |
174 { | |
175 if (badGrammarLocation) | |
176 *badGrammarLocation = -1; | |
177 if (badGrammarLength) | |
178 *badGrammarLength = 0; | |
179 | |
180 if (!m_webView->spellCheckClient()) | |
181 return; | |
182 WebVector<WebTextCheckingResult> webResults; | |
183 m_webView->spellCheckClient()->checkTextOfParagraph(text, WebTextCheckingTyp
eGrammar, &webResults); | |
184 if (!webResults.size()) | |
185 return; | |
186 | |
187 // Convert a list of WebTextCheckingResults to a list of GrammarDetails. If | |
188 // the converted vector of GrammarDetails has grammar errors, we set | |
189 // badGrammarLocation and badGrammarLength to tell WebKit that the input | |
190 // text has grammar errors. | |
191 for (size_t i = 0; i < webResults.size(); ++i) { | |
192 if (webResults[i].decoration == WebTextDecorationTypeGrammar) { | |
193 GrammarDetail detail; | |
194 detail.location = webResults[i].location; | |
195 detail.length = webResults[i].length; | |
196 detail.userDescription = webResults[i].replacement; | |
197 details.append(detail); | |
198 } | |
199 } | |
200 if (!details.size()) | |
201 return; | |
202 if (badGrammarLocation) | |
203 *badGrammarLocation = 0; | |
204 if (badGrammarLength) | |
205 *badGrammarLength = text.length(); | |
206 } | |
207 | |
208 void SpellCheckerClientImpl::updateSpellingUIWithMisspelledWord(const String& mi
sspelledWord) | |
209 { | |
210 if (m_webView->spellCheckClient()) | |
211 m_webView->spellCheckClient()->updateSpellingUIWithMisspelledWord(WebStr
ing(misspelledWord)); | |
212 } | |
213 | |
214 void SpellCheckerClientImpl::showSpellingUI(bool show) | |
215 { | |
216 if (m_webView->spellCheckClient()) | |
217 m_webView->spellCheckClient()->showSpellingUI(show); | |
218 } | |
219 | |
220 bool SpellCheckerClientImpl::spellingUIIsShowing() | |
221 { | |
222 if (m_webView->spellCheckClient()) | |
223 return m_webView->spellCheckClient()->isShowingSpellingUI(); | |
224 return false; | |
225 } | |
226 | |
227 } // namespace blink | |
OLD | NEW |