OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/editing/suggestion/TextSuggestionController.h" |
| 6 |
| 7 #include "core/editing/EditingUtilities.h" |
| 8 #include "core/editing/Editor.h" |
| 9 #include "core/editing/FrameSelection.h" |
| 10 #include "core/editing/PlainTextRange.h" |
| 11 #include "core/editing/Position.h" |
| 12 #include "core/editing/markers/DocumentMarkerController.h" |
| 13 #include "core/editing/markers/SpellCheckMarker.h" |
| 14 #include "core/editing/spellcheck/SpellChecker.h" |
| 15 #include "core/frame/FrameView.h" |
| 16 #include "core/frame/LocalFrame.h" |
| 17 #include "core/layout/LayoutTheme.h" |
| 18 #include "services/service_manager/public/cpp/interface_provider.h" |
| 19 |
| 20 namespace blink { |
| 21 |
| 22 namespace { |
| 23 |
| 24 bool ShouldDeleteNextCharacter(const Node& marker_text_node, |
| 25 const DocumentMarker& marker) { |
| 26 // If the character immediately following the range to be deleted is a space, |
| 27 // delete it if either of these conditions holds: |
| 28 // - We're deleting at the beginning of the editable text (to avoid ending up |
| 29 // with a space at the beginning) |
| 30 // - The character immediately before the range being deleted is also a space |
| 31 // (to avoid ending up with two adjacent spaces) |
| 32 const EphemeralRange next_character_range = |
| 33 PlainTextRange(marker.EndOffset(), marker.EndOffset() + 1) |
| 34 .CreateRange(*marker_text_node.parentNode()); |
| 35 // No character immediately following the range (so it can't be a space) |
| 36 if (next_character_range.IsNull()) |
| 37 return false; |
| 38 |
| 39 const String next_character_str = |
| 40 PlainText(next_character_range, TextIteratorBehavior::Builder().Build()); |
| 41 const UChar next_character = next_character_str[0]; |
| 42 // Character immediately following the range is not a space |
| 43 if (next_character != kSpaceCharacter && |
| 44 next_character != kNoBreakSpaceCharacter) |
| 45 return false; |
| 46 |
| 47 // First case: we're deleting at the beginning of the editable text |
| 48 if (marker.StartOffset() == 0) |
| 49 return true; |
| 50 |
| 51 const EphemeralRange prev_character_range = |
| 52 PlainTextRange(marker.StartOffset() - 1, marker.StartOffset()) |
| 53 .CreateRange(*marker_text_node.parentNode()); |
| 54 // Not at beginning, but there's no character immediately before the range |
| 55 // being deleted (so it can't be a space) |
| 56 if (prev_character_range.IsNull()) |
| 57 return false; |
| 58 |
| 59 const String prev_character_str = |
| 60 PlainText(prev_character_range, TextIteratorBehavior::Builder().Build()); |
| 61 // Return true if the character immediately before the range is a space, false |
| 62 // otherwise |
| 63 const UChar prev_character = prev_character_str[0]; |
| 64 return prev_character == kSpaceCharacter || |
| 65 prev_character == kNoBreakSpaceCharacter; |
| 66 } |
| 67 |
| 68 EphemeralRangeInFlatTree ComputeRangeSurroundingCaret( |
| 69 const PositionInFlatTree& caret_position) { |
| 70 const Node* const position_node = caret_position.ComputeContainerNode(); |
| 71 const bool is_text_node = position_node->IsTextNode(); |
| 72 const int position_offset_in_node = |
| 73 caret_position.ComputeOffsetInContainerNode(); |
| 74 |
| 75 // If we're in the interior of a text node, we can avoid calling |
| 76 // PreviousPositionOf/NextPositionOf for better efficiency. |
| 77 if (is_text_node && position_offset_in_node != 0 && |
| 78 position_offset_in_node != position_node->MaxCharacterOffset()) { |
| 79 return EphemeralRangeInFlatTree( |
| 80 PositionInFlatTree(position_node, position_offset_in_node - 1), |
| 81 PositionInFlatTree(position_node, position_offset_in_node + 1)); |
| 82 } |
| 83 |
| 84 const PositionInFlatTree& previous_position = |
| 85 PreviousPositionOf(caret_position, PositionMoveType::kGraphemeCluster); |
| 86 |
| 87 const PositionInFlatTree& next_position = |
| 88 NextPositionOf(caret_position, PositionMoveType::kGraphemeCluster); |
| 89 |
| 90 return EphemeralRangeInFlatTree( |
| 91 previous_position.IsNull() ? caret_position : previous_position, |
| 92 next_position.IsNull() ? caret_position : next_position); |
| 93 } |
| 94 |
| 95 } // namespace |
| 96 |
| 97 TextSuggestionController::TextSuggestionController(LocalFrame& frame) |
| 98 : is_suggestion_menu_open_(false), frame_(&frame) {} |
| 99 |
| 100 void TextSuggestionController::DocumentAttached(Document* document) { |
| 101 DCHECK(document); |
| 102 SetContext(document); |
| 103 } |
| 104 |
| 105 bool TextSuggestionController::IsMenuOpen() const { |
| 106 return is_suggestion_menu_open_; |
| 107 } |
| 108 |
| 109 void TextSuggestionController::HandlePotentialMisspelledWordTap( |
| 110 const PositionInFlatTree& caret_position) { |
| 111 const EphemeralRangeInFlatTree& range_to_check = |
| 112 ComputeRangeSurroundingCaret(caret_position); |
| 113 |
| 114 const std::pair<const Node*, const DocumentMarker*>& node_and_marker = |
| 115 FirstMarkerIntersectingRange(range_to_check, |
| 116 DocumentMarker::MisspellingMarkers()); |
| 117 if (!node_and_marker.first) |
| 118 return; |
| 119 |
| 120 if (!text_suggestion_host_) { |
| 121 GetFrame().GetInterfaceProvider().GetInterface( |
| 122 mojo::MakeRequest(&text_suggestion_host_)); |
| 123 } |
| 124 |
| 125 text_suggestion_host_->StartSpellCheckMenuTimer(); |
| 126 } |
| 127 |
| 128 DEFINE_TRACE(TextSuggestionController) { |
| 129 visitor->Trace(frame_); |
| 130 DocumentShutdownObserver::Trace(visitor); |
| 131 } |
| 132 |
| 133 void TextSuggestionController::ApplySpellCheckSuggestion( |
| 134 const String& suggestion) { |
| 135 ReplaceSpellingMarkerTouchingSelectionWithText(suggestion); |
| 136 SuggestionMenuClosed(); |
| 137 } |
| 138 |
| 139 void TextSuggestionController::DeleteActiveSuggestionRange() { |
| 140 AttemptToDeleteActiveSuggestionRange(); |
| 141 SuggestionMenuClosed(); |
| 142 } |
| 143 |
| 144 void TextSuggestionController::NewWordAddedToDictionary(const String& word) { |
| 145 // Android pops up a dialog to let the user confirm they actually want to add |
| 146 // the word to the dictionary; this method gets called as soon as the dialog |
| 147 // is shown. So the word isn't actually in the dictionary here, even if the |
| 148 // user will end up confirming the dialog, and we shouldn't try to re-run |
| 149 // spellcheck here. |
| 150 |
| 151 // Note: this actually matches the behavior in native Android text boxes |
| 152 GetDocument().Markers().RemoveSpellingMarkersUnderWords( |
| 153 Vector<String>({word})); |
| 154 SuggestionMenuClosed(); |
| 155 } |
| 156 |
| 157 void TextSuggestionController::SpellCheckMenuTimeoutCallback() { |
| 158 const std::pair<const Node*, const DocumentMarker*>& node_and_marker = |
| 159 FirstMarkerTouchingSelection(DocumentMarker::MisspellingMarkers()); |
| 160 if (!node_and_marker.first) |
| 161 return; |
| 162 |
| 163 const Node* const marker_text_node = node_and_marker.first; |
| 164 const SpellCheckMarker* const marker = |
| 165 ToSpellCheckMarker(node_and_marker.second); |
| 166 |
| 167 const EphemeralRange marker_range = |
| 168 EphemeralRange(Position(marker_text_node, marker->StartOffset()), |
| 169 Position(marker_text_node, marker->EndOffset())); |
| 170 const String& misspelled_word = PlainText(marker_range); |
| 171 const String& description = marker->Description(); |
| 172 |
| 173 is_suggestion_menu_open_ = true; |
| 174 GetFrame().Selection().SetCaretVisible(false); |
| 175 GetDocument().Markers().AddActiveSuggestionMarker( |
| 176 marker_range, SK_ColorTRANSPARENT, StyleableMarker::Thickness::kThin, |
| 177 LayoutTheme::GetTheme().PlatformActiveSpellingMarkerHighlightColor()); |
| 178 |
| 179 Vector<String> suggestions; |
| 180 description.Split('\n', suggestions); |
| 181 |
| 182 Vector<mojom::blink::SpellCheckSuggestionPtr> suggestion_ptrs; |
| 183 for (const String& suggestion : suggestions) { |
| 184 mojom::blink::SpellCheckSuggestionPtr info_ptr( |
| 185 mojom::blink::SpellCheckSuggestion::New()); |
| 186 info_ptr->suggestion = suggestion; |
| 187 suggestion_ptrs.push_back(std::move(info_ptr)); |
| 188 } |
| 189 |
| 190 const IntRect& absolute_bounds = GetFrame().Selection().AbsoluteCaretBounds(); |
| 191 const IntRect& viewport_bounds = |
| 192 GetFrame().View()->ContentsToViewport(absolute_bounds); |
| 193 |
| 194 text_suggestion_host_->ShowSpellCheckSuggestionMenu( |
| 195 viewport_bounds.X(), viewport_bounds.MaxY(), std::move(misspelled_word), |
| 196 std::move(suggestion_ptrs)); |
| 197 } |
| 198 |
| 199 void TextSuggestionController::SuggestionMenuClosed() { |
| 200 if (!IsAvailable()) |
| 201 return; |
| 202 |
| 203 GetDocument().Markers().RemoveMarkersOfTypes( |
| 204 DocumentMarker::kActiveSuggestion); |
| 205 GetFrame().Selection().SetCaretVisible(true); |
| 206 is_suggestion_menu_open_ = false; |
| 207 } |
| 208 |
| 209 Document& TextSuggestionController::GetDocument() const { |
| 210 DCHECK(IsAvailable()); |
| 211 return *LifecycleContext(); |
| 212 } |
| 213 |
| 214 bool TextSuggestionController::IsAvailable() const { |
| 215 return LifecycleContext(); |
| 216 } |
| 217 |
| 218 LocalFrame& TextSuggestionController::GetFrame() const { |
| 219 DCHECK(frame_); |
| 220 return *frame_; |
| 221 } |
| 222 |
| 223 std::pair<const Node*, const DocumentMarker*> |
| 224 TextSuggestionController::FirstMarkerIntersectingRange( |
| 225 const EphemeralRangeInFlatTree& range, |
| 226 DocumentMarker::MarkerTypes types) const { |
| 227 const Node* const range_start_container = |
| 228 range.StartPosition().ComputeContainerNode(); |
| 229 const int range_start_offset = |
| 230 range.StartPosition().ComputeOffsetInContainerNode(); |
| 231 const Node* const range_end_container = |
| 232 range.EndPosition().ComputeContainerNode(); |
| 233 const int range_end_offset = |
| 234 range.EndPosition().ComputeOffsetInContainerNode(); |
| 235 |
| 236 for (const Node& node : range.Nodes()) { |
| 237 if (!node.IsTextNode()) |
| 238 continue; |
| 239 |
| 240 const int start_offset = |
| 241 node == range_start_container ? range_start_offset : 0; |
| 242 const int end_offset = node == range_end_container |
| 243 ? range_end_offset |
| 244 : node.MaxCharacterOffset(); |
| 245 |
| 246 const DocumentMarker* const found_marker = |
| 247 GetFrame().GetDocument()->Markers().FirstMarkerIntersectingOffsetRange( |
| 248 ToText(node), start_offset, end_offset, types); |
| 249 if (found_marker) |
| 250 return std::make_pair(&node, found_marker); |
| 251 } |
| 252 |
| 253 return {}; |
| 254 } |
| 255 |
| 256 std::pair<const Node*, const DocumentMarker*> |
| 257 TextSuggestionController::FirstMarkerTouchingSelection( |
| 258 DocumentMarker::MarkerTypes types) const { |
| 259 const VisibleSelectionInFlatTree& selection = |
| 260 GetFrame().Selection().ComputeVisibleSelectionInFlatTree(); |
| 261 if (selection.IsNone()) |
| 262 return {}; |
| 263 |
| 264 const EphemeralRangeInFlatTree& range_to_check = |
| 265 selection.IsRange() |
| 266 ? EphemeralRangeInFlatTree(selection.Start(), selection.End()) |
| 267 : ComputeRangeSurroundingCaret(selection.Start()); |
| 268 |
| 269 return FirstMarkerIntersectingRange(range_to_check, types); |
| 270 } |
| 271 |
| 272 void TextSuggestionController::AttemptToDeleteActiveSuggestionRange() { |
| 273 const std::pair<const Node*, const DocumentMarker*>& node_and_marker = |
| 274 FirstMarkerTouchingSelection(DocumentMarker::kActiveSuggestion); |
| 275 if (!node_and_marker.first) |
| 276 return; |
| 277 |
| 278 const Node* const marker_text_node = node_and_marker.first; |
| 279 const DocumentMarker* const marker = node_and_marker.second; |
| 280 |
| 281 const bool delete_next_char = |
| 282 ShouldDeleteNextCharacter(*marker_text_node, *marker); |
| 283 |
| 284 const EphemeralRange range_to_delete = EphemeralRange( |
| 285 Position(marker_text_node, marker->StartOffset()), |
| 286 Position(marker_text_node, marker->EndOffset() + delete_next_char)); |
| 287 ReplaceRangeWithText(range_to_delete, ""); |
| 288 } |
| 289 |
| 290 void TextSuggestionController::ReplaceSpellingMarkerTouchingSelectionWithText( |
| 291 const String& suggestion) { |
| 292 const std::pair<const Node*, const DocumentMarker*>& node_and_marker = |
| 293 FirstMarkerTouchingSelection(DocumentMarker::MisspellingMarkers()); |
| 294 if (!node_and_marker.first) |
| 295 return; |
| 296 |
| 297 const Node* const marker_text_node = node_and_marker.first; |
| 298 const DocumentMarker* const marker = node_and_marker.second; |
| 299 |
| 300 const EphemeralRange range_to_replace( |
| 301 Position(marker_text_node, marker->StartOffset()), |
| 302 Position(marker_text_node, marker->EndOffset())); |
| 303 ReplaceRangeWithText(range_to_replace, suggestion); |
| 304 } |
| 305 |
| 306 void TextSuggestionController::ReplaceRangeWithText(const EphemeralRange& range, |
| 307 const String& replacement) { |
| 308 GetFrame().Selection().SetSelection( |
| 309 SelectionInDOMTree::Builder().SetBaseAndExtent(range).Build()); |
| 310 |
| 311 // Dispatch 'beforeinput'. |
| 312 Element* const target = GetFrame().GetEditor().FindEventTargetFromSelection(); |
| 313 DataTransfer* const data_transfer = DataTransfer::Create( |
| 314 DataTransfer::DataTransferType::kInsertReplacementText, |
| 315 DataTransferAccessPolicy::kDataTransferReadable, |
| 316 DataObject::CreateFromString(replacement)); |
| 317 |
| 318 const bool is_canceled = |
| 319 DispatchBeforeInputDataTransfer( |
| 320 target, InputEvent::InputType::kInsertReplacementText, |
| 321 data_transfer) != DispatchEventResult::kNotCanceled; |
| 322 |
| 323 // 'beforeinput' event handler may destroy target frame. |
| 324 if (!IsAvailable()) |
| 325 return; |
| 326 |
| 327 // TODO(editing-dev): The use of updateStyleAndLayoutIgnorePendingStylesheets |
| 328 // needs to be audited. See http://crbug.com/590369 for more details. |
| 329 GetFrame().GetDocument()->UpdateStyleAndLayoutIgnorePendingStylesheets(); |
| 330 |
| 331 if (is_canceled) |
| 332 return; |
| 333 GetFrame().GetEditor().ReplaceSelectionWithText( |
| 334 replacement, false, false, InputEvent::InputType::kInsertReplacementText); |
| 335 } |
| 336 |
| 337 } // namespace blink |
OLD | NEW |