| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/shell/renderer/test_runner/MockSpellCheck.h" | 5 #include "content/shell/renderer/test_runner/MockSpellCheck.h" |
| 6 | 6 |
| 7 #include "content/shell/renderer/test_runner/TestCommon.h" | 7 #include "content/shell/renderer/test_runner/TestCommon.h" |
| 8 #include "third_party/WebKit/public/platform/WebCString.h" | 8 #include "third_party/WebKit/public/platform/WebCString.h" |
| 9 | 9 |
| 10 using namespace blink; | 10 using namespace blink; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 BLINK_ASSERT(misspelledOffset); | 35 BLINK_ASSERT(misspelledOffset); |
| 36 BLINK_ASSERT(misspelledLength); | 36 BLINK_ASSERT(misspelledLength); |
| 37 | 37 |
| 38 // Initialize this spellchecker. | 38 // Initialize this spellchecker. |
| 39 initializeIfNeeded(); | 39 initializeIfNeeded(); |
| 40 | 40 |
| 41 // Reset the result values as our spellchecker does. | 41 // Reset the result values as our spellchecker does. |
| 42 *misspelledOffset = 0; | 42 *misspelledOffset = 0; |
| 43 *misspelledLength = 0; | 43 *misspelledLength = 0; |
| 44 | 44 |
| 45 // Convert to a string16 because we store string16 instances in | 45 // Convert to a base::string16 because we store base::string16 instances in |
| 46 // m_misspelledWords and WebString has no find(). | 46 // m_misspelledWords and WebString has no find(). |
| 47 string16 stringText = text; | 47 base::string16 stringText = text; |
| 48 int skippedLength = 0; | 48 int skippedLength = 0; |
| 49 | 49 |
| 50 while (!stringText.empty()) { | 50 while (!stringText.empty()) { |
| 51 // Extract the first possible English word from the given string. | 51 // Extract the first possible English word from the given string. |
| 52 // The given string may include non-ASCII characters or numbers. So, we | 52 // The given string may include non-ASCII characters or numbers. So, we |
| 53 // should filter out such characters before start looking up our | 53 // should filter out such characters before start looking up our |
| 54 // misspelled-word table. | 54 // misspelled-word table. |
| 55 // (This is a simple version of our SpellCheckWordIterator class.) | 55 // (This is a simple version of our SpellCheckWordIterator class.) |
| 56 // If the given string doesn't include any ASCII characters, we can trea
t the | 56 // If the given string doesn't include any ASCII characters, we can trea
t the |
| 57 // string as valid one. | 57 // string as valid one. |
| 58 string16::iterator firstChar = find_if(stringText.begin(), stringText.en
d(), isASCIIAlpha); | 58 base::string16::iterator firstChar = find_if(stringText.begin(), stringT
ext.end(), isASCIIAlpha); |
| 59 if (firstChar == stringText.end()) | 59 if (firstChar == stringText.end()) |
| 60 return true; | 60 return true; |
| 61 int wordOffset = distance(stringText.begin(), firstChar); | 61 int wordOffset = distance(stringText.begin(), firstChar); |
| 62 int maxWordLength = static_cast<int>(stringText.length()) - wordOffset; | 62 int maxWordLength = static_cast<int>(stringText.length()) - wordOffset; |
| 63 int wordLength; | 63 int wordLength; |
| 64 string16 word; | 64 base::string16 word; |
| 65 | 65 |
| 66 // Look up our misspelled-word table to check if the extracted word is a | 66 // Look up our misspelled-word table to check if the extracted word is a |
| 67 // known misspelled word, and return the offset and the length of the | 67 // known misspelled word, and return the offset and the length of the |
| 68 // extracted word if this word is a known misspelled word. | 68 // extracted word if this word is a known misspelled word. |
| 69 // (See the comment in MockSpellCheck::initializeIfNeeded() why we use a | 69 // (See the comment in MockSpellCheck::initializeIfNeeded() why we use a |
| 70 // misspelled-word table.) | 70 // misspelled-word table.) |
| 71 for (size_t i = 0; i < m_misspelledWords.size(); ++i) { | 71 for (size_t i = 0; i < m_misspelledWords.size(); ++i) { |
| 72 wordLength = static_cast<int>(m_misspelledWords.at(i).length()) > ma
xWordLength ? maxWordLength : static_cast<int>(m_misspelledWords.at(i).length())
; | 72 wordLength = static_cast<int>(m_misspelledWords.at(i).length()) > ma
xWordLength ? maxWordLength : static_cast<int>(m_misspelledWords.at(i).length())
; |
| 73 word = stringText.substr(wordOffset, wordLength); | 73 word = stringText.substr(wordOffset, wordLength); |
| 74 if (word == m_misspelledWords.at(i) && (static_cast<int>(stringText.
length()) == wordOffset + wordLength || isNotASCIIAlpha(stringText[wordOffset +
wordLength]))) { | 74 if (word == m_misspelledWords.at(i) && (static_cast<int>(stringText.
length()) == wordOffset + wordLength || isNotASCIIAlpha(stringText[wordOffset +
wordLength]))) { |
| 75 *misspelledOffset = wordOffset + skippedLength; | 75 *misspelledOffset = wordOffset + skippedLength; |
| 76 *misspelledLength = wordLength; | 76 *misspelledLength = wordLength; |
| 77 break; | 77 break; |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 | 80 |
| 81 if (*misspelledLength > 0) | 81 if (*misspelledLength > 0) |
| 82 break; | 82 break; |
| 83 | 83 |
| 84 string16::iterator lastChar = find_if(stringText.begin() + wordOffset, s
tringText.end(), isNotASCIIAlpha); | 84 base::string16::iterator lastChar = find_if(stringText.begin() + wordOff
set, stringText.end(), isNotASCIIAlpha); |
| 85 if (lastChar == stringText.end()) | 85 if (lastChar == stringText.end()) |
| 86 wordLength = static_cast<int>(stringText.length()) - wordOffset; | 86 wordLength = static_cast<int>(stringText.length()) - wordOffset; |
| 87 else | 87 else |
| 88 wordLength = distance(firstChar, lastChar); | 88 wordLength = distance(firstChar, lastChar); |
| 89 | 89 |
| 90 BLINK_ASSERT(0 < wordOffset + wordLength); | 90 BLINK_ASSERT(0 < wordOffset + wordLength); |
| 91 stringText = stringText.substr(wordOffset + wordLength); | 91 stringText = stringText.substr(wordOffset + wordLength); |
| 92 skippedLength += wordOffset + wordLength; | 92 skippedLength += wordOffset + wordLength; |
| 93 } | 93 } |
| 94 | 94 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 // The following words are used by unit tests. | 163 // The following words are used by unit tests. |
| 164 "ifmmp", | 164 "ifmmp", |
| 165 "qwertyuiopasd", | 165 "qwertyuiopasd", |
| 166 "qwertyuiopasdf", | 166 "qwertyuiopasdf", |
| 167 "upper case", | 167 "upper case", |
| 168 "wellcome" | 168 "wellcome" |
| 169 }; | 169 }; |
| 170 | 170 |
| 171 m_misspelledWords.clear(); | 171 m_misspelledWords.clear(); |
| 172 for (size_t i = 0; i < arraysize(misspelledWords); ++i) | 172 for (size_t i = 0; i < arraysize(misspelledWords); ++i) |
| 173 m_misspelledWords.push_back(string16(misspelledWords[i], misspelledWords
[i] + strlen(misspelledWords[i]))); | 173 m_misspelledWords.push_back(base::string16(misspelledWords[i], misspelle
dWords[i] + strlen(misspelledWords[i]))); |
| 174 | 174 |
| 175 // Mark as initialized to prevent this object from being initialized twice | 175 // Mark as initialized to prevent this object from being initialized twice |
| 176 // or more. | 176 // or more. |
| 177 m_initialized = true; | 177 m_initialized = true; |
| 178 | 178 |
| 179 // Since this MockSpellCheck class doesn't download dictionaries, this | 179 // Since this MockSpellCheck class doesn't download dictionaries, this |
| 180 // function always returns false. | 180 // function always returns false. |
| 181 return false; | 181 return false; |
| 182 } | 182 } |
| 183 | 183 |
| 184 } | 184 } |
| OLD | NEW |