Commit 91be04c4 authored by yosin's avatar yosin Committed by Commit bot

Get rid of flat tree version of createVisibleSelection() taking two VisiblePosition

This patch gets rid of flat tree version of |createVisibleSelection()| taking
two |VisiblePosition| by replacing with |SelectionInDOMTree| version to reduce
number of overloads for improving code health.

BUG=657237
TEST=n/a; no behavior changes

Review-Url: https://codereview.chromium.org/2437873008
Cr-Commit-Position: refs/heads/master@{#427031}
parent b80ad154
...@@ -1789,10 +1789,22 @@ VisibleSelection selectionForParagraphIteration( ...@@ -1789,10 +1789,22 @@ VisibleSelection selectionForParagraphIteration(
// we'll want modify is the last one inside the table, not the table itself (a // we'll want modify is the last one inside the table, not the table itself (a
// table is itself a paragraph). // table is itself a paragraph).
if (Element* table = tableElementJustBefore(endOfSelection)) { if (Element* table = tableElementJustBefore(endOfSelection)) {
if (startOfSelection.deepEquivalent().anchorNode()->isDescendantOf(table)) if (startOfSelection.deepEquivalent().anchorNode()->isDescendantOf(table)) {
newSelection = createVisibleSelection( const VisiblePosition& newEnd =
startOfSelection, previousPositionOf(endOfSelection, CannotCrossEditingBoundary);
previousPositionOf(endOfSelection, CannotCrossEditingBoundary)); if (newEnd.isNotNull()) {
newSelection = createVisibleSelection(
SelectionInDOMTree::Builder()
.collapse(startOfSelection.toPositionWithAffinity())
.extend(newEnd.deepEquivalent())
.build());
} else {
newSelection = createVisibleSelection(
SelectionInDOMTree::Builder()
.collapse(startOfSelection.toPositionWithAffinity())
.build());
}
}
} }
// If the start of the selection to modify is just before a table, and if the // If the start of the selection to modify is just before a table, and if the
...@@ -1800,10 +1812,22 @@ VisibleSelection selectionForParagraphIteration( ...@@ -1800,10 +1812,22 @@ VisibleSelection selectionForParagraphIteration(
// want to modify is the first one inside the table, not the paragraph // want to modify is the first one inside the table, not the paragraph
// containing the table itself. // containing the table itself.
if (Element* table = tableElementJustAfter(startOfSelection)) { if (Element* table = tableElementJustAfter(startOfSelection)) {
if (endOfSelection.deepEquivalent().anchorNode()->isDescendantOf(table)) if (endOfSelection.deepEquivalent().anchorNode()->isDescendantOf(table)) {
newSelection = createVisibleSelection( const VisiblePosition newStart =
nextPositionOf(startOfSelection, CannotCrossEditingBoundary), nextPositionOf(startOfSelection, CannotCrossEditingBoundary);
endOfSelection); if (newStart.isNotNull()) {
newSelection = createVisibleSelection(
SelectionInDOMTree::Builder()
.collapse(newStart.toPositionWithAffinity())
.extend(endOfSelection.deepEquivalent())
.build());
} else {
newSelection = createVisibleSelection(
SelectionInDOMTree::Builder()
.collapse(endOfSelection.toPositionWithAffinity())
.build());
}
}
} }
return newSelection; return newSelection;
......
...@@ -945,10 +945,15 @@ void FrameSelection::selectFrameElementInParentIfFullySelected() { ...@@ -945,10 +945,15 @@ void FrameSelection::selectFrameElementInParentIfFullySelected() {
Position(ownerElementParent, ownerElementNodeIndex + 1), Position(ownerElementParent, ownerElementNodeIndex + 1),
VP_UPSTREAM_IF_POSSIBLE); VP_UPSTREAM_IF_POSSIBLE);
SelectionInDOMTree::Builder builder;
builder
.setBaseAndExtentDeprecated(beforeOwnerElement.deepEquivalent(),
afterOwnerElement.deepEquivalent())
.setAffinity(beforeOwnerElement.affinity());
// Focus on the parent frame, and then select from before this element to // Focus on the parent frame, and then select from before this element to
// after. // after.
VisibleSelection newSelection = VisibleSelection newSelection = createVisibleSelection(builder.build());
createVisibleSelection(beforeOwnerElement, afterOwnerElement);
page->focusController().setFocusedFrame(parent); page->focusController().setFocusedFrame(parent);
// setFocusedFrame can dispatch synchronous focus/blur events. The document // setFocusedFrame can dispatch synchronous focus/blur events. The document
// tree might be modified. // tree might be modified.
...@@ -1387,9 +1392,13 @@ bool FrameSelection::selectWordAroundPosition(const VisiblePosition& position) { ...@@ -1387,9 +1392,13 @@ bool FrameSelection::selectWordAroundPosition(const VisiblePosition& position) {
String text = String text =
plainText(EphemeralRange(start.deepEquivalent(), end.deepEquivalent())); plainText(EphemeralRange(start.deepEquivalent(), end.deepEquivalent()));
if (!text.isEmpty() && !isSeparator(text.characterStartingAt(0))) { if (!text.isEmpty() && !isSeparator(text.characterStartingAt(0))) {
setSelection(createVisibleSelection(start, end), setSelection(
CloseTyping | ClearTypingStyle, createVisibleSelection(SelectionInDOMTree::Builder()
CursorAlignOnScroll::IfNeeded, WordGranularity); .collapse(start.toPositionWithAffinity())
.extend(end.deepEquivalent())
.build()),
CloseTyping | ClearTypingStyle, CursorAlignOnScroll::IfNeeded,
WordGranularity);
return true; return true;
} }
} }
...@@ -1428,11 +1437,18 @@ void FrameSelection::moveRangeSelectionExtent(const IntPoint& contentsPoint) { ...@@ -1428,11 +1437,18 @@ void FrameSelection::moveRangeSelectionExtent(const IntPoint& contentsPoint) {
CursorAlignOnScroll::IfNeeded, CharacterGranularity); CursorAlignOnScroll::IfNeeded, CharacterGranularity);
} }
// TODO(yosin): We should make |FrameSelection::moveRangeSelection()| to take
// two |IntPoint| instead of two |VisiblePosition| like
// |moveRangeSelectionExtent()|.
void FrameSelection::moveRangeSelection(const VisiblePosition& basePosition, void FrameSelection::moveRangeSelection(const VisiblePosition& basePosition,
const VisiblePosition& extentPosition, const VisiblePosition& extentPosition,
TextGranularity granularity) { TextGranularity granularity) {
VisibleSelection newSelection = VisibleSelection newSelection = createVisibleSelection(
createVisibleSelection(basePosition, extentPosition); SelectionInDOMTree::Builder()
.setBaseAndExtentDeprecated(basePosition.deepEquivalent(),
extentPosition.deepEquivalent())
.setAffinity(basePosition.affinity())
.build());
newSelection.expandUsingGranularity(granularity); newSelection.expandUsingGranularity(granularity);
if (newSelection.isNone()) if (newSelection.isNone())
......
...@@ -71,7 +71,11 @@ VisibleSelection CharacterGranularityStrategy::updateExtent( ...@@ -71,7 +71,11 @@ VisibleSelection CharacterGranularityStrategy::updateExtent(
if (selection.visibleBase().deepEquivalent() == if (selection.visibleBase().deepEquivalent() ==
extentPosition.deepEquivalent()) extentPosition.deepEquivalent())
return selection; return selection;
return createVisibleSelection(selection.visibleBase(), extentPosition); return createVisibleSelection(SelectionInDOMTree::Builder()
.collapse(selection.base())
.extend(extentPosition.deepEquivalent())
.setAffinity(selection.affinity())
.build());
} }
DirectionGranularityStrategy::DirectionGranularityStrategy() DirectionGranularityStrategy::DirectionGranularityStrategy()
...@@ -147,8 +151,12 @@ VisibleSelection DirectionGranularityStrategy::updateExtent( ...@@ -147,8 +151,12 @@ VisibleSelection DirectionGranularityStrategy::updateExtent(
// without a line change. // without a line change.
if (verticalChange && if (verticalChange &&
inSameLine(newOffsetExtentPosition, oldOffsetExtentPosition)) { inSameLine(newOffsetExtentPosition, oldOffsetExtentPosition)) {
return createVisibleSelection(selection.visibleBase(), return createVisibleSelection(
newOffsetExtentPosition); SelectionInDOMTree::Builder()
.collapse(selection.base())
.extend(newOffsetExtentPosition.deepEquivalent())
.setAffinity(selection.affinity())
.build());
} }
int oldExtentBaseOrder = selection.isBaseFirst() ? 1 : -1; int oldExtentBaseOrder = selection.isBaseFirst() ? 1 : -1;
......
...@@ -98,15 +98,6 @@ VisibleSelection createVisibleSelection(const Position& base, ...@@ -98,15 +98,6 @@ VisibleSelection createVisibleSelection(const Position& base,
return createVisibleSelection(builder.build()); return createVisibleSelection(builder.build());
} }
VisibleSelection createVisibleSelection(const VisiblePosition& base,
const VisiblePosition& extent,
bool isDirectional) {
DCHECK(base.isValid());
DCHECK(extent.isValid());
return createVisibleSelection(base.deepEquivalent(), extent.deepEquivalent(),
base.affinity(), isDirectional);
}
VisibleSelectionInFlatTree createVisibleSelection( VisibleSelectionInFlatTree createVisibleSelection(
const SelectionInFlatTree& selection) { const SelectionInFlatTree& selection) {
return VisibleSelectionInFlatTree::create(selection); return VisibleSelectionInFlatTree::create(selection);
......
...@@ -217,9 +217,6 @@ createVisibleSelection(const Position& base, ...@@ -217,9 +217,6 @@ createVisibleSelection(const Position& base,
const Position& extent, const Position& extent,
TextAffinity = SelDefaultAffinity, TextAffinity = SelDefaultAffinity,
bool isDirectional = false); bool isDirectional = false);
CORE_EXPORT VisibleSelection createVisibleSelection(const VisiblePosition&,
const VisiblePosition&,
bool isDirectional = false);
CORE_EXPORT VisibleSelectionInFlatTree CORE_EXPORT VisibleSelectionInFlatTree
createVisibleSelection(const SelectionInFlatTree&); createVisibleSelection(const SelectionInFlatTree&);
......
...@@ -77,10 +77,16 @@ void ApplyBlockElementCommand::doApply(EditingState* editingState) { ...@@ -77,10 +77,16 @@ void ApplyBlockElementCommand::doApply(EditingState* editingState) {
// consistent and then use a left margin/padding rule here. // consistent and then use a left margin/padding rule here.
if (visibleEnd.deepEquivalent() != visibleStart.deepEquivalent() && if (visibleEnd.deepEquivalent() != visibleStart.deepEquivalent() &&
isStartOfParagraph(visibleEnd)) { isStartOfParagraph(visibleEnd)) {
VisibleSelection newSelection = createVisibleSelection( const Position& newEnd =
visibleStart, previousPositionOf(visibleEnd, CannotCrossEditingBoundary)
previousPositionOf(visibleEnd, CannotCrossEditingBoundary), .deepEquivalent();
endingSelection().isDirectional()); SelectionInDOMTree::Builder builder;
builder.collapse(visibleStart.toPositionWithAffinity());
if (newEnd.isNotNull())
builder.extend(newEnd);
builder.setIsDirectional(endingSelection().isDirectional());
const VisibleSelection newSelection =
createVisibleSelection(builder.build());
if (newSelection.isNone()) if (newSelection.isNone())
return; return;
setEndingSelection(newSelection); setEndingSelection(newSelection);
...@@ -109,9 +115,14 @@ void ApplyBlockElementCommand::doApply(EditingState* editingState) { ...@@ -109,9 +115,14 @@ void ApplyBlockElementCommand::doApply(EditingState* editingState) {
if (startScope == endScope && startIndex >= 0 && startIndex <= endIndex) { if (startScope == endScope && startIndex >= 0 && startIndex <= endIndex) {
VisiblePosition start(visiblePositionForIndex(startIndex, startScope)); VisiblePosition start(visiblePositionForIndex(startIndex, startScope));
VisiblePosition end(visiblePositionForIndex(endIndex, endScope)); VisiblePosition end(visiblePositionForIndex(endIndex, endScope));
if (start.isNotNull() && end.isNotNull()) if (start.isNotNull() && end.isNotNull()) {
setEndingSelection(createVisibleSelection( setEndingSelection(createVisibleSelection(
start, end, endingSelection().isDirectional())); SelectionInDOMTree::Builder()
.collapse(start.toPositionWithAffinity())
.extend(end.deepEquivalent())
.setIsDirectional(endingSelection().isDirectional())
.build()));
}
} }
} }
......
...@@ -195,10 +195,19 @@ void DeleteSelectionCommand::setStartingSelectionOnSmartDelete( ...@@ -195,10 +195,19 @@ void DeleteSelectionCommand::setStartingSelectionOnSmartDelete(
document().lifecycle()); document().lifecycle());
bool isBaseFirst = startingSelection().isBaseFirst(); bool isBaseFirst = startingSelection().isBaseFirst();
// TODO(yosin): We should not call |createVisiblePosition()| here and use
// |start| and |end| as base/extent since |VisibleSelection| also calls
// |createVisiblePosition()| during construction.
// Because of |newBase.affinity()| can be |Upstream|, we can't simply
// use |start| and |end| here.
VisiblePosition newBase = createVisiblePosition(isBaseFirst ? start : end); VisiblePosition newBase = createVisiblePosition(isBaseFirst ? start : end);
VisiblePosition newExtent = createVisiblePosition(isBaseFirst ? end : start); VisiblePosition newExtent = createVisiblePosition(isBaseFirst ? end : start);
setStartingSelection(createVisibleSelection( SelectionInDOMTree::Builder builder;
newBase, newExtent, startingSelection().isDirectional())); builder.setAffinity(newBase.affinity())
.setBaseAndExtentDeprecated(newBase.deepEquivalent(),
newExtent.deepEquivalent())
.setIsDirectional(startingSelection().isDirectional());
setStartingSelection(createVisibleSelection(builder.build()));
} }
void DeleteSelectionCommand::initializePositionData( void DeleteSelectionCommand::initializePositionData(
......
...@@ -155,10 +155,14 @@ void InsertListCommand::doApply(EditingState* editingState) { ...@@ -155,10 +155,14 @@ void InsertListCommand::doApply(EditingState* editingState) {
// consistent and then use a left margin/padding rule here. // consistent and then use a left margin/padding rule here.
if (visibleEnd.deepEquivalent() != visibleStart.deepEquivalent() && if (visibleEnd.deepEquivalent() != visibleStart.deepEquivalent() &&
isStartOfParagraph(visibleEnd, CanSkipOverEditingBoundary)) { isStartOfParagraph(visibleEnd, CanSkipOverEditingBoundary)) {
setEndingSelection(createVisibleSelection( const VisiblePosition& newEnd =
visibleStart, previousPositionOf(visibleEnd, CannotCrossEditingBoundary);
previousPositionOf(visibleEnd, CannotCrossEditingBoundary), SelectionInDOMTree::Builder builder;
endingSelection().isDirectional())); builder.setIsDirectional(endingSelection().isDirectional());
builder.collapse(visibleStart.toPositionWithAffinity());
if (newEnd.isNotNull())
builder.extend(newEnd.deepEquivalent());
setEndingSelection(createVisibleSelection(builder.build()));
if (!endingSelection().rootEditableElement()) if (!endingSelection().rootEditableElement())
return; return;
} }
...@@ -277,9 +281,14 @@ void InsertListCommand::doApply(EditingState* editingState) { ...@@ -277,9 +281,14 @@ void InsertListCommand::doApply(EditingState* editingState) {
visibleStartOfSelection = createVisiblePosition(startOfSelection); visibleStartOfSelection = createVisiblePosition(startOfSelection);
} }
setEndingSelection( setEndingSelection(createVisibleSelection(
createVisibleSelection(visibleStartOfSelection, visibleEndOfSelection, SelectionInDOMTree::Builder()
endingSelection().isDirectional())); .setAffinity(visibleStartOfSelection.affinity())
.setBaseAndExtentDeprecated(
visibleStartOfSelection.deepEquivalent(),
visibleEndOfSelection.deepEquivalent())
.setIsDirectional(endingSelection().isDirectional())
.build()));
return; return;
} }
......
...@@ -134,6 +134,17 @@ static EphemeralRange expandRangeToSentenceBoundary( ...@@ -134,6 +134,17 @@ static EphemeralRange expandRangeToSentenceBoundary(
range.endPosition())); range.endPosition()));
} }
SelectionInDOMTree selectWord(const VisiblePosition& position) {
// TODO(yosin): We should fix |startOfWord()| and |endOfWord()| not to return
// null position.
const VisiblePosition& start = startOfWord(position, LeftWordIfOnBoundary);
const VisiblePosition& end = endOfWord(position, RightWordIfOnBoundary);
return SelectionInDOMTree::Builder()
.setBaseAndExtentDeprecated(start.deepEquivalent(), end.deepEquivalent())
.setAffinity(start.affinity())
.build();
}
} // namespace } // namespace
SpellChecker* SpellChecker::create(LocalFrame& frame) { SpellChecker* SpellChecker::create(LocalFrame& frame) {
...@@ -444,8 +455,13 @@ void SpellChecker::markMisspellingsAfterTypingCommand( ...@@ -444,8 +455,13 @@ void SpellChecker::markMisspellingsAfterTypingCommand(
if (cmd.commandTypeOfOpenCommand() == if (cmd.commandTypeOfOpenCommand() ==
TypingCommand::InsertParagraphSeparator) { TypingCommand::InsertParagraphSeparator) {
VisiblePosition nextWord = nextWordPosition(start); VisiblePosition nextWord = nextWordPosition(start);
VisibleSelection words = // TODO(yosin): We should make |endOfWord()| not to return null position.
createVisibleSelection(wordStartOfPrevious, endOfWord(nextWord)); VisibleSelection words = createVisibleSelection(
SelectionInDOMTree::Builder()
.setBaseAndExtentDeprecated(wordStartOfPrevious.deepEquivalent(),
endOfWord(nextWord).deepEquivalent())
.setAffinity(wordStartOfPrevious.affinity())
.build());
markMisspellingsAfterLineBreak(words); markMisspellingsAfterLineBreak(words);
return; return;
} }
...@@ -470,8 +486,7 @@ void SpellChecker::markMisspellingsAfterTypingToWord( ...@@ -470,8 +486,7 @@ void SpellChecker::markMisspellingsAfterTypingToWord(
TRACE_EVENT0("blink", "SpellChecker::markMisspellingsAfterTypingToWord"); TRACE_EVENT0("blink", "SpellChecker::markMisspellingsAfterTypingToWord");
VisibleSelection adjacentWords = VisibleSelection adjacentWords =
createVisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary), createVisibleSelection(selectWord(wordStart));
endOfWord(wordStart, RightWordIfOnBoundary));
markMisspellingsAndBadGrammar(adjacentWords); markMisspellingsAndBadGrammar(adjacentWords);
} }
...@@ -868,10 +883,8 @@ void SpellChecker::respondToChangedSelection( ...@@ -868,10 +883,8 @@ void SpellChecker::respondToChangedSelection(
HTMLTextFormControlElement::endOfWord(newStart)); HTMLTextFormControlElement::endOfWord(newStart));
} else { } else {
if (newSelection.isContentEditable()) { if (newSelection.isContentEditable()) {
const VisiblePosition newStart(newSelection.visibleStart());
newAdjacentWords = newAdjacentWords =
createVisibleSelection(startOfWord(newStart, LeftWordIfOnBoundary), createVisibleSelection(selectWord(newSelection.visibleStart()));
endOfWord(newStart, RightWordIfOnBoundary));
} }
} }
...@@ -929,8 +942,7 @@ void SpellChecker::spellCheckOldSelection( ...@@ -929,8 +942,7 @@ void SpellChecker::spellCheckOldSelection(
VisiblePosition oldStart = createVisiblePosition(oldSelectionStart); VisiblePosition oldStart = createVisiblePosition(oldSelectionStart);
VisibleSelection oldAdjacentWords = VisibleSelection oldAdjacentWords =
createVisibleSelection(startOfWord(oldStart, LeftWordIfOnBoundary), createVisibleSelection(selectWord(oldStart));
endOfWord(oldStart, RightWordIfOnBoundary));
if (oldAdjacentWords == newAdjacentWords) if (oldAdjacentWords == newAdjacentWords)
return; return;
markMisspellingsAndBadGrammar(oldAdjacentWords); markMisspellingsAndBadGrammar(oldAdjacentWords);
......
...@@ -225,8 +225,14 @@ void SVGTextContentElement::selectSubString(unsigned charnum, ...@@ -225,8 +225,14 @@ void SVGTextContentElement::selectSubString(unsigned charnum,
for (unsigned i = 0; i < nchars; ++i) for (unsigned i = 0; i < nchars; ++i)
end = nextPositionOf(end); end = nextPositionOf(end);
// TODO(editing-dev): We assume |start| and |end| are not null and we don't
// known when |start| and |end| are null. Once we get a such case, we check
// null for |start| and |end|.
document().frame()->selection().setSelection( document().frame()->selection().setSelection(
createVisibleSelection(start, end)); SelectionInDOMTree::Builder()
.setBaseAndExtent(start.deepEquivalent(), end.deepEquivalent())
.setAffinity(start.affinity())
.build());
} }
bool SVGTextContentElement::isPresentationAttribute( bool SVGTextContentElement::isPresentationAttribute(
......
...@@ -1952,7 +1952,10 @@ void AXLayoutObject::setSelection(const AXRange& selection) { ...@@ -1952,7 +1952,10 @@ void AXLayoutObject::setSelection(const AXRange& selection) {
return; return;
frame->selection().setSelection( frame->selection().setSelection(
createVisibleSelection(anchorVisiblePosition, focusVisiblePosition)); SelectionInDOMTree::Builder()
.collapse(anchorVisiblePosition.toPositionWithAffinity())
.extend(focusVisiblePosition.deepEquivalent())
.build());
} }
bool AXLayoutObject::isValidSelectionBound(const AXObject* boundObject) const { bool AXLayoutObject::isValidSelectionBound(const AXObject* boundObject) const {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment