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

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

Issue 2959553002: [MarkersIntersectingRange #4] Refactor ContextMenuClient::SelectMisspellingAsync() (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 unified diff | Download patch
« no previous file with comments | « no previous file | 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) 2006, 2007, 2008, 2011 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) 3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 854 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 GetFrame().GetDocument()->Markers().FirstMarkerIntersectingOffsetRange( 865 GetFrame().GetDocument()->Markers().FirstMarkerIntersectingOffsetRange(
866 ToText(*selection_start_container), selection_start_offset, 866 ToText(*selection_start_container), selection_start_offset,
867 selection_end_offset, DocumentMarker::MisspellingMarkers()); 867 selection_end_offset, DocumentMarker::MisspellingMarkers());
868 if (!marker) 868 if (!marker)
869 return {}; 869 return {};
870 870
871 return std::make_pair(selection_start_container, ToSpellCheckMarker(marker)); 871 return std::make_pair(selection_start_container, ToSpellCheckMarker(marker));
872 } 872 }
873 873
874 std::pair<String, String> SpellChecker::SelectMisspellingAsync() { 874 std::pair<String, String> SpellChecker::SelectMisspellingAsync() {
875 VisibleSelection selection = 875 const std::pair<Node*, SpellCheckMarker*>& node_and_marker =
876 GetFrame().Selection().ComputeVisibleSelectionInDOMTree(); 876 GetSpellCheckMarkerUnderSelection();
877 if (selection.IsNone()) 877 if (!node_and_marker.first)
878 return {}; 878 return {};
879 879
880 // Caret and range selections always return valid normalized ranges. 880 Node* const marker_node = node_and_marker.first;
881 const SpellCheckMarker* const marker = node_and_marker.second;
882
883 const VisibleSelection& selection =
884 GetFrame().Selection().ComputeVisibleSelectionInDOMTree();
885 // Caret and range selections (one of which we must have since we found a
886 // marker) always return valid normalized ranges.
881 const EphemeralRange& selection_range = 887 const EphemeralRange& selection_range =
882 selection.ToNormalizedEphemeralRange(); 888 selection.ToNormalizedEphemeralRange();
883 889
884 Node* const selection_start_container = 890 const EphemeralRange marker_range(
885 selection_range.StartPosition().ComputeContainerNode(); 891 Position(marker_node, marker->StartOffset()),
886 Node* const selection_end_container = 892 Position(marker_node, marker->EndOffset()));
887 selection_range.EndPosition().ComputeContainerNode(); 893 const String& marked_text = PlainText(marker_range);
888 894 if (marked_text.StripWhiteSpace(&IsWhiteSpaceOrPunctuation) !=
889 // We don't currently support the case where a misspelling spans multiple 895 PlainText(selection_range).StripWhiteSpace(&IsWhiteSpaceOrPunctuation))
890 // nodes. See crbug.com/720065
891 if (selection_start_container != selection_end_container)
892 return {}; 896 return {};
893 897
894 const unsigned selection_start_offset = 898 return std::make_pair(marked_text, marker->Description());
895 selection_range.StartPosition().ComputeOffsetInContainerNode();
896 const unsigned selection_end_offset =
897 selection_range.EndPosition().ComputeOffsetInContainerNode();
898
899 const DocumentMarkerVector& markers_in_node =
900 GetFrame().GetDocument()->Markers().MarkersFor(
901 selection_start_container, DocumentMarker::MisspellingMarkers());
902
903 const auto marker_it =
904 std::find_if(markers_in_node.begin(), markers_in_node.end(),
905 [=](const DocumentMarker* marker) {
906 return marker->StartOffset() < selection_end_offset &&
907 marker->EndOffset() > selection_start_offset;
908 });
909 if (marker_it == markers_in_node.end())
910 return {};
911
912 const SpellCheckMarker* const found_marker = ToSpellCheckMarker(*marker_it);
913
914 Range* const marker_range =
915 Range::Create(*GetFrame().GetDocument(), selection_start_container,
916 found_marker->StartOffset(), selection_start_container,
917 found_marker->EndOffset());
918
919 if (marker_range->GetText().StripWhiteSpace(&IsWhiteSpaceOrPunctuation) !=
920 CreateRange(selection_range)
921 ->GetText()
922 .StripWhiteSpace(&IsWhiteSpaceOrPunctuation))
923 return {};
924
925 return std::make_pair(marker_range->GetText(), found_marker->Description());
926 } 899 }
927 900
928 void SpellChecker::ReplaceMisspelledRange(const String& text) { 901 void SpellChecker::ReplaceMisspelledRange(const String& text) {
929 const std::pair<Node*, SpellCheckMarker*>& node_and_marker = 902 const std::pair<Node*, SpellCheckMarker*>& node_and_marker =
930 GetSpellCheckMarkerUnderSelection(); 903 GetSpellCheckMarkerUnderSelection();
931 if (!node_and_marker.first) 904 if (!node_and_marker.first)
932 return; 905 return;
933 906
934 Node* const container_node = node_and_marker.first; 907 Node* const container_node = node_and_marker.first;
935 const SpellCheckMarker* const marker = node_and_marker.second; 908 const SpellCheckMarker* const marker = node_and_marker.second;
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
1322 if (!input.IsFocusedElementInDocument()) 1295 if (!input.IsFocusedElementInDocument())
1323 return false; 1296 return false;
1324 } 1297 }
1325 } 1298 }
1326 HTMLElement* element = 1299 HTMLElement* element =
1327 Traversal<HTMLElement>::FirstAncestorOrSelf(*position.AnchorNode()); 1300 Traversal<HTMLElement>::FirstAncestorOrSelf(*position.AnchorNode());
1328 return element && element->IsSpellCheckingEnabled(); 1301 return element && element->IsSpellCheckingEnabled();
1329 } 1302 }
1330 1303
1331 } // namespace blink 1304 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698