| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/mock_spell_check.h" | 5 #include "content/shell/renderer/test_runner/mock_spell_check.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/test_common.h" |
| 9 #include "third_party/WebKit/public/platform/WebCString.h" | 9 #include "third_party/WebKit/public/platform/WebCString.h" |
| 10 | 10 |
| 11 namespace content { | 11 namespace content { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 void Append(blink::WebVector<blink::WebString>* data, | 15 void Append(blink::WebVector<blink::WebString>* data, |
| 16 const blink::WebString& item) { | 16 const blink::WebString& item) { |
| 17 blink::WebVector<blink::WebString> result(data->size() + 1); | 17 blink::WebVector<blink::WebString> result(data->size() + 1); |
| 18 for (size_t i = 0; i < data->size(); ++i) | 18 for (size_t i = 0; i < data->size(); ++i) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 49 | 49 |
| 50 while (!string_text.empty()) { | 50 while (!string_text.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 treat | 56 // If the given string doesn't include any ASCII characters, we can treat |
| 57 // the string as valid one. | 57 // the string as valid one. |
| 58 base::string16::iterator first_char = | 58 base::string16::iterator first_char = |
| 59 std::find_if(string_text.begin(), string_text.end(), isASCIIAlpha); | 59 std::find_if(string_text.begin(), string_text.end(), IsASCIIAlpha); |
| 60 if (first_char == string_text.end()) | 60 if (first_char == string_text.end()) |
| 61 return true; | 61 return true; |
| 62 int word_offset = std::distance(string_text.begin(), first_char); | 62 int word_offset = std::distance(string_text.begin(), first_char); |
| 63 int max_word_length = static_cast<int>(string_text.length()) - word_offset; | 63 int max_word_length = static_cast<int>(string_text.length()) - word_offset; |
| 64 int word_length; | 64 int word_length; |
| 65 base::string16 word; | 65 base::string16 word; |
| 66 | 66 |
| 67 // Look up our misspelled-word table to check if the extracted word is a | 67 // 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 | 68 // known misspelled word, and return the offset and the length of the |
| 69 // extracted word if this word is a known misspelled word. | 69 // extracted word if this word is a known misspelled word. |
| 70 // (See the comment in MockSpellCheck::InitializeIfNeeded() why we use a | 70 // (See the comment in MockSpellCheck::InitializeIfNeeded() why we use a |
| 71 // misspelled-word table.) | 71 // misspelled-word table.) |
| 72 for (size_t i = 0; i < misspelled_words_.size(); ++i) { | 72 for (size_t i = 0; i < misspelled_words_.size(); ++i) { |
| 73 word_length = | 73 word_length = |
| 74 static_cast<int>(misspelled_words_.at(i).length()) > max_word_length | 74 static_cast<int>(misspelled_words_.at(i).length()) > max_word_length |
| 75 ? max_word_length | 75 ? max_word_length |
| 76 : static_cast<int>(misspelled_words_.at(i).length()); | 76 : static_cast<int>(misspelled_words_.at(i).length()); |
| 77 word = string_text.substr(word_offset, word_length); | 77 word = string_text.substr(word_offset, word_length); |
| 78 if (word == misspelled_words_.at(i) && | 78 if (word == misspelled_words_.at(i) && |
| 79 (static_cast<int>(string_text.length()) == | 79 (static_cast<int>(string_text.length()) == |
| 80 word_offset + word_length || | 80 word_offset + word_length || |
| 81 isNotASCIIAlpha(string_text[word_offset + word_length]))) { | 81 IsNotASCIIAlpha(string_text[word_offset + word_length]))) { |
| 82 *misspelled_offset = word_offset + skipped_length; | 82 *misspelled_offset = word_offset + skipped_length; |
| 83 *misspelled_length = word_length; | 83 *misspelled_length = word_length; |
| 84 break; | 84 break; |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 if (*misspelled_length > 0) | 88 if (*misspelled_length > 0) |
| 89 break; | 89 break; |
| 90 | 90 |
| 91 base::string16::iterator last_char = std::find_if( | 91 base::string16::iterator last_char = std::find_if( |
| 92 string_text.begin() + word_offset, string_text.end(), isNotASCIIAlpha); | 92 string_text.begin() + word_offset, string_text.end(), IsNotASCIIAlpha); |
| 93 if (last_char == string_text.end()) | 93 if (last_char == string_text.end()) |
| 94 word_length = static_cast<int>(string_text.length()) - word_offset; | 94 word_length = static_cast<int>(string_text.length()) - word_offset; |
| 95 else | 95 else |
| 96 word_length = std::distance(first_char, last_char); | 96 word_length = std::distance(first_char, last_char); |
| 97 | 97 |
| 98 DCHECK_LT(0, word_offset + word_length); | 98 DCHECK_LT(0, word_offset + word_length); |
| 99 string_text = string_text.substr(word_offset + word_length); | 99 string_text = string_text.substr(word_offset + word_length); |
| 100 skipped_length += word_offset + word_length; | 100 skipped_length += word_offset + word_length; |
| 101 } | 101 } |
| 102 | 102 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 // Mark as initialized to prevent this object from being initialized twice | 165 // Mark as initialized to prevent this object from being initialized twice |
| 166 // or more. | 166 // or more. |
| 167 initialized_ = true; | 167 initialized_ = true; |
| 168 | 168 |
| 169 // Since this MockSpellCheck class doesn't download dictionaries, this | 169 // Since this MockSpellCheck class doesn't download dictionaries, this |
| 170 // function always returns false. | 170 // function always returns false. |
| 171 return false; | 171 return false; |
| 172 } | 172 } |
| 173 | 173 |
| 174 } // namespace content | 174 } // namespace content |
| OLD | NEW |