Skip to content

Truly independent web browser

LibWeb+LibWebView: Only let the browser's own paste read the clipboard

Shannon Boothc369117be921
diff --git a/Libraries/LibWeb/Clipboard/Clipboard.cpp b/Libraries/LibWeb/Clipboard/Clipboard.cpp
index 18acd7fb5d3..551edf244ab 100644
--- a/Libraries/LibWeb/Clipboard/Clipboard.cpp
+++ b/Libraries/LibWeb/Clipboard/Clipboard.cpp
@@ -152,7 +152,7 @@ static void write_blobs_and_option_to_clipboard(JS::Realm& realm, ReadonlySpan<G
}
// https://w3c.github.io/clipboard-apis/#h-clipboard-read-permission
-static bool check_clipboard_read_permission(JS::Realm& realm)
+static bool check_clipboard_read_permission(JS::Realm& realm, bool is_running_from_paste)
{
// NOTE: The clipboard permission is undergoing a refactor because the clipboard-read permission was removed from
// the Permissions spec. So this partially implements the proposed update:
@@ -163,9 +163,10 @@ static bool check_clipboard_read_permission(JS::Realm& realm)
// 2. If hasGesture then,
if (has_gesture) {
- // FIXME: 1. Return true if the current script is running as a result of user interaction with a "Paste" element
- // created by the user agent or operating system.
- return true;
+ // 1. Return true if the current script is running as a result of user interaction with a "Paste" element
+ // created by the user agent or operating system.
+ if (is_running_from_paste)
+ return true;
}
// 3. Otherwise, return false.
@@ -206,10 +207,16 @@ void Clipboard::read(JS::Realm& realm, ClipboardReadOptions formats, GC::Ref<Web
}
}
+ // NB: The steps below run in parallel, after the script that called this has returned. So take now the data that a
+ // paste of the system clipboard is pasting, which a script running as a result of that paste reads.
+ Optional<Vector<SystemClipboardItem>> pasted_data;
+ if (auto const* data = HTML::relevant_window(realm.global_object()).system_clipboard_data_being_pasted())
+ pasted_data = *data;
+
// 4. Run the following steps in parallel:
- Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(GC::Heap::the(), GC::captures(realm, promise, move(formats)), [](auto& realm, auto& promise, auto& formats) {
+ Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(GC::Heap::the(), GC::captures(realm, promise, move(formats), move(pasted_data)), [](auto& realm, auto& promise, auto& formats, auto& pasted_data) {
// 1. Let r be the result of running check clipboard read permission.
- auto result = check_clipboard_read_permission(realm);
+ auto result = check_clipboard_read_permission(realm, pasted_data.has_value());
// 2. If r is false, then:
if (!result) {
@@ -217,15 +224,15 @@ void Clipboard::read(JS::Realm& realm, ClipboardReadOptions formats, GC::Ref<Web
// "NotAllowedError" DOMException in realm.
queue_global_task(HTML::Task::Source::Permissions, realm.global_object(), GC::create_function(GC::Heap::the(), GC::captures(realm, promise), [](auto& realm, auto& promise) {
HTML::TemporaryExecutionContext execution_context { realm };
- WebIDL::reject_promise(promise, WebIDL::NotAllowedError::create("Clipboard reading is only allowed through user activation"_utf16));
+ WebIDL::reject_promise(promise, WebIDL::NotAllowedError::create("Clipboard reading is only allowed while the user pastes"_utf16));
}));
// 2. Abort these steps.
return;
}
- // 3. Let data be the system clipboard data.
- HTML::relevant_window(realm.global_object()).page().request_clipboard_entries(GC::create_function(GC::Heap::the(), GC::captures(realm, promise, move(formats)), [](auto& realm, auto& promise, auto& formats, Vector<SystemClipboardItem> data) {
+ // NB: The steps after step 3 run once the system clipboard data is in hand.
+ auto steps_with_data = GC::create_function(GC::Heap::the(), GC::captures(realm, promise, move(formats)), [](auto& realm, auto& promise, auto& formats, Vector<SystemClipboardItem> data) {
HTML::TemporaryExecutionContext execution_context { realm };
// 4. Let snapshotChangeCount be the current clipboard change count.
@@ -325,17 +332,28 @@ void Clipboard::read(JS::Realm& realm, ClipboardReadOptions formats, GC::Ref<Web
// 3. Resolve p with clipboardItems.
resolve_clipboard_items_promise(realm, promise, clipboard_items->elements());
}));
- }));
+ });
+
+ // 3. Let data be the system clipboard data.
+ // NB: Only a read running as a result of a paste of the system clipboard gets here, and it reads the data being
+ // pasted.
+ steps_with_data->function()(pasted_data.release_value());
}));
}
// https://w3c.github.io/clipboard-apis/#dom-clipboard-readtext
void Clipboard::read_text(JS::Realm& realm, GC::Ref<WebIDL::Promise> promise)
{
+ // NB: The steps below run in parallel, after the script that called this has returned. So take now the data that a
+ // paste of the system clipboard is pasting, which a script running as a result of that paste reads.
+ Optional<Vector<SystemClipboardItem>> pasted_data;
+ if (auto const* data = HTML::relevant_window(realm.global_object()).system_clipboard_data_being_pasted())
+ pasted_data = *data;
+
// 3. Run the following steps in parallel:
- Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(GC::Heap::the(), GC::captures(realm, promise), [](auto& realm, auto& promise) {
+ Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(GC::Heap::the(), GC::captures(realm, promise, move(pasted_data)), [](auto& realm, auto& promise, auto& pasted_data) {
// 1. Let r be the result of running check clipboard read permission.
- auto result = check_clipboard_read_permission(realm);
+ auto result = check_clipboard_read_permission(realm, pasted_data.has_value());
// 2. If r is false, then:
if (!result) {
@@ -343,15 +361,15 @@ void Clipboard::read_text(JS::Realm& realm, GC::Ref<WebIDL::Promise> promise)
// "NotAllowedError" DOMException in realm.
queue_global_task(HTML::Task::Source::Permissions, realm.global_object(), GC::create_function(GC::Heap::the(), GC::captures(realm, promise), [](auto& realm, auto& promise) {
HTML::TemporaryExecutionContext execution_context { realm };
- WebIDL::reject_promise(promise, WebIDL::NotAllowedError::create("Clipboard reading is only allowed through user activation"_utf16));
+ WebIDL::reject_promise(promise, WebIDL::NotAllowedError::create("Clipboard reading is only allowed while the user pastes"_utf16));
}));
// 2. Abort these steps.
return;
}
- // 3. Let data be a copy of the system clipboard data.
- HTML::relevant_window(realm.global_object()).page().request_clipboard_entries(GC::create_function(GC::Heap::the(), GC::captures(realm, promise), [](auto& realm, auto& promise, Vector<SystemClipboardItem> data) {
+ // NB: The steps after step 3 run once the system clipboard data is in hand.
+ auto steps_with_data = GC::create_function(GC::Heap::the(), GC::captures(realm, promise), [](auto& realm, auto& promise, Vector<SystemClipboardItem> data) {
// 4. Queue a global task on the clipboard task source, given realm’s global object, to perform the below steps:
queue_global_task(HTML::Task::Source::Clipboard, realm.global_object(), GC::create_function(GC::Heap::the(), GC::captures(realm, promise, move(data)), [](auto& realm, auto& promise, auto& data) {
HTML::TemporaryExecutionContext execution_context { realm };
@@ -409,7 +427,12 @@ void Clipboard::read_text(JS::Realm& realm, GC::Ref<WebIDL::Promise> promise)
// 2. Reject p with "NotFoundError" DOMException in realm.
WebIDL::reject_promise(promise, WebIDL::NotFoundError::create("Did not find a text item in the system clipboard"_utf16));
}));
- }));
+ });
+
+ // 3. Let data be a copy of the system clipboard data.
+ // NB: Only a read running as a result of a paste of the system clipboard gets here, and it reads the data being
+ // pasted.
+ steps_with_data->function()(pasted_data.release_value());
}));
}
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index ec9b2a99503..ef32b7a6d88 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -6319,14 +6319,14 @@ void LocalNavigable::paste(Utf16View text)
.file_data = {},
.file_name = {},
});
- m_event_handler.perform_paste_action(data_store);
+ m_event_handler.perform_paste_action(data_store, nullptr);
}
-void LocalNavigable::paste_from_clipboard()
+void LocalNavigable::paste_from_clipboard(Vector<Clipboard::SystemClipboardItem> const& system_clipboard_data)
{
- // Run the whole paste action: It retrieves the system clipboard's contents from the UI process itself, which
- // preserves every supported clipboard representation — where a bare-text handover would keep only the text.
- (void)m_event_handler.perform_paste_action();
+ // The UI process hands over the system clipboard's contents with its Paste command, keeping every supported
+ // clipboard representation — where a bare-text handover would keep only the text.
+ (void)m_event_handler.perform_paste_action(system_clipboard_data);
}
void LocalNavigable::undo()
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.h
index 01afc542fce..efe6a0718fe 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalNavigable.h
@@ -281,7 +281,7 @@ public:
Utf16String cut_selected_text() const;
void select_all();
void paste(Utf16View);
- void paste_from_clipboard();
+ void paste_from_clipboard(Vector<Clipboard::SystemClipboardItem> const&);
void undo();
void redo();
void set_marked_text_from_input_method(Utf16View text);
diff --git a/Libraries/LibWeb/HTML/Window.h b/Libraries/LibWeb/HTML/Window.h
index a2f5c52dcf3..551b3878975 100644
--- a/Libraries/LibWeb/HTML/Window.h
+++ b/Libraries/LibWeb/HTML/Window.h
@@ -295,6 +295,10 @@ public:
void consume_history_action_user_activation();
+ // The system clipboard data that the paste event this window is dispatching pastes, if it pastes the clipboard.
+ Vector<Clipboard::SystemClipboardItem> const* system_clipboard_data_being_pasted() const { return m_system_clipboard_data_being_pasted; }
+ void set_system_clipboard_data_being_pasted(Vector<Clipboard::SystemClipboardItem> const* data) { m_system_clipboard_data_being_pasted = data; }
+
// Steps 4 and 5 of both consumptions for the windows a page hosts. The UI process runs this in every page of the
// tab, since the navigables of top's active document span its pages.
static void consume_user_activation_of_windows_hosted_by(Page&, UserActivationConsumption);
@@ -391,6 +395,8 @@ private:
// https://html.spec.whatwg.org/multipage/interaction.html#last-history-action-activation-timestamp
HighResolutionTime::DOMHighResTimeStamp m_last_history_action_activation_timestamp { AK::Infinity<double> };
+ Vector<Clipboard::SystemClipboardItem> const* m_system_clipboard_data_being_pasted { nullptr };
+
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-window-status
// When the Window object is created, the attribute must be set to the empty string. It does not do anything else.
Utf16String m_status;
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index f243d8c76ba..9efd2f71ffe 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -907,8 +907,7 @@ void Internals::paste(HTML::HTMLElement& target, Utf16String const& text)
void Internals::paste_from_clipboard()
{
- if (auto navigable = page().hosted_focused_navigable())
- navigable->paste_from_clipboard();
+ page().client().page_did_request_paste_from_clipboard_for_testing();
}
void Internals::commit_text()
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index d32cf1e9d0c..5f665075f79 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -2844,68 +2844,76 @@ EventResult EventHandler::perform_paste_action()
// NB: The system clipboard lives in the UI process. So, retrieve its contents first — and run the rest of the paste
// action once the contents arrive.
- document->page().request_clipboard_entries(GC::create_function(document->heap(), GC::captures(GC::Ref { *document }), [](auto& document, Vector<Clipboard::SystemClipboardItem> items) {
- auto data_store = HTML::DragDataStore::create();
-
- auto add_text_item = [&](Utf16FlyString type, Utf16String data) {
- data_store->add_item({
- .kind = HTML::DragDataStoreItem::Kind::Text,
- .type_string = move(type),
- .data = move(data),
- .file_data = {},
- .file_name = {},
- });
- };
-
- auto add_file_item = [&](Utf16FlyString type, Utf16String name, ByteBuffer data) {
- data_store->add_item({
- .kind = HTML::DragDataStoreItem::Kind::File,
- .type_string = move(type),
- .data = {},
- .file_data = move(data),
- .file_name = move(name),
- });
- };
-
- if (!items.is_empty()) {
- for (auto& representation : items.first().system_clipboard_representations) {
- auto mime_type = MimeSniff::MimeType::parse(representation.name);
- if (!mime_type.has_value())
- continue;
-
- auto essence = Utf16FlyString::from_utf8(mime_type->essence());
-
- representation.data.visit(
- [&](ByteString const& text) {
- auto data = String::from_utf8_with_replacement_character(text);
-
- if (essence.is_one_of("text/plain"sv, "text/html"sv)) {
- add_text_item(move(essence), Utf16String::from_utf8_with_replacement_character(text));
- } else if (essence == "image/png"sv) {
- // AD-HOC: The spec doesn't say what to do here, but other engines add a file named
- // image.png for any PNG inserted via navigator.clipboard. Note that only PNG
- // images are supported via that API.
- add_file_item(move(essence), "image.png"_utf16, text.to_byte_buffer());
- }
- },
- [&](HTML::SelectedFile& file) {
- add_file_item(move(essence), file.name(), file.take_contents());
- });
- }
- }
-
+ document->page().request_clipboard_entries_for_paste(GC::create_function(document->heap(), GC::captures(GC::Ref { *document }), [](auto& document, Vector<Clipboard::SystemClipboardItem> system_clipboard_data) {
if (auto navigable = document->navigable())
- navigable->event_handler().perform_paste_action(data_store);
+ navigable->event_handler().perform_paste_action(system_clipboard_data);
}));
return EventResult::Handled;
}
// https://w3c.github.io/clipboard-apis/#paste-action
-// This is the paste action from the point where the content to paste is already in hand, either because async retrieval
-// of the system clipboard's contents above finished, or because the paste arrived through LocalNavigable::paste() —
-// which the UI process hands the content to paste directly.
-EventResult EventHandler::perform_paste_action(NonnullRefPtr<HTML::DragDataStore> const& data_store)
+// This is the paste action from the point where the system clipboard's contents are in hand, either because async
+// retrieval of them above finished, or because the UI process handed them over with its own Paste command.
+EventResult EventHandler::perform_paste_action(Vector<Clipboard::SystemClipboardItem> const& system_clipboard_data)
+{
+ auto data_store = HTML::DragDataStore::create();
+
+ auto add_text_item = [&](Utf16FlyString type, Utf16String data) {
+ data_store->add_item({
+ .kind = HTML::DragDataStoreItem::Kind::Text,
+ .type_string = move(type),
+ .data = move(data),
+ .file_data = {},
+ .file_name = {},
+ });
+ };
+
+ auto add_file_item = [&](Utf16FlyString type, Utf16String name, ByteBuffer data) {
+ data_store->add_item({
+ .kind = HTML::DragDataStoreItem::Kind::File,
+ .type_string = move(type),
+ .data = {},
+ .file_data = move(data),
+ .file_name = move(name),
+ });
+ };
+
+ if (!system_clipboard_data.is_empty()) {
+ for (auto const& representation : system_clipboard_data.first().system_clipboard_representations) {
+ auto mime_type = MimeSniff::MimeType::parse(representation.name);
+ if (!mime_type.has_value())
+ continue;
+
+ auto essence = Utf16FlyString::from_utf8(mime_type->essence());
+
+ representation.data.visit(
+ [&](ByteString const& text) {
+ auto data = String::from_utf8_with_replacement_character(text);
+
+ if (essence.is_one_of("text/plain"sv, "text/html"sv)) {
+ add_text_item(move(essence), Utf16String::from_utf8_with_replacement_character(text));
+ } else if (essence == "image/png"sv) {
+ // AD-HOC: The spec doesn't say what to do here, but other engines add a file named
+ // image.png for any PNG inserted via navigator.clipboard. Note that only PNG
+ // images are supported via that API.
+ add_file_item(move(essence), "image.png"_utf16, text.to_byte_buffer());
+ }
+ },
+ [&](HTML::SelectedFile const& file) {
+ auto contents = file;
+ add_file_item(move(essence), file.name(), contents.take_contents());
+ });
+ }
+ }
+
+ return perform_paste_action(data_store, &system_clipboard_data);
+}
+
+// https://w3c.github.io/clipboard-apis/#paste-action
+// This is the paste action from the point where the content to paste is in a data store: the system clipboard's contents,
+// or content that the UI process hands over in their place through LocalNavigable::paste().
+EventResult EventHandler::perform_paste_action(NonnullRefPtr<HTML::DragDataStore> const& data_store, Vector<Clipboard::SystemClipboardItem> const* system_clipboard_data)
{
auto document = m_navigable->active_document();
if (!document || !document->is_fully_active())
@@ -2914,8 +2922,16 @@ EventResult EventHandler::perform_paste_action(NonnullRefPtr<HTML::DragDataStore
data_store->set_mode(HTML::DragDataStore::Mode::ReadOnly);
auto data_transfer = HTML::DataTransfer::create(data_store);
+ // NB: Pasting the system clipboard is the user's interaction with the user agent's "Paste" control, so script
+ // running as a result of the paste event may read the data being pasted. A paste of handed-over content lets
+ // the page see only that content.
+ GC::Ref window = *document->window();
+ auto const* data_being_pasted = window->system_clipboard_data_being_pasted();
+ window->set_system_clipboard_data_being_pasted(system_clipboard_data);
+
// 2. Fire a clipboard event named paste.
bool event_was_not_canceled = fire_clipboard_event(*document, HTML::EventNames::paste, data_transfer);
+ window->set_system_clipboard_data_being_pasted(data_being_pasted);
data_store->set_mode(HTML::DragDataStore::Mode::Protected);
// 3. If the event was not canceled, then: if there is a selection or cursor in an editable context where
diff --git a/Libraries/LibWeb/Page/EventHandler.h b/Libraries/LibWeb/Page/EventHandler.h
index 7fda8e5f30b..912a7a171df 100644
--- a/Libraries/LibWeb/Page/EventHandler.h
+++ b/Libraries/LibWeb/Page/EventHandler.h
@@ -124,7 +124,9 @@ public:
EventResult handle_drag_and_drop_event(DragEvent::Type, CSSPixelPoint, CSSPixelPoint screen_position, unsigned button, unsigned buttons, unsigned modifiers, Vector<HTML::SelectedFile> files);
EventResult handle_pinch_event(CSSPixelPoint, PinchEvent const&, Optional<RemoteInputEventTarget>* remote_target);
[[nodiscard]] EventResult perform_paste_action();
- EventResult perform_paste_action(NonnullRefPtr<HTML::DragDataStore> const&);
+ EventResult perform_paste_action(Vector<Clipboard::SystemClipboardItem> const& system_clipboard_data);
+ // A null system_clipboard_data stands for content the UI process pastes in its place, like the primary selection.
+ EventResult perform_paste_action(NonnullRefPtr<HTML::DragDataStore> const&, Vector<Clipboard::SystemClipboardItem> const* system_clipboard_data);
enum class ClipboardActionTrigger {
User,
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 1e0a89f7faf..4cb4f79c19f 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -1502,12 +1502,12 @@ void Page::select_dropdown_closed(Optional<u32> const& selected_item_id)
}
}
-void Page::request_clipboard_entries(ClipboardRequest request)
+void Page::request_clipboard_entries_for_paste(ClipboardRequest request)
{
auto request_id = m_next_clipboard_request_id++;
m_pending_clipboard_requests.set(request_id, request);
- client().page_did_request_clipboard_entries(request_id);
+ client().page_did_request_clipboard_entries_for_paste(request_id);
}
void Page::retrieved_clipboard_entries(u64 request_id, Vector<Clipboard::SystemClipboardItem> items)
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index a9c5f907bdf..62b5cc7c104 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -325,7 +325,7 @@ public:
void select_dropdown_closed(Optional<u32> const& selected_item_id);
using ClipboardRequest = GC::Ref<GC::Function<void(Vector<Clipboard::SystemClipboardItem>)>>;
- void request_clipboard_entries(ClipboardRequest);
+ void request_clipboard_entries_for_paste(ClipboardRequest);
void retrieved_clipboard_entries(u64 request_id, Vector<Clipboard::SystemClipboardItem>);
using SpellCheckRequest = GC::Ref<GC::Function<void(Vector<SpellingError>)>>;
@@ -982,7 +982,8 @@ public:
virtual void page_did_change_background_color(Gfx::Color) { }
virtual void page_did_insert_clipboard_item(Clipboard::SystemClipboardItem const&, [[maybe_unused]] StringView presentation_style) { }
- virtual void page_did_request_clipboard_entries([[maybe_unused]] u64 request_id) { }
+ virtual void page_did_request_clipboard_entries_for_paste([[maybe_unused]] u64 request_id) { }
+ virtual void page_did_request_paste_from_clipboard_for_testing() { }
virtual void page_did_request_spell_check([[maybe_unused]] u64 request_id, [[maybe_unused]] Utf16String const& text) { }
virtual void page_did_request_translation([[maybe_unused]] TranslationUnit const&) { }
virtual void page_did_request_page_translation_for_testing([[maybe_unused]] String const& target_language) { }
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 691976d59d5..5cc499b064b 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -885,6 +885,15 @@ static bool is_activation_triggering_input_event(Web::InputEvent const& event)
return false;
}
+// NB: The shortcut matches the one EventHandler pastes on.
+static bool is_paste_shortcut(Web::InputEvent const& event)
+{
+ auto const* key_event = event.get_pointer<Web::KeyEvent>();
+ return key_event && key_event->type == Web::KeyEvent::Type::KeyDown && key_event->key == Web::UIEvents::KeyCode::Key_V
+ && (key_event->modifiers & Web::UIEvents::Mod_PlatformCtrl) != 0
+ && (key_event->modifiers & (Web::UIEvents::Mod_Shift | Web::UIEvents::Mod_Alt)) == 0;
+}
+
// A page busy for a while handles input late, and its activation counts from then. A page that never finishes handling
// the input keeps the activation no longer than this.
static constexpr auto longest_user_activation_delivery = AK::Duration::from_seconds(30);
@@ -951,6 +960,8 @@ void ViewImplementation::enqueue_input_event(Web::InputEvent event)
});
m_inputs_to_open_links_in_new_tabs.append({ Web::input_event_id(pending.event), now });
}
+ if (is_paste_shortcut(pending.event))
+ pending.paste_shortcut_time = MonotonicTime::now();
// A focused navigable another page hosts takes key events directly.
if (pending.event.has<Web::KeyEvent>())
pending.endpoint = focused_navigable_host();
@@ -1316,6 +1327,17 @@ bool ViewImplementation::is_delivering_user_activation_to(WebContentPage const&
return index_of_input_in_flight_giving_user_activation_to(page).has_value();
}
+// NB: A page handles its input in the order the view delivered it, so the earliest input to the page that the page has
+// yet to finish is the input it is handling, if any. Each paste shortcut lets the page read the clipboard once.
+bool ViewImplementation::take_paste_shortcut_handled_by(Badge<WebContentPage>, WebContentPage const& page)
+{
+ auto index = m_pending_input_events.find_first_index_if([&](auto const& pending) { return pending.endpoint.ptr() == &page; });
+ if (!index.has_value())
+ return false;
+ auto paste_shortcut_time = exchange(m_pending_input_events[*index].paste_shortcut_time, {});
+ return paste_shortcut_time.has_value() && MonotonicTime::now() - *paste_shortcut_time < longest_user_activation_delivery;
+}
+
void ViewImplementation::consume_user_activation_of_input_in_flight_to(Badge<WebContentPage>, WebContentPage const& page)
{
if (auto index = index_of_input_in_flight_giving_user_activation_to(page); index.has_value())
@@ -2780,10 +2802,17 @@ WebContentPage& ViewImplementation::focused_navigable_host() const
void ViewImplementation::paste_from_clipboard()
{
- // The user asked the browser to paste, which lets the page read the clipboard.
- auto& host = focused_navigable_host();
- host.give_user_activation();
- host.async_paste_from_clipboard();
+ // The user asked the browser to paste, which hands the page the system clipboard data.
+ read_clipboard_item([host = NonnullRefPtr { focused_navigable_host() }](Web::Clipboard::SystemClipboardItem item) {
+ if (!host->is_open())
+ return;
+ Vector<Web::Clipboard::SystemClipboardItem> system_clipboard_data;
+ if (!item.system_clipboard_representations.is_empty())
+ system_clipboard_data.append(move(item));
+
+ host->give_user_activation();
+ host->async_paste_from_clipboard(move(system_clipboard_data));
+ });
}
void ViewImplementation::set_marked_text_from_input_method(Utf16String const& text)
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index 5da8a7c26d5..5a6664ebc67 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -304,6 +304,7 @@ public:
void did_forward_pinch_event(Badge<WebContentPage>, WebContentPage const& reporting_page, Web::PinchEvent const&, WebContentPage& endpoint);
// Whether the page has yet to finish handling input that gives it user activation.
bool is_delivering_user_activation_to(WebContentPage const&) const;
+ bool take_paste_shortcut_handled_by(Badge<WebContentPage>, WebContentPage const&);
// Consumes the activation of the earliest input giving the page activation that the page has yet to finish
// handling, which leaves the input queued behind it the activation it gives.
void consume_user_activation_of_input_in_flight_to(Badge<WebContentPage>, WebContentPage const&);
@@ -962,6 +963,9 @@ protected:
// When the view delivered an event that gives user activation, until something consumes that activation.
Optional<MonotonicTime> user_activation_time {};
+
+ // When the view delivered a paste shortcut, until the page reads the clipboard for it.
+ Optional<MonotonicTime> paste_shortcut_time {};
};
Vector<PendingInputEvent> m_pending_input_events;
void send_queued_input_events();
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 9625bfc8955..9d273bfada8 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -2441,10 +2441,12 @@ void WebContentPage::did_update_indexed_database(String update)
view().notify_indexed_database_changed(parse_json(update, "IndexedDB update"sv));
}
-void WebContentPage::did_request_clipboard_entries(u64 request_id)
+// NB: The page gets the system clipboard data for its paste action once, while it handles the paste shortcut the view
+// delivered to it. The browser's Paste command hands the data over itself.
+void WebContentPage::did_request_clipboard_entries_for_paste(u64 request_id)
{
Vector<Web::Clipboard::SystemClipboardItem> items;
- if (!has_transient_user_activation()) {
+ if (!WebContentClient::renderers_may_synthesize_input() && !view().take_paste_shortcut_handled_by({}, *this)) {
async_retrieved_clipboard_entries(request_id, items);
return;
}
@@ -2458,6 +2460,16 @@ void WebContentPage::did_request_clipboard_entries(u64 request_id)
});
}
+void WebContentPage::did_request_paste_from_clipboard_for_testing()
+{
+ // NB: This stands in for the browser's Paste command, which hands the page the system clipboard data.
+ if (!WebContentClient::renderers_may_synthesize_input()) {
+ client().did_misbehave("did_request_paste_from_clipboard_for_testing"sv, "not driven by WebDriver or a test"sv);
+ return;
+ }
+ view().paste_from_clipboard();
+}
+
void WebContentPage::did_request_spell_check(u64 request_id, Utf16String text)
{
view().did_request_spell_check({}, *this, request_id, move(text));
diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.h
index 4045ce4661c..0d9dbdb8a02 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -336,7 +336,8 @@ private:
virtual void did_complete_js_console_input(u64 request_id, Optional<Vector<String>> matches, String match_prop) override;
virtual void did_receive_network_response_headers(u64 request_id, u32 status_code, Optional<String> reason_phrase, Vector<HTTP::Header> response_headers, Requests::CacheState cache_state) override;
virtual void did_update_indexed_database(String update) override;
- virtual void did_request_clipboard_entries(u64 request_id) override;
+ virtual void did_request_clipboard_entries_for_paste(u64 request_id) override;
+ virtual void did_request_paste_from_clipboard_for_testing() override;
virtual void did_request_spell_check(u64 request_id, Utf16String text) override;
virtual void did_request_writing_suggestion(u64 request_id, Utf16String text, u32 caret_offset) override;
virtual void did_get_translation_language_sample(u64 request_id, Web::TranslationLanguageSample sample) override;
diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp
index cf8d4d9b9c7..5a2364f41a5 100644
--- a/Services/WebContent/ConnectionFromClient.cpp
+++ b/Services/WebContent/ConnectionFromClient.cpp
@@ -3196,11 +3196,11 @@ void ConnectionFromClient::paste(Web::PageId page_id, Utf16String text)
}
}
-void ConnectionFromClient::paste_from_clipboard(Web::PageId page_id)
+void ConnectionFromClient::paste_from_clipboard(Web::PageId page_id, Vector<Web::Clipboard::SystemClipboardItem> system_clipboard_data)
{
if (auto page = this->page(page_id); page.has_value()) {
if (auto navigable = page->page().hosted_focused_navigable())
- navigable->paste_from_clipboard();
+ navigable->paste_from_clipboard(system_clipboard_data);
}
}
diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.h
index a5e2d567c75..8217f4450c2 100644
--- a/Services/WebContent/ConnectionFromClient.h
+++ b/Services/WebContent/ConnectionFromClient.h
@@ -339,7 +339,7 @@ private:
virtual void find_in_page_end(Web::PageId page_id) override;
virtual void paste(Web::PageId page_id, Utf16String text) override;
- virtual void paste_from_clipboard(Web::PageId page_id) override;
+ virtual void paste_from_clipboard(Web::PageId page_id, Vector<Web::Clipboard::SystemClipboardItem> system_clipboard_data) override;
virtual void set_marked_text_from_input_method(Web::PageId page_id, Utf16String text) override;
virtual void commit_text_from_input_method(Web::PageId page_id, Utf16String text, i32 replacement_start, i32 replacement_length) override;
virtual void unmark_text_from_input_method(Web::PageId page_id) override;
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index 1ef55d54904..efda6f1d2b7 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -2148,9 +2148,14 @@ void PageClient::page_did_insert_clipboard_item(Web::Clipboard::SystemClipboardI
client().async_did_insert_clipboard_item(m_id, item, presentation_style);
}
-void PageClient::page_did_request_clipboard_entries(u64 request_id)
+void PageClient::page_did_request_clipboard_entries_for_paste(u64 request_id)
{
- client().async_did_request_clipboard_entries(m_id, request_id);
+ client().async_did_request_clipboard_entries_for_paste(m_id, request_id);
+}
+
+void PageClient::page_did_request_paste_from_clipboard_for_testing()
+{
+ client().async_did_request_paste_from_clipboard_for_testing(m_id);
}
void PageClient::page_did_request_spell_check(u64 request_id, Utf16String const& text)
diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.h
index 707d7d8aaec..060f18ce1b0 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -393,7 +393,8 @@ private:
virtual void page_did_change_theme_color(Gfx::Color color) override;
virtual void page_did_change_background_color(Gfx::Color color) override;
virtual void page_did_insert_clipboard_item(Web::Clipboard::SystemClipboardItem const&, StringView presentation_style) override;
- virtual void page_did_request_clipboard_entries(u64 request_id) override;
+ virtual void page_did_request_clipboard_entries_for_paste(u64 request_id) override;
+ virtual void page_did_request_paste_from_clipboard_for_testing() override;
virtual void page_did_request_spell_check(u64 request_id, Utf16String const& text) override;
virtual void page_did_request_translation(Web::TranslationUnit const&) override;
virtual void page_did_request_page_translation_for_testing(String const& target_language) override;
diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipc
index 0d50f6982b0..4aef1bc8992 100644
--- a/Services/WebContent/WebContentClient.ipc
+++ b/Services/WebContent/WebContentClient.ipc
@@ -261,7 +261,8 @@ endpoint WebContentClient routed by Web::PageId page_id
did_change_background_color(Web::PageId page_id, Gfx::Color color) =|
did_insert_clipboard_item(Web::PageId page_id, Web::Clipboard::SystemClipboardItem item, String presentation_style) =|
- did_request_clipboard_entries(Web::PageId page_id, u64 request_id) =|
+ did_request_clipboard_entries_for_paste(Web::PageId page_id, u64 request_id) =|
+ did_request_paste_from_clipboard_for_testing(Web::PageId page_id) =|
did_request_spell_check(Web::PageId page_id, u64 request_id, Utf16String text) =|
did_request_writing_suggestion(Web::PageId page_id, u64 request_id, Utf16String text, u32 caret_offset) =|
did_get_translation_language_sample(Web::PageId page_id, u64 request_id, Web::TranslationLanguageSample sample) =|
diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipc
index 531dc216b73..8a6419f7383 100644
--- a/Services/WebContent/WebContentServer.ipc
+++ b/Services/WebContent/WebContentServer.ipc
@@ -247,7 +247,7 @@ endpoint WebContentServer routed by Web::PageId page_id
undo(Web::PageId page_id) =|
redo(Web::PageId page_id) =|
paste(Web::PageId page_id, Utf16String text) =|
- paste_from_clipboard(Web::PageId page_id) =|
+ paste_from_clipboard(Web::PageId page_id, Vector<Web::Clipboard::SystemClipboardItem> system_clipboard_data) =|
set_marked_text_from_input_method(Web::PageId page_id, Utf16String text) =|
commit_text_from_input_method(Web::PageId page_id, Utf16String text, i32 replacement_start, i32 replacement_length) =|
unmark_text_from_input_method(Web::PageId page_id) =|
diff --git a/Tests/LibWeb/Text/expected/Clipboard/clipboard-read-requires-paste.txt b/Tests/LibWeb/Text/expected/Clipboard/clipboard-read-requires-paste.txt
new file mode 100644
index 00000000000..c26fb57eef0
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Clipboard/clipboard-read-requires-paste.txt
@@ -0,0 +1,7 @@
+readText() without activation: rejected with NotAllowedError
+readText() after a click: rejected with NotAllowedError
+read() after a click: rejected with NotAllowedError
+read() in a paste event the browser did not fire: rejected with NotAllowedError
+read() in a handed-over paste: rejected with NotAllowedError
+read() in a task after a browser paste: rejected with NotAllowedError
+read() in a browser paste: resolved with types []
diff --git a/Tests/LibWeb/Text/expected/wpt-import/clipboard-apis/async-navigator-clipboard-basics.https.txt b/Tests/LibWeb/Text/expected/wpt-import/clipboard-apis/async-navigator-clipboard-basics.https.txt
index dd5d7573322..1f2c7f0b2f7 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/clipboard-apis/async-navigator-clipboard-basics.https.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/clipboard-apis/async-navigator-clipboard-basics.https.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 17 tests
-14 Pass
-3 Fail
+13 Pass
+4 Fail
Pass navigator.clipboard exists
Pass navigator.clipboard.write([text/plain ClipboardItem]) succeeds
Fail navigator.clipboard.write([>1 ClipboardItems]) fails (not implemented)
@@ -17,7 +17,7 @@ Pass navigator.clipboard.write({string : DOMString}) succeeds
Pass navigator.clipboard.write({string : image/png Blob}) succeeds
Pass navigator.clipboard.write([text + png] succeeds
Fail navigator.clipboard.write(image/png DOMString) fails
-Pass navigator.clipboard.read() succeeds
+Fail navigator.clipboard.read() succeeds
Fail navigator.clipboard.readText() succeeds
Pass navigator.clipboard.write(Promise<Blob>) succeeds
Pass navigator.clipboard.write(Promise<Blob>s) succeeds
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/Clipboard/clipboard-png-identity.html b/Tests/LibWeb/Text/input/Clipboard/clipboard-png-identity.html
index 9598e6b81f3..1fb6d1d4891 100644
--- a/Tests/LibWeb/Text/input/Clipboard/clipboard-png-identity.html
+++ b/Tests/LibWeb/Text/input/Clipboard/clipboard-png-identity.html
@@ -35,8 +35,7 @@
internals.dispatchUserActivatedEvent(document.body, new Event("mousedown"));
await navigator.clipboard.write([new ClipboardItem({ "image/png": fetchedBlob })]);
- internals.dispatchUserActivatedEvent(document.body, new Event("mousedown"));
- const items = await navigator.clipboard.read();
+ const items = await readClipboardFromPaste(document.body);
for (const item of items) {
const imageType = item.types.find(type => type.startsWith("image/"));
diff --git a/Tests/LibWeb/Text/input/Clipboard/clipboard-read-requires-paste.html b/Tests/LibWeb/Text/input/Clipboard/clipboard-read-requires-paste.html
new file mode 100644
index 00000000000..213a1ac6011
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Clipboard/clipboard-read-requires-paste.html
@@ -0,0 +1,51 @@
+<!doctype html>
+<script src="../include.js"></script>
+<div id="editor" contenteditable="true"></div>
+<script>
+ promiseTest(async () => {
+ const describe = promise =>
+ promise.then(
+ value => (Array.isArray(value) ? `resolved with types ${JSON.stringify(value.map(item => item.types))}` : `resolved with ${JSON.stringify(value)}`),
+ error => `rejected with ${error.name}`
+ );
+ const activate = () => internals.dispatchUserActivatedEvent(editor, new Event("mousedown"));
+ const readAfterClick = read => {
+ activate();
+ return describe(read(navigator.clipboard));
+ };
+ const readText = clipboard => clipboard.readText();
+ const read = clipboard => clipboard.read();
+
+ editor.focus();
+
+ internals.setClipboardFile("other.txt", "text/plain", "other application");
+ println(`readText() without activation: ${await describe(navigator.clipboard.readText())}`);
+ println(`readText() after a click: ${await readAfterClick(readText)}`);
+ println(`read() after a click: ${await readAfterClick(read)}`);
+
+ activate();
+ let readInEvent;
+ editor.addEventListener("paste", () => { readInEvent = navigator.clipboard.read(); }, { once: true });
+ internals.dispatchUserActivatedEvent(editor, new ClipboardEvent("paste", { bubbles: true, cancelable: true }));
+ println(`read() in a paste event the browser did not fire: ${await describe(readInEvent)}`);
+
+ // A middle-click paste hands over the primary selection, so it lets the page see only that.
+ readInEvent = undefined;
+ editor.addEventListener("paste", event => {
+ event.preventDefault();
+ readInEvent = navigator.clipboard.read();
+ }, { once: true });
+ activate();
+ internals.paste(editor, "primary selection");
+ println(`read() in a handed-over paste: ${await describe(readInEvent)}`);
+
+ let readLater;
+ await readClipboardFromPaste(editor, () => new Promise(resolve => setTimeout(() => {
+ readLater = navigator.clipboard.read();
+ resolve();
+ })));
+ println(`read() in a task after a browser paste: ${await describe(readLater)}`);
+
+ println(`read() in a browser paste: ${await describe(readClipboardFromPaste(editor))}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/Clipboard/clipboard-shortcut-paste-image.html b/Tests/LibWeb/Text/input/Clipboard/clipboard-shortcut-paste-image.html
index 868ffd50e79..1e71ca3dc47 100644
--- a/Tests/LibWeb/Text/input/Clipboard/clipboard-shortcut-paste-image.html
+++ b/Tests/LibWeb/Text/input/Clipboard/clipboard-shortcut-paste-image.html
@@ -23,8 +23,7 @@
};
const readClipboard = async () => {
- internals.dispatchUserActivatedEvent(editor, new Event("mousedown"));
- const items = await navigator.clipboard.read();
+ const items = await readClipboardFromPaste(editor);
for (const item of items) {
const type = item.types.find(type => type.startsWith("image/"));
diff --git a/Tests/LibWeb/Text/input/Editing/clipboard-paste-html-with-trusted-types-enforced.html b/Tests/LibWeb/Text/input/Editing/clipboard-paste-html-with-trusted-types-enforced.html
index baf2cc2fc1b..8f07ebe4740 100644
--- a/Tests/LibWeb/Text/input/Editing/clipboard-paste-html-with-trusted-types-enforced.html
+++ b/Tests/LibWeb/Text/input/Editing/clipboard-paste-html-with-trusted-types-enforced.html
@@ -18,8 +18,7 @@
selection.setBaseAndExtent(editor.firstChild, 0, editor.firstChild, 1);
internals.sendKey(editor, "C", platformControl);
- internals.dispatchUserActivatedEvent(editor, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(editor);
selection.collapse(editor.firstChild, 1);
const pasteInput = new Promise(resolve => editor.addEventListener("input", resolve, { once: true }));
diff --git a/Tests/LibWeb/Text/input/Editing/clipboard-paste-sanitization.html b/Tests/LibWeb/Text/input/Editing/clipboard-paste-sanitization.html
index 32f5590b9cb..f0bfd10c95d 100644
--- a/Tests/LibWeb/Text/input/Editing/clipboard-paste-sanitization.html
+++ b/Tests/LibWeb/Text/input/Editing/clipboard-paste-sanitization.html
@@ -16,8 +16,7 @@
globalThis.executed = false;
selection.setBaseAndExtent(editor.firstChild, 0, editor.firstChild, 1);
internals.sendKey(editor, "C", platformControl);
- internals.dispatchUserActivatedEvent(editor, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(editor);
selection.collapse(editor.firstChild, 1);
const pasteInput = new Promise(resolve => editor.addEventListener("input", resolve, { once: true }));
diff --git a/Tests/LibWeb/Text/input/Editing/clipboard-shortcut-copy-cut.html b/Tests/LibWeb/Text/input/Editing/clipboard-shortcut-copy-cut.html
index 2a5106577cc..db7f6a80763 100644
--- a/Tests/LibWeb/Text/input/Editing/clipboard-shortcut-copy-cut.html
+++ b/Tests/LibWeb/Text/input/Editing/clipboard-shortcut-copy-cut.html
@@ -11,10 +11,7 @@
events.push(`${type}(cancelable=${e.cancelable} composed=${e.composed} data=${JSON.stringify(e.clipboardData?.getData("text/plain") ?? null)})`);
});
}
- const readClipboard = async () => {
- internals.dispatchUserActivatedEvent(editor, new Event("mousedown"));
- return navigator.clipboard.readText().catch(() => null);
- };
+ const readClipboard = () => readClipboardFromPaste(editor, clipboard => clipboard.readText().catch(() => null));
// Select "hello" and copy it.
editor.focus();
@@ -39,8 +36,7 @@
getSelection().setBaseAndExtent(editor.firstChild, 0, editor.firstChild, 5);
internals.sendKey(editor, "C", modCtrl);
println(`clipboard after canceled copy: ${JSON.stringify(await readClipboard())}`);
- internals.dispatchUserActivatedEvent(editor, new Event("mousedown"));
- const [clipboardItem] = await navigator.clipboard.read();
+ const [clipboardItem] = await readClipboardFromPaste(editor);
println(`formats after canceled copy: ${JSON.stringify(clipboardItem.types)}`);
for (const line of events) println(line);
diff --git a/Tests/LibWeb/Text/input/Editing/clipboard-shortcut-iframe.html b/Tests/LibWeb/Text/input/Editing/clipboard-shortcut-iframe.html
index 31b0b2a08cc..f1b27971490 100644
--- a/Tests/LibWeb/Text/input/Editing/clipboard-shortcut-iframe.html
+++ b/Tests/LibWeb/Text/input/Editing/clipboard-shortcut-iframe.html
@@ -16,8 +16,7 @@
event.preventDefault();
}, { once: true });
internals.sendKey(body, "C", platformControl);
- internals.dispatchUserActivatedEvent(body, new Event("mousedown"));
- const [clipboardItem] = await frame.contentWindow.navigator.clipboard.read();
+ const [clipboardItem] = await readClipboardFromPaste(body);
println(`copy formats: ${JSON.stringify(clipboardItem.types)}`);
const pasteEvent = new Promise(resolve => body.addEventListener("paste", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/clipboard-text-control-actions.html b/Tests/LibWeb/Text/input/Editing/clipboard-text-control-actions.html
index b3fe0c4a087..a22f6216406 100644
--- a/Tests/LibWeb/Text/input/Editing/clipboard-text-control-actions.html
+++ b/Tests/LibWeb/Text/input/Editing/clipboard-text-control-actions.html
@@ -26,9 +26,8 @@
activate();
await navigator.clipboard.writeText("seed");
}
- async function clipboard() {
- activate();
- return navigator.clipboard.readText();
+ function clipboard() {
+ return readClipboardFromPaste(document.body, clipboard => clipboard.readText());
}
function events(element) {
const result = [];
diff --git a/Tests/LibWeb/Text/input/Editing/contenteditable-paste-placeholder.html b/Tests/LibWeb/Text/input/Editing/contenteditable-paste-placeholder.html
index f7a5e0e7df0..70b2cf47061 100644
--- a/Tests/LibWeb/Text/input/Editing/contenteditable-paste-placeholder.html
+++ b/Tests/LibWeb/Text/input/Editing/contenteditable-paste-placeholder.html
@@ -11,8 +11,7 @@
selection.setBaseAndExtent(text, 0, text, 4);
editor.focus();
internals.sendKey(editor, "C", platformControl);
- internals.dispatchUserActivatedEvent(editor, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(editor);
selection.collapse(text, 3);
const pasteInput = new Promise(resolve => editor.addEventListener("input", resolve, { once: true }));
diff --git a/Tests/LibWeb/Text/input/Editing/contenteditable-paste-split-style.html b/Tests/LibWeb/Text/input/Editing/contenteditable-paste-split-style.html
index 0b0d950ff8b..14d9418e692 100644
--- a/Tests/LibWeb/Text/input/Editing/contenteditable-paste-split-style.html
+++ b/Tests/LibWeb/Text/input/Editing/contenteditable-paste-split-style.html
@@ -12,8 +12,7 @@
selection.selectAllChildren(source);
editor.focus();
internals.sendKey(editor, "C", platformControl);
- internals.dispatchUserActivatedEvent(editor, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(editor);
selection.collapse(targetText, 4);
const pasteInput = new Promise(resolve => editor.addEventListener("input", resolve, { once: true }));
diff --git a/Tests/LibWeb/Text/input/Editing/execcommand-cut-contenteditable-recursion.html b/Tests/LibWeb/Text/input/Editing/execcommand-cut-contenteditable-recursion.html
index 6a9a6ef2fb2..5825c5c2b8e 100644
--- a/Tests/LibWeb/Text/input/Editing/execcommand-cut-contenteditable-recursion.html
+++ b/Tests/LibWeb/Text/input/Editing/execcommand-cut-contenteditable-recursion.html
@@ -15,9 +15,8 @@
activate();
await navigator.clipboard.writeText("seed");
}
- async function clipboard() {
- activate();
- return navigator.clipboard.readText();
+ function clipboard() {
+ return readClipboardFromPaste(document.body, clipboard => clipboard.readText());
}
function makeEditor() {
const editor = document.body.appendChild(document.createElement("div"));
diff --git a/Tests/LibWeb/Text/input/Editing/rich-clipboard-sceditor.html b/Tests/LibWeb/Text/input/Editing/rich-clipboard-sceditor.html
index 2f834ee0ad3..7ab42558249 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-clipboard-sceditor.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-clipboard-sceditor.html
@@ -67,8 +67,7 @@
}, { once: true });
}
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- const clipboardItems = await navigator.clipboard.read();
+ const clipboardItems = await readClipboardFromPaste(host);
expect(clipboardItems.some(item => item.types.includes("text/html")), true, `copy ${label} includes text/html`);
selection.collapse(host.querySelector("#last").firstChild, 5);
@@ -104,8 +103,7 @@
const blueText = host.querySelector("#blue").firstChild;
selection.setBaseAndExtent(blueText, 0, blueText, 9);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- const [scriptedClipboardItem] = await navigator.clipboard.read();
+ const [scriptedClipboardItem] = await readClipboardFromPaste(host);
expect(scriptedClipboardItem.types.includes("text/html"), true, "scripted copy includes text/html");
const scriptedPasteEvent = new Promise(resolve => host.addEventListener("paste", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-copy-terminal-placeholder.html b/Tests/LibWeb/Text/input/Editing/rich-copy-terminal-placeholder.html
index 6adc0fb900b..13012937af0 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-copy-terminal-placeholder.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-copy-terminal-placeholder.html
@@ -27,8 +27,7 @@
const source = host.querySelector("#source");
selection.setBaseAndExtent(source.firstElementChild.firstChild, 0, source.lastElementChild, 2);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host.querySelector("#target").firstChild, 1);
let clipboardHTML;
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-atomic-whitespace.html b/Tests/LibWeb/Text/input/Editing/rich-paste-atomic-whitespace.html
index 3a06aedc471..fcd64dc2589 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-atomic-whitespace.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-atomic-whitespace.html
@@ -28,8 +28,7 @@
const targetText = host.querySelector("#target").firstChild;
selection.setBaseAndExtent(source.firstChild, 0, source.lastChild, 6);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(targetText, 1);
const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-block-fragment.html b/Tests/LibWeb/Text/input/Editing/rich-paste-block-fragment.html
index ce0c049cbc0..9abe6312339 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-block-fragment.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-block-fragment.html
@@ -168,8 +168,7 @@
internals.sendKey(host, "C", platformControl);
// Synchronize the renderer-to-UI-process clipboard write without relying on timing.
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const lastLineText = host.lastElementChild.firstChild;
selection.collapse(lastLineText, 5);
@@ -251,8 +250,7 @@
const emptyParagraphBreak = host.querySelector("ul + div").firstChild;
selection.setBaseAndExtent(emptyParagraphBreak, 0, listItemText, 3);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(listItemText, 3);
const listPasteInput = new Promise(resolve => host.addEventListener("input", event => {
@@ -278,8 +276,7 @@
const redParagraph = host.querySelector('font[color="#ff00"]').parentNode;
selection.setBaseAndExtent(redParagraph, redParagraph.childNodes.length, firstParagraphText, 1);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const listItemMiddle = host.querySelector("li").firstChild;
selection.collapse(listItemMiddle, 2);
@@ -306,8 +303,7 @@
const trailingText = host.lastElementChild.childNodes[2];
selection.setBaseAndExtent(trailingText, 29, secondListItemText, 4);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(trailingText, 20);
const whitespacePasteInput = new Promise(resolve => host.addEventListener("input", event => {
@@ -332,8 +328,7 @@
const listItemTexts = host.querySelectorAll("li");
selection.setBaseAndExtent(listItemTexts[0].firstChild, 1, listItemTexts[1].firstChild, 12);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const listTargetText = host.lastElementChild.childNodes[2];
selection.collapse(listTargetText, 2);
@@ -359,8 +354,7 @@
const blockBoundaryListItems = host.querySelectorAll("li");
selection.setBaseAndExtent(blockBoundaryListItems[0].firstChild, 4, blockBoundaryListItems[1].firstChild, 12);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host, 4);
const blockBoundaryPasteInput = new Promise(resolve => host.addEventListener("input", event => {
@@ -386,8 +380,7 @@
const boundaryFirstListItem = boundaryList.firstElementChild;
selection.setBaseAndExtent(boundaryList, 2, boundaryFirstListItem, 1);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host, 11);
const leadingListBoundaryPasteInput = new Promise(resolve => host.addEventListener("input", event => {
@@ -412,8 +405,7 @@
const whitespaceListItems = host.querySelectorAll("li");
selection.setBaseAndExtent(whitespaceListItems[0].firstChild, 12, whitespaceListItems[1].firstChild, 14);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(whitespaceListItems[1].firstChild, 8);
const listWhitespacePasteInput = new Promise(resolve => host.addEventListener("input", event => {
@@ -438,8 +430,7 @@
const emptyParagraphListItems = host.querySelectorAll("li");
selection.setBaseAndExtent(emptyParagraphListItems[0].firstChild, 9, emptyParagraphListItems[1].firstChild, 12);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const firstEmptyParagraph = host.childNodes[2];
selection.collapse(firstEmptyParagraph, 0);
@@ -466,8 +457,7 @@
const tailSourceText = host.lastElementChild.firstChild;
selection.setBaseAndExtent(listWithTail, 0, tailSourceText, 4);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const plainTextTailTarget = host.lastElementChild.childNodes[2];
selection.collapse(plainTextTailTarget, 25);
@@ -494,8 +484,7 @@
const paragraphAfterLeadingSpaceList = leadingSpaceList.nextElementSibling;
selection.setBaseAndExtent(leadingSpaceList, 0, paragraphAfterLeadingSpaceList.nextSibling, 1);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const ordinaryLeadingSpaceTarget = host.lastElementChild.childNodes[2];
selection.collapse(ordinaryLeadingSpaceTarget, 1);
@@ -522,8 +511,7 @@
const singleRunTransferSourceText = host.lastElementChild.firstChild;
selection.setBaseAndExtent(singleRunTransferSourceList, 2, singleRunTransferSourceText, 6);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const singleRunTransferTarget = host.lastElementChild.childNodes[2];
selection.collapse(singleRunTransferTarget, 3);
@@ -549,8 +537,7 @@
const foldedParagraphSource = host.lastElementChild.childNodes[2];
selection.setBaseAndExtent(host.lastChild, 0, foldedParagraphSource, 23);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const foldedParagraphTarget = host.lastElementChild.lastChild;
selection.collapse(foldedParagraphTarget, 10);
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-empty-terminal-container.html b/Tests/LibWeb/Text/input/Editing/rich-paste-empty-terminal-container.html
index c1fef15ba95..b93b3d4c636 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-empty-terminal-container.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-empty-terminal-container.html
@@ -26,8 +26,7 @@
selection.setBaseAndExtent(host.children[0].firstChild, 3, host.children[2], 0);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host, 3);
const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-integrated-paragraph-whitespace.html b/Tests/LibWeb/Text/input/Editing/rich-paste-integrated-paragraph-whitespace.html
index 377850b845c..f151f5068d8 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-integrated-paragraph-whitespace.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-integrated-paragraph-whitespace.html
@@ -52,8 +52,7 @@
const finalLineSuffix = host.childNodes[11].childNodes[2];
selection.setBaseAndExtent(blueText, 3, finalLineSuffix, 29);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(blueText, 1);
const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-interchange-newline.html b/Tests/LibWeb/Text/input/Editing/rich-paste-interchange-newline.html
index ca3f6360842..b2524f24758 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-interchange-newline.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-interchange-newline.html
@@ -156,8 +156,7 @@
const blueText = host.querySelector('font[color="#3399ff"]').firstChild;
selection.setBaseAndExtent(host.childNodes[1], 0, blueText, 0);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const redText = host.querySelector('font[color="#ff00"]').firstChild;
selection.collapse(redText, 2);
@@ -182,8 +181,7 @@
host.focus();
selection.setBaseAndExtent(host.childNodes[7], 1, host.childNodes[10], 0);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const firstListItemText = host.querySelector("li").firstChild;
selection.collapse(firstListItemText, 6);
@@ -208,8 +206,7 @@
host.focus();
selection.setBaseAndExtent(host.childNodes[6], 1, host.childNodes[10], 1);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const listItemMiddle = host.querySelector("li").firstChild;
selection.collapse(listItemMiddle, 9);
@@ -235,8 +232,7 @@
const firstParagraphText = host.firstElementChild.firstChild;
selection.setBaseAndExtent(firstParagraphText, 3, host, 4);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const listItemStart = host.querySelector("li").firstChild;
selection.collapse(listItemStart, 0);
@@ -261,8 +257,7 @@
host.focus();
selection.setBaseAndExtent(host.childNodes[3], 1, host.childNodes[2], 1);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const firstListItem = host.querySelector("li");
selection.collapse(firstListItem, firstListItem.childNodes.length);
@@ -288,8 +283,7 @@
const nestedBoundaryTrailingText = host.lastElementChild.lastChild;
selection.setBaseAndExtent(host, 2, nestedBoundaryTrailingText, 12);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const nestedBoundaryTarget = host.lastElementChild.childNodes[2];
selection.collapse(nestedBoundaryTarget, 12);
@@ -314,8 +308,7 @@
host.focus();
selection.setBaseAndExtent(host.childNodes[8].childNodes[0], 1, host, 1);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const fragmentEdgeBoundaryTarget = host.lastElementChild.childNodes[2];
selection.collapse(fragmentEdgeBoundaryTarget, 23);
@@ -340,8 +333,7 @@
host.focus();
selection.setBaseAndExtent(host.childNodes[4], 2, host.childNodes[8].childNodes[1], 1);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const emptyParagraphBeforeListTarget = host.lastElementChild.firstChild;
selection.collapse(emptyParagraphBeforeListTarget, 8);
@@ -368,8 +360,7 @@
const trailingListItemBoundaryEnd = host.childNodes[4].childNodes[1].firstChild;
selection.setBaseAndExtent(trailingListItemBoundaryStart, 0, trailingListItemBoundaryEnd, 6);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const trailingListItemBoundaryTarget = host.lastElementChild.childNodes[2];
selection.collapse(trailingListItemBoundaryTarget, 8);
@@ -395,8 +386,7 @@
const partialListItemPrefixEnd = host.querySelector("li").firstChild;
selection.setBaseAndExtent(host.childNodes[7], 0, partialListItemPrefixEnd, 1);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const partialListItemPrefixTarget = host.querySelectorAll("li")[1];
selection.collapse(partialListItemPrefixTarget, 0);
@@ -422,8 +412,7 @@
const boundaryListItemPrefixEnd = host.querySelector("li").firstChild;
selection.setBaseAndExtent(boundaryListItemPrefixEnd, 3, host.childNodes[5], 1);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const boundaryListItemPrefixTarget = host.querySelectorAll("li")[1].firstChild;
selection.collapse(boundaryListItemPrefixTarget, 5);
@@ -448,8 +437,7 @@
host.focus();
selection.setBaseAndExtent(host.childNodes[10], 0, host.childNodes[11].firstChild, 8);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const localTextSeamTarget = host.firstElementChild.firstChild;
selection.collapse(localTextSeamTarget, 9);
@@ -478,8 +466,7 @@
const movedParagraphWhitespaceEnd = host.querySelector("li").firstChild;
selection.setBaseAndExtent(host.firstElementChild.firstChild, 6, movedParagraphWhitespaceEnd, 8);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const movedParagraphWhitespaceTarget = host.querySelectorAll("li")[1].firstChild;
selection.collapse(movedParagraphWhitespaceTarget, 14);
@@ -508,8 +495,7 @@
const afterStyledRunSource = host.lastElementChild;
selection.setBaseAndExtent(afterStyledRunSource, 5, afterStyledRunSource.childNodes[2], 31);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const afterStyledRunTarget = host.querySelector('font[color="#3399ff"]');
selection.collapse(afterStyledRunTarget, 1);
@@ -535,8 +521,7 @@
const completeEndTextSeamSource = host.lastElementChild;
selection.setBaseAndExtent(completeEndTextSeamSource.childNodes[4], 2, completeEndTextSeamSource.childNodes[1], 1);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host.firstElementChild.firstChild, 8);
const completeEndTextSeamPasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-into-empty-block.html b/Tests/LibWeb/Text/input/Editing/rich-paste-into-empty-block.html
index e4663ee9831..fb4337a39f2 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-into-empty-block.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-into-empty-block.html
@@ -38,8 +38,7 @@
const sourceEnd = host.children[2].querySelector("i").firstChild;
selection.setBaseAndExtent(sourceStart, 0, sourceEnd, sourceEnd.length);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host.firstElementChild, 0);
const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-list-end-placeholder.html b/Tests/LibWeb/Text/input/Editing/rich-paste-list-end-placeholder.html
index a47e337f5ee..d4c56d278fa 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-list-end-placeholder.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-list-end-placeholder.html
@@ -26,8 +26,7 @@
selection.setBaseAndExtent(host.children[0].firstChild, 1, host.children[1].firstChild, 2);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host.children[2].firstChild.firstChild, 4);
const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-partial-block-wrapper.html b/Tests/LibWeb/Text/input/Editing/rich-paste-partial-block-wrapper.html
index aa9e2f2427a..d4c3e545654 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-partial-block-wrapper.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-partial-block-wrapper.html
@@ -88,8 +88,7 @@
async function copyAndPaste(selection, sourceStart, sourceStartOffset, sourceEnd, sourceEndOffset, target, targetOffset) {
selection.setBaseAndExtent(sourceStart, sourceStartOffset, sourceEnd, sourceEndOffset);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(target, targetOffset);
const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-partial-legacy-font.html b/Tests/LibWeb/Text/input/Editing/rich-paste-partial-legacy-font.html
index 663f432445f..61f11e69f82 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-partial-legacy-font.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-partial-legacy-font.html
@@ -60,8 +60,7 @@
const firstLine = host.firstChild.firstChild;
selection.setBaseAndExtent(redText, 12, firstLine, 15);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host.lastElementChild, 0);
const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
@@ -90,8 +89,7 @@
const tailText = host.children[2].firstChild;
selection.setBaseAndExtent(blueText, 1, tailText, tailText.length);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const destinationText = host.children[0].querySelector("font").firstChild;
selection.collapse(destinationText, 1);
@@ -121,8 +119,7 @@
const matchingTailText = host.children[2].firstChild;
selection.setBaseAndExtent(redSourceText, 0, matchingTailText, matchingTailText.length);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const blueDestinationText = host.children[0].querySelector("font").firstChild;
selection.collapse(blueDestinationText, 1);
@@ -157,8 +154,7 @@
const partialRedText = host.querySelector('font[color="#ff00"]').firstChild;
selection.setBaseAndExtent(firstListItemText, 12, partialRedText, 13);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host, 13);
const partialAncestorPasteInput = new Promise(resolve => host.addEventListener("input", event => {
@@ -192,8 +188,7 @@
const finalParagraphText = host.lastElementChild.childNodes[2];
selection.setBaseAndExtent(blueFont, 1, finalParagraphText, 13);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(finalParagraphText, 14);
const emptyInlineEdgePasteInput = new Promise(resolve => host.addEventListener("input", event => {
@@ -227,8 +222,7 @@
const integratedFinalParagraph = host.lastElementChild;
selection.setBaseAndExtent(integratedRedText, 6, integratedFinalParagraph, 5);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await frame.contentWindow.navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(integratedRedText, 13);
const integratedPasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-preserve-placeholder.html b/Tests/LibWeb/Text/input/Editing/rich-paste-preserve-placeholder.html
index 433cc5c7224..056ab412dc6 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-preserve-placeholder.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-preserve-placeholder.html
@@ -28,8 +28,7 @@
const sourceEnd = host.children[2].querySelector("i").firstChild;
selection.setBaseAndExtent(sourceStart, 0, sourceEnd, sourceEnd.length);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host.firstElementChild.firstChild, 1);
const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
@@ -58,8 +57,7 @@
const seamSourceEnd = host.children[0].firstChild;
selection.setBaseAndExtent(seamSourceStart, 1, seamSourceEnd, 4);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host.firstElementChild.firstChild, 11);
const seamPasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-replacement-boundary.html b/Tests/LibWeb/Text/input/Editing/rich-paste-replacement-boundary.html
index 40c2b1fc370..e266706bb1c 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-replacement-boundary.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-replacement-boundary.html
@@ -56,8 +56,7 @@
internals.sendKey(host, "C", platformControl);
// Synchronize the renderer-to-UI-process clipboard write without relying on timing.
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(nodeAt(target[0]), target[1]);
const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-style-cleanup.html b/Tests/LibWeb/Text/input/Editing/rich-paste-style-cleanup.html
index 64e614b39c3..a545a3e3e51 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-style-cleanup.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-style-cleanup.html
@@ -49,8 +49,7 @@
}, { once: true });
selection.setBaseAndExtent(host.firstChild, 0, host.firstChild, 1);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host.firstChild, 1);
const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-terminal-empty-paragraph.html b/Tests/LibWeb/Text/input/Editing/rich-paste-terminal-empty-paragraph.html
index db55fc5509b..d7e00c1791b 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-terminal-empty-paragraph.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-terminal-empty-paragraph.html
@@ -26,8 +26,7 @@
selection.setBaseAndExtent(host.children[0].firstChild, 0, host.children[1], 1);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(host.children[2].firstChild.firstChild, 4);
const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-tracked-insertion-seam.html b/Tests/LibWeb/Text/input/Editing/rich-paste-tracked-insertion-seam.html
index 8a5ce567752..bf7077bbd91 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-tracked-insertion-seam.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-tracked-insertion-seam.html
@@ -28,8 +28,7 @@
const targetText = host.querySelector("#target").firstChild;
selection.setBaseAndExtent(sourceStart, 1, targetText, 4);
internals.sendKey(host, "C", platformControl);
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
selection.collapse(targetText, 9);
const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
diff --git a/Tests/LibWeb/Text/input/Editing/rich-paste-tracked-start.html b/Tests/LibWeb/Text/input/Editing/rich-paste-tracked-start.html
index 3203a3c488d..2b43c8435f4 100644
--- a/Tests/LibWeb/Text/input/Editing/rich-paste-tracked-start.html
+++ b/Tests/LibWeb/Text/input/Editing/rich-paste-tracked-start.html
@@ -56,8 +56,7 @@
internals.sendKey(host, "C", platformControl);
// Synchronize the renderer-to-UI-process clipboard write without relying on timing.
- internals.dispatchUserActivatedEvent(host, new Event("mousedown"));
- await navigator.clipboard.read();
+ await readClipboardFromPaste(host);
const blueText = host.querySelector('font[color="#3399ff"]').firstChild;
selection.collapse(blueText, 1);
diff --git a/Tests/LibWeb/Text/input/include.js b/Tests/LibWeb/Text/input/include.js
index d5405d4a8ea..f78b456cbf4 100644
--- a/Tests/LibWeb/Text/input/include.js
+++ b/Tests/LibWeb/Text/input/include.js
@@ -60,6 +60,26 @@ async function waitForCondition(condition, attempts = 600, interval = 25) {
return true;
}
+// A page may read the clipboard only while the browser pastes it. So this pastes the clipboard into the focused navigable,
+// which must be target's, and calls read() from the paste event, which it cancels and hides from the page's listeners.
+// The read goes through the UI process, so it also waits for earlier clipboard writes to land.
+async function readClipboardFromPaste(target, read = clipboard => clipboard.read()) {
+ const targetWindow = target.ownerDocument.defaultView;
+ const { promise, resolve, reject } = Promise.withResolvers();
+ targetWindow.addEventListener(
+ "paste",
+ event => {
+ event.preventDefault();
+ event.stopImmediatePropagation();
+ read(targetWindow.navigator.clipboard).then(resolve, reject);
+ },
+ { capture: true, once: true }
+ );
+ internals.dispatchUserActivatedEvent(target, new Event("mousedown"));
+ internals.pasteFromClipboard();
+ return promise;
+}
+
// Each round runs in a task of its own, so that pending tasks can drop their references and the objects that WeakRefs
// kept alive for an earlier job are released.
async function collectGarbageUntilCleared(weakRefs, rounds = 100) {