Commit 14d17b46 authored by Yoshifumi Inoue's avatar Yoshifumi Inoue Committed by Commit Bot

Make VisibleSelectionTemplate::AdjustSelectionToAvoidCrossingEditingBoundaries() as static function

This patch makes |AdjustSelectionToAvoidCrossingEditingBoundaries()| in
|VisibleSelectionTemplate| as static function to avoid modifying member
variables, |start_| and |end_|, of |VisibleSelectionTemplate| for improve
readability and a preparation of removing |start_| and |end_|.

Bug: 230267
Change-Id: I1f5824f71ba115d3a11a3c534cf2f5024e715bbc
Reviewed-on: https://chromium-review.googlesource.com/578908
Commit-Queue: Yoichi Osato <yoichio@chromium.org>
Reviewed-by: default avatarYoichi Osato <yoichio@chromium.org>
Cr-Commit-Position: refs/heads/master@{#488591}
parent b05f97c3
...@@ -448,6 +448,14 @@ void VisibleSelectionTemplate<Strategy>::UpdateSelectionType() { ...@@ -448,6 +448,14 @@ void VisibleSelectionTemplate<Strategy>::UpdateSelectionType() {
affinity_ = TextAffinity::kDownstream; affinity_ = TextAffinity::kDownstream;
} }
// TODO(editing-dev): Once we move all static functions into anonymous
// namespace, we should get rid of this forward declaration.
template <typename Strategy>
static EphemeralRangeTemplate<Strategy>
AdjustSelectionToAvoidCrossingEditingBoundaries(
const EphemeralRangeTemplate<Strategy>&,
const PositionTemplate<Strategy>& base);
template <typename Strategy> template <typename Strategy>
void VisibleSelectionTemplate<Strategy>::Validate( void VisibleSelectionTemplate<Strategy>::Validate(
const SelectionTemplate<Strategy>& passed_selection, const SelectionTemplate<Strategy>& passed_selection,
...@@ -492,7 +500,11 @@ void VisibleSelectionTemplate<Strategy>::Validate( ...@@ -492,7 +500,11 @@ void VisibleSelectionTemplate<Strategy>::Validate(
EphemeralRangeTemplate<Strategy>(start_, end_)); EphemeralRangeTemplate<Strategy>(start_, end_));
} }
AdjustSelectionToAvoidCrossingEditingBoundaries(); const EphemeralRangeTemplate<Strategy> editing_adjusted_range =
AdjustSelectionToAvoidCrossingEditingBoundaries(
EphemeralRangeTemplate<Strategy>(start_, end_), base_);
start_ = editing_adjusted_range.StartPosition();
end_ = editing_adjusted_range.EndPosition();
UpdateSelectionType(); UpdateSelectionType();
if (GetSelectionType() == kRangeSelection) { if (GetSelectionType() == kRangeSelection) {
...@@ -653,22 +665,24 @@ PositionTemplate<Strategy> AdjustSelectionStartToAvoidCrossingEditingBoundaries( ...@@ -653,22 +665,24 @@ PositionTemplate<Strategy> AdjustSelectionStartToAvoidCrossingEditingBoundaries(
} }
template <typename Strategy> template <typename Strategy>
void VisibleSelectionTemplate< static EphemeralRangeTemplate<Strategy>
Strategy>::AdjustSelectionToAvoidCrossingEditingBoundaries() { AdjustSelectionToAvoidCrossingEditingBoundaries(
if (base_.IsNull() || start_.IsNull() || end_.IsNull()) const EphemeralRangeTemplate<Strategy>& range,
return; const PositionTemplate<Strategy>& base) {
DCHECK(base.IsNotNull());
DCHECK(range.IsNotNull());
ContainerNode* base_root = HighestEditableRoot(base_); ContainerNode* base_root = HighestEditableRoot(base);
ContainerNode* start_root = HighestEditableRoot(start_); ContainerNode* start_root = HighestEditableRoot(range.StartPosition());
ContainerNode* end_root = HighestEditableRoot(end_); ContainerNode* end_root = HighestEditableRoot(range.EndPosition());
Element* base_editable_ancestor = Element* base_editable_ancestor =
LowestEditableAncestor(base_.ComputeContainerNode()); LowestEditableAncestor(base.ComputeContainerNode());
// The base, start and end are all in the same region. No adjustment // The base, start and end are all in the same region. No adjustment
// necessary. // necessary.
if (base_root == start_root && base_root == end_root) if (base_root == start_root && base_root == end_root)
return; return range;
// The selection is based in editable content. // The selection is based in editable content.
if (base_root) { if (base_root) {
...@@ -677,49 +691,53 @@ void VisibleSelectionTemplate< ...@@ -677,49 +691,53 @@ void VisibleSelectionTemplate<
// If the start is in non-editable content that is inside the base's // If the start is in non-editable content that is inside the base's
// editable root, put it at the first editable position after start inside // editable root, put it at the first editable position after start inside
// the base's editable root. // the base's editable root.
PositionTemplate<Strategy> start = range.StartPosition();
if (start_root != base_root) { if (start_root != base_root) {
const VisiblePositionTemplate<Strategy> first = const VisiblePositionTemplate<Strategy> first =
FirstEditableVisiblePositionAfterPositionInRoot(start_, *base_root); FirstEditableVisiblePositionAfterPositionInRoot(start, *base_root);
start_ = first.DeepEquivalent(); start = first.DeepEquivalent();
if (start_.IsNull()) { if (start.IsNull()) {
NOTREACHED(); NOTREACHED();
start_ = end_; return {};
} }
} }
// If the end is outside the base's editable root, cap it at the end of that // If the end is outside the base's editable root, cap it at the end of that
// root. // root.
// If the end is in non-editable content that is inside the base's root, put // If the end is in non-editable content that is inside the base's root, put
// it at the last editable position before the end inside the base's root. // it at the last editable position before the end inside the base's root.
PositionTemplate<Strategy> end = range.EndPosition();
if (end_root != base_root) { if (end_root != base_root) {
const VisiblePositionTemplate<Strategy> last = const VisiblePositionTemplate<Strategy> last =
LastEditableVisiblePositionBeforePositionInRoot(end_, *base_root); LastEditableVisiblePositionBeforePositionInRoot(end, *base_root);
end_ = last.DeepEquivalent(); end = last.DeepEquivalent();
if (end_.IsNull()) if (end.IsNull())
end_ = start_; end = start;
} }
// The selection is based in non-editable content. return {start, end};
} else { } else {
// The selection is based in non-editable content.
// FIXME: Non-editable pieces inside editable content should be atomic, in // FIXME: Non-editable pieces inside editable content should be atomic, in
// the same way that editable pieces in non-editable content are atomic. // the same way that editable pieces in non-editable content are atomic.
end_ = AdjustSelectionEndToAvoidCrossingEditingBoundaries( const PositionTemplate<Strategy>& end =
end_, end_root, base_editable_ancestor); AdjustSelectionEndToAvoidCrossingEditingBoundaries(
if (end_.IsNull()) { range.EndPosition(), end_root, base_editable_ancestor);
if (end.IsNull()) {
// The selection crosses an Editing boundary. This is a // The selection crosses an Editing boundary. This is a
// programmer error in the editing code. Happy debugging! // programmer error in the editing code. Happy debugging!
NOTREACHED(); NOTREACHED();
*this = VisibleSelectionTemplate<Strategy>(); return {};
return;
} }
start_ = AdjustSelectionStartToAvoidCrossingEditingBoundaries( const PositionTemplate<Strategy>& start =
start_, start_root, base_editable_ancestor); AdjustSelectionStartToAvoidCrossingEditingBoundaries(
if (start_.IsNull()) { range.StartPosition(), start_root, base_editable_ancestor);
if (start.IsNull()) {
// The selection crosses an Editing boundary. This is a // The selection crosses an Editing boundary. This is a
// programmer error in the editing code. Happy debugging! // programmer error in the editing code. Happy debugging!
NOTREACHED(); NOTREACHED();
*this = VisibleSelectionTemplate<Strategy>(); return {};
return;
} }
return {start, end};
} }
} }
......
...@@ -155,7 +155,6 @@ class CORE_TEMPLATE_CLASS_EXPORT VisibleSelectionTemplate { ...@@ -155,7 +155,6 @@ class CORE_TEMPLATE_CLASS_EXPORT VisibleSelectionTemplate {
void Validate(const SelectionTemplate<Strategy>&, TextGranularity); void Validate(const SelectionTemplate<Strategy>&, TextGranularity);
// Support methods for Validate() // Support methods for Validate()
void AdjustSelectionToAvoidCrossingEditingBoundaries();
void UpdateSelectionType(); void UpdateSelectionType();
// We need to store these as Positions because VisibleSelection is // We need to store these as Positions because VisibleSelection is
......
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