Skip to content

Truly independent web browser

Ladybird: Add Copy Link to Highlight context action

Sam Atkinsf8bfd39ad30d
diff --git a/Libraries/LibWeb/HTML/TextDirectiveGenerator.cpp b/Libraries/LibWeb/HTML/TextDirectiveGenerator.cpp
index acd3f4152ff..dbb4e7a7427 100644
--- a/Libraries/LibWeb/HTML/TextDirectiveGenerator.cpp
+++ b/Libraries/LibWeb/HTML/TextDirectiveGenerator.cpp
@@ -395,7 +395,8 @@ Optional<URL::URL> generate_text_fragment_url(DOM::Document& document, DOM::Rang
return {};
document.update_layout(DOM::UpdateLayoutReason::DocumentFindMatchingText);
- auto* viewport = document.layout_node();
+ Layout::ForcedReadScope read { document };
+ auto* viewport = document.layout_node(read);
if (!viewport)
return {};
auto const& searchable_text = viewport->searchable_text();
@@ -578,13 +579,13 @@ Optional<URL::URL> generate_text_fragment_url(DOM::Document& document, DOM::Rang
// Retain the ordinary fragment and unknown directive parameters, but replace existing text directives with the
// newly generated selection.
auto result = current_url;
- auto ordinary_fragment = String {};
+ auto ordinary_fragment = ""sv;
Vector<StringView> retained_directives;
if (auto const& fragment = result.fragment(); fragment.has_value()) {
- auto delimiter = fragment->find_byte_offset(":~:"sv);
+ auto delimiter = fragment->find(":~:"sv);
if (delimiter.has_value()) {
- ordinary_fragment = MUST(fragment->substring_from_byte_offset(0, *delimiter));
- auto directive_part = fragment->bytes_as_string_view().substring_view(*delimiter + 3);
+ ordinary_fragment = fragment->substring_view(0, *delimiter);
+ auto directive_part = fragment->substring_view(*delimiter + 3);
for (auto parameter : directive_part.split_view('&', SplitBehavior::KeepEmpty)) {
if (!parameter.starts_with("text="sv) && !parameter.is_empty())
retained_directives.append(parameter);
@@ -600,7 +601,8 @@ Optional<URL::URL> generate_text_fragment_url(DOM::Document& document, DOM::Rang
for (auto parameter : retained_directives)
fragment_builder.appendff("{}&", parameter);
fragment_builder.append(generated_directive);
- result.set_fragment(MUST(fragment_builder.to_string()));
+ auto fragment_string = MUST(fragment_builder.to_string());
+ result.set_fragment(fragment_string.bytes_as_string_view());
return result;
}
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index b44966bce07..80fb7ac3945 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -111,7 +111,7 @@ interface Internals {
undefined setClipboardFile(DOMString name, DOMString mimeType, DOMString data);
// Test-only entry point for the browser's "Copy Link to Highlight" generator.
- Utf16DOMString? generateTextFragmentURL(Range range, Utf16USVString currentURL);
+ DOMString? generateTextFragmentURL(Range range, USVString currentURL);
// Drive marked/preedit-text composition via the same path platform input methods use. setMarkedTextFromInputMethod
// updates the inline preedit — replacing any previous preedit. commitText finalizes the composition with the given
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 5353abfb753..82490e84993 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -27,6 +27,7 @@
#include <LibWeb/DOM/Text.h>
#include <LibWeb/Editing/EditingHistory.h>
#include <LibWeb/Editing/Internal/Algorithms.h>
+#include <LibWeb/Geometry/DOMRectList.h>
#include <LibWeb/GraphemeEdgeTracker.h>
#include <LibWeb/HTML/CloseWatcherManager.h>
#include <LibWeb/HTML/DataTransfer.h>
@@ -52,6 +53,7 @@
#include <LibWeb/HTML/Navigator.h>
#include <LibWeb/HTML/PaintConfig.h>
#include <LibWeb/HTML/Scripting/Environments.h>
+#include <LibWeb/HTML/TextDirectiveGenerator.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/Layout/LayoutRustFFI.h>
#include <LibWeb/Layout/TextNode.h>
@@ -3377,14 +3379,45 @@ void EventHandler::maybe_show_context_menu(GC::Ref<DOM::Node> node, MouseEventCo
auto local_root_id = m_navigable->local_root()->id();
auto page_viewport_position = m_navigable->to_page_position(viewport_position);
+ auto selected_text_range_at_context_menu_position = [&]() -> GC::Ptr<DOM::Range> {
+ if (!m_navigable->is_top_level_traversable())
+ return nullptr;
+
+ auto selection = document->get_selection();
+ if (!selection || selection->is_collapsed())
+ return nullptr;
+
+ auto range = selection->range();
+ if (!range)
+ return nullptr;
+
+ auto selection_rects = range->get_client_rects();
+ auto click_x = coordinates.viewport_position.x().to_double();
+ auto click_y = coordinates.viewport_position.y().to_double();
+ for (u32 index = 0; index < selection_rects->length(); ++index) {
+ auto const* rect = selection_rects->item(index);
+ if (click_x >= rect->left() && click_x < rect->right()
+ && click_y >= rect->top() && click_y < rect->bottom()) {
+ if (HTML::selection_is_eligible_for_text_fragment_generation(*document, *range))
+ return range->clone_range();
+ return nullptr;
+ }
+ }
+
+ return nullptr;
+ };
+
if (auto const* link = node_under_pointer->enclosing_link_element()) {
+ // Right-clicking link text without an existing selection first selects the word under the pointer, as the
+ // ordinary page context menu does. A selected range is still only offered when the click lands within it.
+ (void)select_context_menu_text(read, document, coordinates.visual_viewport_position);
auto href = link->href();
auto url = document->encoding_parse_url(href);
if (url.has_value()) {
auto const& element = link->hyperlink_element_utils_element();
auto navigation = HTML::prepare_navigation_from_document(*document, url.release_value(), element.hyperlink_referrer_policy());
m_navigable->page().record_context_menu_request({}, { .kind = Page::ContextMenuRequest::Kind::Link, .target = const_cast<DOM::Element&>(element) });
- m_navigable->page().client().page_did_request_link_context_menu(local_root_id, page_viewport_position, move(navigation), link->target().to_byte_string(), modifiers);
+ m_navigable->page().client().page_did_request_link_context_menu(local_root_id, page_viewport_position, move(navigation), link->target().to_byte_string(), modifiers, selected_text_range_at_context_menu_position());
}
} else {
// AD-HOC: Skip up the tree to the first ancestor that is not a UA shadow DOM node, and use its context menu.
@@ -3438,7 +3471,7 @@ void EventHandler::maybe_show_context_menu(GC::Ref<DOM::Node> node, MouseEventCo
auto for_input_events_target = document->active_input_events_target() ? ContextMenuForInputEventsTarget::Yes : ContextMenuForInputEventsTarget::No;
m_navigable->page().record_context_menu_request({}, { .kind = Page::ContextMenuRequest::Kind::Page, .target = context_menu_node });
- m_navigable->page().client().page_did_request_context_menu(local_root_id, page_viewport_position, for_input_events_target, move(misspelled_word));
+ m_navigable->page().client().page_did_request_context_menu(local_root_id, page_viewport_position, for_input_events_target, move(misspelled_word), selected_text_range_at_context_menu_position());
}
}
}
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index 0a9f7f2d973..ff8a2ff6e42 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -733,8 +733,8 @@ public:
// The node an assistive technology should treat as focused: the focused element, or the document element once
// nothing in the document is focused.
virtual void page_did_change_accessibility_focus(Web::UniqueNodeID) { }
- virtual void page_did_request_context_menu([[maybe_unused]] HTML::CrossProcessId local_root_id, CSSPixelPoint, ContextMenuForInputEventsTarget, [[maybe_unused]] Optional<Utf16String> misspelled_word) { }
- virtual void page_did_request_link_context_menu([[maybe_unused]] HTML::CrossProcessId local_root_id, CSSPixelPoint, HTML::PreparedNavigationDescriptor, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers) { }
+ virtual void page_did_request_context_menu([[maybe_unused]] HTML::CrossProcessId local_root_id, CSSPixelPoint, ContextMenuForInputEventsTarget, [[maybe_unused]] Optional<Utf16String> misspelled_word, [[maybe_unused]] GC::Ptr<DOM::Range> selected_text_range) { }
+ virtual void page_did_request_link_context_menu([[maybe_unused]] HTML::CrossProcessId local_root_id, CSSPixelPoint, HTML::PreparedNavigationDescriptor, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers, [[maybe_unused]] GC::Ptr<DOM::Range> selected_text_range) { }
virtual void page_did_request_image_context_menu([[maybe_unused]] HTML::CrossProcessId local_root_id, CSSPixelPoint, HTML::PreparedNavigationDescriptor, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers, Optional<Gfx::Bitmap const*>) { }
virtual void page_did_request_media_context_menu([[maybe_unused]] HTML::CrossProcessId local_root_id, CSSPixelPoint, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers, Page::MediaContextMenu const&, HTML::PreparedNavigationDescriptor) { }
virtual void page_did_click_link(HTML::PreparedNavigationDescriptor, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers) { }
diff --git a/Libraries/LibWebView/Menu.h b/Libraries/LibWebView/Menu.h
index a0ffc094f56..c1a982eda19 100644
--- a/Libraries/LibWebView/Menu.h
+++ b/Libraries/LibWebView/Menu.h
@@ -41,6 +41,7 @@ enum class ActionID {
LookUpSelectedText,
SearchSelectedText,
+ CopyLinkToHighlight,
SpellingGuess,
NoSpellingGuesses,
IgnoreSpelling,
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 454ead67a6e..1590e5f04ee 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -3770,6 +3770,14 @@ void ViewImplementation::initialize_context_menus()
});
m_search_selected_text_action->set_visible(false);
+ m_copy_link_to_highlight_action = Action::create("Copy Link to Highlight"sv, ActionID::CopyLinkToHighlight, [this] {
+ if (!m_text_fragment_url.has_value())
+ return;
+ Application::the().insert_clipboard_entry({ "text/plain"_string, url_text_to_copy(*m_text_fragment_url) });
+ });
+ m_copy_link_to_highlight_action->set_visible(false);
+ m_copy_link_to_highlight_action->set_enabled(false);
+
m_look_up_selected_text_action = Action::create("Look Up"sv, ActionID::LookUpSelectedText, [this] {
if (!m_look_up.has_value() || !on_request_dictionary_lookup)
return;
@@ -3941,6 +3949,7 @@ void ViewImplementation::initialize_context_menus()
add_text_edit_actions(*m_page_context_menu);
m_page_context_menu->add_separator();
m_page_context_menu->add_action(*m_search_selected_text_action);
+ m_page_context_menu->add_action(*m_copy_link_to_highlight_action);
m_page_context_menu->add_separator();
m_page_context_menu->add_action(*m_take_visible_screenshot_action);
m_page_context_menu->add_action(*m_take_full_screenshot_action);
@@ -3955,6 +3964,7 @@ void ViewImplementation::initialize_context_menus()
m_link_context_menu->add_action(*m_download_linked_file_as_action);
m_link_context_menu->add_separator();
m_link_context_menu->add_action(*m_copy_url_action);
+ m_link_context_menu->add_action(*m_copy_link_to_highlight_action);
m_selected_text_link_context_menu = Menu::create("Selected Text Link Context Menu"sv);
m_selected_text_link_context_menu->add_action(*m_look_up_selected_text_action);
@@ -3963,6 +3973,7 @@ void ViewImplementation::initialize_context_menus()
add_text_edit_actions(*m_selected_text_link_context_menu);
m_selected_text_link_context_menu->add_separator();
m_selected_text_link_context_menu->add_action(*m_search_selected_text_action);
+ m_selected_text_link_context_menu->add_action(*m_copy_link_to_highlight_action);
m_image_context_menu = Menu::create("Image Context Menu"sv);
m_image_context_menu->add_action(*m_look_up_selected_text_action);
@@ -4303,8 +4314,32 @@ void ViewImplementation::activate_spelling_menu_action_for_testing(Badge<WebCont
requesting_page.async_did_complete_spelling_menu_action_for_testing(menu);
}
-void ViewImplementation::did_request_page_context_menu(Badge<WebContentPage>, WebContentPage& requesting_page, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word)
+void ViewImplementation::request_text_fragment_url(Optional<u64> generation_id)
+{
+ m_text_fragment_url.clear();
+ m_pending_text_fragment_url_request_id.clear();
+ m_copy_link_to_highlight_action->set_enabled(false);
+ m_copy_link_to_highlight_action->set_visible(generation_id.has_value());
+ if (!generation_id.has_value())
+ return;
+
+ auto request_id = m_next_text_fragment_url_request_id++;
+ m_pending_text_fragment_url_request_id = request_id;
+ page().client().async_generate_text_fragment_url(page().id(), request_id, *generation_id);
+}
+
+void ViewImplementation::did_generate_text_fragment_url(u64 request_id, Optional<URL::URL> url)
+{
+ if (!m_pending_text_fragment_url_request_id.has_value() || *m_pending_text_fragment_url_request_id != request_id)
+ return;
+ m_pending_text_fragment_url_request_id.clear();
+ m_text_fragment_url = move(url);
+ m_copy_link_to_highlight_action->set_enabled(m_text_fragment_url.has_value());
+}
+
+void ViewImplementation::did_request_page_context_menu(Badge<WebContentPage>, WebContentPage& requesting_page, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word, Optional<u64> text_fragment_generation_id)
{
+ request_text_fragment_url(text_fragment_generation_id);
m_spelling_context_menu_page = requesting_page;
update_spelling_menu_actions(move(misspelled_word));
@@ -4361,8 +4396,9 @@ void ViewImplementation::did_request_page_context_menu(Badge<WebContentPage>, We
});
}
-void ViewImplementation::did_request_link_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation)
+void ViewImplementation::did_request_link_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, Optional<u64> text_fragment_generation_id)
{
+ request_text_fragment_url(text_fragment_generation_id);
auto request_id = ++m_context_menu_request_id;
auto weak_this = make_weak_ptr();
request_context_menu_dictionary_lookup([weak_this, request_id, content_position, navigation = move(navigation)](auto const& lookup) mutable {
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index fb12d13e916..c65dbc0d5b4 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -543,12 +543,13 @@ public:
Menu& bookmark_context_menu() { return *m_bookmark_context_menu; }
Menu& bookmark_folder_context_menu() { return *m_bookmark_folder_context_menu; }
- void did_request_page_context_menu(Badge<WebContentPage>, WebContentPage& requesting_page, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word);
+ void did_request_page_context_menu(Badge<WebContentPage>, WebContentPage& requesting_page, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word, Optional<u64> text_fragment_generation_id);
void activate_spelling_menu_action_for_testing(Badge<WebContentPage>, WebContentPage& requesting_page, Web::SpellingMenuActionForTesting, u32 guess_index);
void did_request_spell_check(Badge<WebContentPage>, WebContentPage& requesting_page, u64 request_id, Utf16String text);
void reset_spell_checking();
- void did_request_link_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor);
+ void did_request_link_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor, Optional<u64> text_fragment_generation_id);
+ void did_generate_text_fragment_url(u64 request_id, Optional<URL::URL>);
void did_request_image_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor, Optional<Gfx::ShareableBitmap> bitmap);
void did_request_media_context_menu(Badge<WebContentPage>, WebContentPage& requesting_page, Gfx::IntPoint content_position, Web::MediaContextMenu menu, Web::HTML::PreparedNavigationDescriptor);
void send_to_media_context_menu_page(Function<void(WebContentPage&)> const&);
@@ -670,6 +671,7 @@ protected:
void complete_external_url_request();
void process_next_external_url_request();
void update_look_up_selected_text_action(Optional<DictionaryLookup> const& lookup, Gfx::IntPoint content_position);
+ void request_text_fragment_url(Optional<u64> generation_id);
void request_context_menu_dictionary_lookup(Function<void(Optional<DictionaryLookup> const&)> on_complete);
NonnullRefPtr<Core::Promise<bool>> select_word_for_dictionary_lookup(Gfx::IntPoint widget_position);
void reject_pending_selection_requests();
@@ -742,7 +744,11 @@ protected:
Gfx::IntPoint m_look_up_position;
RefPtr<Action> m_search_selected_text_action;
+ RefPtr<Action> m_copy_link_to_highlight_action;
Optional<String> m_search_text;
+ Optional<URL::URL> m_text_fragment_url;
+ u64 m_next_text_fragment_url_request_id { 1 };
+ Optional<u64> m_pending_text_fragment_url_request_id;
u64 m_next_selection_request_id { 1 };
HashMap<u64, NonnullRefPtr<Core::Promise<ByteString>>> m_pending_selected_text_requests;
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index ed96a141f6e..ea52e2b9294 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -2326,10 +2326,10 @@ void WebContentPage::did_change_title(Utf16String title)
}
}
-void WebContentPage::did_request_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word)
+void WebContentPage::did_request_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word, Optional<u64> text_fragment_generation_id)
{
if (auto target = view_position(local_root_id, content_position); target.has_value())
- target->view.did_request_page_context_menu({}, *this, target->position, for_input_events_target, move(misspelled_word));
+ target->view.did_request_page_context_menu({}, *this, target->position, for_input_events_target, move(misspelled_word), text_fragment_generation_id);
}
void WebContentPage::did_request_spelling_menu_action_for_testing(Web::SpellingMenuActionForTesting action, u32 guess_index)
@@ -2342,13 +2342,18 @@ void WebContentPage::did_request_spelling_menu_action_for_testing(Web::SpellingM
view().activate_spelling_menu_action_for_testing({}, *this, action, guess_index);
}
-void WebContentPage::did_request_link_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString, unsigned)
+void WebContentPage::did_request_link_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString, unsigned, Optional<u64> text_fragment_generation_id)
{
auto verified_navigation = navigation_from_page(client(), move(navigation));
if (!verified_navigation.has_value())
return;
if (auto target = view_position(local_root_id, content_position); target.has_value())
- target->view.did_request_link_context_menu({}, target->position, verified_navigation.release_value());
+ target->view.did_request_link_context_menu({}, target->position, verified_navigation.release_value(), text_fragment_generation_id);
+}
+
+void WebContentPage::did_generate_text_fragment_url(u64 request_id, Optional<URL::URL> url)
+{
+ view().did_generate_text_fragment_url(request_id, move(url));
}
void WebContentPage::did_request_image_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString, unsigned, Optional<Gfx::ShareableBitmap> bitmap)
diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.h
index ff08d6d218c..868801717f5 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -314,8 +314,9 @@ private:
virtual void did_fail_download(u64 download_id, String error) override;
virtual void did_finish_loading(Web::HTML::CrossProcessId navigable_id, Optional<Utf16String> navigation_id) override;
virtual void did_change_title(Utf16String title) override;
- virtual void did_request_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word) override;
- virtual void did_request_link_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString, unsigned) override;
+ virtual void did_request_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word, Optional<u64> text_fragment_generation_id) override;
+ virtual void did_request_link_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString, unsigned, Optional<u64> text_fragment_generation_id) override;
+ virtual void did_generate_text_fragment_url(u64 request_id, Optional<URL::URL> url) override;
virtual void did_request_image_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString, unsigned, Optional<Gfx::ShareableBitmap> bitmap) override;
virtual void did_request_media_context_menu(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, ByteString, unsigned, Web::MediaContextMenu menu, Web::HTML::PreparedNavigationDescriptor navigation) override;
virtual void did_get_highlighted_source(String html) override;
diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp
index b63f9da0827..202cde5433b 100644
--- a/Services/WebContent/ConnectionFromClient.cpp
+++ b/Services/WebContent/ConnectionFromClient.cpp
@@ -2781,6 +2781,14 @@ void ConnectionFromClient::get_selected_text(Web::PageId page_id, u64 request_id
async_did_get_selected_text(page_id, request_id, selection);
}
+void ConnectionFromClient::generate_text_fragment_url(Web::PageId page_id, u64 request_id, u64 generation_id)
+{
+ Optional<URL::URL> generated_url;
+ if (auto page = this->page(page_id); page.has_value())
+ generated_url = page->generate_text_fragment_url(generation_id);
+ async_did_generate_text_fragment_url(page_id, request_id, move(generated_url));
+}
+
static WebView::DictionaryLookupTextStyle dictionary_lookup_text_style_from_layout_node(Web::Layout::Node const& layout_node, double zoom_level)
{
auto const& font = layout_node.first_available_font();
diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.h
index 358a8d2342e..550d6d4a378 100644
--- a/Services/WebContent/ConnectionFromClient.h
+++ b/Services/WebContent/ConnectionFromClient.h
@@ -296,6 +296,7 @@ private:
virtual void request_internal_page_info(Web::PageId page_id, WebView::PageInfoType) override;
virtual void get_selected_text(Web::PageId page_id, u64 request_id) override;
+ virtual void generate_text_fragment_url(Web::PageId page_id, u64 request_id, u64 generation_id) override;
virtual void get_selected_text_for_lookup(Web::PageId page_id, u64 request_id) override;
virtual void select_word_for_dictionary_lookup(Web::PageId page_id, u64 request_id, Web::DevicePixelPoint position) override;
virtual void cut_selected_text(Web::PageId page_id, u64 request_id) override;
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index a4eb6974bb8..993c62130e9 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -47,6 +47,7 @@
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HTML/SessionHistoryEntry.h>
+#include <LibWeb/HTML/TextDirectiveGenerator.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/Infra/SerializedURL.h>
@@ -159,6 +160,8 @@ void PageClient::visit_edges(JS::Cell::Visitor& visitor)
Base::visit_edges(visitor);
visitor.visit(m_page);
visitor.visit(m_top_level_document_console_client);
+ for (auto& range : m_pending_text_fragment_generation_ranges)
+ visitor.visit(range.value);
for (auto& promise : m_pending_delete_all_cookies_promises)
visitor.visit(promise.value);
for (auto& check : m_pending_unload_checks)
@@ -1142,9 +1145,38 @@ void PageClient::page_did_set_device_pixel_ratio_for_testing(double ratio)
}));
}
-void PageClient::page_did_request_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word)
+Optional<u64> PageClient::prepare_text_fragment_generation(GC::Ptr<Web::DOM::Range> range)
{
- client().async_did_request_context_menu(m_id, local_root_id, page().css_to_device_point(content_position).to_type<int>(), for_input_events_target, move(misspelled_word));
+ // Only the latest context menu can request a URL; release ranges from earlier requests.
+ m_pending_text_fragment_generation_ranges.clear();
+ if (!range)
+ return {};
+
+ auto generation_id = m_next_text_fragment_generation_id++;
+ m_pending_text_fragment_generation_ranges.set(generation_id, *range);
+ return generation_id;
+}
+
+Optional<URL::URL> PageClient::generate_text_fragment_url(u64 generation_id)
+{
+ auto range = m_pending_text_fragment_generation_ranges.take(generation_id);
+ if (!range.has_value())
+ return {};
+
+ auto navigable = page().hosted_focused_navigable();
+ GC::Ptr<Web::DOM::Document> document;
+ if (navigable)
+ document = navigable->active_document();
+ if (!document || &(*range)->start_container()->document() != document.ptr() || &(*range)->end_container()->document() != document.ptr())
+ return {};
+
+ return Web::HTML::generate_text_fragment_url(*document, **range, document->url());
+}
+
+void PageClient::page_did_request_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word, GC::Ptr<Web::DOM::Range> selected_text_range)
+{
+ auto text_fragment_generation_id = prepare_text_fragment_generation(selected_text_range);
+ client().async_did_request_context_menu(m_id, local_root_id, page().css_to_device_point(content_position).to_type<int>(), for_input_events_target, move(misspelled_word), text_fragment_generation_id);
}
void PageClient::page_did_request_spelling_menu_action_for_testing(Web::SpellingMenuActionForTesting action, u32 guess_index)
@@ -1152,9 +1184,10 @@ void PageClient::page_did_request_spelling_menu_action_for_testing(Web::Spelling
client().async_did_request_spelling_menu_action_for_testing(m_id, action, guess_index);
}
-void PageClient::page_did_request_link_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString const& target, unsigned modifiers)
+void PageClient::page_did_request_link_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString const& target, unsigned modifiers, GC::Ptr<Web::DOM::Range> selected_text_range)
{
- client().async_did_request_link_context_menu(m_id, local_root_id, page().css_to_device_point(content_position).to_type<int>(), move(navigation), target, modifiers);
+ auto text_fragment_generation_id = prepare_text_fragment_generation(selected_text_range);
+ client().async_did_request_link_context_menu(m_id, local_root_id, page().css_to_device_point(content_position).to_type<int>(), move(navigation), target, modifiers, text_fragment_generation_id);
}
void PageClient::page_did_request_image_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString const& target, unsigned modifiers, Optional<Gfx::Bitmap const*> bitmap_pointer)
diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.h
index a362331f362..8c37f1faea8 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -104,6 +104,7 @@ public:
void set_geolocation_emulated_position(WebView::GeolocationPositionData const&, Optional<u16> error_code);
void geolocation_position_response(u64 request_id, WebView::GeolocationPositionData const&, Optional<u16> error_code);
void permission_request_completed(u64 request_id, bool granted);
+ Optional<URL::URL> generate_text_fragment_url(u64 generation_id);
void alert_closed();
void confirm_closed(bool accepted);
@@ -244,9 +245,9 @@ private:
virtual void page_did_click_link(Web::HTML::PreparedNavigationDescriptor, ByteString const& target, unsigned modifiers) override;
virtual void page_did_middle_click_link(Web::HTML::PreparedNavigationDescriptor, ByteString const& target, unsigned modifiers) override;
virtual void page_did_request_external_url(URL::URL const&, URL::Origin const& initiator_origin, bool has_transient_activation) override;
- virtual void page_did_request_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint, Web::ContextMenuForInputEventsTarget, Optional<Utf16String> misspelled_word) override;
+ virtual void page_did_request_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint, Web::ContextMenuForInputEventsTarget, Optional<Utf16String> misspelled_word, GC::Ptr<Web::DOM::Range> selected_text_range) override;
virtual void page_did_request_spelling_menu_action_for_testing(Web::SpellingMenuActionForTesting, u32 guess_index) override;
- virtual void page_did_request_link_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint, Web::HTML::PreparedNavigationDescriptor, ByteString const& target, unsigned modifiers) override;
+ virtual void page_did_request_link_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint, Web::HTML::PreparedNavigationDescriptor, ByteString const& target, unsigned modifiers, GC::Ptr<Web::DOM::Range> selected_text_range) override;
virtual void page_did_request_image_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint, Web::HTML::PreparedNavigationDescriptor, ByteString const& target, unsigned modifiers, Optional<Gfx::Bitmap const*>) override;
virtual void page_did_request_media_context_menu(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint, ByteString const& target, unsigned modifiers, Web::Page::MediaContextMenu const&, Web::HTML::PreparedNavigationDescriptor) override;
virtual void page_did_change_active_document_in_top_level_browsing_context(Web::DOM::Document&) override;
@@ -379,6 +380,7 @@ private:
void setup_palette();
ConnectionFromClient& client() const;
void send_dom_mutation(Web::DOM::Node const& target, WebView::Mutation mutation);
+ Optional<u64> prepare_text_fragment_generation(GC::Ptr<Web::DOM::Range>);
PageHost& m_owner;
GC::Ref<Web::Page> m_page;
@@ -393,6 +395,8 @@ private:
double m_maximum_frames_per_second { 60.0 };
Web::PageId m_id { 0 };
u64 m_next_delete_all_cookies_request_id { 1 };
+ u64 m_next_text_fragment_generation_id { 1 };
+ HashMap<u64, GC::Ref<Web::DOM::Range>> m_pending_text_fragment_generation_ranges;
HashMap<u64, GC::Ref<Web::WebIDL::Promise>> m_pending_delete_all_cookies_promises;
HashMap<Web::HTML::CrossProcessId, GC::Ref<GC::Function<void(Web::HTML::CheckIfUnloadingIsCanceledResult)>>> m_pending_unload_checks;
HashMap<u64, GC::Ref<Web::Fetch::Infrastructure::FetchController>> m_download_controllers;
diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipc
index 39916cb691d..deba78060d1 100644
--- a/Services/WebContent/WebContentClient.ipc
+++ b/Services/WebContent/WebContentClient.ipc
@@ -125,8 +125,9 @@ endpoint WebContentClient routed by Web::PageId page_id
did_click_link(Web::PageId page_id, Web::HTML::PreparedNavigationDescriptor navigation, ByteString target, unsigned modifiers) =|
did_middle_click_link(Web::PageId page_id, Web::HTML::PreparedNavigationDescriptor navigation, ByteString target, unsigned modifiers) =|
did_request_external_url(Web::PageId page_id, URL::URL url, URL::Origin initiator_origin, bool has_transient_activation) =|
- did_request_context_menu(Web::PageId page_id, Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word) =|
- did_request_link_context_menu(Web::PageId page_id, Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString target, unsigned modifiers) =|
+ did_request_context_menu(Web::PageId page_id, Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word, Optional<u64> text_fragment_generation_id) =|
+ did_request_link_context_menu(Web::PageId page_id, Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString target, unsigned modifiers, Optional<u64> text_fragment_generation_id) =|
+ did_generate_text_fragment_url(Web::PageId page_id, u64 request_id, Optional<URL::URL> url) =|
did_request_image_context_menu(Web::PageId page_id, Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, ByteString target, unsigned modifiers, Optional<Gfx::ShareableBitmap> bitmap) =|
did_request_media_context_menu(Web::PageId page_id, Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, ByteString target, unsigned modifiers, Web::MediaContextMenu menu, Web::HTML::PreparedNavigationDescriptor navigation) =|
did_request_alert(Web::PageId page_id, Utf16String message) =|
diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipc
index 9a317a3fd88..77ee6aa8ea7 100644
--- a/Services/WebContent/WebContentServer.ipc
+++ b/Services/WebContent/WebContentServer.ipc
@@ -221,6 +221,7 @@ endpoint WebContentServer routed by Web::PageId page_id
request_internal_page_info(Web::PageId page_id, WebView::PageInfoType type) =|
get_selected_text(Web::PageId page_id, u64 request_id) =|
+ generate_text_fragment_url(Web::PageId page_id, u64 request_id, u64 generation_id) =|
get_selected_text_for_lookup(Web::PageId page_id, u64 request_id) =|
select_word_for_dictionary_lookup(Web::PageId page_id, u64 request_id, Web::DevicePixelPoint position) =|
cut_selected_text(Web::PageId page_id, u64 request_id) =|
diff --git a/Tests/LibWeb/Text/expected/context-menu-selects-text.txt b/Tests/LibWeb/Text/expected/context-menu-selects-text.txt
index e6ad317dea4..a24fbe67333 100644
--- a/Tests/LibWeb/Text/expected/context-menu-selects-text.txt
+++ b/Tests/LibWeb/Text/expected/context-menu-selects-text.txt
@@ -3,5 +3,6 @@ url token: "ladybird.org/path?x=1"
canceled context menu: ""
existing selection preserved: "existing"
existing selection replaced: "other"
+anchor selection preserved: "anchor"
image preserves selection: "existing"
user-select none: ""
diff --git a/Tests/LibWeb/Text/input/context-menu-selects-text.html b/Tests/LibWeb/Text/input/context-menu-selects-text.html
index 126809a16bc..a5650ccc85b 100644
--- a/Tests/LibWeb/Text/input/context-menu-selects-text.html
+++ b/Tests/LibWeb/Text/input/context-menu-selects-text.html
@@ -11,6 +11,7 @@
<div id="prevented">prevented</div>
<div id="existing">existing selection</div>
<div id="other">other target</div>
+<a id="anchor" href="https://example.com/">anchor selection</a>
<img id="image" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==" style="width: 20px; height: 20px;">
<div id="none" style="user-select: none;">hidden.org</div>
<script>
@@ -49,6 +50,14 @@
rightClick(other);
println(`existing selection replaced: "${selectedText()}"`);
+ const anchorRange = document.createRange();
+ anchorRange.setStart(anchor.firstChild, 0);
+ anchorRange.setEnd(anchor.firstChild, 6);
+ getSelection().removeAllRanges();
+ getSelection().addRange(anchorRange);
+ rightClick(anchor);
+ println(`anchor selection preserved: "${selectedText()}"`);
+
getSelection().removeAllRanges();
getSelection().addRange(range);
rightClick(image);