Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: chrome/renderer/spellchecker/spellcheck.cc

Issue 1092243002: Handle typographical apostrophe with spelling service client (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: base::char16 Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
100 } // namespace 123 } // namespace
101 124
102 class SpellCheck::SpellcheckRequest { 125 class SpellCheck::SpellcheckRequest {
103 public: 126 public:
104 SpellcheckRequest(const base::string16& text, 127 SpellcheckRequest(const base::string16& text,
105 blink::WebTextCheckingCompletion* completion) 128 blink::WebTextCheckingCompletion* completion)
106 : text_(text), completion_(completion) { 129 : text_(text), completion_(completion) {
107 DCHECK(completion); 130 DCHECK(completion);
108 } 131 }
109 ~SpellcheckRequest() {} 132 ~SpellcheckRequest() {}
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 } 399 }
377 } 400 }
378 #endif 401 #endif
379 402
380 void SpellCheck::CreateTextCheckingResults( 403 void SpellCheck::CreateTextCheckingResults(
381 ResultFilter filter, 404 ResultFilter filter,
382 int line_offset, 405 int line_offset,
383 const base::string16& line_text, 406 const base::string16& line_text,
384 const std::vector<SpellCheckResult>& spellcheck_results, 407 const std::vector<SpellCheckResult>& spellcheck_results,
385 WebVector<WebTextCheckingResult>* textcheck_results) { 408 WebVector<WebTextCheckingResult>* textcheck_results) {
386 // Double-check misspelled words with our spellchecker and attach grammar 409 std::vector<WebTextCheckingResult> results;
387 // markers to them if our spellchecker tells they are correct words, i.e. they 410 for (const SpellCheckResult& spellcheck_result : spellcheck_results) {
388 // are probably contextually-misspelled words. 411 const base::string16& misspelled_word =
389 const base::char16* text = line_text.c_str(); 412 line_text.substr(spellcheck_result.location, spellcheck_result.length);
390 std::vector<WebTextCheckingResult> list; 413
391 for (size_t i = 0; i < spellcheck_results.size(); ++i) { 414 // Ignore words in custom dictionary.
392 SpellCheckResult::Decoration decoration = spellcheck_results[i].decoration; 415 if (custom_dictionary_.SpellCheckWord(misspelled_word, 0,
393 int word_location = spellcheck_results[i].location; 416 misspelled_word.length())) {
394 int word_length = spellcheck_results[i].length; 417 continue;
395 int misspelling_start = 0; 418 }
396 int misspelling_length = 0; 419
420 // Use the same types of appostrophes as in the mispelled word.
421 base::string16 replacement = spellcheck_result.replacement;
422 PreserveOriginalApostropheTypes(misspelled_word, &replacement);
423
424 // Ignore misspellings due the typographical apostrophe.
425 if (misspelled_word == replacement)
426 continue;
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;
397 if (decoration == SpellCheckResult::SPELLING && 434 if (decoration == SpellCheckResult::SPELLING &&
398 filter == USE_NATIVE_CHECKER) { 435 filter == USE_NATIVE_CHECKER &&
399 if (SpellCheckWord(text + word_location, word_length, 0, 436 SpellCheckWord(misspelled_word.c_str(), misspelled_word.length(), 0,
400 &misspelling_start, &misspelling_length, NULL)) { 437 &unused_misspelling_start, &unused_misspelling_length,
401 decoration = SpellCheckResult::GRAMMAR; 438 nullptr)) {
402 } 439 decoration = SpellCheckResult::GRAMMAR;
403 } 440 }
404 if (!custom_dictionary_.SpellCheckWord( 441
405 line_text, word_location, word_length)) { 442 results.push_back(WebTextCheckingResult(
406 list.push_back(WebTextCheckingResult( 443 static_cast<WebTextDecorationType>(decoration),
407 static_cast<WebTextDecorationType>(decoration), 444 line_offset + spellcheck_result.location, spellcheck_result.length,
408 word_location + line_offset, 445 replacement, spellcheck_result.hash));
409 word_length,
410 spellcheck_results[i].replacement,
411 spellcheck_results[i].hash));
412 }
413 } 446 }
414 textcheck_results->assign(list); 447
448 textcheck_results->assign(results);
415 } 449 }
OLDNEW
« no previous file with comments | « chrome/browser/spellchecker/spelling_service_client_unittest.cc ('k') | chrome/renderer/spellchecker/spellcheck_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698