2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "SpellChecker.h"
30 #include "DocumentMarkerController.h"
31 #include "EditorClient.h"
33 #include "HTMLInputElement.h"
34 #include "HTMLTextAreaElement.h"
37 #include "PositionIterator.h"
39 #include "RenderObject.h"
41 #include "TextCheckerClient.h"
42 #include "TextCheckingHelper.h"
43 #include "TextIterator.h"
44 #include "htmlediting.h"
48 SpellChecker::SpellChecker(Frame* frame)
50 , m_requestSequence(0)
54 SpellChecker::~SpellChecker()
58 TextCheckerClient* SpellChecker::client() const
60 Page* page = m_frame->page();
63 return page->editorClient()->textChecker();
66 bool SpellChecker::initRequest(PassRefPtr<Range> range)
68 ASSERT(canCheckAsynchronously(range.get()));
70 String text = range->text();
74 m_requestRange = range;
81 void SpellChecker::clearRequest()
83 m_requestRange.clear();
84 m_requestText = String();
87 bool SpellChecker::isAsynchronousEnabled() const
89 return m_frame->settings() && m_frame->settings()->asynchronousSpellCheckingEnabled();
92 bool SpellChecker::canCheckAsynchronously(Range* range) const
94 return client() && isCheckable(range) && isAsynchronousEnabled() && !isBusy();
97 bool SpellChecker::isBusy() const
99 return m_requestRange.get();
102 bool SpellChecker::isValid(int sequence) const
104 return m_requestRange.get() && m_requestText.length() && m_requestSequence == sequence;
107 bool SpellChecker::isCheckable(Range* range) const
109 return range && range->firstNode() && range->firstNode()->renderer();
112 void SpellChecker::requestCheckingFor(TextCheckingTypeMask mask, PassRefPtr<Range> range)
114 if (!canCheckAsynchronously(range.get()))
117 doRequestCheckingFor(mask, range);
120 void SpellChecker::doRequestCheckingFor(TextCheckingTypeMask mask, PassRefPtr<Range> range)
122 ASSERT(canCheckAsynchronously(range.get()));
124 if (!initRequest(range))
126 client()->requestCheckingOfString(this, m_requestSequence, mask, m_requestText);
129 static bool forwardIterator(PositionIterator& iterator, int distance)
131 int remaining = distance;
132 while (!iterator.atEnd()) {
133 if (iterator.node()->isCharacterDataNode()) {
134 int length = lastOffsetForEditing(iterator.node());
135 int last = length - iterator.offsetInLeafNode();
136 if (remaining < last) {
137 iterator.setOffsetInLeafNode(iterator.offsetInLeafNode() + remaining);
142 iterator.setOffsetInLeafNode(iterator.offsetInLeafNode() + last);
145 iterator.increment();
151 static DocumentMarker::MarkerType toMarkerType(TextCheckingType type)
153 if (type == TextCheckingTypeSpelling)
154 return DocumentMarker::Spelling;
155 ASSERT(type == TextCheckingTypeGrammar);
156 return DocumentMarker::Grammar;
159 // Currenntly ignoring TextCheckingResult::details but should be handled. See Bug 56368.
160 void SpellChecker::didCheck(int sequence, const Vector<TextCheckingResult>& results)
162 if (!isValid(sequence))
165 if (!isCheckable(m_requestRange.get())) {
171 PositionIterator start = m_requestRange->startPosition();
172 for (size_t i = 0; i < results.size(); ++i) {
173 if (results[i].type != TextCheckingTypeSpelling && results[i].type != TextCheckingTypeGrammar)
176 // To avoid moving the position backward, we assume the given results are sorted with
177 // startOffset as the ones returned by [NSSpellChecker requestCheckingOfString:].
178 ASSERT(startOffset <= results[i].location);
179 if (!forwardIterator(start, results[i].location - startOffset))
181 PositionIterator end = start;
182 if (!forwardIterator(end, results[i].length))
185 // Users or JavaScript applications may change text while a spell-checker checks its
186 // spellings in the background. To avoid adding markers to the words modified by users or
187 // JavaScript applications, retrieve the words in the specified region and compare them with
188 // the original ones.
189 RefPtr<Range> range = Range::create(m_requestRange->ownerDocument(), start, end);
190 // FIXME: Use textContent() compatible string conversion.
191 String destination = range->text();
192 String source = m_requestText.substring(results[i].location, results[i].length);
193 if (destination == source)
194 m_requestRange->ownerDocument()->markers()->addMarker(range.get(), toMarkerType(results[i].type));
196 startOffset = results[i].location;
203 } // namespace WebCore