Chromium Code Reviews| Index: third_party/WebKit/Source/core/editing/TextSuggestionController.cpp | 
| diff --git a/third_party/WebKit/Source/core/editing/TextSuggestionController.cpp b/third_party/WebKit/Source/core/editing/TextSuggestionController.cpp | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..0248162d5fbdeece1297f96c8abcbbe0df49f0bb | 
| --- /dev/null | 
| +++ b/third_party/WebKit/Source/core/editing/TextSuggestionController.cpp | 
| @@ -0,0 +1,184 @@ | 
| +// Copyright 2017 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "core/editing/TextSuggestionController.h" | 
| + | 
| +#include "core/editing/Editor.h" | 
| +#include "core/editing/FrameSelection.h" | 
| +#include "core/editing/PlainTextRange.h" | 
| +#include "core/editing/markers/DocumentMarkerController.h" | 
| +#include "core/editing/markers/SpellCheckMarker.h" | 
| +#include "core/editing/spellcheck/SpellChecker.h" | 
| +#include "core/frame/FrameView.h" | 
| +#include "core/frame/LocalFrame.h" | 
| +#include "public/platform/InterfaceProvider.h" | 
| + | 
| +namespace blink { | 
| + | 
| +namespace { | 
| + | 
| +const double kSuggestionUnderlineAlphaMultiplier = 0.4; | 
| + | 
| +} // anonymous namespace | 
| + | 
| +TextSuggestionController::TextSuggestionController(LocalFrame& frame) | 
| + : suggestion_menu_is_open_(false), frame_(&frame) {} | 
| + | 
| +bool TextSuggestionController::IsMenuOpen() const { | 
| + return suggestion_menu_is_open_; | 
| +} | 
| + | 
| +void TextSuggestionController::HandlePotentialMisspelledWordTap() { | 
| + Optional<std::pair<Node*, SpellCheckMarker*>> node_and_marker = | 
| + GetFrame().GetSpellChecker().GetSpellCheckMarkerTouchingSelection(); | 
| + if (!node_and_marker) | 
| + return; | 
| + | 
| + if (!text_suggestion_host_) { | 
| + GetFrame().GetInterfaceProvider()->GetInterface( | 
| + mojo::MakeRequest(&text_suggestion_host_)); | 
| + } | 
| + | 
| + text_suggestion_host_->StartSpellCheckMenuTimer(); | 
| +} | 
| + | 
| +DEFINE_TRACE(TextSuggestionController) { | 
| + visitor->Trace(frame_); | 
| +} | 
| + | 
| +void TextSuggestionController::ApplySpellCheckSuggestion( | 
| + const String& suggestion) { | 
| + GetFrame().GetSpellChecker().ReplaceMisspelledRange(suggestion); | 
| + SuggestionMenuClosed(); | 
| +} | 
| + | 
| +void TextSuggestionController::DeleteActiveSuggestionRange() { | 
| + Optional<std::pair<Node*, SpellCheckMarker*>> node_and_marker = | 
| + GetFrame().GetSpellChecker().GetSpellCheckMarkerTouchingSelection(); | 
| + if (!node_and_marker) | 
| + return; | 
| + | 
| + Node* marker_text_node = node_and_marker.value().first; | 
| + DocumentMarker* marker = node_and_marker.value().second; | 
| + | 
| + // If the character immediately following the range to be deleted is a space, | 
| + // delete it if either of these conditions holds: | 
| + // - We're deleting at the beginning of the editable text (to avoid ending up | 
| + // with a space at the beginning) | 
| + // - The character immediately before the range being deleted is also a space | 
| + // (to avoid ending up with two adjacent spaces) | 
| + bool delete_next_char = false; | 
| 
 
yosin_UTC9
2017/06/07 01:34:15
Could you move calculation of |delete_next_char| i
 
 | 
| + const EphemeralRange next_character_range = | 
| + PlainTextRange(marker->EndOffset(), marker->EndOffset() + 1) | 
| + .CreateRange(*marker_text_node->parentNode()); | 
| + if (next_character_range.IsNotNull()) { | 
| + String next_character_str = PlainText( | 
| + next_character_range, | 
| + TextIteratorBehavior::Builder().SetEmitsSpaceForNbsp(true).Build()); | 
| + | 
| + if (WTF::IsASCIISpace(next_character_str[0])) { | 
| + if (marker->StartOffset() == 0) { | 
| + delete_next_char = true; | 
| + } else { | 
| + const EphemeralRange prev_character_range = | 
| + PlainTextRange(marker->StartOffset() - 1, marker->StartOffset()) | 
| + .CreateRange(*marker_text_node->parentNode()); | 
| + if (prev_character_range.IsNotNull()) { | 
| + String prev_character_str = | 
| + PlainText(prev_character_range, TextIteratorBehavior::Builder() | 
| + .SetEmitsSpaceForNbsp(true) | 
| + .Build()); | 
| + if (WTF::IsASCIISpace(prev_character_str[0])) | 
| + delete_next_char = true; | 
| + } | 
| + } | 
| + } | 
| + } | 
| + | 
| + EphemeralRange range_to_delete = EphemeralRange( | 
| 
 
yosin_UTC9
2017/06/07 01:34:15
nit: s/EphemeralRange/const EphemeralRange/
 
 | 
| + Position(marker_text_node, marker->StartOffset()), | 
| + Position(marker_text_node, marker->EndOffset() + delete_next_char)); | 
| 
 
yosin_UTC9
2017/06/07 01:34:15
At first glance "int + bool" is strange, but study
 
 | 
| + | 
| + GetFrame().Selection().SetSelection( | 
| + SelectionInDOMTree::Builder().SetBaseAndExtent(range_to_delete).Build()); | 
| + | 
| + GetFrame().GetEditor().ReplaceSelectionWithText( | 
| + "", false, false, InputEvent::InputType::kDeleteByComposition); | 
| + | 
| + SuggestionMenuClosed(); | 
| +} | 
| + | 
| +void TextSuggestionController::NewWordAddedToDictionary(const String& word) { | 
| + // Android pops up a dialog to let the user confirm they actually want to add | 
| + // the word to the dictionary; this method gets called as soon as the dialog | 
| + // is shown. So the word isn't actually in the dictionary here, even if the | 
| + // user will end up confirming the dialog, and we shouldn't try to re-run | 
| + // spellcheck here. | 
| + | 
| + // Note: this actually matches the behavior in native Android text boxes | 
| + GetDocument().Markers().RemoveSpellingMarkersUnderWords( | 
| + Vector<String>({word})); | 
| + SuggestionMenuClosed(); | 
| +} | 
| + | 
| +void TextSuggestionController::SpellCheckMenuTimeoutCallback() { | 
| + Optional<std::pair<Node*, SpellCheckMarker*>> const node_and_marker = | 
| + GetFrame().GetSpellChecker().GetSpellCheckMarkerTouchingSelection(); | 
| + if (!node_and_marker) | 
| + return; | 
| + | 
| + Node* const container_node = node_and_marker.value().first; | 
| + SpellCheckMarker* const marker = node_and_marker.value().second; | 
| + | 
| + Range* marker_range = | 
| + Range::Create(GetDocument(), container_node, marker->StartOffset(), | 
| + container_node, marker->EndOffset()); | 
| + | 
| + const String& misspelled_word = marker_range->GetText(); | 
| + const String& description = marker->Description(); | 
| + | 
| + suggestion_menu_is_open_ = true; | 
| + GetFrame().Selection().SetCaretVisible(false); | 
| + GetDocument().Markers().AddActiveSuggestionMarker( | 
| + EphemeralRange(marker_range), SK_ColorTRANSPARENT, | 
| + StyleableMarker::Thickness::kThin, | 
| + Color(SK_ColorRED).CombineWithAlpha(kSuggestionUnderlineAlphaMultiplier)); | 
| + | 
| + Vector<String> suggestions; | 
| + description.Split('\n', suggestions); | 
| + | 
| + Vector<mojom::blink::SpellCheckSuggestionPtr> suggestion_ptrs; | 
| + for (const String& suggestion : suggestions) { | 
| + mojom::blink::SpellCheckSuggestionPtr info_ptr( | 
| + mojom::blink::SpellCheckSuggestion::New()); | 
| + info_ptr->suggestion = suggestion; | 
| + suggestion_ptrs.push_back(std::move(info_ptr)); | 
| + } | 
| + | 
| + const IntRect& absolute_bounds = GetFrame().Selection().AbsoluteCaretBounds(); | 
| + const IntRect& viewport_bounds = | 
| + GetFrame().View()->ContentsToViewport(absolute_bounds); | 
| + | 
| + text_suggestion_host_->ShowSpellCheckSuggestionMenu( | 
| + viewport_bounds.X(), viewport_bounds.MaxY(), std::move(misspelled_word), | 
| + std::move(suggestion_ptrs)); | 
| +} | 
| + | 
| +void TextSuggestionController::SuggestionMenuClosed() { | 
| + GetDocument().Markers().RemoveMarkersOfTypes( | 
| + DocumentMarker::kActiveSuggestion); | 
| + GetFrame().Selection().SetCaretVisible(true); | 
| + suggestion_menu_is_open_ = false; | 
| +} | 
| + | 
| +Document& TextSuggestionController::GetDocument() const { | 
| + DCHECK(IsAvailable()); | 
| + return *GetFrame().GetDocument(); | 
| +} | 
| + | 
| +bool TextSuggestionController::IsAvailable() const { | 
| + return GetFrame().GetDocument(); | 
| +} | 
| + | 
| +} // namespace blink |