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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "MockSpellCheck.h"
34 #include "platform/WebString.h"
35 #include <wtf/ASCIICType.h>
36 #include <wtf/Assertions.h>
37 #include <wtf/text/WTFString.h>
39 using namespace WebKit;
41 MockSpellCheck::MockSpellCheck()
42 : m_initialized(false) { }
44 MockSpellCheck::~MockSpellCheck() { }
46 static bool isNotASCIIAlpha(UChar ch) { return !isASCIIAlpha(ch); }
48 bool MockSpellCheck::spellCheckWord(const WebString& text, int* misspelledOffset, int* misspelledLength)
50 ASSERT(misspelledOffset);
51 ASSERT(misspelledLength);
53 // Initialize this spellchecker.
56 // Reset the result values as our spellchecker does.
57 *misspelledOffset = 0;
58 *misspelledLength = 0;
60 // Convert to a String because we store String instances in
61 // m_misspelledWords and WebString has no find().
62 WTF::String stringText(text.data(), text.length());
63 int skippedLength = 0;
65 while (!stringText.isEmpty()) {
66 // Extract the first possible English word from the given string.
67 // The given string may include non-ASCII characters or numbers. So, we
68 // should filter out such characters before start looking up our
69 // misspelled-word table.
70 // (This is a simple version of our SpellCheckWordIterator class.)
71 // If the given string doesn't include any ASCII characters, we can treat the
72 // string as valid one.
73 // Unfortunately, This implementation splits a contraction, i.e. "isn't" is
74 // split into two pieces "isn" and "t". This is OK because webkit tests
75 // don't have misspelled contractions.
76 int wordOffset = stringText.find(isASCIIAlpha);
79 int wordEnd = stringText.find(isNotASCIIAlpha, wordOffset);
80 int wordLength = wordEnd == -1 ? static_cast<int>(stringText.length()) - wordOffset : wordEnd - wordOffset;
82 // Look up our misspelled-word table to check if the extracted word is a
83 // known misspelled word, and return the offset and the length of the
84 // extracted word if this word is a known misspelled word.
85 // (See the comment in MockSpellCheck::initializeIfNeeded() why we use a
86 // misspelled-word table.)
87 WTF::String word = stringText.substring(wordOffset, wordLength);
88 if (m_misspelledWords.contains(word)) {
89 *misspelledOffset = wordOffset + skippedLength;
90 *misspelledLength = wordLength;
94 ASSERT(0 < wordOffset + wordLength);
95 stringText = stringText.substring(wordOffset + wordLength);
96 skippedLength += wordOffset + wordLength;
102 void MockSpellCheck::fillSuggestionList(const WebString& word, Vector<WebString>* suggestions)
104 if (word == WebString::fromUTF8("wellcome"))
105 suggestions->append(WebString::fromUTF8("welcome"));
108 bool MockSpellCheck::initializeIfNeeded()
110 // Exit if we have already initialized this object.
114 // Create a table that consists of misspelled words used in WebKit layout
116 // Since WebKit layout tests don't have so many misspelled words as
117 // well-spelled words, it is easier to compare the given word with misspelled
118 // ones than to compare with well-spelled ones.
119 static const char* misspelledWords[] = {
120 // These words are known misspelled words in webkit tests.
121 // If there are other misspelled words in webkit tests, please add them in
146 // The following words are used by unit tests.
153 m_misspelledWords.clear();
154 for (size_t i = 0; i < arraysize(misspelledWords); ++i)
155 m_misspelledWords.add(WTF::String::fromUTF8(misspelledWords[i]), false);
157 // Mark as initialized to prevent this object from being initialized twice
159 m_initialized = true;
161 // Since this MockSpellCheck class doesn't download dictionaries, this
162 // function always returns false.