• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30 
31 #include "config.h"
32 #include "MockSpellCheck.h"
33 
34 #include "WebString.h"
35 #include <wtf/ASCIICType.h>
36 #include <wtf/Assertions.h>
37 #include <wtf/text/WTFString.h>
38 
39 using namespace WebKit;
40 
MockSpellCheck()41 MockSpellCheck::MockSpellCheck()
42     : m_initialized(false) {}
43 
~MockSpellCheck()44 MockSpellCheck::~MockSpellCheck() {}
45 
isNotASCIIAlpha(UChar ch)46 static bool isNotASCIIAlpha(UChar ch) { return !isASCIIAlpha(ch); }
47 
spellCheckWord(const WebString & text,int * misspelledOffset,int * misspelledLength)48 bool MockSpellCheck::spellCheckWord(const WebString& text, int* misspelledOffset, int* misspelledLength)
49 {
50     ASSERT(misspelledOffset);
51     ASSERT(misspelledLength);
52 
53     // Initialize this spellchecker.
54     initializeIfNeeded();
55 
56     // Reset the result values as our spellchecker does.
57     *misspelledOffset = 0;
58     *misspelledLength = 0;
59 
60     // Convert to a String because we store String instances in
61     // m_misspelledWords and WebString has no find().
62     const WTF::String stringText(text.data(), text.length());
63 
64     // Extract the first possible English word from the given string.
65     // The given string may include non-ASCII characters or numbers. So, we
66     // should filter out such characters before start looking up our
67     // misspelled-word table.
68     // (This is a simple version of our SpellCheckWordIterator class.)
69     // If the given string doesn't include any ASCII characters, we can treat the
70     // string as valid one.
71     // Unfortunately, This implementation splits a contraction, i.e. "isn't" is
72     // split into two pieces "isn" and "t". This is OK because webkit tests
73     // don't have misspelled contractions.
74     int wordOffset = stringText.find(isASCIIAlpha);
75     if (wordOffset == -1)
76         return true;
77     int wordEnd = stringText.find(isNotASCIIAlpha, wordOffset);
78     int wordLength = wordEnd == -1 ? stringText.length() - wordOffset : wordEnd - wordOffset;
79 
80     // Look up our misspelled-word table to check if the extracted word is a
81     // known misspelled word, and return the offset and the length of the
82     // extracted word if this word is a known misspelled word.
83     // (See the comment in MockSpellCheck::initializeIfNeeded() why we use a
84     // misspelled-word table.)
85     WTF::String word = stringText.substring(wordOffset, wordLength);
86     if (!m_misspelledWords.contains(word))
87         return true;
88 
89     *misspelledOffset = wordOffset;
90     *misspelledLength = wordLength;
91     return false;
92 }
93 
fillSuggestionList(const WebString & word,Vector<WebString> * suggestions)94 void MockSpellCheck::fillSuggestionList(const WebString& word, Vector<WebString>* suggestions)
95 {
96     if (word == WebString::fromUTF8("wellcome"))
97         suggestions->append(WebString::fromUTF8("welcome"));
98 }
99 
initializeIfNeeded()100 bool MockSpellCheck::initializeIfNeeded()
101 {
102     // Exit if we have already initialized this object.
103     if (m_initialized)
104         return false;
105 
106     // Create a table that consists of misspelled words used in WebKit layout
107     // tests.
108     // Since WebKit layout tests don't have so many misspelled words as
109     // well-spelled words, it is easier to compare the given word with misspelled
110     // ones than to compare with well-spelled ones.
111     static const char* misspelledWords[] = {
112         // These words are known misspelled words in webkit tests.
113         // If there are other misspelled words in webkit tests, please add them in
114         // this array.
115         "foo",
116         "Foo",
117         "baz",
118         "fo",
119         "LibertyF",
120         "chello",
121         "xxxtestxxx",
122         "XXxxx",
123         "Textx",
124         "blockquoted",
125         "asd",
126         "Lorem",
127         "Nunc",
128         "Curabitur",
129         "eu",
130         "adlj",
131         "adaasj",
132         "sdklj",
133         "jlkds",
134         "jsaada",
135         "jlda",
136         "zz",
137         "contentEditable",
138         // The following words are used by unit tests.
139         "ifmmp",
140         "qwertyuiopasd",
141         "qwertyuiopasdf",
142         "wellcome"
143     };
144 
145     m_misspelledWords.clear();
146     for (size_t i = 0; i < arraysize(misspelledWords); ++i)
147         m_misspelledWords.add(WTF::String::fromUTF8(misspelledWords[i]), false);
148 
149     // Mark as initialized to prevent this object from being initialized twice
150     // or more.
151     m_initialized = true;
152 
153     // Since this MockSpellCheck class doesn't download dictionaries, this
154     // function always returns false.
155     return false;
156 }
157