Commit 2ddd7868 authored by Xiaocheng Hu's avatar Xiaocheng Hu Committed by Commit Bot

Stop using VisiblePosition in GranularityStrategy's NextWordBound

This patch changes the function to use VisiblePosition-free versions
of Start/EndOfWord, and hoists the creation of VisiblePosition to
callers to reduce the usage of VisiblePosition.

Bug: 657237
Change-Id: I8565ab3fa839b79c3e464c8622fc7a75161cc621
Reviewed-on: https://chromium-review.googlesource.com/c/1330755
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: default avatarYoichi Osato <yoichio@chromium.org>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608066}
parent 304bca03
...@@ -40,19 +40,19 @@ static bool ArePositionsInSpecifiedOrder(const VisiblePosition& vp1, ...@@ -40,19 +40,19 @@ static bool ArePositionsInSpecifiedOrder(const VisiblePosition& vp1,
// the direction in which to search for the next bound. nextIfOnBound // the direction in which to search for the next bound. nextIfOnBound
// controls whether |pos| or the next boundary is returned when |pos| is // controls whether |pos| or the next boundary is returned when |pos| is
// located exactly on word boundary. // located exactly on word boundary.
static VisiblePosition NextWordBound(const VisiblePosition& pos, static Position NextWordBound(const Position& pos,
SearchDirection direction, SearchDirection direction,
BoundAdjust word_bound_adjust) { BoundAdjust word_bound_adjust) {
bool next_bound_if_on_bound = bool next_bound_if_on_bound =
word_bound_adjust == BoundAdjust::kNextBoundIfOnBound; word_bound_adjust == BoundAdjust::kNextBoundIfOnBound;
if (direction == SearchDirection::kSearchForward) { if (direction == SearchDirection::kSearchForward) {
EWordSide word_side = next_bound_if_on_bound ? kNextWordIfOnBoundary EWordSide word_side = next_bound_if_on_bound ? kNextWordIfOnBoundary
: kPreviousWordIfOnBoundary; : kPreviousWordIfOnBoundary;
return EndOfWord(pos, word_side); return EndOfWordPosition(pos, word_side);
} }
EWordSide word_side = next_bound_if_on_bound ? kPreviousWordIfOnBoundary EWordSide word_side = next_bound_if_on_bound ? kPreviousWordIfOnBoundary
: kNextWordIfOnBoundary; : kNextWordIfOnBoundary;
return StartOfWord(pos, word_side); return StartOfWordPosition(pos, word_side);
} }
GranularityStrategy::GranularityStrategy() = default; GranularityStrategy::GranularityStrategy() = default;
...@@ -198,32 +198,34 @@ SelectionInDOMTree DirectionGranularityStrategy::UpdateExtent( ...@@ -198,32 +198,34 @@ SelectionInDOMTree DirectionGranularityStrategy::UpdateExtent(
// Determine the word boundary, i.e. the boundary extending beyond which // Determine the word boundary, i.e. the boundary extending beyond which
// should change the granularity to WordGranularity. // should change the granularity to WordGranularity.
VisiblePosition word_boundary; Position word_boundary_position;
if (extent_base_order_switched) { if (extent_base_order_switched) {
// Special case. // Special case.
// If the extent-base order was switched, then the selection is now // If the extent-base order was switched, then the selection is now
// expanding in a different direction than before. Therefore we // expanding in a different direction than before. Therefore we
// calculate the word boundary in this new direction and based on // calculate the word boundary in this new direction and based on
// the |base| position. // the |base| position.
word_boundary = NextWordBound(base, word_boundary_position = NextWordBound(
new_extent_base_order > 0 base.DeepEquivalent(),
? SearchDirection::kSearchForward new_extent_base_order > 0 ? SearchDirection::kSearchForward
: SearchDirection::kSearchBackwards, : SearchDirection::kSearchBackwards,
BoundAdjust::kNextBoundIfOnBound); BoundAdjust::kNextBoundIfOnBound);
granularity_ = TextGranularity::kCharacter; granularity_ = TextGranularity::kCharacter;
} else { } else {
// Calculate the word boundary based on |oldExtentWithGranularity|. // Calculate the word boundary based on |oldExtentWithGranularity|.
// If selection was shrunk in the last update and the extent is now // If selection was shrunk in the last update and the extent is now
// exactly on the word boundary - we need to take the next bound as // exactly on the word boundary - we need to take the next bound as
// the bound of the current word. // the bound of the current word.
word_boundary = NextWordBound(old_offset_extent_position, word_boundary_position = NextWordBound(
old_extent_base_order > 0 old_offset_extent_position.DeepEquivalent(),
? SearchDirection::kSearchForward old_extent_base_order > 0 ? SearchDirection::kSearchForward
: SearchDirection::kSearchBackwards, : SearchDirection::kSearchBackwards,
state_ == StrategyState::kShrinking state_ == StrategyState::kShrinking
? BoundAdjust::kNextBoundIfOnBound ? BoundAdjust::kNextBoundIfOnBound
: BoundAdjust::kCurrentPosIfOnBound); : BoundAdjust::kCurrentPosIfOnBound);
} }
VisiblePosition word_boundary =
CreateVisiblePosition(word_boundary_position);
bool expanded_beyond_word_boundary; bool expanded_beyond_word_boundary;
if (selection_expanded) if (selection_expanded)
...@@ -251,12 +253,12 @@ SelectionInDOMTree DirectionGranularityStrategy::UpdateExtent( ...@@ -251,12 +253,12 @@ SelectionInDOMTree DirectionGranularityStrategy::UpdateExtent(
// Determine the bounds of the word where the extent is located. // Determine the bounds of the word where the extent is located.
// Set the selection extent to one of the two bounds depending on // Set the selection extent to one of the two bounds depending on
// whether the extent is passed the middle of the word. // whether the extent is passed the middle of the word.
VisiblePosition bound_before_extent = NextWordBound( VisiblePosition bound_before_extent = CreateVisiblePosition(NextWordBound(
new_offset_extent_position, SearchDirection::kSearchBackwards, new_offset_extent_position.DeepEquivalent(),
BoundAdjust::kCurrentPosIfOnBound); SearchDirection::kSearchBackwards, BoundAdjust::kCurrentPosIfOnBound));
VisiblePosition bound_after_extent = NextWordBound( VisiblePosition bound_after_extent = CreateVisiblePosition(NextWordBound(
new_offset_extent_position, SearchDirection::kSearchForward, new_offset_extent_position.DeepEquivalent(),
BoundAdjust::kCurrentPosIfOnBound); SearchDirection::kSearchForward, BoundAdjust::kCurrentPosIfOnBound));
int x_middle_between_bounds = (PositionLocation(bound_after_extent).X() + int x_middle_between_bounds = (PositionLocation(bound_after_extent).X() +
PositionLocation(bound_before_extent).X()) / PositionLocation(bound_before_extent).X()) /
2; 2;
......
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