Commit f7e27224 authored by yosin's avatar yosin Committed by Commit Bot

Introduce VisibleSelection::CreateWithoutValidationDeprecated()

This patch introduces |VisibleSelection::CreateWithoutValidationDeprecated()|
as replacement of |SetWithoutValidation()| to make |VisibleSelection| as
virtually immutable for improving code health.

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

Review-Url: https://codereview.chromium.org/2952973002
Cr-Commit-Position: refs/heads/master@{#481799}
parent 1e2af458
...@@ -473,32 +473,36 @@ bool VisibleSelectionTemplate<Strategy>::IsValidFor( ...@@ -473,32 +473,36 @@ bool VisibleSelectionTemplate<Strategy>::IsValidFor(
// |VisibleSelection| or create a new class for editing to use that can // |VisibleSelection| or create a new class for editing to use that can
// manipulate selections that are not currently valid. // manipulate selections that are not currently valid.
template <typename Strategy> template <typename Strategy>
void VisibleSelectionTemplate<Strategy>::SetWithoutValidation( VisibleSelectionTemplate<Strategy>
VisibleSelectionTemplate<Strategy>::CreateWithoutValidationDeprecated(
const PositionTemplate<Strategy>& base, const PositionTemplate<Strategy>& base,
const PositionTemplate<Strategy>& extent) { const PositionTemplate<Strategy>& extent,
if (base.IsNull() || extent.IsNull()) { TextAffinity affinity) {
base_ = extent_ = start_ = end_ = PositionTemplate<Strategy>(); DCHECK(base.IsNotNull());
UpdateSelectionType(); DCHECK(extent.IsNotNull());
return;
} VisibleSelectionTemplate<Strategy> visible_selection;
visible_selection.base_ = base;
base_ = base; visible_selection.extent_ = extent;
extent_ = extent; visible_selection.base_is_first_ = base.CompareTo(extent) <= 0;
base_is_first_ = base.CompareTo(extent) <= 0; if (visible_selection.base_is_first_) {
if (base_is_first_) { visible_selection.start_ = base;
start_ = base; visible_selection.end_ = extent;
end_ = extent;
} else { } else {
start_ = extent; visible_selection.start_ = extent;
end_ = base; visible_selection.end_ = base;
} }
selection_type_ = base == extent ? kCaretSelection : kRangeSelection; if (base == extent) {
if (selection_type_ != kCaretSelection) { visible_selection.selection_type_ = kCaretSelection;
// Since |m_affinity| for non-|CaretSelection| is always |Downstream|, visible_selection.affinity_ = affinity;
// we should keep this invariant. Note: This function can be called with return visible_selection;
// |m_affinity| is |TextAffinity::Upstream|.
affinity_ = TextAffinity::kDownstream;
} }
// Since |affinity_| for non-|CaretSelection| is always |kDownstream|,
// we should keep this invariant. Note: This function can be called with
// |affinity_| is |kUpstream|.
visible_selection.selection_type_ = kRangeSelection;
visible_selection.affinity_ = TextAffinity::kDownstream;
return visible_selection;
} }
template <typename Strategy> template <typename Strategy>
......
...@@ -125,8 +125,14 @@ class CORE_TEMPLATE_CLASS_EXPORT VisibleSelectionTemplate { ...@@ -125,8 +125,14 @@ class CORE_TEMPLATE_CLASS_EXPORT VisibleSelectionTemplate {
bool IsContentRichlyEditable() const; bool IsContentRichlyEditable() const;
bool IsValidFor(const Document&) const; bool IsValidFor(const Document&) const;
void SetWithoutValidation(const PositionTemplate<Strategy>&,
const PositionTemplate<Strategy>&); // TODO(editing-dev): |CreateWithoutValidationDeprecated()| is allowed
// only to use in |TypingCommand| to remove part of grapheme cluster.
// Note: |base| and |extent| can be disconnect position.
static VisibleSelectionTemplate<Strategy> CreateWithoutValidationDeprecated(
const PositionTemplate<Strategy>& base,
const PositionTemplate<Strategy>& extent,
TextAffinity);
DECLARE_TRACE(); DECLARE_TRACE();
......
...@@ -809,10 +809,12 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity, ...@@ -809,10 +809,12 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity,
1) { 1) {
// If there are multiple Unicode code points to be deleted, adjust the // If there are multiple Unicode code points to be deleted, adjust the
// range to match platform conventions. // range to match platform conventions.
selection_to_delete.SetWithoutValidation( selection_to_delete =
VisibleSelection::CreateWithoutValidationDeprecated(
selection_to_delete.End(), selection_to_delete.End(),
PreviousPositionOf(selection_to_delete.End(), PreviousPositionOf(selection_to_delete.End(),
PositionMoveType::kBackwardDeletion)); PositionMoveType::kBackwardDeletion),
selection_to_delete.Affinity());
} }
if (!StartingSelection().IsRange() || if (!StartingSelection().IsRange() ||
...@@ -823,8 +825,10 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity, ...@@ -823,8 +825,10 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity,
// have been in the original document. We can't let the VisibleSelection // have been in the original document. We can't let the VisibleSelection
// class's validation kick in or it'll adjust for us based on the // class's validation kick in or it'll adjust for us based on the
// current state of the document and we'll get the wrong result. // current state of the document and we'll get the wrong result.
selection_after_undo.SetWithoutValidation(StartingSelection().End(), selection_after_undo =
selection_to_delete.Extent()); VisibleSelection::CreateWithoutValidationDeprecated(
StartingSelection().End(), selection_to_delete.Extent(),
selection_after_undo.Affinity());
} }
break; break;
} }
...@@ -955,8 +959,10 @@ void TypingCommand::ForwardDeleteKeyPressed(TextGranularity granularity, ...@@ -955,8 +959,10 @@ void TypingCommand::ForwardDeleteKeyPressed(TextGranularity granularity,
extent.ComputeContainerNode(), extent.ComputeContainerNode(),
extent.ComputeOffsetInContainerNode() + extra_characters); extent.ComputeOffsetInContainerNode() + extra_characters);
} }
selection_after_undo.SetWithoutValidation(StartingSelection().Start(), selection_after_undo =
extent); VisibleSelection::CreateWithoutValidationDeprecated(
StartingSelection().Start(), extent,
selection_after_undo.Affinity());
} }
break; break;
} }
......
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