Commit e1978cb1 authored by Yoichi Osato's avatar Yoichi Osato Committed by Commit Bot

Refactor selection type computing.

This patch moves selection type computing at ComputeVisibleSelection
to SelectionAdjuster for readability.

Bug: 787295
Change-Id: Ic17ed8e63c1e9a0033099afd20bc75ca3149991e
Reviewed-on: https://chromium-review.googlesource.com/1029754
Commit-Queue: Yoichi Osato <yoichio@chromium.org>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Reviewed-by: default avatarXiaocheng Hu <xiaochengh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#557083}
parent f4dcd8e4
......@@ -766,4 +766,67 @@ SelectionAdjuster::AdjustSelectionToAvoidCrossingEditingBoundaries(
return EditingBoundaryAdjuster::AdjustSelection(selection);
}
class SelectionTypeAdjuster final {
STATIC_ONLY(SelectionTypeAdjuster);
public:
template <typename Strategy>
static SelectionTemplate<Strategy> AdjustSelection(
const SelectionTemplate<Strategy>& selection) {
const EphemeralRangeTemplate<Strategy>& range = selection.ComputeRange();
const SelectionType selection_type = ComputeSelectionType(range);
if (selection_type == kCaretSelection) {
return typename SelectionTemplate<Strategy>::Builder()
.Collapse(PositionWithAffinityTemplate<Strategy>(
range.StartPosition(), selection.Affinity()))
.Build();
}
DCHECK_EQ(selection_type, kRangeSelection);
// "Constrain" the selection to be the smallest equivalent range of
// nodes. This is a somewhat arbitrary choice, but experience shows that
// it is useful to make to make the selection "canonical" (if only for
// purposes of comparing selections). This is an ideal point of the code
// to do this operation, since all selection changes that result in a
// RANGE come through here before anyone uses it.
// TODO(editing-dev): Consider this canonicalization is really needed.
const EphemeralRangeTemplate<Strategy> minimal_range(
MostForwardCaretPosition(range.StartPosition()),
MostBackwardCaretPosition(range.EndPosition()));
if (selection.IsBaseFirst()) {
return typename SelectionTemplate<Strategy>::Builder()
.SetAsForwardSelection(minimal_range)
.Build();
}
return typename SelectionTemplate<Strategy>::Builder()
.SetAsBackwardSelection(minimal_range)
.Build();
}
private:
template <typename Strategy>
static SelectionType ComputeSelectionType(
const EphemeralRangeTemplate<Strategy>& range) {
if (range.IsNull())
return kNoSelection;
DCHECK(!NeedsLayoutTreeUpdate(range.StartPosition())) << range;
if (range.IsCollapsed())
return kCaretSelection;
// TODO(editing-dev): Consider this canonicalization is really needed.
if (MostBackwardCaretPosition(range.StartPosition()) ==
MostBackwardCaretPosition(range.EndPosition()))
return kCaretSelection;
return kRangeSelection;
}
};
SelectionInDOMTree SelectionAdjuster::AdjustSelectionType(
const SelectionInDOMTree& selection) {
return SelectionTypeAdjuster::AdjustSelection(selection);
}
SelectionInFlatTree SelectionAdjuster::AdjustSelectionType(
const SelectionInFlatTree& selection) {
return SelectionTypeAdjuster::AdjustSelection(selection);
}
} // namespace blink
......@@ -33,6 +33,8 @@ class CORE_EXPORT SelectionAdjuster final {
const SelectionInDOMTree&);
static SelectionInFlatTree AdjustSelectionToAvoidCrossingEditingBoundaries(
const SelectionInFlatTree&);
static SelectionInDOMTree AdjustSelectionType(const SelectionInDOMTree&);
static SelectionInFlatTree AdjustSelectionType(const SelectionInFlatTree&);
};
} // namespace blink
......
......@@ -99,20 +99,6 @@ VisibleSelectionInFlatTree CreateVisibleSelectionWithGranularity(
granularity);
}
template <typename Strategy>
static SelectionType ComputeSelectionType(
const EphemeralRangeTemplate<Strategy>& range) {
if (range.IsNull())
return kNoSelection;
DCHECK(!NeedsLayoutTreeUpdate(range.StartPosition())) << range;
if (range.IsCollapsed())
return kCaretSelection;
if (MostBackwardCaretPosition(range.StartPosition()) ==
MostBackwardCaretPosition(range.EndPosition()))
return kCaretSelection;
return kRangeSelection;
}
template <typename Strategy>
VisibleSelectionTemplate<Strategy>::VisibleSelectionTemplate(
const VisibleSelectionTemplate<Strategy>& other)
......@@ -268,42 +254,13 @@ static SelectionTemplate<Strategy> ComputeVisibleSelection(
const SelectionTemplate<Strategy>& editing_adjusted_selection =
SelectionAdjuster::AdjustSelectionToAvoidCrossingEditingBoundaries(
shadow_adjusted_selection);
const EphemeralRangeTemplate<Strategy> editing_adjusted_range =
editing_adjusted_selection.ComputeRange();
// TODO(editing-dev): Implement
// const SelectionTemplate<Strategy>& adjusted_selection =
// AdjustSelectionType(editing_adjusted_range);
const SelectionType selection_type =
ComputeSelectionType(editing_adjusted_range);
if (selection_type == kCaretSelection) {
return typename SelectionTemplate<Strategy>::Builder()
.Collapse(PositionWithAffinityTemplate<Strategy>(
editing_adjusted_range.StartPosition(),
passed_selection.Affinity()))
.Build();
}
DCHECK_EQ(selection_type, kRangeSelection);
// "Constrain" the selection to be the smallest equivalent range of
// nodes. This is a somewhat arbitrary choice, but experience shows that
// it is useful to make to make the selection "canonical" (if only for
// purposes of comparing selections). This is an ideal point of the code
// to do this operation, since all selection changes that result in a
// RANGE come through here before anyone uses it.
// TODO(yosin) Canonicalizing is good, but haven't we already done it
// (when we set these two positions to |VisiblePosition|
// |DeepEquivalent()|s above)?
const EphemeralRangeTemplate<Strategy> range(
MostForwardCaretPosition(editing_adjusted_range.StartPosition()),
MostBackwardCaretPosition(editing_adjusted_range.EndPosition()));
if (canonicalized_selection.IsBaseFirst()) {
return typename SelectionTemplate<Strategy>::Builder()
.SetAsForwardSelection(range)
.Build();
}
return typename SelectionTemplate<Strategy>::Builder()
.SetAsBackwardSelection(range)
.Build();
const SelectionTemplate<Strategy>& type_adjusted_selection =
SelectionAdjuster::AdjustSelectionType(
typename SelectionTemplate<Strategy>::Builder(
editing_adjusted_selection)
.SetAffinity(passed_selection.Affinity())
.Build());
return type_adjusted_selection;
}
template <typename Strategy>
......
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