diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index 839e268722..fecaf974e1 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -775,6 +776,7 @@ void Window::initialize_web_interfaces(Badge) define_native_function(realm, "cancelAnimationFrame", cancel_animation_frame, 1, attr); define_native_function(realm, "atob", atob, 1, attr); define_native_function(realm, "btoa", btoa, 1, attr); + define_native_function(realm, "focus", focus, 0, attr); define_native_function(realm, "queueMicrotask", queue_microtask, 1, attr); @@ -1082,6 +1084,28 @@ JS_DEFINE_NATIVE_FUNCTION(Window::btoa) return JS::js_string(vm, move(encoded)); } +// https://html.spec.whatwg.org/multipage/interaction.html#dom-window-focus +JS_DEFINE_NATIVE_FUNCTION(Window::focus) +{ + auto* impl = TRY(impl_from(vm)); + + // 1. Let current be this Window object's browsing context. + auto* current = impl->browsing_context(); + + // 2. If current is null, then return. + if (!current) + return JS::js_undefined(); + + // 3. Run the focusing steps with current. + // FIXME: We should pass in the browsing context itself instead of the active document, however the focusing steps don't currently accept browsing contexts. + // Passing in a browsing context always makes it resolve to its active document for focus, so this is fine for now. + run_focusing_steps(current->active_document()); + + // FIXME: 4. If current is a top-level browsing context, user agents are encouraged to trigger some sort of notification to indicate to the user that the page is attempting to gain focus. + + return JS::js_undefined(); +} + // https://html.spec.whatwg.org/multipage/window-object.html#number-of-document-tree-child-browsing-contexts JS::ThrowCompletionOr Window::document_tree_child_browsing_context_count() const { diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index 03fdcdd02f..9b5b405ff0 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -256,6 +256,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame); JS_DECLARE_NATIVE_FUNCTION(atob); JS_DECLARE_NATIVE_FUNCTION(btoa); + JS_DECLARE_NATIVE_FUNCTION(focus); JS_DECLARE_NATIVE_FUNCTION(get_computed_style); JS_DECLARE_NATIVE_FUNCTION(match_media);