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

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

Issue 2947093003: [MarkersIntersectingRange #3] Optimize SpellChecker::GetSpellCheckMarkerUnderSelection() (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 815 matching lines...) Expand 10 before | Expand all | Expand 10 after
826 marker_types); 826 marker_types);
827 } 827 }
828 } 828 }
829 } 829 }
830 830
831 Optional<std::pair<Node*, SpellCheckMarker*>> 831 Optional<std::pair<Node*, SpellCheckMarker*>>
832 SpellChecker::GetSpellCheckMarkerUnderSelection() { 832 SpellChecker::GetSpellCheckMarkerUnderSelection() {
833 const VisibleSelection& selection = 833 const VisibleSelection& selection =
834 GetFrame().Selection().ComputeVisibleSelectionInDOMTree(); 834 GetFrame().Selection().ComputeVisibleSelectionInDOMTree();
835 if (selection.IsNone()) 835 if (selection.IsNone())
836 return Optional<std::pair<Node*, SpellCheckMarker*>>(); 836 return Optional<std::pair<Node*, SpellCheckMarker*>>();
Xiaocheng 2017/07/20 21:41:42 Use |return WTF::nullopt|
837 837
838 const EphemeralRange& range_to_check = FirstEphemeralRangeOf(selection); 838 // Caret and range selections always return valid normalized ranges.
839 const EphemeralRange& selection_range =
840 selection.ToNormalizedEphemeralRange();
yosin_UTC9 2017/07/20 01:12:11 Could you use |FirstEphemeralRangeOf(selection)| a
839 841
840 Node* const start_container = 842 Node* const selection_start_container =
841 range_to_check.StartPosition().ComputeContainerNode(); 843 selection_range.StartPosition().ComputeContainerNode();
842 const unsigned start_offset = 844 Node* const selection_end_container =
843 range_to_check.StartPosition().ComputeOffsetInContainerNode(); 845 selection_range.EndPosition().ComputeContainerNode();
844 Node* const end_container =
845 range_to_check.EndPosition().ComputeContainerNode();
846 const unsigned end_offset =
847 range_to_check.EndPosition().ComputeOffsetInContainerNode();
848 846
849 for (Node& node : range_to_check.Nodes()) { 847 // We don't currently support the case where a misspelling spans multiple
850 const DocumentMarkerVector& markers_in_node = 848 // nodes. See crbug.com/720065
851 GetFrame().GetDocument()->Markers().MarkersFor( 849 if (selection_start_container != selection_end_container)
852 &node, DocumentMarker::MisspellingMarkers()); 850 return Optional<std::pair<Node*, SpellCheckMarker*>>();
yosin_UTC9 2017/07/20 01:12:11 Could you try to write |return {}| to make code si
Xiaocheng 2017/07/20 21:41:42 I prefer WTF::nullopt
853 for (DocumentMarker* marker : markers_in_node) {
854 if (node == start_container && marker->EndOffset() <= start_offset)
855 continue;
856 if (node == end_container && marker->StartOffset() >= end_offset)
857 continue;
858 851
859 return std::make_pair(&node, &ToSpellCheckMarker(*marker)); 852 if (!selection_start_container->IsTextNode())
860 } 853 return Optional<std::pair<Node*, SpellCheckMarker*>>();
861 }
862 854
863 // No marker found 855 const unsigned selection_start_offset =
864 return Optional<std::pair<Node*, SpellCheckMarker*>>(); 856 selection_range.StartPosition().ComputeOffsetInContainerNode();
857 const unsigned selection_end_offset =
858 selection_range.EndPosition().ComputeOffsetInContainerNode();
859
860 DocumentMarker* const marker =
861 GetFrame().GetDocument()->Markers().FirstMarkerIntersectingOffsetRange(
862 ToText(*selection_start_container), selection_start_offset,
863 selection_end_offset, DocumentMarker::MisspellingMarkers());
864 if (!marker)
865 return Optional<std::pair<Node*, SpellCheckMarker*>>();
866
867 return std::make_pair(selection_start_container, ToSpellCheckMarker(marker));
865 } 868 }
866 869
867 void SpellChecker::ReplaceMisspelledRange(const String& text) { 870 void SpellChecker::ReplaceMisspelledRange(const String& text) {
868 const Optional<std::pair<Node*, SpellCheckMarker*>>& node_and_marker = 871 const Optional<std::pair<Node*, SpellCheckMarker*>>& node_and_marker =
869 GetSpellCheckMarkerUnderSelection(); 872 GetSpellCheckMarkerUnderSelection();
870 if (!node_and_marker) 873 if (!node_and_marker)
871 return; 874 return;
872 875
873 Node* const container_node = node_and_marker.value().first; 876 Node* const container_node = node_and_marker.value().first;
874 const SpellCheckMarker* const marker = node_and_marker.value().second; 877 const SpellCheckMarker* const marker = node_and_marker.value().second;
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
1261 if (!input.IsFocusedElementInDocument()) 1264 if (!input.IsFocusedElementInDocument())
1262 return false; 1265 return false;
1263 } 1266 }
1264 } 1267 }
1265 HTMLElement* element = 1268 HTMLElement* element =
1266 Traversal<HTMLElement>::FirstAncestorOrSelf(*position.AnchorNode()); 1269 Traversal<HTMLElement>::FirstAncestorOrSelf(*position.AnchorNode());
1267 return element && element->IsSpellCheckingEnabled(); 1270 return element && element->IsSpellCheckingEnabled();
1268 } 1271 }
1269 1272
1270 } // namespace blink 1273 } // 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