Commit 50885f55 authored by Yoshifumi Inoue's avatar Yoshifumi Inoue Committed by Commit Bot

Introduce DeleteSelectionOptions for DeleteSelectionCommand construction

This patch introduces |DeleteSelectionOptions| to avoid |bool| parameters of
|DeleteSelectionCommand| construction for improving code health.

Following patch will utilize |DeleteSelectionOptions| for
|CompositeEditCommand::DeleteSelection()|[1] and change member variables
in |DeleteSelectionCommand| class.

[1] http://crrev.com/c/923615 Utilize DeleteSelectionOptions in

CompositeEditCommand: :DeleteSelection()
Change-Id: I84d036d109199287df250409ce99901e7bb1e589
Reviewed-on: https://chromium-review.googlesource.com/925125
Commit-Queue: Yoshifumi Inoue <yosin@chromium.org>
Reviewed-by: default avatarYoichi Osato <yoichio@chromium.org>
Cr-Commit-Position: refs/heads/master@{#537728}
parent 35d9f298
......@@ -109,6 +109,8 @@ blink_core_sources("editing") {
"commands/DeleteFromTextNodeCommand.h",
"commands/DeleteSelectionCommand.cpp",
"commands/DeleteSelectionCommand.h",
"commands/DeleteSelectionOptions.cpp",
"commands/DeleteSelectionOptions.h",
"commands/DocumentExecCommand.cpp",
"commands/DragAndDropCommand.cpp",
"commands/DragAndDropCommand.h",
......
......@@ -388,13 +388,15 @@ void Editor::DeleteSelectionWithSmartDelete(
.IsNone())
return;
const bool kMergeBlocksAfterDelete = true;
const bool kExpandForSpecialElements = false;
const bool kSanitizeMarkup = true;
DCHECK(GetFrame().GetDocument());
DeleteSelectionCommand::Create(
*GetFrame().GetDocument(), delete_mode == DeleteMode::kSmart,
kMergeBlocksAfterDelete, kExpandForSpecialElements, kSanitizeMarkup,
*GetFrame().GetDocument(),
DeleteSelectionOptions::Builder()
.SetSmartDelete(delete_mode == DeleteMode::kSmart)
.SetMergeBlocksAfterDelete(true)
.SetExpandForSpecialElements(true)
.SetSanitizeMarkup(true)
.Build(),
input_type, reference_move_position)
->Apply();
}
......
......@@ -622,8 +622,13 @@ bool CompositeEditCommand::DeleteSelection(EditingState* editing_state,
ApplyCommandToComposite(
DeleteSelectionCommand::Create(
GetDocument(), smart_delete, merge_blocks_after_delete,
expand_for_special_elements, sanitize_markup),
GetDocument(),
DeleteSelectionOptions::Builder()
.SetSmartDelete(smart_delete)
.SetMergeBlocksAfterDelete(merge_blocks_after_delete)
.SetExpandForSpecialElements(expand_for_special_elements)
.SetSanitizeMarkup(sanitize_markup)
.Build()),
editing_state);
if (editing_state->IsAborted())
return false;
......
......@@ -78,21 +78,18 @@ static bool CanMergeListElements(Element* first_list, Element* second_list) {
DeleteSelectionCommand::DeleteSelectionCommand(
Document& document,
bool smart_delete,
bool merge_blocks_after_delete,
bool expand_for_special_elements,
bool sanitize_markup,
const DeleteSelectionOptions& options,
InputEvent::InputType input_type,
const Position& reference_move_position)
: CompositeEditCommand(document),
has_selection_to_delete_(false),
smart_delete_(smart_delete),
merge_blocks_after_delete_(merge_blocks_after_delete),
smart_delete_(options.IsSmartDelete()),
merge_blocks_after_delete_(options.IsMergeBlocksAfterDelete()),
need_placeholder_(false),
expand_for_special_elements_(expand_for_special_elements),
expand_for_special_elements_(options.IsExpandForSpecialElements()),
prune_start_block_if_necessary_(false),
starts_at_empty_line_(false),
sanitize_markup_(sanitize_markup),
sanitize_markup_(options.IsSanitizeMarkup()),
input_type_(input_type),
reference_move_position_(reference_move_position),
start_block_(nullptr),
......@@ -102,20 +99,17 @@ DeleteSelectionCommand::DeleteSelectionCommand(
DeleteSelectionCommand::DeleteSelectionCommand(
const VisibleSelection& selection,
bool smart_delete,
bool merge_blocks_after_delete,
bool expand_for_special_elements,
bool sanitize_markup,
const DeleteSelectionOptions& options,
InputEvent::InputType input_type)
: CompositeEditCommand(*selection.Start().GetDocument()),
has_selection_to_delete_(true),
smart_delete_(smart_delete),
merge_blocks_after_delete_(merge_blocks_after_delete),
smart_delete_(options.IsSmartDelete()),
merge_blocks_after_delete_(options.IsMergeBlocksAfterDelete()),
need_placeholder_(false),
expand_for_special_elements_(expand_for_special_elements),
expand_for_special_elements_(options.IsExpandForSpecialElements()),
prune_start_block_if_necessary_(false),
starts_at_empty_line_(false),
sanitize_markup_(sanitize_markup),
sanitize_markup_(options.IsSanitizeMarkup()),
input_type_(input_type),
selection_to_delete_(selection),
start_block_(nullptr),
......
......@@ -28,6 +28,7 @@
#include "core/editing/VisibleSelection.h"
#include "core/editing/commands/CompositeEditCommand.h"
#include "core/editing/commands/DeleteSelectionOptions.h"
namespace blink {
......@@ -38,44 +39,28 @@ class CORE_EXPORT DeleteSelectionCommand final : public CompositeEditCommand {
public:
static DeleteSelectionCommand* Create(
Document& document,
bool smart_delete = false,
bool merge_blocks_after_delete = true,
bool expand_for_special_elements = false,
bool sanitize_markup = true,
const DeleteSelectionOptions& options,
InputEvent::InputType input_type = InputEvent::InputType::kNone,
const Position& reference_move_position = Position()) {
return new DeleteSelectionCommand(
document, smart_delete, merge_blocks_after_delete,
expand_for_special_elements, sanitize_markup, input_type,
reference_move_position);
return new DeleteSelectionCommand(document, options, input_type,
reference_move_position);
}
static DeleteSelectionCommand* Create(
const VisibleSelection& selection,
bool smart_delete = false,
bool merge_blocks_after_delete = true,
bool expand_for_special_elements = false,
bool sanitize_markup = true,
const DeleteSelectionOptions& options,
InputEvent::InputType input_type = InputEvent::InputType::kNone) {
return new DeleteSelectionCommand(
selection, smart_delete, merge_blocks_after_delete,
expand_for_special_elements, sanitize_markup, input_type);
return new DeleteSelectionCommand(selection, options, input_type);
}
virtual void Trace(blink::Visitor*);
private:
DeleteSelectionCommand(Document&,
bool smart_delete,
bool merge_blocks_after_delete,
bool expand_for_special_elements,
bool santize_markup,
const DeleteSelectionOptions&,
InputEvent::InputType,
const Position& reference_move_position);
DeleteSelectionCommand(const VisibleSelection&,
bool smart_delete,
bool merge_blocks_after_delete,
bool expand_for_special_elements,
bool sanitize_markup,
const DeleteSelectionOptions&,
InputEvent::InputType);
void DoApply(EditingState*) override;
......
......@@ -43,14 +43,13 @@ TEST_F(DeleteSelectionCommandTest, deleteListFromTable) {
.Extend(Position(table, PositionAnchorType::kAfterAnchor))
.Build());
const bool kNoSmartDelete = false;
const bool kMergeBlocksAfterDelete = true;
const bool kNoExpandForSpecialElements = false;
const bool kSanitizeMarkup = true;
DeleteSelectionCommand* command = DeleteSelectionCommand::Create(
GetDocument(), kNoSmartDelete, kMergeBlocksAfterDelete,
kNoExpandForSpecialElements, kSanitizeMarkup,
InputEvent::InputType::kDeleteByCut);
DeleteSelectionCommand* command =
DeleteSelectionCommand::Create(GetDocument(),
DeleteSelectionOptions::Builder()
.SetMergeBlocksAfterDelete(true)
.SetSanitizeMarkup(true)
.Build(),
InputEvent::InputType::kDeleteByCut);
EXPECT_TRUE(command->Apply()) << "the delete command should have succeeded";
EXPECT_EQ("<div contenteditable=\"true\"><br></div>",
......@@ -67,13 +66,11 @@ TEST_F(DeleteSelectionCommandTest, ForwardDeleteWithFirstLetter) {
Selection().SetSelectionAndEndTyping(
SetSelectionTextToBody("<p contenteditable>a^b|c</p>"));
const bool kNoSmartDelete = false;
const bool kMergeBlocksAfterDelete = true;
const bool kNoExpandForSpecialElements = false;
const bool kSanitizeMarkup = true;
DeleteSelectionCommand& command = *DeleteSelectionCommand::Create(
GetDocument(), kNoSmartDelete, kMergeBlocksAfterDelete,
kNoExpandForSpecialElements, kSanitizeMarkup);
GetDocument(), DeleteSelectionOptions::Builder()
.SetMergeBlocksAfterDelete(true)
.SetSanitizeMarkup(true)
.Build());
EXPECT_TRUE(command.Apply()) << "the delete command should have succeeded";
EXPECT_EQ("<p contenteditable>a|c</p>",
GetSelectionTextFromBody(Selection().GetSelectionInDOMTree()));
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/editing/commands/DeleteSelectionOptions.h"
namespace blink {
DeleteSelectionOptions::DeleteSelectionOptions(const DeleteSelectionOptions&) =
default;
DeleteSelectionOptions::DeleteSelectionOptions() = default;
bool DeleteSelectionOptions::IsExpandForSpecialElements() const {
return is_expand_for_special_elements_;
}
bool DeleteSelectionOptions::IsMergeBlocksAfterDelete() const {
return is_merge_blocks_after_delete_;
}
bool DeleteSelectionOptions::IsSanitizeMarkup() const {
return is_sanitize_markup_;
}
bool DeleteSelectionOptions::IsSmartDelete() const {
return is_smart_delete_;
}
// static
DeleteSelectionOptions DeleteSelectionOptions::NormalDelete() {
return Builder()
.SetMergeBlocksAfterDelete(true)
.SetExpandForSpecialElements(true)
.SetSanitizeMarkup(true)
.Build();
}
DeleteSelectionOptions DeleteSelectionOptions::SmartDelete() {
return Builder()
.SetSmartDelete(true)
.SetMergeBlocksAfterDelete(true)
.SetExpandForSpecialElements(true)
.SetSanitizeMarkup(true)
.Build();
}
// ----
DeleteSelectionOptions::Builder::Builder() = default;
DeleteSelectionOptions DeleteSelectionOptions::Builder::Build() const {
return options_;
}
DeleteSelectionOptions::Builder&
DeleteSelectionOptions::Builder::SetExpandForSpecialElements(bool value) {
options_.is_expand_for_special_elements_ = value;
return *this;
}
DeleteSelectionOptions::Builder&
DeleteSelectionOptions::Builder::SetMergeBlocksAfterDelete(bool value) {
options_.is_merge_blocks_after_delete_ = value;
return *this;
}
DeleteSelectionOptions::Builder&
DeleteSelectionOptions::Builder::SetSanitizeMarkup(bool value) {
options_.is_sanitize_markup_ = value;
return *this;
}
DeleteSelectionOptions::Builder&
DeleteSelectionOptions::Builder::SetSmartDelete(bool value) {
options_.is_smart_delete_ = value;
return *this;
}
} // namespace blink
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef DeleteSelectionOptions_h
#define DeleteSelectionOptions_h
#include "base/macros.h"
#include "core/CoreExport.h"
#include "platform/wtf/Allocator.h"
namespace blink {
// DeleteSelectionOptions of |DeleteSelectionCommand|.
class CORE_EXPORT DeleteSelectionOptions final {
DISALLOW_NEW();
public:
class Builder;
DeleteSelectionOptions(const DeleteSelectionOptions&);
bool IsExpandForSpecialElements() const;
bool IsMergeBlocksAfterDelete() const;
bool IsSanitizeMarkup() const;
bool IsSmartDelete() const;
static DeleteSelectionOptions NormalDelete();
static DeleteSelectionOptions SmartDelete();
private:
DeleteSelectionOptions();
bool is_expand_for_special_elements_ = false;
bool is_merge_blocks_after_delete_ = false;
bool is_sanitize_markup_ = false;
bool is_smart_delete_ = false;
};
// Build |DeleteSelectionCommand::Options|.
class CORE_EXPORT DeleteSelectionOptions::Builder final {
DISALLOW_NEW();
public:
Builder();
DeleteSelectionOptions Build() const;
Builder& SetExpandForSpecialElements(bool);
Builder& SetMergeBlocksAfterDelete(bool);
Builder& SetSanitizeMarkup(bool);
Builder& SetSmartDelete(bool);
private:
DeleteSelectionOptions options_;
DISALLOW_COPY_AND_ASSIGN(Builder);
};
} // namespace blink
#endif // DeleteSelectionOptions_h
......@@ -41,6 +41,7 @@
#include "core/editing/VisibleUnits.h"
#include "core/editing/commands/BreakBlockquoteCommand.h"
#include "core/editing/commands/DeleteSelectionCommand.h"
#include "core/editing/commands/DeleteSelectionOptions.h"
#include "core/editing/commands/EditingCommandsUtilities.h"
#include "core/editing/commands/InsertIncrementalTextCommand.h"
#include "core/editing/commands/InsertLineBreakCommand.h"
......@@ -207,12 +208,13 @@ void TypingCommand::DeleteSelectionIfRange(const VisibleSelection& selection,
EditingState* editing_state) {
if (!selection.IsRange())
return;
const bool kMergeBlocksAfterDelete = true;
const bool kExpandForSpecialElements = true;
const bool kSanitizeMarkup = true;
ApplyCommandToComposite(DeleteSelectionCommand::Create(
selection, smart_delete_, kMergeBlocksAfterDelete,
kExpandForSpecialElements, kSanitizeMarkup),
selection, DeleteSelectionOptions::Builder()
.SetSmartDelete(smart_delete_)
.SetMergeBlocksAfterDelete(true)
.SetExpandForSpecialElements(true)
.SetSanitizeMarkup(true)
.Build()),
editing_state);
}
......
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