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(
// we'll want modify is the last one inside the table, not the table itself (a
// table is itself a paragraph).
if (Element* table = tableElementJustBefore(endOfSelection)) {
if (startOfSelection.deepEquivalent().anchorNode()->isDescendantOf(table))
newSelection = createVisibleSelection(
startOfSelection,
previousPositionOf(endOfSelection, CannotCrossEditingBoundary));
if (startOfSelection.deepEquivalent().anchorNode()->isDescendantOf(table)) {
const VisiblePosition& newEnd =
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
......@@ -1800,10 +1812,22 @@ VisibleSelection selectionForParagraphIteration(
// want to modify is the first one inside the table, not the paragraph
// containing the table itself.
if (Element* table = tableElementJustAfter(startOfSelection)) {
if (endOfSelection.deepEquivalent().anchorNode()->isDescendantOf(table))
newSelection = createVisibleSelection(
nextPositionOf(startOfSelection, CannotCrossEditingBoundary),
endOfSelection);
if (endOfSelection.deepEquivalent().anchorNode()->isDescendantOf(table)) {
const VisiblePosition newStart =
nextPositionOf(startOfSelection, CannotCrossEditingBoundary);
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;
......
......@@ -945,10 +945,15 @@ void FrameSelection::selectFrameElementInParentIfFullySelected() {
Position(ownerElementParent, ownerElementNodeIndex + 1),
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
// after.
VisibleSelection newSelection =
createVisibleSelection(beforeOwnerElement, afterOwnerElement);
VisibleSelection newSelection = createVisibleSelection(builder.build());
page->focusController().setFocusedFrame(parent);
// setFocusedFrame can dispatch synchronous focus/blur events. The document
// tree might be modified.
......@@ -1387,9 +1392,13 @@ bool FrameSelection::selectWordAroundPosition(const VisiblePosition& position) {
String text =
plainText(EphemeralRange(start.deepEquivalent(), end.deepEquivalent()));
if (!text.isEmpty() && !isSeparator(text.characterStartingAt(0))) {
setSelection(createVisibleSelection(start, end),
CloseTyping | ClearTypingStyle,
CursorAlignOnScroll::IfNeeded, WordGranularity);
setSelection(
createVisibleSelection(SelectionInDOMTree::Builder()
.collapse(start.toPositionWithAffinity())
.extend(end.deepEquivalent())
.build()),
CloseTyping | ClearTypingStyle, CursorAlignOnScroll::IfNeeded,
WordGranularity);
return true;
}
}
......@@ -1428,11 +1437,18 @@ void FrameSelection::moveRangeSelectionExtent(const IntPoint& contentsPoint) {
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,
const VisiblePosition& extentPosition,
TextGranularity granularity) {
VisibleSelection newSelection =
createVisibleSelection(basePosition, extentPosition);
VisibleSelection newSelection = createVisibleSelection(
SelectionInDOMTree::Builder()
.setBaseAndExtentDeprecated(basePosition.deepEquivalent(),
extentPosition.deepEquivalent())
.setAffinity(basePosition.affinity())
.build());
newSelection.expandUsingGranularity(granularity);
if (newSelection.isNone())
......
......@@ -71,7 +71,11 @@ VisibleSelection CharacterGranularityStrategy::updateExtent(
if (selection.visibleBase().deepEquivalent() ==
extentPosition.deepEquivalent())
return selection;
return createVisibleSelection(selection.visibleBase(), extentPosition);
return createVisibleSelection(SelectionInDOMTree::Builder()
.collapse(selection.base())
.extend(extentPosition.deepEquivalent())
.setAffinity(selection.affinity())
.build());
}
DirectionGranularityStrategy::DirectionGranularityStrategy()
......@@ -147,8 +151,12 @@ VisibleSelection DirectionGranularityStrategy::updateExtent(
// without a line change.
if (verticalChange &&
inSameLine(newOffsetExtentPosition, oldOffsetExtentPosition)) {
return createVisibleSelection(selection.visibleBase(),
newOffsetExtentPosition);
return createVisibleSelection(
SelectionInDOMTree::Builder()
.collapse(selection.base())
.extend(newOffsetExtentPosition.deepEquivalent())
.setAffinity(selection.affinity())
.build());
}
int oldExtentBaseOrder = selection.isBaseFirst() ? 1 : -1;
......
......@@ -98,15 +98,6 @@ VisibleSelection createVisibleSelection(const Position& base,
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(
const SelectionInFlatTree& selection) {
return VisibleSelectionInFlatTree::create(selection);
......
......@@ -217,9 +217,6 @@ createVisibleSelection(const Position& base,
const Position& extent,
TextAffinity = SelDefaultAffinity,
bool isDirectional = false);
CORE_EXPORT VisibleSelection createVisibleSelection(const VisiblePosition&,
const VisiblePosition&,
bool isDirectional = false);
CORE_EXPORT VisibleSelectionInFlatTree
createVisibleSelection(const SelectionInFlatTree&);
......
......@@ -77,10 +77,16 @@ void ApplyBlockElementCommand::doApply(EditingState* editingState) {
// consistent and then use a left margin/padding rule here.
if (visibleEnd.deepEquivalent() != visibleStart.deepEquivalent() &&
isStartOfParagraph(visibleEnd)) {
VisibleSelection newSelection = createVisibleSelection(
visibleStart,
previousPositionOf(visibleEnd, CannotCrossEditingBoundary),
endingSelection().isDirectional());
const Position& newEnd =
previousPositionOf(visibleEnd, CannotCrossEditingBoundary)
.deepEquivalent();
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())
return;
setEndingSelection(newSelection);
......@@ -109,9 +115,14 @@ void ApplyBlockElementCommand::doApply(EditingState* editingState) {
if (startScope == endScope && startIndex >= 0 && startIndex <= endIndex) {
VisiblePosition start(visiblePositionForIndex(startIndex, startScope));
VisiblePosition end(visiblePositionForIndex(endIndex, endScope));
if (start.isNotNull() && end.isNotNull())
if (start.isNotNull() && end.isNotNull()) {
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(
document().lifecycle());
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 newExtent = createVisiblePosition(isBaseFirst ? end : start);
setStartingSelection(createVisibleSelection(
newBase, newExtent, startingSelection().isDirectional()));
SelectionInDOMTree::Builder builder;
builder.setAffinity(newBase.affinity())
.setBaseAndExtentDeprecated(newBase.deepEquivalent(),
newExtent.deepEquivalent())
.setIsDirectional(startingSelection().isDirectional());
setStartingSelection(createVisibleSelection(builder.build()));
}
void DeleteSelectionCommand::initializePositionData(
......
......@@ -155,10 +155,14 @@ void InsertListCommand::doApply(EditingState* editingState) {
// consistent and then use a left margin/padding rule here.
if (visibleEnd.deepEquivalent() != visibleStart.deepEquivalent() &&
isStartOfParagraph(visibleEnd, CanSkipOverEditingBoundary)) {
setEndingSelection(createVisibleSelection(
visibleStart,
previousPositionOf(visibleEnd, CannotCrossEditingBoundary),
endingSelection().isDirectional()));
const VisiblePosition& newEnd =
previousPositionOf(visibleEnd, CannotCrossEditingBoundary);
SelectionInDOMTree::Builder builder;
builder.setIsDirectional(endingSelection().isDirectional());
builder.collapse(visibleStart.toPositionWithAffinity());
if (newEnd.isNotNull())
builder.extend(newEnd.deepEquivalent());
setEndingSelection(createVisibleSelection(builder.build()));
if (!endingSelection().rootEditableElement())
return;
}
......@@ -277,9 +281,14 @@ void InsertListCommand::doApply(EditingState* editingState) {
visibleStartOfSelection = createVisiblePosition(startOfSelection);
}
setEndingSelection(
createVisibleSelection(visibleStartOfSelection, visibleEndOfSelection,
endingSelection().isDirectional()));
setEndingSelection(createVisibleSelection(
SelectionInDOMTree::Builder()
.setAffinity(visibleStartOfSelection.affinity())
.setBaseAndExtentDeprecated(
visibleStartOfSelection.deepEquivalent(),
visibleEndOfSelection.deepEquivalent())
.setIsDirectional(endingSelection().isDirectional())
.build()));
return;
}
......
......@@ -134,6 +134,17 @@ static EphemeralRange expandRangeToSentenceBoundary(
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
SpellChecker* SpellChecker::create(LocalFrame& frame) {
......@@ -444,8 +455,13 @@ void SpellChecker::markMisspellingsAfterTypingCommand(
if (cmd.commandTypeOfOpenCommand() ==
TypingCommand::InsertParagraphSeparator) {
VisiblePosition nextWord = nextWordPosition(start);
VisibleSelection words =
createVisibleSelection(wordStartOfPrevious, endOfWord(nextWord));
// TODO(yosin): We should make |endOfWord()| not to return null position.
VisibleSelection words = createVisibleSelection(
SelectionInDOMTree::Builder()
.setBaseAndExtentDeprecated(wordStartOfPrevious.deepEquivalent(),
endOfWord(nextWord).deepEquivalent())
.setAffinity(wordStartOfPrevious.affinity())
.build());
markMisspellingsAfterLineBreak(words);
return;
}
......@@ -470,8 +486,7 @@ void SpellChecker::markMisspellingsAfterTypingToWord(
TRACE_EVENT0("blink", "SpellChecker::markMisspellingsAfterTypingToWord");
VisibleSelection adjacentWords =
createVisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary),
endOfWord(wordStart, RightWordIfOnBoundary));
createVisibleSelection(selectWord(wordStart));
markMisspellingsAndBadGrammar(adjacentWords);
}
......@@ -868,10 +883,8 @@ void SpellChecker::respondToChangedSelection(
HTMLTextFormControlElement::endOfWord(newStart));
} else {
if (newSelection.isContentEditable()) {
const VisiblePosition newStart(newSelection.visibleStart());
newAdjacentWords =
createVisibleSelection(startOfWord(newStart, LeftWordIfOnBoundary),
endOfWord(newStart, RightWordIfOnBoundary));
createVisibleSelection(selectWord(newSelection.visibleStart()));
}
}
......@@ -929,8 +942,7 @@ void SpellChecker::spellCheckOldSelection(
VisiblePosition oldStart = createVisiblePosition(oldSelectionStart);
VisibleSelection oldAdjacentWords =
createVisibleSelection(startOfWord(oldStart, LeftWordIfOnBoundary),
endOfWord(oldStart, RightWordIfOnBoundary));
createVisibleSelection(selectWord(oldStart));
if (oldAdjacentWords == newAdjacentWords)
return;
markMisspellingsAndBadGrammar(oldAdjacentWords);
......
......@@ -225,8 +225,14 @@ void SVGTextContentElement::selectSubString(unsigned charnum,
for (unsigned i = 0; i < nchars; ++i)
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(
createVisibleSelection(start, end));
SelectionInDOMTree::Builder()
.setBaseAndExtent(start.deepEquivalent(), end.deepEquivalent())
.setAffinity(start.affinity())
.build());
}
bool SVGTextContentElement::isPresentationAttribute(
......
......@@ -1952,7 +1952,10 @@ void AXLayoutObject::setSelection(const AXRange& selection) {
return;
frame->selection().setSelection(
createVisibleSelection(anchorVisiblePosition, focusVisiblePosition));
SelectionInDOMTree::Builder()
.collapse(anchorVisiblePosition.toPositionWithAffinity())
.extend(focusVisiblePosition.deepEquivalent())
.build());
}
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