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(
// |VisibleSelection| or create a new class for editing to use that can
// manipulate selections that are not currently valid.
template <typename Strategy>
void VisibleSelectionTemplate<Strategy>::SetWithoutValidation(
VisibleSelectionTemplate<Strategy>
VisibleSelectionTemplate<Strategy>::CreateWithoutValidationDeprecated(
const PositionTemplate<Strategy>& base,
const PositionTemplate<Strategy>& extent) {
if (base.IsNull() || extent.IsNull()) {
base_ = extent_ = start_ = end_ = PositionTemplate<Strategy>();
UpdateSelectionType();
return;
}
base_ = base;
extent_ = extent;
base_is_first_ = base.CompareTo(extent) <= 0;
if (base_is_first_) {
start_ = base;
end_ = extent;
const PositionTemplate<Strategy>& extent,
TextAffinity affinity) {
DCHECK(base.IsNotNull());
DCHECK(extent.IsNotNull());
VisibleSelectionTemplate<Strategy> visible_selection;
visible_selection.base_ = base;
visible_selection.extent_ = extent;
visible_selection.base_is_first_ = base.CompareTo(extent) <= 0;
if (visible_selection.base_is_first_) {
visible_selection.start_ = base;
visible_selection.end_ = extent;
} else {
start_ = extent;
end_ = base;
visible_selection.start_ = extent;
visible_selection.end_ = base;
}
selection_type_ = base == extent ? kCaretSelection : kRangeSelection;
if (selection_type_ != kCaretSelection) {
// Since |m_affinity| for non-|CaretSelection| is always |Downstream|,
// we should keep this invariant. Note: This function can be called with
// |m_affinity| is |TextAffinity::Upstream|.
affinity_ = TextAffinity::kDownstream;
if (base == extent) {
visible_selection.selection_type_ = kCaretSelection;
visible_selection.affinity_ = affinity;
return visible_selection;
}
// 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>
......
......@@ -125,8 +125,14 @@ class CORE_TEMPLATE_CLASS_EXPORT VisibleSelectionTemplate {
bool IsContentRichlyEditable() 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();
......
......@@ -809,10 +809,12 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity,
1) {
// If there are multiple Unicode code points to be deleted, adjust the
// range to match platform conventions.
selection_to_delete.SetWithoutValidation(
selection_to_delete.End(),
PreviousPositionOf(selection_to_delete.End(),
PositionMoveType::kBackwardDeletion));
selection_to_delete =
VisibleSelection::CreateWithoutValidationDeprecated(
selection_to_delete.End(),
PreviousPositionOf(selection_to_delete.End(),
PositionMoveType::kBackwardDeletion),
selection_to_delete.Affinity());
}
if (!StartingSelection().IsRange() ||
......@@ -823,8 +825,10 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity,
// 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
// current state of the document and we'll get the wrong result.
selection_after_undo.SetWithoutValidation(StartingSelection().End(),
selection_to_delete.Extent());
selection_after_undo =
VisibleSelection::CreateWithoutValidationDeprecated(
StartingSelection().End(), selection_to_delete.Extent(),
selection_after_undo.Affinity());
}
break;
}
......@@ -955,8 +959,10 @@ void TypingCommand::ForwardDeleteKeyPressed(TextGranularity granularity,
extent.ComputeContainerNode(),
extent.ComputeOffsetInContainerNode() + extra_characters);
}
selection_after_undo.SetWithoutValidation(StartingSelection().Start(),
extent);
selection_after_undo =
VisibleSelection::CreateWithoutValidationDeprecated(
StartingSelection().Start(), extent,
selection_after_undo.Affinity());
}
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