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

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

Issue 2437873008: Get rid of flat tree version of createVisibleSelection() taking two VisiblePosition (Closed)
Patch Set: 2016-10-21T16:03:11 Created 4 years, 2 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
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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 // TODO(xiaochengh): |sentenceStart > range.startPosition()| is possible, 127 // TODO(xiaochengh): |sentenceStart > range.startPosition()| is possible,
128 // which would trigger a DCHECK in EphemeralRange's constructor if we return 128 // which would trigger a DCHECK in EphemeralRange's constructor if we return
129 // it directly. However, this shouldn't happen and needs to be fixed. 129 // it directly. However, this shouldn't happen and needs to be fixed.
130 return expandEndToSentenceBoundary(EphemeralRange( 130 return expandEndToSentenceBoundary(EphemeralRange(
131 sentenceStart.isNotNull() && sentenceStart < range.startPosition() 131 sentenceStart.isNotNull() && sentenceStart < range.startPosition()
132 ? sentenceStart 132 ? sentenceStart
133 : range.startPosition(), 133 : range.startPosition(),
134 range.endPosition())); 134 range.endPosition()));
135 } 135 }
136 136
137 SelectionInDOMTree selectionFromBaseAndExtent(const VisiblePosition& base,
Xiaocheng 2016/10/21 11:18:36 We should use setBaseAndExtentDeprecated. Though t
yosin_UTC9 2016/10/24 06:19:44 Done.
138 const VisiblePosition& extent) {
139 if (base.isNotNull() && extent.isNotNull()) {
140 return SelectionInDOMTree::Builder()
141 .collapse(base.toPositionWithAffinity())
142 .extend(extent.deepEquivalent())
143 .build();
144 }
145 if (base.isNotNull()) {
146 return SelectionInDOMTree::Builder()
147 .collapse(base.toPositionWithAffinity())
148 .build();
149 }
150 if (extent.isNotNull()) {
151 return SelectionInDOMTree::Builder()
152 .collapse(extent.toPositionWithAffinity())
153 .build();
154 }
155 return SelectionInDOMTree();
156 }
157
158 SelectionInDOMTree selectWord(const VisiblePosition& position) {
159 return selectionFromBaseAndExtent(startOfWord(position, LeftWordIfOnBoundary),
160 endOfWord(position, RightWordIfOnBoundary));
161 }
162
137 } // namespace 163 } // namespace
138 164
139 SpellChecker* SpellChecker::create(LocalFrame& frame) { 165 SpellChecker* SpellChecker::create(LocalFrame& frame) {
140 return new SpellChecker(frame); 166 return new SpellChecker(frame);
141 } 167 }
142 168
143 static SpellCheckerClient& emptySpellCheckerClient() { 169 static SpellCheckerClient& emptySpellCheckerClient() {
144 DEFINE_STATIC_LOCAL(EmptySpellCheckerClient, client, ()); 170 DEFINE_STATIC_LOCAL(EmptySpellCheckerClient, client, ());
145 return client; 171 return client;
146 } 172 }
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 VisiblePosition start = createVisiblePosition( 463 VisiblePosition start = createVisiblePosition(
438 cmd.endingSelection().start(), cmd.endingSelection().affinity()); 464 cmd.endingSelection().start(), cmd.endingSelection().affinity());
439 VisiblePosition previous = previousPositionOf(start); 465 VisiblePosition previous = previousPositionOf(start);
440 466
441 VisiblePosition wordStartOfPrevious = 467 VisiblePosition wordStartOfPrevious =
442 startOfWord(previous, LeftWordIfOnBoundary); 468 startOfWord(previous, LeftWordIfOnBoundary);
443 469
444 if (cmd.commandTypeOfOpenCommand() == 470 if (cmd.commandTypeOfOpenCommand() ==
445 TypingCommand::InsertParagraphSeparator) { 471 TypingCommand::InsertParagraphSeparator) {
446 VisiblePosition nextWord = nextWordPosition(start); 472 VisiblePosition nextWord = nextWordPosition(start);
447 VisibleSelection words = 473 VisibleSelection words = createVisibleSelection(
448 createVisibleSelection(wordStartOfPrevious, endOfWord(nextWord)); 474 selectionFromBaseAndExtent(wordStartOfPrevious, endOfWord(nextWord)));
Xiaocheng 2016/10/21 11:18:36 We should use setBaseAndExtentDeprecated.
yosin_UTC9 2016/10/24 06:19:44 Done
449 markMisspellingsAfterLineBreak(words); 475 markMisspellingsAfterLineBreak(words);
450 return; 476 return;
451 } 477 }
452 478
453 if (previous.isNull()) 479 if (previous.isNull())
454 return; 480 return;
455 VisiblePosition currentWordStart = startOfWord(start, LeftWordIfOnBoundary); 481 VisiblePosition currentWordStart = startOfWord(start, LeftWordIfOnBoundary);
456 if (wordStartOfPrevious.deepEquivalent() == currentWordStart.deepEquivalent()) 482 if (wordStartOfPrevious.deepEquivalent() == currentWordStart.deepEquivalent())
457 return; 483 return;
458 markMisspellingsAfterTypingToWord(wordStartOfPrevious); 484 markMisspellingsAfterTypingToWord(wordStartOfPrevious);
459 } 485 }
460 486
461 void SpellChecker::markMisspellingsAfterLineBreak( 487 void SpellChecker::markMisspellingsAfterLineBreak(
462 const VisibleSelection& wordSelection) { 488 const VisibleSelection& wordSelection) {
463 TRACE_EVENT0("blink", "SpellChecker::markMisspellingsAfterLineBreak"); 489 TRACE_EVENT0("blink", "SpellChecker::markMisspellingsAfterLineBreak");
464 490
465 markMisspellingsAndBadGrammar(wordSelection); 491 markMisspellingsAndBadGrammar(wordSelection);
466 } 492 }
467 493
468 void SpellChecker::markMisspellingsAfterTypingToWord( 494 void SpellChecker::markMisspellingsAfterTypingToWord(
469 const VisiblePosition& wordStart) { 495 const VisiblePosition& wordStart) {
470 TRACE_EVENT0("blink", "SpellChecker::markMisspellingsAfterTypingToWord"); 496 TRACE_EVENT0("blink", "SpellChecker::markMisspellingsAfterTypingToWord");
471 497
472 VisibleSelection adjacentWords = 498 VisibleSelection adjacentWords =
473 createVisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary), 499 createVisibleSelection(selectWord(wordStart));
474 endOfWord(wordStart, RightWordIfOnBoundary));
475 markMisspellingsAndBadGrammar(adjacentWords); 500 markMisspellingsAndBadGrammar(adjacentWords);
476 } 501 }
477 502
478 bool SpellChecker::isSpellCheckingEnabledInFocusedNode() const { 503 bool SpellChecker::isSpellCheckingEnabledInFocusedNode() const {
479 Node* focusedNode = frame().selection().start().anchorNode(); 504 Node* focusedNode = frame().selection().start().anchorNode();
480 if (!focusedNode) 505 if (!focusedNode)
481 return false; 506 return false;
482 const Element* focusedElement = focusedNode->isElementNode() 507 const Element* focusedElement = focusedNode->isElementNode()
483 ? toElement(focusedNode) 508 ? toElement(focusedNode)
484 : focusedNode->parentElement(); 509 : focusedNode->parentElement();
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
861 886
862 VisibleSelection newAdjacentWords; 887 VisibleSelection newAdjacentWords;
863 const VisibleSelection newSelection = frame().selection().selection(); 888 const VisibleSelection newSelection = frame().selection().selection();
864 if (isSelectionInTextFormControl(newSelection)) { 889 if (isSelectionInTextFormControl(newSelection)) {
865 const Position newStart = newSelection.start(); 890 const Position newStart = newSelection.start();
866 newAdjacentWords.setWithoutValidation( 891 newAdjacentWords.setWithoutValidation(
867 HTMLTextFormControlElement::startOfWord(newStart), 892 HTMLTextFormControlElement::startOfWord(newStart),
868 HTMLTextFormControlElement::endOfWord(newStart)); 893 HTMLTextFormControlElement::endOfWord(newStart));
869 } else { 894 } else {
870 if (newSelection.isContentEditable()) { 895 if (newSelection.isContentEditable()) {
871 const VisiblePosition newStart(newSelection.visibleStart());
872 newAdjacentWords = 896 newAdjacentWords =
873 createVisibleSelection(startOfWord(newStart, LeftWordIfOnBoundary), 897 createVisibleSelection(selectWord(newSelection.visibleStart()));
874 endOfWord(newStart, RightWordIfOnBoundary));
875 } 898 }
876 } 899 }
877 900
878 // When typing we check spelling elsewhere, so don't redo it here. 901 // When typing we check spelling elsewhere, so don't redo it here.
879 // If this is a change in selection resulting from a delete operation, 902 // If this is a change in selection resulting from a delete operation,
880 // oldSelection may no longer be in the document. 903 // oldSelection may no longer be in the document.
881 // FIXME(http://crbug.com/382809): if oldSelection is on a textarea 904 // FIXME(http://crbug.com/382809): if oldSelection is on a textarea
882 // element, we cause synchronous layout. 905 // element, we cause synchronous layout.
883 spellCheckOldSelection(oldSelectionStart, newAdjacentWords); 906 spellCheckOldSelection(oldSelectionStart, newAdjacentWords);
884 } 907 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
922 void SpellChecker::spellCheckOldSelection( 945 void SpellChecker::spellCheckOldSelection(
923 const Position& oldSelectionStart, 946 const Position& oldSelectionStart,
924 const VisibleSelection& newAdjacentWords) { 947 const VisibleSelection& newAdjacentWords) {
925 if (!isSpellCheckingEnabled()) 948 if (!isSpellCheckingEnabled())
926 return; 949 return;
927 950
928 TRACE_EVENT0("blink", "SpellChecker::spellCheckOldSelection"); 951 TRACE_EVENT0("blink", "SpellChecker::spellCheckOldSelection");
929 952
930 VisiblePosition oldStart = createVisiblePosition(oldSelectionStart); 953 VisiblePosition oldStart = createVisiblePosition(oldSelectionStart);
931 VisibleSelection oldAdjacentWords = 954 VisibleSelection oldAdjacentWords =
932 createVisibleSelection(startOfWord(oldStart, LeftWordIfOnBoundary), 955 createVisibleSelection(selectWord(oldStart));
933 endOfWord(oldStart, RightWordIfOnBoundary));
934 if (oldAdjacentWords == newAdjacentWords) 956 if (oldAdjacentWords == newAdjacentWords)
935 return; 957 return;
936 markMisspellingsAndBadGrammar(oldAdjacentWords); 958 markMisspellingsAndBadGrammar(oldAdjacentWords);
937 } 959 }
938 960
939 static Node* findFirstMarkable(Node* node) { 961 static Node* findFirstMarkable(Node* node) {
940 while (node) { 962 while (node) {
941 if (!node->layoutObject()) 963 if (!node->layoutObject())
942 return 0; 964 return 0;
943 if (node->layoutObject()->isText()) 965 if (node->layoutObject()->isText())
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
1129 startOfNextParagraph(createVisiblePosition(paragraphEnd)); 1151 startOfNextParagraph(createVisiblePosition(paragraphEnd));
1130 paragraphStart = newParagraphStart.toParentAnchoredPosition(); 1152 paragraphStart = newParagraphStart.toParentAnchoredPosition();
1131 paragraphEnd = endOfParagraph(newParagraphStart).toParentAnchoredPosition(); 1153 paragraphEnd = endOfParagraph(newParagraphStart).toParentAnchoredPosition();
1132 firstIteration = false; 1154 firstIteration = false;
1133 totalLengthProcessed += currentLength; 1155 totalLengthProcessed += currentLength;
1134 } 1156 }
1135 return std::make_pair(firstFoundItem, firstFoundOffset); 1157 return std::make_pair(firstFoundItem, firstFoundOffset);
1136 } 1158 }
1137 1159
1138 } // namespace blink 1160 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698