1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
/*
* Copyright (C) 2011 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "WebEditorClient.h"
#include <WebCore/Document.h>
#include <WebCore/Editor.h>
#include <WebCore/EventNames.h>
#include <WebCore/KeyboardEvent.h>
#include <WebCore/LocalFrame.h>
#include <WebCore/PagePasteboardContext.h>
#include <WebCore/Pasteboard.h>
#include <WebCore/PlatformKeyboardEvent.h>
#include <WebCore/TextIterator.h>
#include <WebCore/markup.h>
#include <WebPage.h>
#include <variant>
#include <wtf/glib/GRefPtr.h>
namespace WebKit {
using namespace WebCore;
bool WebEditorClient::handleGtkEditorCommand(LocalFrame& frame, const String& command, bool allowTextInsertion)
{
if (command == "GtkInsertEmoji"_s) {
if (!allowTextInsertion)
return false;
m_page->showEmojiPicker(frame);
return true;
}
return false;
}
bool WebEditorClient::executePendingEditorCommands(LocalFrame& frame, const Vector<WTF::String>& pendingEditorCommands, bool allowTextInsertion)
{
Vector<std::variant<Editor::Command, String>> commands;
for (auto& commandString : pendingEditorCommands) {
if (commandString.startsWith("Gtk"_s))
commands.append(commandString);
else {
Editor::Command command = frame.editor().command(commandString);
if (command.isTextInsertion() && !allowTextInsertion)
return false;
commands.append(WTFMove(command));
}
}
for (auto& commandVariant : commands) {
if (std::holds_alternative<String>(commandVariant)) {
if (!handleGtkEditorCommand(frame, std::get<String>(commandVariant), allowTextInsertion))
return false;
} else {
auto& command = std::get<Editor::Command>(commandVariant);
if (!command.execute())
return false;
}
}
return true;
}
void WebEditorClient::handleKeyboardEvent(KeyboardEvent& event)
{
auto* platformEvent = event.underlyingPlatformEvent();
if (!platformEvent)
return;
// If this was an IME event don't do anything.
if (platformEvent->handledByInputMethod())
return;
ASSERT(event.target());
auto* frame = downcast<Node>(event.target())->document().frame();
ASSERT(frame);
const Vector<String> pendingEditorCommands = platformEvent->commands();
if (!pendingEditorCommands.isEmpty()) {
// During RawKeyDown events if an editor command will insert text, defer
// the insertion until the keypress event. We want keydown to bubble up
// through the DOM first.
if (platformEvent->type() == PlatformEvent::Type::RawKeyDown) {
if (executePendingEditorCommands(*frame, pendingEditorCommands, false))
event.setDefaultHandled();
return;
}
// Only allow text insertion commands if the current node is editable.
if (executePendingEditorCommands(*frame, pendingEditorCommands, frame->editor().canEdit())) {
event.setDefaultHandled();
return;
}
}
// Don't allow text insertion for nodes that cannot edit.
if (!frame->editor().canEdit())
return;
// This is just a normal text insertion, so wait to execute the insertion
// until a keypress event happens. This will ensure that the insertion will not
// be reflected in the contents of the field until the keyup DOM event.
if (event.type() != eventNames().keypressEvent)
return;
// Don't insert null or control characters as they can result in unexpected behaviour
if (event.charCode() < ' ')
return;
// Don't insert anything if a modifier is pressed
if (platformEvent->controlKey() || platformEvent->altKey())
return;
if (frame->editor().insertText(platformEvent->text(), &event))
event.setDefaultHandled();
}
void WebEditorClient::updateGlobalSelection(LocalFrame* frame)
{
if (!frame->selection().isRange())
return;
auto range = frame->selection().selection().toNormalizedRange();
if (!range)
return;
PasteboardWebContent pasteboardContent;
pasteboardContent.canSmartCopyOrDelete = false;
pasteboardContent.text = plainText(*range);
pasteboardContent.markup = serializePreservingVisualAppearance(frame->selection().selection(), ResolveURLs::YesExcludingURLsForPrivacy);
Pasteboard::createForGlobalSelection(PagePasteboardContext::create(frame->pageID()))->write(pasteboardContent);
}
bool WebEditorClient::shouldShowUnicodeMenu()
{
return true;
}
}
|