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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "content/shell/renderer/test_runner/TestCommon.h" | 8 #include "content/shell/renderer/test_runner/TestCommon.h" |
9 #include "third_party/WebKit/public/platform/WebCString.h" | 9 #include "third_party/WebKit/public/platform/WebCString.h" |
10 | 10 |
11 using namespace blink; | 11 using namespace blink; |
12 using namespace std; | |
13 | 12 |
14 namespace content { | 13 namespace content { |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 void append(WebVector<WebString>* data, const WebString& item) | 17 void append(WebVector<WebString>* data, const WebString& item) |
19 { | 18 { |
20 WebVector<WebString> result(data->size() + 1); | 19 WebVector<WebString> result(data->size() + 1); |
21 for (size_t i = 0; i < data->size(); ++i) | 20 for (size_t i = 0; i < data->size(); ++i) |
22 result[i] = (*data)[i]; | 21 result[i] = (*data)[i]; |
(...skipping 26 matching lines...) Expand all Loading... |
49 int skippedLength = 0; | 48 int skippedLength = 0; |
50 | 49 |
51 while (!stringText.empty()) { | 50 while (!stringText.empty()) { |
52 // Extract the first possible English word from the given string. | 51 // Extract the first possible English word from the given string. |
53 // 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 |
54 // should filter out such characters before start looking up our | 53 // should filter out such characters before start looking up our |
55 // misspelled-word table. | 54 // misspelled-word table. |
56 // (This is a simple version of our SpellCheckWordIterator class.) | 55 // (This is a simple version of our SpellCheckWordIterator class.) |
57 // 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 |
58 // string as valid one. | 57 // string as valid one. |
59 base::string16::iterator firstChar = find_if(stringText.begin(), stringT
ext.end(), isASCIIAlpha); | 58 base::string16::iterator firstChar = std::find_if(stringText.begin(), st
ringText.end(), isASCIIAlpha); |
60 if (firstChar == stringText.end()) | 59 if (firstChar == stringText.end()) |
61 return true; | 60 return true; |
62 int wordOffset = distance(stringText.begin(), firstChar); | 61 int wordOffset = std::distance(stringText.begin(), firstChar); |
63 int maxWordLength = static_cast<int>(stringText.length()) - wordOffset; | 62 int maxWordLength = static_cast<int>(stringText.length()) - wordOffset; |
64 int wordLength; | 63 int wordLength; |
65 base::string16 word; | 64 base::string16 word; |
66 | 65 |
67 // 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 |
68 // 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 |
69 // extracted word if this word is a known misspelled word. | 68 // extracted word if this word is a known misspelled word. |
70 // (See the comment in MockSpellCheck::initializeIfNeeded() why we use a | 69 // (See the comment in MockSpellCheck::initializeIfNeeded() why we use a |
71 // misspelled-word table.) | 70 // misspelled-word table.) |
72 for (size_t i = 0; i < m_misspelledWords.size(); ++i) { | 71 for (size_t i = 0; i < m_misspelledWords.size(); ++i) { |
73 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())
; |
74 word = stringText.substr(wordOffset, wordLength); | 73 word = stringText.substr(wordOffset, wordLength); |
75 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]))) { |
76 *misspelledOffset = wordOffset + skippedLength; | 75 *misspelledOffset = wordOffset + skippedLength; |
77 *misspelledLength = wordLength; | 76 *misspelledLength = wordLength; |
78 break; | 77 break; |
79 } | 78 } |
80 } | 79 } |
81 | 80 |
82 if (*misspelledLength > 0) | 81 if (*misspelledLength > 0) |
83 break; | 82 break; |
84 | 83 |
85 base::string16::iterator lastChar = find_if(stringText.begin() + wordOff
set, stringText.end(), isNotASCIIAlpha); | 84 base::string16::iterator lastChar = std::find_if(stringText.begin() + wo
rdOffset, stringText.end(), isNotASCIIAlpha); |
86 if (lastChar == stringText.end()) | 85 if (lastChar == stringText.end()) |
87 wordLength = static_cast<int>(stringText.length()) - wordOffset; | 86 wordLength = static_cast<int>(stringText.length()) - wordOffset; |
88 else | 87 else |
89 wordLength = distance(firstChar, lastChar); | 88 wordLength = std::distance(firstChar, lastChar); |
90 | 89 |
91 DCHECK_LT(0, wordOffset + wordLength); | 90 DCHECK_LT(0, wordOffset + wordLength); |
92 stringText = stringText.substr(wordOffset + wordLength); | 91 stringText = stringText.substr(wordOffset + wordLength); |
93 skippedLength += wordOffset + wordLength; | 92 skippedLength += wordOffset + wordLength; |
94 } | 93 } |
95 | 94 |
96 return false; | 95 return false; |
97 } | 96 } |
98 | 97 |
99 bool MockSpellCheck::hasInCache(const WebString& word) | 98 bool MockSpellCheck::hasInCache(const WebString& word) |
100 { | 99 { |
101 return word == WebString::fromUTF8("Spell wellcome. Is it broken?") || word
== WebString::fromUTF8("Spell wellcome.\x007F"); | 100 return word == WebString::fromUTF8("Spell wellcome. Is it broken?") || word
== WebString::fromUTF8("Spell wellcome.\x007F"); |
102 } | 101 } |
103 | 102 |
104 bool MockSpellCheck::isMultiWordMisspelling(const WebString& text, vector<WebTex
tCheckingResult>* results) | 103 bool MockSpellCheck::isMultiWordMisspelling(const WebString& text, std::vector<W
ebTextCheckingResult>* results) |
105 { | 104 { |
106 if (text == WebString::fromUTF8("Helllo wordl.")) { | 105 if (text == WebString::fromUTF8("Helllo wordl.")) { |
107 results->push_back(WebTextCheckingResult(WebTextDecorationTypeSpelling,
0, 6, WebString("Hello"))); | 106 results->push_back(WebTextCheckingResult(WebTextDecorationTypeSpelling,
0, 6, WebString("Hello"))); |
108 results->push_back(WebTextCheckingResult(WebTextDecorationTypeSpelling,
7, 5, WebString("world"))); | 107 results->push_back(WebTextCheckingResult(WebTextDecorationTypeSpelling,
7, 5, WebString("world"))); |
109 return true; | 108 return true; |
110 } | 109 } |
111 return false; | 110 return false; |
112 } | 111 } |
113 | 112 |
114 void MockSpellCheck::fillSuggestionList(const WebString& word, WebVector<WebStri
ng>* suggestions) | 113 void MockSpellCheck::fillSuggestionList(const WebString& word, WebVector<WebStri
ng>* suggestions) |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 // Mark as initialized to prevent this object from being initialized twice | 175 // Mark as initialized to prevent this object from being initialized twice |
177 // or more. | 176 // or more. |
178 m_initialized = true; | 177 m_initialized = true; |
179 | 178 |
180 // Since this MockSpellCheck class doesn't download dictionaries, this | 179 // Since this MockSpellCheck class doesn't download dictionaries, this |
181 // function always returns false. | 180 // function always returns false. |
182 return false; | 181 return false; |
183 } | 182 } |
184 | 183 |
185 } // namespace content | 184 } // namespace content |
OLD | NEW |