Commit a6fe6c1f authored by Shimi Zhang's avatar Shimi Zhang Committed by Commit Bot

[Editing] Refactor DeleteSelectionCommand to not use VisibleSelection

Currently DeleteSelectionCommand accept a VisibleSelection as the
parameter, which is not necessary in many places. This CL converts the
VisibleSelection |selection_to_delete_| to SelectionForUndoStep. So we
could avoid canonicalization.

This enables us to avoiding to use
VisibleSelection::CreateWithoutValidationDeprecated() later.

Bug: 1024738
Change-Id: I4b32507afca4e77338e9f6a83c01b3a30ea3b188
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2013905Reviewed-by: default avatarXiaocheng Hu <xiaochengh@chromium.org>
Commit-Queue: Shimi Zhang <ctzsm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#734513}
parent b821dbe0
...@@ -89,10 +89,10 @@ DeleteSelectionCommand::DeleteSelectionCommand( ...@@ -89,10 +89,10 @@ DeleteSelectionCommand::DeleteSelectionCommand(
reference_move_position_(reference_move_position) {} reference_move_position_(reference_move_position) {}
DeleteSelectionCommand::DeleteSelectionCommand( DeleteSelectionCommand::DeleteSelectionCommand(
const VisibleSelection& selection, const SelectionForUndoStep& selection,
const DeleteSelectionOptions& options, const DeleteSelectionOptions& options,
InputEvent::InputType input_type) InputEvent::InputType input_type)
: CompositeEditCommand(*selection.Start().GetDocument()), : CompositeEditCommand(*selection.Base().GetDocument()),
options_(options), options_(options),
has_selection_to_delete_(true), has_selection_to_delete_(true),
merge_blocks_after_delete_(options.IsMergeBlocksAfterDelete()), merge_blocks_after_delete_(options.IsMergeBlocksAfterDelete()),
...@@ -137,9 +137,10 @@ void DeleteSelectionCommand::InitializeStartEnd(Position& start, ...@@ -137,9 +137,10 @@ void DeleteSelectionCommand::InitializeStartEnd(Position& start,
break; break;
if (CreateVisiblePosition(start).DeepEquivalent() != if (CreateVisiblePosition(start).DeepEquivalent() !=
selection_to_delete_.VisibleStart().DeepEquivalent() || CreateVisiblePosition(selection_to_delete_.Start())
.DeepEquivalent() ||
CreateVisiblePosition(end).DeepEquivalent() != CreateVisiblePosition(end).DeepEquivalent() !=
selection_to_delete_.VisibleEnd().DeepEquivalent()) CreateVisiblePosition(selection_to_delete_.End()).DeepEquivalent())
break; break;
// If we're going to expand to include the startSpecialContainer, it must be // If we're going to expand to include the startSpecialContainer, it must be
...@@ -561,7 +562,8 @@ void DeleteSelectionCommand::DeleteTextFromNode(Text* node, ...@@ -561,7 +562,8 @@ void DeleteSelectionCommand::DeleteTextFromNode(Text* node,
void DeleteSelectionCommand:: void DeleteSelectionCommand::
MakeStylingElementsDirectChildrenOfEditableRootToPreventStyleLoss( MakeStylingElementsDirectChildrenOfEditableRootToPreventStyleLoss(
EditingState* editing_state) { EditingState* editing_state) {
Range* range = CreateRange(selection_to_delete_.ToNormalizedEphemeralRange()); Range* range = CreateRange(CreateVisibleSelection(selection_to_delete_)
.ToNormalizedEphemeralRange());
Node* node = range->FirstNode(); Node* node = range->FirstNode();
while (node && node != range->PastLastNode()) { while (node && node != range->PastLastNode()) {
Node* next_node = NodeTraversal::Next(*node); Node* next_node = NodeTraversal::Next(*node);
...@@ -1020,7 +1022,7 @@ void DeleteSelectionCommand::CalculateTypingStyleAfterDelete() { ...@@ -1020,7 +1022,7 @@ void DeleteSelectionCommand::CalculateTypingStyleAfterDelete() {
} }
void DeleteSelectionCommand::ClearTransientState() { void DeleteSelectionCommand::ClearTransientState() {
selection_to_delete_ = VisibleSelection(); selection_to_delete_ = SelectionForUndoStep();
upstream_start_ = Position(); upstream_start_ = Position();
downstream_start_ = Position(); downstream_start_ = Position();
upstream_end_ = Position(); upstream_end_ = Position();
...@@ -1059,12 +1061,14 @@ void DeleteSelectionCommand::RemoveRedundantBlocks( ...@@ -1059,12 +1061,14 @@ void DeleteSelectionCommand::RemoveRedundantBlocks(
void DeleteSelectionCommand::DoApply(EditingState* editing_state) { void DeleteSelectionCommand::DoApply(EditingState* editing_state) {
// If selection has not been set to a custom selection when the command was // If selection has not been set to a custom selection when the command was
// created, use the current ending selection. // created, use the current ending selection.
if (!has_selection_to_delete_) if (!has_selection_to_delete_) {
selection_to_delete_ = EndingVisibleSelection(); selection_to_delete_ =
SelectionForUndoStep::From(EndingVisibleSelection().AsSelection());
}
if (!selection_to_delete_.IsValidFor(GetDocument()) || if (!selection_to_delete_.IsValidFor(GetDocument()) ||
!selection_to_delete_.IsRange() || !selection_to_delete_.IsRange() ||
!selection_to_delete_.IsContentEditable()) { !IsEditablePosition(selection_to_delete_.Base())) {
// editing/execCommand/delete-non-editable-range-crash.html reaches here. // editing/execCommand/delete-non-editable-range-crash.html reaches here.
return; return;
} }
...@@ -1084,20 +1088,26 @@ void DeleteSelectionCommand::DoApply(EditingState* editing_state) { ...@@ -1084,20 +1088,26 @@ void DeleteSelectionCommand::DoApply(EditingState* editing_state) {
(downstream_end.ComputeContainerNode()->IsTextNode() && (downstream_end.ComputeContainerNode()->IsTextNode() &&
downstream_end.ComputeContainerNode()->parentNode() == downstream_end.ComputeContainerNode()->parentNode() ==
RootEditableElement(*downstream_end.ComputeContainerNode())); RootEditableElement(*downstream_end.ComputeContainerNode()));
VisiblePosition visible_start = CreateVisiblePosition(
selection_to_delete_.Start(),
selection_to_delete_.IsRange() ? TextAffinity::kDownstream : affinity);
VisiblePosition visible_end = CreateVisiblePosition(
selection_to_delete_.End(),
selection_to_delete_.IsRange() ? TextAffinity::kUpstream : affinity);
bool line_break_at_end_of_selection_to_delete = bool line_break_at_end_of_selection_to_delete =
LineBreakExistsAtVisiblePosition(selection_to_delete_.VisibleEnd()); LineBreakExistsAtVisiblePosition(visible_end);
need_placeholder_ = !root_will_stay_open_without_placeholder &&
IsStartOfParagraph(selection_to_delete_.VisibleStart(), need_placeholder_ =
kCanCrossEditingBoundary) && !root_will_stay_open_without_placeholder &&
IsEndOfParagraph(selection_to_delete_.VisibleEnd(), IsStartOfParagraph(visible_start, kCanCrossEditingBoundary) &&
kCanCrossEditingBoundary) && IsEndOfParagraph(visible_end, kCanCrossEditingBoundary) &&
!line_break_at_end_of_selection_to_delete; !line_break_at_end_of_selection_to_delete;
if (need_placeholder_) { if (need_placeholder_) {
// Don't need a placeholder when deleting a selection that starts just // Don't need a placeholder when deleting a selection that starts just
// before a table and ends inside it (we do need placeholders to hold // before a table and ends inside it (we do need placeholders to hold
// open empty cells, but that's handled elsewhere). // open empty cells, but that's handled elsewhere).
if (Element* table = if (Element* table = TableElementJustAfter(visible_start)) {
TableElementJustAfter(selection_to_delete_.VisibleStart())) {
if (selection_to_delete_.End().AnchorNode()->IsDescendantOf(table)) if (selection_to_delete_.End().AnchorNode()->IsDescendantOf(table))
need_placeholder_ = false; need_placeholder_ = false;
} }
......
...@@ -43,7 +43,7 @@ class CORE_EXPORT DeleteSelectionCommand final : public CompositeEditCommand { ...@@ -43,7 +43,7 @@ class CORE_EXPORT DeleteSelectionCommand final : public CompositeEditCommand {
InputEvent::InputType input_type = InputEvent::InputType::kNone, InputEvent::InputType input_type = InputEvent::InputType::kNone,
const Position& reference_move_position = Position()); const Position& reference_move_position = Position());
DeleteSelectionCommand( DeleteSelectionCommand(
const VisibleSelection&, const SelectionForUndoStep&,
const DeleteSelectionOptions&, const DeleteSelectionOptions&,
InputEvent::InputType input_type = InputEvent::InputType::kNone); InputEvent::InputType input_type = InputEvent::InputType::kNone);
...@@ -85,7 +85,7 @@ class CORE_EXPORT DeleteSelectionCommand final : public CompositeEditCommand { ...@@ -85,7 +85,7 @@ class CORE_EXPORT DeleteSelectionCommand final : public CompositeEditCommand {
// This data is transient and should be cleared at the end of the doApply // This data is transient and should be cleared at the end of the doApply
// function. // function.
VisibleSelection selection_to_delete_; SelectionForUndoStep selection_to_delete_;
Position upstream_start_; Position upstream_start_;
Position downstream_start_; Position downstream_start_;
Position upstream_end_; Position upstream_end_;
......
...@@ -205,8 +205,9 @@ void TypingCommand::DeleteSelection(Document& document, Options options) { ...@@ -205,8 +205,9 @@ void TypingCommand::DeleteSelection(Document& document, Options options) {
->Apply(); ->Apply();
} }
void TypingCommand::DeleteSelectionIfRange(const VisibleSelection& selection, void TypingCommand::DeleteSelectionIfRange(
EditingState* editing_state) { const SelectionForUndoStep& selection,
EditingState* editing_state) {
if (!selection.IsRange()) if (!selection.IsRange())
return; return;
// Although the 'selection' to delete is indeed a Range, it may have been // Although the 'selection' to delete is indeed a Range, it may have been
...@@ -785,8 +786,8 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity, ...@@ -785,8 +786,8 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity,
return; return;
if (EndingSelection().IsRange()) { if (EndingSelection().IsRange()) {
DeleteKeyPressedInternal(EndingVisibleSelection(), EndingSelection(), DeleteKeyPressedInternal(EndingSelection(), EndingSelection(), kill_ring,
kill_ring, editing_state); editing_state);
return; return;
} }
...@@ -894,7 +895,7 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity, ...@@ -894,7 +895,7 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity,
if (!StartingSelection().IsRange() || if (!StartingSelection().IsRange() ||
selection_to_delete.Base() != StartingSelection().Start()) { selection_to_delete.Base() != StartingSelection().Start()) {
DeleteKeyPressedInternal( DeleteKeyPressedInternal(
selection_to_delete, SelectionForUndoStep::From(selection_to_delete.AsSelection()),
SelectionForUndoStep::From(selection_to_delete.AsSelection()), SelectionForUndoStep::From(selection_to_delete.AsSelection()),
kill_ring, editing_state); kill_ring, editing_state);
return; return;
...@@ -908,12 +909,13 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity, ...@@ -908,12 +909,13 @@ void TypingCommand::DeleteKeyPressed(TextGranularity granularity,
CreateVisiblePosition(selection_to_delete.Extent()) CreateVisiblePosition(selection_to_delete.Extent())
.DeepEquivalent()) .DeepEquivalent())
.Build(); .Build();
DeleteKeyPressedInternal(selection_to_delete, selection_after_undo, kill_ring, DeleteKeyPressedInternal(
editing_state); SelectionForUndoStep::From(selection_to_delete.AsSelection()),
selection_after_undo, kill_ring, editing_state);
} }
void TypingCommand::DeleteKeyPressedInternal( void TypingCommand::DeleteKeyPressedInternal(
const VisibleSelection& selection_to_delete, const SelectionForUndoStep& selection_to_delete,
const SelectionForUndoStep& selection_after_undo, const SelectionForUndoStep& selection_after_undo,
bool kill_ring, bool kill_ring,
EditingState* editing_state) { EditingState* editing_state) {
...@@ -927,9 +929,10 @@ void TypingCommand::DeleteKeyPressedInternal( ...@@ -927,9 +929,10 @@ void TypingCommand::DeleteKeyPressedInternal(
LocalFrame* frame = GetDocument().GetFrame(); LocalFrame* frame = GetDocument().GetFrame();
DCHECK(frame); DCHECK(frame);
if (kill_ring) if (kill_ring) {
frame->GetEditor().AddToKillRing( frame->GetEditor().AddToKillRing(CreateVisibleSelection(selection_to_delete)
selection_to_delete.ToNormalizedEphemeralRange()); .ToNormalizedEphemeralRange());
}
// On Mac, make undo select everything that has been deleted, unless an undo // On Mac, make undo select everything that has been deleted, unless an undo
// will undo more than just this deletion. // will undo more than just this deletion.
// FIXME: This behaves like TextEdit except for the case where you open with // FIXME: This behaves like TextEdit except for the case where you open with
...@@ -969,7 +972,7 @@ void TypingCommand::ForwardDeleteKeyPressed(TextGranularity granularity, ...@@ -969,7 +972,7 @@ void TypingCommand::ForwardDeleteKeyPressed(TextGranularity granularity,
return; return;
if (EndingSelection().IsRange()) { if (EndingSelection().IsRange()) {
ForwardDeleteKeyPressedInternal(EndingVisibleSelection(), EndingSelection(), ForwardDeleteKeyPressedInternal(EndingSelection(), EndingSelection(),
kill_ring, editing_state); kill_ring, editing_state);
return; return;
} }
...@@ -1041,7 +1044,7 @@ void TypingCommand::ForwardDeleteKeyPressed(TextGranularity granularity, ...@@ -1041,7 +1044,7 @@ void TypingCommand::ForwardDeleteKeyPressed(TextGranularity granularity,
MostBackwardCaretPosition(selection_to_delete.Base()) != MostBackwardCaretPosition(selection_to_delete.Base()) !=
StartingSelection().Start()) { StartingSelection().Start()) {
ForwardDeleteKeyPressedInternal( ForwardDeleteKeyPressedInternal(
selection_to_delete, SelectionForUndoStep::From(selection_to_delete.AsSelection()),
SelectionForUndoStep::From(selection_to_delete.AsSelection()), SelectionForUndoStep::From(selection_to_delete.AsSelection()),
kill_ring, editing_state); kill_ring, editing_state);
return; return;
...@@ -1054,12 +1057,13 @@ void TypingCommand::ForwardDeleteKeyPressed(TextGranularity granularity, ...@@ -1054,12 +1057,13 @@ void TypingCommand::ForwardDeleteKeyPressed(TextGranularity granularity,
ComputeExtentForForwardDeleteUndo(selection_to_delete, ComputeExtentForForwardDeleteUndo(selection_to_delete,
StartingSelection().End())) StartingSelection().End()))
.Build(); .Build();
ForwardDeleteKeyPressedInternal(selection_to_delete, selection_after_undo, ForwardDeleteKeyPressedInternal(
kill_ring, editing_state); SelectionForUndoStep::From(selection_to_delete.AsSelection()),
selection_after_undo, kill_ring, editing_state);
} }
void TypingCommand::ForwardDeleteKeyPressedInternal( void TypingCommand::ForwardDeleteKeyPressedInternal(
const VisibleSelection& selection_to_delete, const SelectionForUndoStep& selection_to_delete,
const SelectionForUndoStep& selection_after_undo, const SelectionForUndoStep& selection_after_undo,
bool kill_ring, bool kill_ring,
EditingState* editing_state) { EditingState* editing_state) {
...@@ -1073,9 +1077,10 @@ void TypingCommand::ForwardDeleteKeyPressedInternal( ...@@ -1073,9 +1077,10 @@ void TypingCommand::ForwardDeleteKeyPressedInternal(
LocalFrame* frame = GetDocument().GetFrame(); LocalFrame* frame = GetDocument().GetFrame();
DCHECK(frame); DCHECK(frame);
if (kill_ring) if (kill_ring) {
frame->GetEditor().AddToKillRing( frame->GetEditor().AddToKillRing(CreateVisibleSelection(selection_to_delete)
selection_to_delete.ToNormalizedEphemeralRange()); .ToNormalizedEphemeralRange());
}
// Make undo select what was deleted on Mac alone // Make undo select what was deleted on Mac alone
if (frame->GetEditor().Behavior().ShouldUndoOfDeleteSelectText()) if (frame->GetEditor().Behavior().ShouldUndoOfDeleteSelectText())
SetStartingSelection(selection_after_undo); SetStartingSelection(selection_after_undo);
......
...@@ -145,15 +145,15 @@ class CORE_EXPORT TypingCommand final : public CompositeEditCommand { ...@@ -145,15 +145,15 @@ class CORE_EXPORT TypingCommand final : public CompositeEditCommand {
bool IsIncrementalInsertion() const { return is_incremental_insertion_; } bool IsIncrementalInsertion() const { return is_incremental_insertion_; }
void DeleteKeyPressedInternal( void DeleteKeyPressedInternal(
const VisibleSelection& selection_to_delete, const SelectionForUndoStep& selection_to_delete,
const SelectionForUndoStep& selection_after_undo, const SelectionForUndoStep& selection_after_undo,
bool kill_ring, bool kill_ring,
EditingState*); EditingState*);
void DeleteSelectionIfRange(const VisibleSelection&, EditingState*); void DeleteSelectionIfRange(const SelectionForUndoStep&, EditingState*);
void ForwardDeleteKeyPressedInternal( void ForwardDeleteKeyPressedInternal(
const VisibleSelection& selection_to_delete, const SelectionForUndoStep& selection_to_delete,
const SelectionForUndoStep& selection_after_undo, const SelectionForUndoStep& selection_after_undo,
bool kill_ring, bool kill_ring,
EditingState*); EditingState*);
......
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