[Release] Webkit-EFL Ver. 2.0_beta_118996_0.6.24
[framework/web/webkit-efl.git] / Tools / DumpRenderTree / chromium / MockSpellCheck.cpp
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 "platform/WebString.h"
35 #include <wtf/ASCIICType.h>
36 #include <wtf/Assertions.h>
37 #include <wtf/text/WTFString.h>
38
39 using namespace WebKit;
40
41 MockSpellCheck::MockSpellCheck()
42     : m_initialized(false) { }
43
44 MockSpellCheck::~MockSpellCheck() { }
45
46 static bool isNotASCIIAlpha(UChar ch) { return !isASCIIAlpha(ch); }
47
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     WTF::String stringText(text.data(), text.length());
63     int skippedLength = 0;
64
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);
77         if (wordOffset == -1)
78             return true;
79         int wordEnd = stringText.find(isNotASCIIAlpha, wordOffset);
80         int wordLength = wordEnd == -1 ? static_cast<int>(stringText.length()) - wordOffset : wordEnd - wordOffset;
81
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;
91             break;
92         }
93
94         ASSERT(0 < wordOffset + wordLength);
95         stringText = stringText.substring(wordOffset + wordLength);
96         skippedLength += wordOffset + wordLength;
97     }
98
99     return false;
100 }
101
102 void MockSpellCheck::fillSuggestionList(const WebString& word, Vector<WebString>* suggestions)
103 {
104     if (word == WebString::fromUTF8("wellcome"))
105         suggestions->append(WebString::fromUTF8("welcome"));
106 }
107
108 bool MockSpellCheck::initializeIfNeeded()
109 {
110     // Exit if we have already initialized this object.
111     if (m_initialized)
112         return false;
113
114     // Create a table that consists of misspelled words used in WebKit layout
115     // tests.
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
122         // this array.
123         "foo",
124         "Foo",
125         "baz",
126         "fo",
127         "LibertyF",
128         "chello",
129         "xxxtestxxx",
130         "XXxxx",
131         "Textx",
132         "blockquoted",
133         "asd",
134         "Lorem",
135         "Nunc",
136         "Curabitur",
137         "eu",
138         "adlj",
139         "adaasj",
140         "sdklj",
141         "jlkds",
142         "jsaada",
143         "jlda",
144         "zz",
145         "contentEditable",
146         // The following words are used by unit tests.
147         "ifmmp",
148         "qwertyuiopasd",
149         "qwertyuiopasdf",
150         "wellcome"
151     };
152
153     m_misspelledWords.clear();
154     for (size_t i = 0; i < arraysize(misspelledWords); ++i)
155         m_misspelledWords.add(WTF::String::fromUTF8(misspelledWords[i]), false);
156
157     // Mark as initialized to prevent this object from being initialized twice
158     // or more.
159     m_initialized = true;
160
161     // Since this MockSpellCheck class doesn't download dictionaries, this
162     // function always returns false.
163     return false;
164 }