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> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
9 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/common/render_messages.h" | 12 #include "chrome/common/render_messages.h" |
11 #include "chrome/common/spellcheck_common.h" | 13 #include "chrome/common/spellcheck_common.h" |
12 #include "chrome/common/spellcheck_messages.h" | 14 #include "chrome/common/spellcheck_messages.h" |
13 #include "chrome/common/spellcheck_result.h" | 15 #include "chrome/common/spellcheck_result.h" |
14 #include "chrome/renderer/spellchecker/spellcheck_language.h" | 16 #include "chrome/renderer/spellchecker/spellcheck_language.h" |
15 #include "chrome/renderer/spellchecker/spellcheck_provider.h" | 17 #include "chrome/renderer/spellchecker/spellcheck_provider.h" |
16 #include "content/public/renderer/render_thread.h" | 18 #include "content/public/renderer/render_thread.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 for (size_t i = 0; i < words.size(); ++i) | 87 for (size_t i = 0; i < words.size(); ++i) |
86 words_[i] = WebString::fromUTF8(words[i]); | 88 words_[i] = WebString::fromUTF8(words[i]); |
87 } | 89 } |
88 | 90 |
89 bool DocumentMarkersRemover::Visit(content::RenderView* render_view) { | 91 bool DocumentMarkersRemover::Visit(content::RenderView* render_view) { |
90 if (render_view && render_view->GetWebView()) | 92 if (render_view && render_view->GetWebView()) |
91 render_view->GetWebView()->removeSpellingMarkersUnderWords(words_); | 93 render_view->GetWebView()->removeSpellingMarkersUnderWords(words_); |
92 return true; | 94 return true; |
93 } | 95 } |
94 | 96 |
97 // Makes sure that the apostrophes in |to| are the same type as in the range | |
98 // |from_begin| to |from_end|, in the same order. | |
99 void PreserveOriginalApostropheTypes( | |
100 const base::string16::const_iterator& from_begin, | |
101 const base::string16::const_iterator& from_end, | |
102 base::string16* to) { | |
103 const base::string16 kApostrophes(base::WideToUTF16(L"\x2019'")); | |
groby-ooo-7-16
2015/04/25 00:19:02
Please declare as constant
please use gerrit instead
2015/06/17 03:52:07
No longer applicable after using literals in IsApo
| |
104 base::string16::const_iterator from_it = std::find_first_of( | |
105 from_begin, from_end, kApostrophes.begin(), kApostrophes.end()); | |
106 for (base::string16::iterator to_it = std::find_first_of( | |
groby-ooo-7-16
2015/04/25 00:19:02
You'll need to handle the case of differing apostr
please use gerrit instead
2015/06/17 03:52:07
I think that's an edge case that requires some mor
| |
107 to->begin(), to->end(), kApostrophes.begin(), kApostrophes.end()); | |
108 from_it != from_end && to_it != to->end(); | |
groby-ooo-7-16
2015/04/25 00:19:02
Also, this is the for-loop from hell.
For readabi
please use gerrit instead
2015/06/17 03:52:07
Done.
| |
109 from_it = std::find_first_of( | |
110 from_it + 1, from_end, kApostrophes.begin(), kApostrophes.end()), | |
111 to_it = std::find_first_of( | |
112 to_it + 1, to->end(), kApostrophes.begin(), kApostrophes.end())) { | |
113 *to_it = *from_it; | |
114 } | |
115 } | |
116 | |
95 } // namespace | 117 } // namespace |
96 | 118 |
97 class SpellCheck::SpellcheckRequest { | 119 class SpellCheck::SpellcheckRequest { |
98 public: | 120 public: |
99 SpellcheckRequest(const base::string16& text, | 121 SpellcheckRequest(const base::string16& text, |
100 blink::WebTextCheckingCompletion* completion) | 122 blink::WebTextCheckingCompletion* completion) |
101 : text_(text), completion_(completion) { | 123 : text_(text), completion_(completion) { |
102 DCHECK(completion); | 124 DCHECK(completion); |
103 } | 125 } |
104 ~SpellcheckRequest() {} | 126 ~SpellcheckRequest() {} |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
376 | 398 |
377 void SpellCheck::CreateTextCheckingResults( | 399 void SpellCheck::CreateTextCheckingResults( |
378 ResultFilter filter, | 400 ResultFilter filter, |
379 int line_offset, | 401 int line_offset, |
380 const base::string16& line_text, | 402 const base::string16& line_text, |
381 const std::vector<SpellCheckResult>& spellcheck_results, | 403 const std::vector<SpellCheckResult>& spellcheck_results, |
382 WebVector<WebTextCheckingResult>* textcheck_results) { | 404 WebVector<WebTextCheckingResult>* textcheck_results) { |
383 // Double-check misspelled words with our spellchecker and attach grammar | 405 // Double-check misspelled words with our spellchecker and attach grammar |
384 // markers to them if our spellchecker tells they are correct words, i.e. they | 406 // markers to them if our spellchecker tells they are correct words, i.e. they |
385 // are probably contextually-misspelled words. | 407 // are probably contextually-misspelled words. |
386 const base::char16* text = line_text.c_str(); | 408 std::vector<WebTextCheckingResult> result; |
387 std::vector<WebTextCheckingResult> list; | |
388 for (size_t i = 0; i < spellcheck_results.size(); ++i) { | 409 for (size_t i = 0; i < spellcheck_results.size(); ++i) { |
groby-ooo-7-16
2015/04/25 00:19:02
While you're here:
for (const SpellCheckResult& sp
please use gerrit instead
2015/06/17 03:52:07
Done.
| |
410 if (custom_dictionary_.SpellCheckWord(line_text, | |
groby-ooo-7-16
2015/04/25 00:19:02
Makes sense to move this up - thanks! (If this CL
please use gerrit instead
2015/06/17 03:52:07
Ack
| |
411 spellcheck_results[i].location, | |
412 spellcheck_results[i].length)) { | |
413 continue; | |
414 } | |
415 | |
416 base::string16 replacement = spellcheck_results[i].replacement; | |
417 PreserveOriginalApostropheTypes( | |
418 line_text.begin() + spellcheck_results[i].location, | |
groby-ooo-7-16
2015/04/25 00:19:02
Given that we refer to the substring several times
please use gerrit instead
2015/06/17 03:52:07
Done.
| |
419 line_text.begin() + spellcheck_results[i].location + | |
420 spellcheck_results[i].length, | |
421 &replacement); | |
422 if (line_text.compare(spellcheck_results[i].location, | |
423 spellcheck_results[i].length, replacement) == 0) { | |
424 continue; | |
425 } | |
426 | |
389 SpellCheckResult::Decoration decoration = spellcheck_results[i].decoration; | 427 SpellCheckResult::Decoration decoration = spellcheck_results[i].decoration; |
390 int word_location = spellcheck_results[i].location; | |
391 int word_length = spellcheck_results[i].length; | |
392 int misspelling_start = 0; | 428 int misspelling_start = 0; |
393 int misspelling_length = 0; | 429 int misspelling_length = 0; |
394 if (decoration == SpellCheckResult::SPELLING && | 430 if (decoration == SpellCheckResult::SPELLING && |
395 filter == USE_NATIVE_CHECKER) { | 431 filter == USE_NATIVE_CHECKER && |
396 if (SpellCheckWord(text + word_location, word_length, 0, | 432 SpellCheckWord(line_text.c_str() + spellcheck_results[i].location, |
397 &misspelling_start, &misspelling_length, NULL)) { | 433 spellcheck_results[i].length, 0, &misspelling_start, |
398 decoration = SpellCheckResult::GRAMMAR; | 434 &misspelling_length, NULL)) { |
399 } | 435 decoration = SpellCheckResult::GRAMMAR; |
400 } | 436 } |
401 if (!custom_dictionary_.SpellCheckWord( | 437 |
402 line_text, word_location, word_length)) { | 438 result.push_back(WebTextCheckingResult( |
403 list.push_back(WebTextCheckingResult( | 439 static_cast<WebTextDecorationType>(decoration), |
404 static_cast<WebTextDecorationType>(decoration), | 440 line_offset + spellcheck_results[i].location, |
405 word_location + line_offset, | 441 spellcheck_results[i].length, replacement, spellcheck_results[i].hash)); |
406 word_length, | |
407 spellcheck_results[i].replacement, | |
408 spellcheck_results[i].hash)); | |
409 } | |
410 } | 442 } |
411 textcheck_results->assign(list); | 443 |
444 textcheck_results->assign(result); | |
412 } | 445 } |
OLD | NEW |