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

Unified Diff: third_party/WebKit/Source/core/editing/spellcheck/SpellChecker.cpp

Issue 2966223003: [SelectMisspellingAsync #1] Move SelectMisspellingAsync() from ContextMenuClient.cpp to SpellChecker (Closed)
Patch Set: Rebase Created 3 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/editing/spellcheck/SpellChecker.cpp
diff --git a/third_party/WebKit/Source/core/editing/spellcheck/SpellChecker.cpp b/third_party/WebKit/Source/core/editing/spellcheck/SpellChecker.cpp
index e9b038595ed2bcc949c7bbbeb76524d5ab1f6734..c279e5a3433bf1eb4657d5ec6037c7502d5aede7 100644
--- a/third_party/WebKit/Source/core/editing/spellcheck/SpellChecker.cpp
+++ b/third_party/WebKit/Source/core/editing/spellcheck/SpellChecker.cpp
@@ -95,6 +95,10 @@ SelectionInDOMTree SelectWord(const VisiblePosition& position) {
.Build();
}
+static bool IsWhiteSpaceOrPunctuation(UChar c) {
+ return IsSpaceOrNewline(c) || WTF::Unicode::IsPunct(c);
+}
+
} // namespace
SpellChecker* SpellChecker::Create(LocalFrame& frame) {
@@ -867,6 +871,61 @@ SpellChecker::GetSpellCheckMarkerUnderSelection() {
return std::make_pair(selection_start_container, ToSpellCheckMarker(marker));
}
+String SpellChecker::SelectMisspellingAsync(String& description) {
+ VisibleSelection selection =
+ GetFrame().Selection().ComputeVisibleSelectionInDOMTree();
+ if (selection.IsNone())
+ return String();
+
+ // Caret and range selections always return valid normalized ranges.
+ const EphemeralRange& selection_range =
+ selection.ToNormalizedEphemeralRange();
+
+ Node* const selection_start_container =
+ selection_range.StartPosition().ComputeContainerNode();
+ Node* const selection_end_container =
+ selection_range.EndPosition().ComputeContainerNode();
+
+ // We don't currently support the case where a misspelling spans multiple
+ // nodes. See crbug.com/720065
+ if (selection_start_container != selection_end_container)
+ return String();
+
+ const unsigned selection_start_offset =
+ selection_range.StartPosition().ComputeOffsetInContainerNode();
+ const unsigned selection_end_offset =
+ selection_range.EndPosition().ComputeOffsetInContainerNode();
+
+ const DocumentMarkerVector& markers_in_node =
+ GetFrame().GetDocument()->Markers().MarkersFor(
+ selection_start_container, DocumentMarker::MisspellingMarkers());
+
+ const auto marker_it =
+ std::find_if(markers_in_node.begin(), markers_in_node.end(),
+ [=](const DocumentMarker* marker) {
+ return marker->StartOffset() < selection_end_offset &&
+ marker->EndOffset() > selection_start_offset;
+ });
+ if (marker_it == markers_in_node.end())
+ return String();
+
+ const SpellCheckMarker* const found_marker = ToSpellCheckMarker(*marker_it);
+ description = found_marker->Description();
+
+ Range* const marker_range =
+ Range::Create(*GetFrame().GetDocument(), selection_start_container,
+ found_marker->StartOffset(), selection_start_container,
+ found_marker->EndOffset());
+
+ if (marker_range->GetText().StripWhiteSpace(&IsWhiteSpaceOrPunctuation) !=
+ CreateRange(selection_range)
+ ->GetText()
+ .StripWhiteSpace(&IsWhiteSpaceOrPunctuation))
+ return String();
+
+ return marker_range->GetText();
+}
+
void SpellChecker::ReplaceMisspelledRange(const String& text) {
const Optional<std::pair<Node*, SpellCheckMarker*>>& node_and_marker =
GetSpellCheckMarkerUnderSelection();

Powered by Google App Engine
This is Rietveld 408576698