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 "public/WebString.h" |
|
35 #include <wtf/ASCIICType.h> |
|
36 #include <wtf/Assertions.h> |
|
37 |
|
38 using namespace WebCore; |
|
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 const 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 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 |
|
94 bool MockSpellCheck::initializeIfNeeded() |
|
95 { |
|
96 // Exit if we have already initialized this object. |
|
97 if (m_initialized) |
|
98 return false; |
|
99 |
|
100 // Create a table that consists of misspelled words used in WebKit layout |
|
101 // tests. |
|
102 // Since WebKit layout tests don't have so many misspelled words as |
|
103 // well-spelled words, it is easier to compare the given word with misspelled |
|
104 // ones than to compare with well-spelled ones. |
|
105 static const char* misspelledWords[] = { |
|
106 // These words are known misspelled words in webkit tests. |
|
107 // If there are other misspelled words in webkit tests, please add them in |
|
108 // this array. |
|
109 "foo", |
|
110 "Foo", |
|
111 "baz", |
|
112 "fo", |
|
113 "LibertyF", |
|
114 "chello", |
|
115 "xxxtestxxx", |
|
116 "XXxxx", |
|
117 "Textx", |
|
118 "blockquoted", |
|
119 "asd", |
|
120 "Lorem", |
|
121 "Nunc", |
|
122 "Curabitur", |
|
123 "eu", |
|
124 "adlj", |
|
125 "adaasj", |
|
126 "sdklj", |
|
127 "jlkds", |
|
128 "jsaada", |
|
129 "jlda", |
|
130 "zz", |
|
131 "contentEditable", |
|
132 // The following words are used by unit tests. |
|
133 "ifmmp", |
|
134 "qwertyuiopasd", |
|
135 "qwertyuiopasdf", |
|
136 }; |
|
137 |
|
138 m_misspelledWords.clear(); |
|
139 for (size_t i = 0; i < arraysize(misspelledWords); ++i) |
|
140 m_misspelledWords.add(String::fromUTF8(misspelledWords[i]), false); |
|
141 |
|
142 // Mark as initialized to prevent this object from being initialized twice |
|
143 // or more. |
|
144 m_initialized = true; |
|
145 |
|
146 // Since this MockSpellCheck class doesn't download dictionaries, this |
|
147 // function always returns false. |
|
148 return false; |
|
149 } |
|