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 "TextIterator.h"
43 #include "htmlediting.h"
47 SpellChecker::SpellChecker(Frame* frame)
49 , m_requestSequence(0)
53 SpellChecker::~SpellChecker()
57 TextCheckerClient* SpellChecker::client() const
59 Page* page = m_frame->page();
62 return page->editorClient()->textChecker();
65 bool SpellChecker::initRequest(Node* node)
67 ASSERT(canCheckAsynchronously(node));
69 String text = node->textContent();
80 void SpellChecker::clearRequest()
82 m_requestNode.clear();
83 m_requestText = String();
86 bool SpellChecker::isAsynchronousEnabled() const
88 return m_frame->settings() && m_frame->settings()->asynchronousSpellCheckingEnabled();
91 bool SpellChecker::canCheckAsynchronously(Node* node) const
93 return client() && isCheckable(node) && isAsynchronousEnabled() && !isBusy();
96 bool SpellChecker::isBusy() const
98 return m_requestNode.get();
101 bool SpellChecker::isValid(int sequence) const
103 return m_requestNode.get() && m_requestText.length() && m_requestSequence == sequence;
106 bool SpellChecker::isCheckable(Node* node) const
108 return node && node->renderer();
111 void SpellChecker::requestCheckingFor(TextCheckingTypeMask mask, Node* node)
113 ASSERT(canCheckAsynchronously(node));
115 if (!initRequest(node))
117 client()->requestCheckingOfString(this, m_requestSequence, mask, m_requestText);
120 static bool forwardIterator(PositionIterator& iterator, int distance)
122 int remaining = distance;
123 while (!iterator.atEnd()) {
124 if (iterator.node()->isCharacterDataNode()) {
125 int length = lastOffsetForEditing(iterator.node());
126 int last = length - iterator.offsetInLeafNode();
127 if (remaining < last) {
128 iterator.setOffsetInLeafNode(iterator.offsetInLeafNode() + remaining);
133 iterator.setOffsetInLeafNode(iterator.offsetInLeafNode() + last);
136 iterator.increment();
142 static DocumentMarker::MarkerType toMarkerType(TextCheckingType type)
144 if (type == TextCheckingTypeSpelling)
145 return DocumentMarker::Spelling;
146 ASSERT(type == TextCheckingTypeGrammar);
147 return DocumentMarker::Grammar;
150 // Currenntly ignoring TextCheckingResult::details but should be handled. See Bug 56368.
151 void SpellChecker::didCheck(int sequence, const Vector<TextCheckingResult>& results)
153 if (!isValid(sequence))
156 if (!m_requestNode->renderer()) {
162 PositionIterator start = firstPositionInOrBeforeNode(m_requestNode.get());
163 for (size_t i = 0; i < results.size(); ++i) {
164 if (results[i].type != TextCheckingTypeSpelling && results[i].type != TextCheckingTypeGrammar)
167 // To avoid moving the position backward, we assume the given results are sorted with
168 // startOffset as the ones returned by [NSSpellChecker requestCheckingOfString:].
169 ASSERT(startOffset <= results[i].location);
170 if (!forwardIterator(start, results[i].location - startOffset))
172 PositionIterator end = start;
173 if (!forwardIterator(end, results[i].length))
176 // Users or JavaScript applications may change text while a spell-checker checks its
177 // spellings in the background. To avoid adding markers to the words modified by users or
178 // JavaScript applications, retrieve the words in the specified region and compare them with
179 // the original ones.
180 RefPtr<Range> range = Range::create(m_requestNode->document(), start, end);
181 // FIXME: Use textContent() compatible string conversion.
182 String destination = range->text();
183 String source = m_requestText.substring(results[i].location, results[i].length);
184 if (destination == source)
185 m_requestNode->document()->markers()->addMarker(range.get(), toMarkerType(results[i].type));
187 startOffset = results[i].location;
194 } // namespace WebCore