Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/renderer/spellchecker/spellcheck.h" | 5 #include "chrome/renderer/spellchecker/spellcheck.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 std::transform(words.begin(), words.end(), words_.begin(), | 90 std::transform(words.begin(), words.end(), words_.begin(), |
| 91 [](const std::string& w) { return WebString::fromUTF8(w); }); | 91 [](const std::string& w) { return WebString::fromUTF8(w); }); |
| 92 } | 92 } |
| 93 | 93 |
| 94 bool DocumentMarkersRemover::Visit(content::RenderView* render_view) { | 94 bool DocumentMarkersRemover::Visit(content::RenderView* render_view) { |
| 95 if (render_view && render_view->GetWebView()) | 95 if (render_view && render_view->GetWebView()) |
| 96 render_view->GetWebView()->removeSpellingMarkersUnderWords(words_); | 96 render_view->GetWebView()->removeSpellingMarkersUnderWords(words_); |
| 97 return true; | 97 return true; |
| 98 } | 98 } |
| 99 | 99 |
| 100 bool IsApostrophe(base::char16 c) { | |
|
groby-ooo-7-16
2015/06/17 19:12:03
The literal below is wchar_t, not char16
And on s
please use gerrit instead
2015/06/17 20:21:35
Switched to wchar_t.
| |
| 101 return c == '\'' || c == L'\x2019'; | |
| 102 } | |
| 103 | |
| 104 // Makes sure that the apostrophes in the |spelling_suggestion| are the same | |
| 105 // type as in the |misspelled_word| and in the same order. Ignore differences in | |
| 106 // the number of apostrophes. | |
| 107 void PreserveOriginalApostropheTypes(const base::string16& misspelled_word, | |
| 108 base::string16* spelling_suggestion) { | |
| 109 auto it = spelling_suggestion->begin(); | |
| 110 for (const base::char16& c : misspelled_word) { | |
| 111 if (IsApostrophe(c)) { | |
| 112 it = std::find_if(it, spelling_suggestion->end(), IsApostrophe); | |
| 113 if (it == spelling_suggestion->end()) | |
| 114 return; | |
| 115 | |
| 116 *it++ = c; | |
| 117 } | |
| 118 } | |
| 119 } | |
| 120 | |
| 100 } // namespace | 121 } // namespace |
| 101 | 122 |
| 102 class SpellCheck::SpellcheckRequest { | 123 class SpellCheck::SpellcheckRequest { |
| 103 public: | 124 public: |
| 104 SpellcheckRequest(const base::string16& text, | 125 SpellcheckRequest(const base::string16& text, |
| 105 blink::WebTextCheckingCompletion* completion) | 126 blink::WebTextCheckingCompletion* completion) |
| 106 : text_(text), completion_(completion) { | 127 : text_(text), completion_(completion) { |
| 107 DCHECK(completion); | 128 DCHECK(completion); |
| 108 } | 129 } |
| 109 ~SpellcheckRequest() {} | 130 ~SpellcheckRequest() {} |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 376 } | 397 } |
| 377 } | 398 } |
| 378 #endif | 399 #endif |
| 379 | 400 |
| 380 void SpellCheck::CreateTextCheckingResults( | 401 void SpellCheck::CreateTextCheckingResults( |
| 381 ResultFilter filter, | 402 ResultFilter filter, |
| 382 int line_offset, | 403 int line_offset, |
| 383 const base::string16& line_text, | 404 const base::string16& line_text, |
| 384 const std::vector<SpellCheckResult>& spellcheck_results, | 405 const std::vector<SpellCheckResult>& spellcheck_results, |
| 385 WebVector<WebTextCheckingResult>* textcheck_results) { | 406 WebVector<WebTextCheckingResult>* textcheck_results) { |
| 386 // Double-check misspelled words with our spellchecker and attach grammar | 407 std::vector<WebTextCheckingResult> results; |
| 387 // markers to them if our spellchecker tells they are correct words, i.e. they | 408 for (const SpellCheckResult& spellcheck_result : spellcheck_results) { |
| 388 // are probably contextually-misspelled words. | 409 const base::string16& misspelled_word = |
| 389 const base::char16* text = line_text.c_str(); | 410 line_text.substr(spellcheck_result.location, spellcheck_result.length); |
| 390 std::vector<WebTextCheckingResult> list; | 411 |
| 391 for (size_t i = 0; i < spellcheck_results.size(); ++i) { | 412 // Ignore words in custom dictionary. |
| 392 SpellCheckResult::Decoration decoration = spellcheck_results[i].decoration; | 413 if (custom_dictionary_.SpellCheckWord(misspelled_word, 0, |
| 393 int word_location = spellcheck_results[i].location; | 414 misspelled_word.length())) { |
| 394 int word_length = spellcheck_results[i].length; | 415 continue; |
| 395 int misspelling_start = 0; | 416 } |
| 396 int misspelling_length = 0; | 417 |
| 418 // Use the same types of appostrophes as in the mispelled word. | |
| 419 base::string16 replacement = spellcheck_result.replacement; | |
| 420 PreserveOriginalApostropheTypes(misspelled_word, &replacement); | |
| 421 | |
| 422 // Ignore misspellings due the typographical apostrophe. | |
| 423 if (misspelled_word == replacement) | |
| 424 continue; | |
| 425 | |
| 426 // Use grammar decoration when the server spellcheck marks a word | |
| 427 // misspelled, but the local spellcheck does not. | |
| 428 SpellCheckResult::Decoration decoration = spellcheck_result.decoration; | |
| 429 int unused_misspelling_start = 0; | |
| 430 int unused_misspelling_length = 0; | |
| 397 if (decoration == SpellCheckResult::SPELLING && | 431 if (decoration == SpellCheckResult::SPELLING && |
| 398 filter == USE_NATIVE_CHECKER) { | 432 filter == USE_NATIVE_CHECKER && |
| 399 if (SpellCheckWord(text + word_location, word_length, 0, | 433 SpellCheckWord(misspelled_word.c_str(), misspelled_word.length(), 0, |
| 400 &misspelling_start, &misspelling_length, NULL)) { | 434 &unused_misspelling_start, &unused_misspelling_length, |
| 401 decoration = SpellCheckResult::GRAMMAR; | 435 nullptr)) { |
| 402 } | 436 decoration = SpellCheckResult::GRAMMAR; |
| 403 } | 437 } |
| 404 if (!custom_dictionary_.SpellCheckWord( | 438 |
| 405 line_text, word_location, word_length)) { | 439 results.push_back(WebTextCheckingResult( |
| 406 list.push_back(WebTextCheckingResult( | 440 static_cast<WebTextDecorationType>(decoration), |
| 407 static_cast<WebTextDecorationType>(decoration), | 441 line_offset + spellcheck_result.location, spellcheck_result.length, |
| 408 word_location + line_offset, | 442 replacement, spellcheck_result.hash)); |
| 409 word_length, | |
| 410 spellcheck_results[i].replacement, | |
| 411 spellcheck_results[i].hash)); | |
| 412 } | |
| 413 } | 443 } |
| 414 textcheck_results->assign(list); | 444 |
| 445 textcheck_results->assign(results); | |
| 415 } | 446 } |
| OLD | NEW |