Truly independent web browser
LibWeb: Let a handler end only its own document’s hovered link
sideshowbarker15239460e5a2
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cppindex f39fb14f75c..34c4e5a1837 100644--- a/Libraries/LibWeb/Page/EventHandler.cpp+++ b/Libraries/LibWeb/Page/EventHandler.cpp@@ -5832,16 +5832,24 @@ void EventHandler::report_hovered_link_to_client(GC::Ptr<DOM::Node> target){auto& page = m_navigable->page();- Optional<URL::URL> hovered_link_url;- Optional<UniqueNodeID> hovered_document;- if (target) {- hovered_document = target->document().unique_id();- // NB: A link in editable content is text to edit, which clicking does not follow.- auto const* hovered_link_element = target->enclosing_link_element();- if (hovered_link_element && !hovered_link_element->hyperlink_element_utils_element().is_editable_or_editing_host())- hovered_link_url = target->document().encoding_parse_url(hovered_link_element->href());+ // With no target, the pointer is over nothing of this handler's document: It left the view, or the iframe this+ // handler is for, or the document is going away. So, end only a link asked for over this document. Another+ // document's link stands — e.g., one of the page around an iframe the pointer just left, which a hover of the render+ // clock may have reported before this handler heard that the pointer left.+ if (!target) {+ if (auto document = m_navigable->active_document())+ page.hover_link().withdraw(document->unique_id());+ if (page.hover_reporting_navigable() == m_navigable)+ page.set_hover_reporting_navigable({}, nullptr);+ return;}- page.hover_link().request(move(hovered_link_url), hovered_document);++ Optional<URL::URL> hovered_link_url;+ // NB: A link in editable content is text to edit, which clicking does not follow.+ auto const* hovered_link_element = target->enclosing_link_element();+ if (hovered_link_element && !hovered_link_element->hyperlink_element_utils_element().is_editable_or_editing_host())+ hovered_link_url = target->document().encoding_parse_url(hovered_link_element->href());+ page.hover_link().request(move(hovered_link_url), target->document().unique_id());// Whatever the client shows now, this handler reported — so the hover is this handler's to end.GC::Ptr<HTML::LocalNavigable> hover_reporting_navigable;diff --git a/Tests/LibWebView/TestLinkHoverStatus.cpp b/Tests/LibWebView/TestLinkHoverStatus.cppindex ec5d7225543..26bc5e960ec 100644--- a/Tests/LibWebView/TestLinkHoverStatus.cpp+++ b/Tests/LibWebView/TestLinkHoverStatus.cpp@@ -71,6 +71,7 @@ TEST_CASE(link_hover_status)size_t hovers_reported = 0;size_t unhovers_reported = 0;+ size_t unhovers_of_the_page_link = 0;Optional<URL::URL> hovered_url;view->on_link_hover = [&](auto const& url) {++hovers_reported;@@ -78,6 +79,8 @@ TEST_CASE(link_hover_status)};view->on_link_unhover = [&] {++unhovers_reported;+ if (hovered_url.has_value() && hovered_url->serialize() == "https://example.com/parent"sv)+ ++unhovers_of_the_page_link;hovered_url.clear();};@@ -141,8 +144,9 @@ TEST_CASE(link_hover_status)view->on_title_change = [&](auto const& title) { last_title = title; };view->load_html(R"~~~(<!DOCTYPE html><iframe id="frame" style="position:fixed;left:0;top:0;width:400px;height:300px;border:0" srcdoc="<a href='https://example.com/in-iframe' style='position:fixed;left:0;top:0;width:400px;height:300px'>Link</a>"></iframe>-<a href="https://example.com/parent" style="position:fixed;left:400px;top:0;width:400px;height:300px">Link</a>+<a id="page_link" href="https://example.com/parent" style="position:fixed;left:400px;top:0;width:400px;height:300px">Link</a><script>+page_link.addEventListener("mouseover", () => { document.title = "over the page link"; });let frame_loads = 0;frame.addEventListener("load", () => { document.title = "frame load " + (++frame_loads); });function navigate_frame() { frame.srcdoc = frame.getAttribute("srcdoc") + "<!-- " + frame_loads + " -->"; }@@ -160,21 +164,26 @@ function navigate_frame() { frame.srcdoc = frame.getAttribute("srcdoc") + "<!--Core::EventLoop::current().spin_until([&]() { return unhovers_reported >= 6; });Core::EventLoop::current().spin_until([&]() { return last_title == "frame load 2"sv; });- // The new iframe document's link is a fresh target. Then over to the page's own link: The iframe's handler hears- // that the pointer left the iframe, and reports the unhover, before the page's handler reports the page's link.+ // The new iframe document's link is a fresh target. Then over to the page's own link: Either the iframe's handler+ // hears that the pointer left the iframe, and reports the unhover, before the page's handler reports the page's+ // link — or a hover of the render clock reports the page's link first, and the iframe's handler then leaves it be,+ // since it's no link of the iframe's.move_mouse_to(*view, { 20, 20 });Core::EventLoop::current().spin_until([&]() { return hovers_reported >= 7; });VERIFY(hovered_url->serialize() == "https://example.com/in-iframe"sv);+ // The page's handler reports its link after it fires mouseover at it. By then, the iframe's handler has sent all it+ // will for the move, and in neither order may it have ended the page's link.move_mouse_to(*view, { 600, 20 });- Core::EventLoop::current().spin_until([&]() { return hovers_reported >= 8; });- VERIFY(hovered_url->serialize() == "https://example.com/parent"sv);- VERIFY(unhovers_reported == 7);+ Core::EventLoop::current().spin_until([&]() {+ return last_title == "over the page link"sv && hovered_url.has_value() && hovered_url->serialize() == "https://example.com/parent"sv;+ });+ VERIFY(unhovers_of_the_page_link == 0);// So, the iframe navigating now must leave the page's link reported: The hover the client shows is the page's// handler's to end, not the iframe's. The iframe's load comes after any unhover its replacement would have sent.view->run_javascript("navigate_frame()"_string);Core::EventLoop::current().spin_until([&]() { return last_title == "frame load 3"sv; });- VERIFY(unhovers_reported == 7);+ VERIFY(unhovers_of_the_page_link == 0);VERIFY(hovered_url->serialize() == "https://example.com/parent"sv);// Back over the iframe's link, then a new top-level document: The iframe dies along with the old one, and its