From 1165a94624db0e4313a958ebba8290f1478923ff Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 6 Feb 2022 18:45:29 +0100 Subject: [PATCH] LibWeb: Implement BrowsingContext::currently_focused_area() This is "currently focused area of a top level browsing context" from the HTML spec. --- .../Libraries/LibWeb/HTML/BrowsingContext.cpp | 30 +++++++++++++++++++ .../Libraries/LibWeb/HTML/BrowsingContext.h | 2 ++ 2 files changed, 32 insertions(+) diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp index fb15aa9335..76a7ec5999 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -362,4 +363,33 @@ bool BrowsingContext::has_a_rendering_opportunity() const return true; } +// https://html.spec.whatwg.org/multipage/interaction.html#currently-focused-area-of-a-top-level-browsing-context +RefPtr BrowsingContext::currently_focused_area() +{ + // 1. If topLevelBC does not have system focus, then return null. + if (!is_focused_context()) + return nullptr; + + // 2. Let candidate be topLevelBC's active document. + auto* candidate = active_document(); + + // 3. While candidate's focused area is a browsing context container with a non-null nested browsing context: + // set candidate to the active document of that browsing context container's nested browsing context. + while (candidate->focused_element() + && is(candidate->focused_element()) + && static_cast(*candidate->focused_element()).nested_browsing_context()) { + candidate = static_cast(*candidate->focused_element()).nested_browsing_context()->active_document(); + } + + // 4. If candidate's focused area is non-null, set candidate to candidate's focused area. + if (candidate->focused_element()) { + // NOTE: We return right away here instead of assigning to candidate, + // since that would require compromising type safety. + return candidate->focused_element(); + } + + // 5. Return candidate. + return candidate; +} + } diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContext.h b/Userland/Libraries/LibWeb/HTML/BrowsingContext.h index 559a59ffae..b9b71debd5 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContext.h +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContext.h @@ -104,6 +104,8 @@ public: bool has_a_rendering_opportunity() const; + RefPtr currently_focused_area(); + private: explicit BrowsingContext(Page&, HTML::BrowsingContextContainer*);