• 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 "MockSpellCheck.h"
32 
33 #include "TestCommon.h"
34 #include "public/platform/WebCString.h"
35 
36 using namespace blink;
37 using namespace std;
38 
39 namespace WebTestRunner {
40 
41 namespace {
42 
append(WebVector<WebString> * data,const WebString & item)43 void append(WebVector<WebString>* data, const WebString& item)
44 {
45     WebVector<WebString> result(data->size() + 1);
46     for (size_t i = 0; i < data->size(); ++i)
47         result[i] = (*data)[i];
48     result[data->size()] = item;
49     data->swap(result);
50 }
51 
52 }
53 
MockSpellCheck()54 MockSpellCheck::MockSpellCheck()
55     : m_initialized(false) { }
56 
~MockSpellCheck()57 MockSpellCheck::~MockSpellCheck() { }
58 
spellCheckWord(const WebString & text,int * misspelledOffset,int * misspelledLength)59 bool MockSpellCheck::spellCheckWord(const WebString& text, int* misspelledOffset, int* misspelledLength)
60 {
61     BLINK_ASSERT(misspelledOffset);
62     BLINK_ASSERT(misspelledLength);
63 
64     // Initialize this spellchecker.
65     initializeIfNeeded();
66 
67     // Reset the result values as our spellchecker does.
68     *misspelledOffset = 0;
69     *misspelledLength = 0;
70 
71     // Convert to a string16 because we store string16 instances in
72     // m_misspelledWords and WebString has no find().
73     string16 stringText = text;
74     int skippedLength = 0;
75 
76     while (!stringText.empty()) {
77         // Extract the first possible English word from the given string.
78         // The given string may include non-ASCII characters or numbers. So, we
79         // should filter out such characters before start looking up our
80         // misspelled-word table.
81         // (This is a simple version of our SpellCheckWordIterator class.)
82         // If the given string doesn't include any ASCII characters, we can treat the
83         // string as valid one.
84         string16::iterator firstChar = find_if(stringText.begin(), stringText.end(), isASCIIAlpha);
85         if (firstChar == stringText.end())
86             return true;
87         int wordOffset = distance(stringText.begin(), firstChar);
88         int maxWordLength = static_cast<int>(stringText.length()) - wordOffset;
89         int wordLength;
90         string16 word;
91 
92         // Look up our misspelled-word table to check if the extracted word is a
93         // known misspelled word, and return the offset and the length of the
94         // extracted word if this word is a known misspelled word.
95         // (See the comment in MockSpellCheck::initializeIfNeeded() why we use a
96         // misspelled-word table.)
97         for (size_t i = 0; i < m_misspelledWords.size(); ++i) {
98             wordLength = static_cast<int>(m_misspelledWords.at(i).length()) > maxWordLength ? maxWordLength : static_cast<int>(m_misspelledWords.at(i).length());
99             word = stringText.substr(wordOffset, wordLength);
100             if (word == m_misspelledWords.at(i) && (static_cast<int>(stringText.length()) == wordOffset + wordLength || isNotASCIIAlpha(stringText[wordOffset + wordLength]))) {
101                 *misspelledOffset = wordOffset + skippedLength;
102                 *misspelledLength = wordLength;
103                 break;
104             }
105         }
106 
107         if (*misspelledLength > 0)
108             break;
109 
110         string16::iterator lastChar = find_if(stringText.begin() + wordOffset, stringText.end(), isNotASCIIAlpha);
111         if (lastChar == stringText.end())
112             wordLength = static_cast<int>(stringText.length()) - wordOffset;
113         else
114             wordLength = distance(firstChar, lastChar);
115 
116         BLINK_ASSERT(0 < wordOffset + wordLength);
117         stringText = stringText.substr(wordOffset + wordLength);
118         skippedLength += wordOffset + wordLength;
119     }
120 
121     return false;
122 }
123 
hasInCache(const WebString & word)124 bool MockSpellCheck::hasInCache(const WebString& word)
125 {
126     return word == WebString::fromUTF8("Spell wellcome. Is it broken?") || word == WebString::fromUTF8("Spell wellcome.\x007F");
127 }
128 
isMultiWordMisspelling(const WebString & text,vector<WebTextCheckingResult> * results)129 bool MockSpellCheck::isMultiWordMisspelling(const WebString& text, vector<WebTextCheckingResult>* results)
130 {
131     if (text == WebString::fromUTF8("Helllo wordl.")) {
132         results->push_back(WebTextCheckingResult(WebTextDecorationTypeSpelling, 0, 6, WebString("Hello")));
133         results->push_back(WebTextCheckingResult(WebTextDecorationTypeSpelling, 7, 5, WebString("world")));
134         return true;
135     }
136     return false;
137 }
138 
fillSuggestionList(const WebString & word,WebVector<WebString> * suggestions)139 void MockSpellCheck::fillSuggestionList(const WebString& word, WebVector<WebString>* suggestions)
140 {
141     if (word == WebString::fromUTF8("wellcome"))
142         append(suggestions, WebString::fromUTF8("welcome"));
143     else if (word == WebString::fromUTF8("upper case"))
144         append(suggestions, WebString::fromUTF8("uppercase"));
145     else if (word == WebString::fromUTF8("Helllo"))
146         append(suggestions, WebString::fromUTF8("Hello"));
147     else if (word == WebString::fromUTF8("wordl"))
148         append(suggestions, WebString::fromUTF8("world"));
149 }
150 
initializeIfNeeded()151 bool MockSpellCheck::initializeIfNeeded()
152 {
153     // Exit if we have already initialized this object.
154     if (m_initialized)
155         return false;
156 
157     // Create a table that consists of misspelled words used in WebKit layout
158     // tests.
159     // Since WebKit layout tests don't have so many misspelled words as
160     // well-spelled words, it is easier to compare the given word with misspelled
161     // ones than to compare with well-spelled ones.
162     static const char* misspelledWords[] = {
163         // These words are known misspelled words in webkit tests.
164         // If there are other misspelled words in webkit tests, please add them in
165         // this array.
166         "foo",
167         "Foo",
168         "baz",
169         "fo",
170         "LibertyF",
171         "chello",
172         "xxxtestxxx",
173         "XXxxx",
174         "Textx",
175         "blockquoted",
176         "asd",
177         "Lorem",
178         "Nunc",
179         "Curabitur",
180         "eu",
181         "adlj",
182         "adaasj",
183         "sdklj",
184         "jlkds",
185         "jsaada",
186         "jlda",
187         "zz",
188         "contentEditable",
189         // The following words are used by unit tests.
190         "ifmmp",
191         "qwertyuiopasd",
192         "qwertyuiopasdf",
193         "upper case",
194         "wellcome"
195     };
196 
197     m_misspelledWords.clear();
198     for (size_t i = 0; i < arraysize(misspelledWords); ++i)
199         m_misspelledWords.push_back(string16(misspelledWords[i], misspelledWords[i] + strlen(misspelledWords[i])));
200 
201     // Mark as initialized to prevent this object from being initialized twice
202     // or more.
203     m_initialized = true;
204 
205     // Since this MockSpellCheck class doesn't download dictionaries, this
206     // function always returns false.
207     return false;
208 }
209 
210 }
211