Commit 64c6dc07 authored by Zhuoyu Qian's avatar Zhuoyu Qian Committed by Commit Bot

Introduce ClipboardCommands to split EditorCommand.cpp

This Cl introduces "ClipboardCommands.h", move the declarations of some
static functions related to clipboard to the header file. Make
EditorCommand simpler for improving code health.

Bug: 818552
Signed-off-by: default avatarZhuoyu Qian <zhuoyu.qian@samsung.com>
Change-Id: I2ebbd4ebb6a0ee3cc13f3a928c786a8d6e285bf4
Reviewed-on: https://chromium-review.googlesource.com/952744Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Commit-Queue: Yoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#542052}
parent 045cdc93
......@@ -101,6 +101,7 @@ blink_core_sources("editing") {
"commands/ApplyStyleCommand.h",
"commands/BreakBlockquoteCommand.cpp",
"commands/BreakBlockquoteCommand.h",
"commands/ClipboardCommands.h",
"commands/CompositeEditCommand.cpp",
"commands/CompositeEditCommand.h",
"commands/CreateLinkCommand.cpp",
......
/*
* Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
* Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
* Copyright (C) 2009 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// 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 ClipboardCommands_h
#define ClipboardCommands_h
#include "core/clipboard/DataTransferAccessPolicy.h"
#include "core/editing/Forward.h"
#include "platform/wtf/Allocator.h"
#include "platform/wtf/text/WTFString.h"
namespace blink {
class DocumentFragment;
class Element;
class Event;
class LocalFrame;
class Pasteboard;
enum class EditorCommandSource;
enum class PasteMode;
// This class provides static functions about commands related to clipboard.
class ClipboardCommands {
STATIC_ONLY(ClipboardCommands);
public:
static bool EnabledCopy(LocalFrame&, Event*, EditorCommandSource);
static bool EnabledCut(LocalFrame&, Event*, EditorCommandSource);
static bool EnabledPaste(LocalFrame&, Event*, EditorCommandSource);
// Returns |bool| value for Document#execCommand().
static bool ExecuteCopy(LocalFrame&,
Event*,
EditorCommandSource,
const String&);
static bool ExecuteCut(LocalFrame&,
Event*,
EditorCommandSource,
const String&);
static bool ExecutePaste(LocalFrame&,
Event*,
EditorCommandSource,
const String&);
static bool ExecutePasteGlobalSelection(LocalFrame&,
Event*,
EditorCommandSource,
const String&);
static bool ExecutePasteAndMatchStyle(LocalFrame&,
Event*,
EditorCommandSource,
const String&);
static bool PasteSupported(LocalFrame*);
private:
static bool CanReadClipboard(LocalFrame&, EditorCommandSource);
static bool CanWriteClipboard(LocalFrame&, EditorCommandSource);
static bool CanSmartReplaceWithPasteboard(LocalFrame&, Pasteboard*);
static bool CanDeleteRange(const EphemeralRange&);
static Element* FindEventTargetForClipboardEvent(LocalFrame&,
EditorCommandSource);
// Returns true if Editor should continue with default processing.
static bool DispatchClipboardEvent(LocalFrame&,
const AtomicString&,
DataTransferAccessPolicy,
EditorCommandSource,
PasteMode);
static bool DispatchCopyOrCutEvent(LocalFrame&,
EditorCommandSource,
const AtomicString&);
static bool DispatchPasteEvent(LocalFrame&, PasteMode, EditorCommandSource);
static void WriteSelectionToPasteboard(LocalFrame&);
static void Paste(LocalFrame&, EditorCommandSource);
static void PasteAsFragment(LocalFrame&,
DocumentFragment*,
bool smart_replace,
bool match_style,
EditorCommandSource);
static void PasteAsPlainTextWithPasteboard(LocalFrame&,
Pasteboard*,
EditorCommandSource);
static void PasteWithPasteboard(LocalFrame&,
Pasteboard*,
EditorCommandSource);
};
} // namespace blink
#endif
......@@ -49,6 +49,7 @@
#include "core/editing/SetSelectionOptions.h"
#include "core/editing/VisiblePosition.h"
#include "core/editing/commands/ApplyStyleCommand.h"
#include "core/editing/commands/ClipboardCommands.h"
#include "core/editing/commands/CreateLinkCommand.h"
#include "core/editing/commands/EditingCommandsUtilities.h"
#include "core/editing/commands/EditorCommandNames.h"
......@@ -740,7 +741,8 @@ static bool ExecuteBackColor(LocalFrame& frame,
CSSPropertyBackgroundColor, value);
}
static bool CanWriteClipboard(LocalFrame& frame, EditorCommandSource source) {
bool ClipboardCommands::CanWriteClipboard(LocalFrame& frame,
EditorCommandSource source) {
if (source == EditorCommandSource::kMenuOrKeyBinding)
return true;
Settings* settings = frame.GetSettings();
......@@ -752,7 +754,8 @@ static bool CanWriteClipboard(LocalFrame& frame, EditorCommandSource source) {
return frame.GetContentSettingsClient()->AllowWriteToClipboard(default_value);
}
static Element* FindEventTargetForClipboardEvent(LocalFrame& frame,
Element* ClipboardCommands::FindEventTargetForClipboardEvent(
LocalFrame& frame,
EditorCommandSource source) {
// https://www.w3.org/TR/clipboard-apis/#fire-a-clipboard-event says:
// "Set target to be the element that contains the start of the selection in
......@@ -767,7 +770,7 @@ static Element* FindEventTargetForClipboardEvent(LocalFrame& frame,
}
// Returns true if Editor should continue with default processing.
static bool DispatchClipboardEvent(LocalFrame& frame,
bool ClipboardCommands::DispatchClipboardEvent(LocalFrame& frame,
const AtomicString& event_type,
DataTransferAccessPolicy policy,
EditorCommandSource source,
......@@ -795,7 +798,7 @@ static bool DispatchClipboardEvent(LocalFrame& frame,
return !no_default_processing;
}
static bool DispatchCopyOrCutEvent(LocalFrame& frame,
bool ClipboardCommands::DispatchCopyOrCutEvent(LocalFrame& frame,
EditorCommandSource source,
const AtomicString& event_type) {
// TODO(editing-dev): The use of UpdateStyleAndLayoutIgnorePendingStylesheets
......@@ -815,7 +818,7 @@ static bool CanSmartCopyOrDelete(LocalFrame& frame) {
frame.Selection().Granularity() == TextGranularity::kWord;
}
static void WriteSelectionToPasteboard(LocalFrame& frame) {
void ClipboardCommands::WriteSelectionToPasteboard(LocalFrame& frame) {
const KURL& url = frame.GetDocument()->Url();
const String html = frame.Selection().SelectedHTMLForClipboard();
const String plain_text = frame.SelectedTextForClipboard();
......@@ -823,7 +826,7 @@ static void WriteSelectionToPasteboard(LocalFrame& frame) {
CanSmartCopyOrDelete(frame));
}
static bool ExecuteCopy(LocalFrame& frame,
bool ClipboardCommands::ExecuteCopy(LocalFrame& frame,
Event*,
EditorCommandSource source,
const String&) {
......@@ -881,7 +884,7 @@ static bool ExecuteCreateLink(LocalFrame& frame,
return CreateLinkCommand::Create(*frame.GetDocument(), value)->Apply();
}
static bool CanDeleteRange(const EphemeralRange& range) {
bool ClipboardCommands::CanDeleteRange(const EphemeralRange& range) {
if (range.IsCollapsed())
return false;
......@@ -894,7 +897,7 @@ static bool CanDeleteRange(const EphemeralRange& range) {
return HasEditableStyle(*start_container) && HasEditableStyle(*end_container);
}
static bool ExecuteCut(LocalFrame& frame,
bool ClipboardCommands::ExecuteCut(LocalFrame& frame,
Event*,
EditorCommandSource source,
const String&) {
......@@ -2029,7 +2032,8 @@ static bool ExecuteToggleOverwrite(LocalFrame& frame,
return true;
}
static bool CanReadClipboard(LocalFrame& frame, EditorCommandSource source) {
bool ClipboardCommands::CanReadClipboard(LocalFrame& frame,
EditorCommandSource source) {
if (source == EditorCommandSource::kMenuOrKeyBinding)
return true;
Settings* settings = frame.GetSettings();
......@@ -2042,13 +2046,14 @@ static bool CanReadClipboard(LocalFrame& frame, EditorCommandSource source) {
default_value);
}
static bool CanSmartReplaceWithPasteboard(LocalFrame& frame,
bool ClipboardCommands::CanSmartReplaceWithPasteboard(LocalFrame& frame,
Pasteboard* pasteboard) {
return frame.GetEditor().SmartInsertDeleteEnabled() &&
pasteboard->CanSmartReplace();
}
static void PasteAsPlainTextWithPasteboard(LocalFrame& frame,
void ClipboardCommands::PasteAsPlainTextWithPasteboard(
LocalFrame& frame,
Pasteboard* pasteboard,
EditorCommandSource source) {
Element* const target = FindEventTargetForClipboardEvent(frame, source);
......@@ -2059,7 +2064,7 @@ static void PasteAsPlainTextWithPasteboard(LocalFrame& frame,
CanSmartReplaceWithPasteboard(frame, pasteboard)));
}
static bool DispatchPasteEvent(LocalFrame& frame,
bool ClipboardCommands::DispatchPasteEvent(LocalFrame& frame,
PasteMode paste_mode,
EditorCommandSource source) {
return DispatchClipboardEvent(frame, EventTypeNames::paste,
......@@ -2067,7 +2072,7 @@ static bool DispatchPasteEvent(LocalFrame& frame,
paste_mode);
}
static void PasteAsFragment(LocalFrame& frame,
void ClipboardCommands::PasteAsFragment(LocalFrame& frame,
DocumentFragment* pasting_fragment,
bool smart_replace,
bool match_style,
......@@ -2079,7 +2084,7 @@ static void PasteAsFragment(LocalFrame& frame,
frame.DomWindow(), pasting_fragment, smart_replace, match_style));
}
static void PasteWithPasteboard(LocalFrame& frame,
void ClipboardCommands::PasteWithPasteboard(LocalFrame& frame,
Pasteboard* pasteboard,
EditorCommandSource source) {
DocumentFragment* fragment = nullptr;
......@@ -2123,7 +2128,7 @@ static void PasteWithPasteboard(LocalFrame& frame,
chose_plain_text, source);
}
static void Paste(LocalFrame& frame, EditorCommandSource source) {
void ClipboardCommands::Paste(LocalFrame& frame, EditorCommandSource source) {
DCHECK(frame.GetDocument());
if (!DispatchPasteEvent(frame, PasteMode::kAllMimeTypes, source))
return;
......@@ -2170,7 +2175,7 @@ static void Paste(LocalFrame& frame, EditorCommandSource source) {
source);
}
static bool ExecutePaste(LocalFrame& frame,
bool ClipboardCommands::ExecutePaste(LocalFrame& frame,
Event*,
EditorCommandSource source,
const String&) {
......@@ -2185,7 +2190,7 @@ static bool ExecutePaste(LocalFrame& frame,
return true;
}
static bool ExecutePasteGlobalSelection(LocalFrame& frame,
bool ClipboardCommands::ExecutePasteGlobalSelection(LocalFrame& frame,
Event*,
EditorCommandSource source,
const String&) {
......@@ -2207,7 +2212,7 @@ static bool ExecutePasteGlobalSelection(LocalFrame& frame,
return true;
}
static bool ExecutePasteAndMatchStyle(LocalFrame& frame,
bool ClipboardCommands::ExecutePasteAndMatchStyle(LocalFrame& frame,
Event*,
EditorCommandSource source,
const String&) {
......@@ -2617,7 +2622,7 @@ static bool Supported(LocalFrame*) {
return true;
}
static bool PasteSupported(LocalFrame* frame) {
bool ClipboardCommands::PasteSupported(LocalFrame* frame) {
const Settings* const settings = frame->GetSettings();
const bool default_value = settings &&
settings->GetJavaScriptCanAccessClipboard() &&
......@@ -2690,14 +2695,18 @@ static bool EnableCaretInEditableText(LocalFrame& frame,
// because we allow elements that are not normally selectable to implement
// copy/paste (like divs, or a document body).
static bool EnabledCopy(LocalFrame& frame, Event*, EditorCommandSource source) {
bool ClipboardCommands::EnabledCopy(LocalFrame& frame,
Event*,
EditorCommandSource source) {
if (!CanWriteClipboard(frame, source))
return false;
return !DispatchCopyOrCutEvent(frame, source, EventTypeNames::beforecopy) ||
frame.GetEditor().CanCopy();
}
static bool EnabledCut(LocalFrame& frame, Event*, EditorCommandSource source) {
bool ClipboardCommands::EnabledCut(LocalFrame& frame,
Event*,
EditorCommandSource source) {
if (!CanWriteClipboard(frame, source))
return false;
if (source == EditorCommandSource::kMenuOrKeyBinding &&
......@@ -2749,7 +2758,7 @@ static bool EnabledInRichlyEditableText(LocalFrame& frame,
selection.RootEditableElement();
}
static bool EnabledPaste(LocalFrame& frame,
bool ClipboardCommands::EnabledPaste(LocalFrame& frame,
Event*,
EditorCommandSource source) {
if (!CanReadClipboard(frame, source))
......@@ -3014,15 +3023,15 @@ static const EditorInternalCommand* InternalCommand(
{WebEditingCommandType::kBold, ExecuteToggleBold, Supported,
EnabledInRichlyEditableText, StateBold, ValueStateOrNull,
kNotTextInsertion, kDoNotAllowExecutionWhenDisabled},
{WebEditingCommandType::kCopy, ExecuteCopy, Supported, EnabledCopy,
StateNone, ValueStateOrNull, kNotTextInsertion,
kAllowExecutionWhenDisabled},
{WebEditingCommandType::kCopy, ClipboardCommands::ExecuteCopy, Supported,
ClipboardCommands::EnabledCopy, StateNone, ValueStateOrNull,
kNotTextInsertion, kAllowExecutionWhenDisabled},
{WebEditingCommandType::kCreateLink, ExecuteCreateLink, Supported,
EnabledInRichlyEditableText, StateNone, ValueStateOrNull,
kNotTextInsertion, kDoNotAllowExecutionWhenDisabled},
{WebEditingCommandType::kCut, ExecuteCut, Supported, EnabledCut,
StateNone, ValueStateOrNull, kNotTextInsertion,
kAllowExecutionWhenDisabled},
{WebEditingCommandType::kCut, ClipboardCommands::ExecuteCut, Supported,
ClipboardCommands::EnabledCut, StateNone, ValueStateOrNull,
kNotTextInsertion, kAllowExecutionWhenDisabled},
{WebEditingCommandType::kDefaultParagraphSeparator,
ExecuteDefaultParagraphSeparator, Supported, Enabled, StateNone,
ValueDefaultParagraphSeparator, kNotTextInsertion,
......@@ -3346,14 +3355,17 @@ static const EditorInternalCommand* InternalCommand(
{WebEditingCommandType::kOverWrite, ExecuteToggleOverwrite,
SupportedFromMenuOrKeyBinding, EnabledInRichlyEditableText, StateNone,
ValueStateOrNull, kNotTextInsertion, kDoNotAllowExecutionWhenDisabled},
{WebEditingCommandType::kPaste, ExecutePaste, PasteSupported,
EnabledPaste, StateNone, ValueStateOrNull, kNotTextInsertion,
kAllowExecutionWhenDisabled},
{WebEditingCommandType::kPasteAndMatchStyle, ExecutePasteAndMatchStyle,
Supported, EnabledPaste, StateNone, ValueStateOrNull, kNotTextInsertion,
{WebEditingCommandType::kPaste, ClipboardCommands::ExecutePaste,
ClipboardCommands::PasteSupported, ClipboardCommands::EnabledPaste,
StateNone, ValueStateOrNull, kNotTextInsertion,
kAllowExecutionWhenDisabled},
{WebEditingCommandType::kPasteAndMatchStyle,
ClipboardCommands::ExecutePasteAndMatchStyle, Supported,
ClipboardCommands::EnabledPaste, StateNone, ValueStateOrNull,
kNotTextInsertion, kAllowExecutionWhenDisabled},
{WebEditingCommandType::kPasteGlobalSelection,
ExecutePasteGlobalSelection, SupportedFromMenuOrKeyBinding, EnabledPaste,
ClipboardCommands::ExecutePasteGlobalSelection,
SupportedFromMenuOrKeyBinding, ClipboardCommands::EnabledPaste,
StateNone, ValueStateOrNull, kNotTextInsertion,
kAllowExecutionWhenDisabled},
{WebEditingCommandType::kPrint, ExecutePrint, Supported, Enabled,
......
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