Skip to content

Truly independent web browser

LibWeb: Keep the hovered link of a page in a PageHoverLink

Andreas Kling5b26433cbc51
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 020a05a33ff..2131aacc179 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -709,6 +709,7 @@ set(SOURCES
Page/MiddleButtonScrollHandler.cpp
Page/Page.cpp
Page/PageCursor.cpp
+ Page/PageHoverLink.cpp
Page/RenderingStats.cpp
Page/ScreenWakeLockHandle.cpp
PaintTiming/PaintTiming.cpp
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index e1250d411ed..12e00b0938d 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -99,6 +99,7 @@
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HTML/WindowProxy.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
+#include <LibWeb/Infra/SerializedURL.h>
#include <LibWeb/Internals/InternalGamepad.h>
#include <LibWeb/Internals/Internals.h>
#include <LibWeb/Layout/NodeArena.h>
@@ -1086,6 +1087,14 @@ void Internals::reset_zoom()
local_root->reset_zoom();
}
+Optional<Utf16String> Internals::hovered_link()
+{
+ auto url = page().hover_link().current();
+ if (!url.has_value())
+ return {};
+ return utf16_string_from_url_ascii(url->serialize());
+}
+
Utf16String Internals::current_cursor()
{
auto& page = this->page();
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index 3d646f12c2b..b3fdc246aa2 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -130,6 +130,7 @@ public:
void pinch_through_ui_process(double x, double y, double scale_delta, bool continues_gesture);
void reset_zoom();
+ Optional<Utf16String> hovered_link();
Utf16String current_cursor();
Optional<Utf16String> current_cursor_pixel(i32 x, i32 y);
Utf16String theme_color();
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index 8eec2bf1238..3469cd0245a 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -111,6 +111,8 @@ interface Internals {
undefined pinchThroughUIProcess(double x, double y, double scaleDelta, optional boolean continuesGesture = false);
undefined resetZoom();
+ // The URL of the link the page asks its client to show as hovered, or null when it shows none.
+ DOMString? hoveredLink();
DOMString currentCursor();
// The color of one pixel of the current image cursor's bitmap, or null when the cursor is no image.
DOMString? currentCursorPixel(long x, long y);
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 97cebfaa823..63717dae2dc 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -5701,22 +5701,16 @@ void EventHandler::report_hovered_node_to_client(GC::Ptr<DOM::Node> target)
page.set_is_in_tooltip_area(false);
}
- HTML::HTMLHyperlinkElementUtils const* hovered_link_element = nullptr;
- if (target)
- hovered_link_element = target->enclosing_link_element();
- if (hovered_link_element) {
- if (auto link_url = target->document().encoding_parse_url(hovered_link_element->href()); link_url.has_value()) {
- page.client().page_did_hover_link(*link_url);
- page.set_is_hovering_link(true);
- }
- } else if (page.is_hovering_link()) {
- page.client().page_did_unhover_link();
- page.set_is_hovering_link(false);
+ Optional<URL::URL> hovered_link_url;
+ if (target) {
+ if (auto const* hovered_link_element = target->enclosing_link_element())
+ hovered_link_url = target->document().encoding_parse_url(hovered_link_element->href());
}
+ page.hover_link().request(move(hovered_link_url));
// Whatever the client shows now, this handler reported — so the hover is this handler's to end.
GC::Ptr<HTML::LocalNavigable> hover_reporting_navigable;
- if (page.is_hovering_link() || page.is_in_tooltip_area())
+ if (page.hover_link().has_link() || page.is_in_tooltip_area())
hover_reporting_navigable = m_navigable;
page.set_hover_reporting_navigable({}, hover_reporting_navigable);
}
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index ad2bcc31588..c5f381463a2 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -59,6 +59,7 @@
#include <LibWeb/HTML/WebViewHints.h>
#include <LibWeb/Loader/FileRequest.h>
#include <LibWeb/Page/PageCursor.h>
+#include <LibWeb/Page/PageHoverLink.h>
#include <LibWeb/Page/ScreenWakeLockHandle.h>
#include <LibWeb/Painting/ChromeMetrics.h>
#include <LibWebCommon/CSS/PreferredColorScheme.h>
@@ -264,9 +265,6 @@ public:
bool is_webdriver_active() const { return m_is_webdriver_active; }
void set_is_webdriver_active(bool b) { m_is_webdriver_active = b; }
- bool is_hovering_link() const { return m_is_hovering_link; }
- void set_is_hovering_link(bool b) { m_is_hovering_link = b; }
-
bool is_in_tooltip_area() const { return m_is_in_tooltip_area; }
void set_is_in_tooltip_area(bool b) { m_is_in_tooltip_area = b; }
@@ -275,6 +273,7 @@ public:
Gfx::Cursor current_cursor() const { return m_cursor->current(); }
PageCursor& cursor() { return *m_cursor; }
+ PageHoverLink& hover_link() { return *m_hover_link; }
DevicePixelPoint window_position() const { return m_window_position; }
void set_window_position(DevicePixelPoint position) { m_window_position = position; }
@@ -565,10 +564,10 @@ private:
// The webdriver-active flag is set to true when the user agent is under remote control. It is initially false.
bool m_is_webdriver_active { false };
- bool m_is_hovering_link { false };
bool m_is_in_tooltip_area { false };
NonnullRefPtr<PageCursor> m_cursor { PageCursor::create() };
+ NonnullRefPtr<PageHoverLink> m_hover_link { PageHoverLink::create() };
DevicePixelPoint m_window_position {};
DevicePixelSize m_window_size {};
@@ -825,8 +824,6 @@ public:
virtual void page_did_stop_tooltip_override() { }
virtual void page_did_enter_tooltip_area(ByteString const&) { }
virtual void page_did_leave_tooltip_area() { }
- virtual void page_did_hover_link(URL::URL const&) { }
- virtual void page_did_unhover_link() { }
virtual void page_did_change_favicon(Gfx::Bitmap const&) { }
virtual void page_did_request_alert(Utf16String const&) { }
virtual void page_did_request_before_unload(String const&) { }
diff --git a/Libraries/LibWeb/Page/PageHoverLink.cpp b/Libraries/LibWeb/Page/PageHoverLink.cpp
new file mode 100644
index 00000000000..464c5adfa49
--- /dev/null
+++ b/Libraries/LibWeb/Page/PageHoverLink.cpp
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/Page/PageHoverLink.h>
+
+namespace Web {
+
+Optional<URL::URL> PageHoverLink::current() const
+{
+ MutexLocker locker(m_mutex);
+ return m_current;
+}
+
+bool PageHoverLink::has_link() const
+{
+ MutexLocker locker(m_mutex);
+ return m_current.has_value();
+}
+
+void PageHoverLink::request(Optional<URL::URL> url)
+{
+ // NB: A blob URL's entry is counted without atomics, and a client only shows the URL, so none crosses threads here.
+ if (url.has_value())
+ url->set_blob_url_entry({});
+
+ MutexLocker locker(m_mutex);
+ if (m_current == url)
+ return;
+ m_current = move(url);
+ if (m_send)
+ m_send(m_current);
+}
+
+void PageHoverLink::set_send(Send send)
+{
+ MutexLocker locker(m_mutex);
+ m_send = move(send);
+}
+
+}
diff --git a/Libraries/LibWeb/Page/PageHoverLink.h b/Libraries/LibWeb/Page/PageHoverLink.h
new file mode 100644
index 00000000000..17bbdec3303
--- /dev/null
+++ b/Libraries/LibWeb/Page/PageHoverLink.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/AtomicRefCounted.h>
+#include <AK/Function.h>
+#include <AK/Mutex.h>
+#include <AK/NonnullRefPtr.h>
+#include <AK/Optional.h>
+#include <LibURL/URL.h>
+#include <LibWeb/Export.h>
+
+namespace Web {
+
+// The link a page asks its client to show the URL of, as the pointer hovers it. No script takes part in this, so what
+// is hovered may be told from any thread. Each asks under one lock, so the client is left showing the link asked for
+// last.
+class WEB_API PageHoverLink final : public AtomicRefCounted<PageHoverLink> {
+public:
+ // Asks the client to show the URL of a link, or none, from any thread.
+ using Send = Function<void(Optional<URL::URL> const&)>;
+
+ static NonnullRefPtr<PageHoverLink> create() { return adopt_ref(*new PageHoverLink); }
+
+ Optional<URL::URL> current() const;
+ bool has_link() const;
+
+ // Asks the client to show `url`, or no link, where the link asked for last is another.
+ void request(Optional<URL::URL> url);
+
+ // What asks the client from now on. None asks nothing.
+ void set_send(Send);
+
+private:
+ PageHoverLink() = default;
+
+ mutable Mutex m_mutex;
+ Optional<URL::URL> m_current;
+ Send m_send;
+};
+
+}
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index 8cd5faa478d..723403f19fc 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -158,6 +158,12 @@ PageClient::PageClient(PageHost& owner, Web::PageId id, Optional<Web::HTML::Cros
m_page->cursor().set_send([&client = client(), id = m_id](Gfx::Cursor const& cursor) {
client.async_did_request_cursor_change(id, cursor);
});
+ m_page->hover_link().set_send([&client = client(), id = m_id](Optional<URL::URL> const& url) {
+ if (url.has_value())
+ client.async_did_hover_link(id, *url);
+ else
+ client.async_did_unhover_link(id);
+ });
m_frame_timer = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(0), [this] {
if (m_is_closed)
@@ -1005,16 +1011,6 @@ void PageClient::page_did_leave_tooltip_area()
client().async_did_leave_tooltip_area(m_id);
}
-void PageClient::page_did_hover_link(URL::URL const& url)
-{
- client().async_did_hover_link(m_id, url);
-}
-
-void PageClient::page_did_unhover_link()
-{
- client().async_did_unhover_link(m_id);
-}
-
void PageClient::page_did_click_link(Web::HTML::PreparedNavigationDescriptor navigation, ByteString const& target, unsigned modifiers)
{
client().async_did_click_link(m_id, move(navigation), target, modifiers);
diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.h
index 26bea514560..cedbbdb8eb6 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -269,8 +269,6 @@ private:
virtual void page_did_stop_tooltip_override() override;
virtual void page_did_enter_tooltip_area(ByteString const&) override;
virtual void page_did_leave_tooltip_area() override;
- virtual void page_did_hover_link(URL::URL const&) override;
- virtual void page_did_unhover_link() override;
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;
diff --git a/Tests/LibWeb/Text/expected/UIEvents/hovered-link.txt b/Tests/LibWeb/Text/expected/UIEvents/hovered-link.txt
new file mode 100644
index 00000000000..1e4894cf594
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/UIEvents/hovered-link.txt
@@ -0,0 +1,6 @@
+text in a link: https://example.com/absolute
+a relative link: https://example.com/base/relative?query=1
+an anchor without href: null
+back on a link: https://example.com/absolute
+no link: null
+a link around a slot: https://example.com/base/slot
diff --git a/Tests/LibWeb/Text/input/UIEvents/hovered-link.html b/Tests/LibWeb/Text/input/UIEvents/hovered-link.html
new file mode 100644
index 00000000000..62c137ad9e8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/UIEvents/hovered-link.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<base href="https://example.com/base/">
+<style>
+ body { margin: 0; }
+ a, div { display: block; width: 100px; height: 20px; }
+</style>
+<a href="https://example.com/absolute"><span>Text in a link</span></a>
+<a href="relative?query=1"></a>
+<a></a>
+<div></div>
+<div id="host"><span>Slotted text</span></div>
+<script>
+ test(() => {
+ host.attachShadow({ mode: "open" }).innerHTML = `<a href="slot"><slot></slot></a>`;
+
+ const hover = (name, y) => {
+ internals.mouseMove(50, y);
+ println(`${name}: ${internals.hoveredLink()}`);
+ };
+
+ hover("text in a link", 10);
+ hover("a relative link", 30);
+ hover("an anchor without href", 50);
+ hover("back on a link", 10);
+ hover("no link", 70);
+ hover("a link around a slot", 90);
+ });
+</script>