From 973cc67e81ad6a3a70b693a4b0559d93a4c94fc9 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Wed, 3 Apr 2024 16:12:50 +0200 Subject: [PATCH] LibWeb: Implement element activation behavior for ismap s This allows you to click on a that has an ismap attribute, and will result in the navigation URL having the coordinates appended as a query to the URL. --- .../LibWeb/HTML/HTMLAnchorElement.cpp | 47 ++++++++++++------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLAnchorElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLAnchorElement.cpp index 56516ed247f..26a143c7533 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLAnchorElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLAnchorElement.cpp @@ -1,13 +1,19 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2024, Shannon Booth * * SPDX-License-Identifier: BSD-2-Clause */ #include +#include +#include #include +#include #include +#include #include +#include namespace Web::HTML { @@ -61,23 +67,30 @@ void HTMLAnchorElement::activation_behavior(Web::DOM::Event const& event) // 2. Let hyperlinkSuffix be null. Optional hyperlink_suffix {}; - // FIXME: 3. If element is an a element, and event's target is an img with an ismap attribute specified, then: - // - // 3.1. Let x and y be 0. - // - // 3.2. If event's isTrusted attribute is initialized to true, then - // set x to the distance in CSS pixels from the left edge of the image - // to the location of the click, and set y to the distance in CSS - // pixels from the top edge of the image to the location of the click. - // - // 3.3. If x is negative, set x to 0. - // - // 3.4. If y is negative, set y to 0. - // - // 3.5. Set hyperlinkSuffix to the concatenation of U+003F (?), the - // value of x expressed as a base-ten integer using ASCII digits, - // U+002C (,), and the value of y expressed as a base-ten integer - // using ASCII digits. + // 3. If element is an a element, and event's target is an img with an ismap attribute specified, then: + if (event.target() && is(*event.target()) && static_cast(*event.target()).has_attribute(AttributeNames::ismap)) { + // 1. Let x and y be 0. + CSSPixels x { 0 }; + CSSPixels y { 0 }; + + // 2. If event's isTrusted attribute is initialized to true, then set x to the distance in CSS pixels from the left edge of the image + // to the location of the click, and set y to the distance in CSS pixels from the top edge of the image to the location of the click. + if (event.is_trusted() && is(event)) { + auto const& mouse_event = static_cast(event); + x = CSSPixels { mouse_event.offset_x() }; + y = CSSPixels { mouse_event.offset_y() }; + } + + // 3. If x is negative, set x to 0. + x = max(x, 0); + + // 4. If y is negative, set y to 0. + y = max(y, 0); + + // 5. Set hyperlinkSuffix to the concatenation of U+003F (?), the value of x expressed as a base-ten integer using ASCII digits, + // U+002C (,), and the value of y expressed as a base-ten integer using ASCII digits. + hyperlink_suffix = MUST(String::formatted("?{},{}", x.to_int(), y.to_int())); + } // 4. Let userInvolvement be event's user navigation involvement. auto user_involvement = user_navigation_involvement(event);