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

Side by Side Diff: third_party/WebKit/Source/core/page/ContextMenuClient.cpp

Issue 2966223003: [SelectMisspellingAsync #1] Move SelectMisspellingAsync() from ContextMenuClient.cpp to SpellChecker (Closed)
Patch Set: Rebase Created 3 years, 4 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
« no previous file with comments | « third_party/WebKit/Source/core/editing/spellcheck/SpellChecker.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009, 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2009, 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 WebDataSource* ds = WebDataSourceImpl::FromDocumentLoader(dl); 96 WebDataSource* ds = WebDataSourceImpl::FromDocumentLoader(dl);
97 if (ds) { 97 if (ds) {
98 return ds->HasUnreachableURL() ? ds->UnreachableURL() 98 return ds->HasUnreachableURL() ? ds->UnreachableURL()
99 : ds->GetRequest().Url(); 99 : ds->GetRequest().Url();
100 } 100 }
101 } 101 }
102 } 102 }
103 return WebURL(); 103 return WebURL();
104 } 104 }
105 105
106 static bool IsWhiteSpaceOrPunctuation(UChar c) {
107 return IsSpaceOrNewline(c) || WTF::Unicode::IsPunct(c);
108 }
109
110 static String SelectMisspellingAsync(LocalFrame* selected_frame,
111 String& description) {
112 VisibleSelection selection =
113 selected_frame->Selection().ComputeVisibleSelectionInDOMTree();
114 if (selection.IsNone())
115 return String();
116
117 // Caret and range selections always return valid normalized ranges.
118 const EphemeralRange& selection_range =
119 selection.ToNormalizedEphemeralRange();
120
121 Node* const selection_start_container =
122 selection_range.StartPosition().ComputeContainerNode();
123 Node* const selection_end_container =
124 selection_range.EndPosition().ComputeContainerNode();
125
126 // We don't currently support the case where a misspelling spans multiple
127 // nodes
128 if (selection_start_container != selection_end_container)
129 return String();
130
131 const unsigned selection_start_offset =
132 selection_range.StartPosition().ComputeOffsetInContainerNode();
133 const unsigned selection_end_offset =
134 selection_range.EndPosition().ComputeOffsetInContainerNode();
135
136 const DocumentMarkerVector& markers_in_node =
137 selected_frame->GetDocument()->Markers().MarkersFor(
138 selection_start_container, DocumentMarker::MisspellingMarkers());
139
140 const auto marker_it =
141 std::find_if(markers_in_node.begin(), markers_in_node.end(),
142 [=](const DocumentMarker* marker) {
143 return marker->StartOffset() < selection_end_offset &&
144 marker->EndOffset() > selection_start_offset;
145 });
146 if (marker_it == markers_in_node.end())
147 return String();
148
149 const SpellCheckMarker* const found_marker = ToSpellCheckMarker(*marker_it);
150 description = found_marker->Description();
151
152 Range* const marker_range =
153 Range::Create(*selected_frame->GetDocument(), selection_start_container,
154 found_marker->StartOffset(), selection_start_container,
155 found_marker->EndOffset());
156
157 if (marker_range->GetText().StripWhiteSpace(&IsWhiteSpaceOrPunctuation) !=
158 CreateRange(selection_range)
159 ->GetText()
160 .StripWhiteSpace(&IsWhiteSpaceOrPunctuation))
161 return String();
162
163 return marker_range->GetText();
164 }
165
166 // static 106 // static
167 int ContextMenuClient::ComputeEditFlags(Document& selected_document, 107 int ContextMenuClient::ComputeEditFlags(Document& selected_document,
168 Editor& editor) { 108 Editor& editor) {
169 int edit_flags = WebContextMenuData::kCanDoNone; 109 int edit_flags = WebContextMenuData::kCanDoNone;
170 if (!selected_document.IsHTMLDocument() && 110 if (!selected_document.IsHTMLDocument() &&
171 !selected_document.IsXHTMLDocument()) 111 !selected_document.IsXHTMLDocument())
172 return edit_flags; 112 return edit_flags;
173 113
174 edit_flags |= WebContextMenuData::kCanTranslate; 114 edit_flags |= WebContextMenuData::kCanTranslate;
175 if (editor.CanUndo()) 115 if (editor.CanUndo())
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 } 354 }
415 355
416 if (r.IsContentEditable()) { 356 if (r.IsContentEditable()) {
417 data.is_editable = true; 357 data.is_editable = true;
418 358
419 // Spellchecker adds spelling markers to misspelled words and attaches 359 // Spellchecker adds spelling markers to misspelled words and attaches
420 // suggestions to these markers in the background. Therefore, when a 360 // suggestions to these markers in the background. Therefore, when a
421 // user right-clicks a mouse on a word, Chrome just needs to find a 361 // user right-clicks a mouse on a word, Chrome just needs to find a
422 // spelling marker on the word instead of spellchecking it. 362 // spelling marker on the word instead of spellchecking it.
423 String description; 363 String description;
424 data.misspelled_word = SelectMisspellingAsync(selected_frame, description); 364 data.misspelled_word =
365 selected_frame->GetSpellChecker().SelectMisspellingAsync(description);
425 if (description.length()) { 366 if (description.length()) {
426 Vector<String> suggestions; 367 Vector<String> suggestions;
427 description.Split('\n', suggestions); 368 description.Split('\n', suggestions);
428 data.dictionary_suggestions = suggestions; 369 data.dictionary_suggestions = suggestions;
429 } else if (selected_web_frame->TextCheckClient()) { 370 } else if (selected_web_frame->TextCheckClient()) {
430 int misspelled_offset, misspelled_length; 371 int misspelled_offset, misspelled_length;
431 selected_web_frame->TextCheckClient()->CheckSpelling( 372 selected_web_frame->TextCheckClient()->CheckSpelling(
432 data.misspelled_word, misspelled_offset, misspelled_length, 373 data.misspelled_word, misspelled_offset, misspelled_length,
433 &data.dictionary_suggestions); 374 &data.dictionary_suggestions);
434 } 375 }
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
567 output_items[i] = sub_items[i]; 508 output_items[i] = sub_items[i];
568 sub_menu_items.Swap(output_items); 509 sub_menu_items.Swap(output_items);
569 } 510 }
570 511
571 void ContextMenuClient::PopulateCustomMenuItems(const ContextMenu* default_menu, 512 void ContextMenuClient::PopulateCustomMenuItems(const ContextMenu* default_menu,
572 WebContextMenuData* data) { 513 WebContextMenuData* data) {
573 PopulateSubMenuItems(default_menu->Items(), data->custom_items); 514 PopulateSubMenuItems(default_menu->Items(), data->custom_items);
574 } 515 }
575 516
576 } // namespace blink 517 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/editing/spellcheck/SpellChecker.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698