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) { | |
101 const base::char16 kApostrophe = 0x27; | |
102 const base::char16 kRightSingleQuotationMark = 0x2019; | |
103 return c == kApostrophe || c == kRightSingleQuotationMark; | |
104 } | |
105 | |
106 // Makes sure that the apostrophes in the |spelling_suggestion| are the same | |
107 // type as in the |misspelled_word| and in the same order. Ignore differences in | |
108 // the number of apostrophes. | |
109 void PreserveOriginalApostropheTypes(const base::string16& misspelled_word, | |
110 base::string16* spelling_suggestion) { | |
111 auto it = spelling_suggestion->begin(); | |
112 for (const base::char16& c : misspelled_word) { | |
113 if (IsApostrophe(c)) { | |
114 it = std::find_if(it, spelling_suggestion->end(), IsApostrophe); | |
115 if (it == spelling_suggestion->end()) | |
116 return; | |
117 | |
118 *it++ = c; | |
119 } | |
120 } | |
121 } | |
122 | |
123 } // namespace | 100 } // namespace |
124 | 101 |
125 class SpellCheck::SpellcheckRequest { | 102 class SpellCheck::SpellcheckRequest { |
126 public: | 103 public: |
127 SpellcheckRequest(const base::string16& text, | 104 SpellcheckRequest(const base::string16& text, |
128 blink::WebTextCheckingCompletion* completion) | 105 blink::WebTextCheckingCompletion* completion) |
129 : text_(text), completion_(completion) { | 106 : text_(text), completion_(completion) { |
130 DCHECK(completion); | 107 DCHECK(completion); |
131 } | 108 } |
132 ~SpellcheckRequest() {} | 109 ~SpellcheckRequest() {} |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 } | 376 } |
400 } | 377 } |
401 #endif | 378 #endif |
402 | 379 |
403 void SpellCheck::CreateTextCheckingResults( | 380 void SpellCheck::CreateTextCheckingResults( |
404 ResultFilter filter, | 381 ResultFilter filter, |
405 int line_offset, | 382 int line_offset, |
406 const base::string16& line_text, | 383 const base::string16& line_text, |
407 const std::vector<SpellCheckResult>& spellcheck_results, | 384 const std::vector<SpellCheckResult>& spellcheck_results, |
408 WebVector<WebTextCheckingResult>* textcheck_results) { | 385 WebVector<WebTextCheckingResult>* textcheck_results) { |
409 std::vector<WebTextCheckingResult> results; | 386 // Double-check misspelled words with our spellchecker and attach grammar |
410 for (const SpellCheckResult& spellcheck_result : spellcheck_results) { | 387 // markers to them if our spellchecker tells they are correct words, i.e. they |
411 const base::string16& misspelled_word = | 388 // are probably contextually-misspelled words. |
412 line_text.substr(spellcheck_result.location, spellcheck_result.length); | 389 const base::char16* text = line_text.c_str(); |
413 | 390 std::vector<WebTextCheckingResult> list; |
414 // Ignore words in custom dictionary. | 391 for (size_t i = 0; i < spellcheck_results.size(); ++i) { |
415 if (custom_dictionary_.SpellCheckWord(misspelled_word, 0, | 392 SpellCheckResult::Decoration decoration = spellcheck_results[i].decoration; |
416 misspelled_word.length())) { | 393 int word_location = spellcheck_results[i].location; |
417 continue; | 394 int word_length = spellcheck_results[i].length; |
| 395 int misspelling_start = 0; |
| 396 int misspelling_length = 0; |
| 397 if (decoration == SpellCheckResult::SPELLING && |
| 398 filter == USE_NATIVE_CHECKER) { |
| 399 if (SpellCheckWord(text + word_location, word_length, 0, |
| 400 &misspelling_start, &misspelling_length, NULL)) { |
| 401 decoration = SpellCheckResult::GRAMMAR; |
| 402 } |
418 } | 403 } |
419 | 404 if (!custom_dictionary_.SpellCheckWord( |
420 // Use the same types of appostrophes as in the mispelled word. | 405 line_text, word_location, word_length)) { |
421 base::string16 replacement = spellcheck_result.replacement; | 406 list.push_back(WebTextCheckingResult( |
422 PreserveOriginalApostropheTypes(misspelled_word, &replacement); | 407 static_cast<WebTextDecorationType>(decoration), |
423 | 408 word_location + line_offset, |
424 // Ignore misspellings due the typographical apostrophe. | 409 word_length, |
425 if (misspelled_word == replacement) | 410 spellcheck_results[i].replacement, |
426 continue; | 411 spellcheck_results[i].hash)); |
427 | |
428 // Double-check misspelled words with out spellchecker and attach grammar | |
429 // markers to them if our spellchecker tells us they are correct words, | |
430 // i.e. they are probably contextually-misspelled words. | |
431 SpellCheckResult::Decoration decoration = spellcheck_result.decoration; | |
432 int unused_misspelling_start = 0; | |
433 int unused_misspelling_length = 0; | |
434 if (decoration == SpellCheckResult::SPELLING && | |
435 filter == USE_NATIVE_CHECKER && | |
436 SpellCheckWord(misspelled_word.c_str(), misspelled_word.length(), 0, | |
437 &unused_misspelling_start, &unused_misspelling_length, | |
438 nullptr)) { | |
439 decoration = SpellCheckResult::GRAMMAR; | |
440 } | 412 } |
441 | |
442 results.push_back(WebTextCheckingResult( | |
443 static_cast<WebTextDecorationType>(decoration), | |
444 line_offset + spellcheck_result.location, spellcheck_result.length, | |
445 replacement, spellcheck_result.hash)); | |
446 } | 413 } |
447 | 414 textcheck_results->assign(list); |
448 textcheck_results->assign(results); | |
449 } | 415 } |
OLD | NEW |