From 3d075e55f59f02a518bef68b9a7218f0dfb8b7b4 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 6 Mar 2023 21:17:19 +0000 Subject: [PATCH] LibWeb/HTML: Port Window.scrollBy() to IDL --- Userland/Libraries/LibWeb/HTML/Window.cpp | 91 ++++++++++------------- Userland/Libraries/LibWeb/HTML/Window.h | 4 +- Userland/Libraries/LibWeb/HTML/Window.idl | 2 + 3 files changed, 43 insertions(+), 54 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index bb3a4ad6334..6d6ae49cdd6 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -1017,8 +1017,6 @@ WebIDL::ExceptionOr Window::initialize_web_interfaces(Badge> Window::performance() { @@ -1643,56 +1680,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::get_selection) return impl->associated_document().get_selection(); } -// https://www.w3.org/TR/cssom-view/#dom-window-scrollby -JS_DEFINE_NATIVE_FUNCTION(Window::scroll_by) -{ - auto& realm = *vm.current_realm(); - - auto* impl = TRY(impl_from(vm)); - if (!impl->page()) - return JS::js_undefined(); - auto& page = *impl->page(); - - JS::Object* options = nullptr; - - if (vm.argument_count() == 0) { - options = JS::Object::create(realm, nullptr); - } else if (vm.argument_count() == 1) { - options = TRY(vm.argument(0).to_object(vm)); - } else if (vm.argument_count() >= 2) { - // We ignore arguments 2+ in line with behavior of Chrome and Firefox - options = JS::Object::create(realm, nullptr); - MUST(options->set("left", vm.argument(0), ShouldThrowExceptions::No)); - MUST(options->set("top", vm.argument(1), ShouldThrowExceptions::No)); - MUST(options->set("behavior", MUST_OR_THROW_OOM(JS::PrimitiveString::create(vm, "auto"sv)), ShouldThrowExceptions::No)); - } - - auto left_value = TRY(options->get("left")); - auto left = TRY(left_value.to_double(vm)); - - auto top_value = TRY(options->get("top")); - auto top = TRY(top_value.to_double(vm)); - - left = JS::Value(left).is_finite_number() ? left : 0.0; - top = JS::Value(top).is_finite_number() ? top : 0.0; - - auto current_scroll_position = page.top_level_browsing_context().viewport_scroll_offset().to_type(); - left = left + static_cast(current_scroll_position.x()); - top = top + static_cast(current_scroll_position.y()); - - auto behavior_string_value = TRY(options->get("behavior")); - auto behavior_string = behavior_string_value.is_undefined() ? "auto" : TRY(behavior_string_value.to_deprecated_string(vm)); - if (behavior_string != "smooth" && behavior_string != "auto") - return vm.throw_completion("Behavior is not one of 'smooth' or 'auto'"sv); - auto behavior = (behavior_string == "smooth") ? Bindings::ScrollBehavior::Smooth : Bindings::ScrollBehavior::Auto; - - // FIXME: Spec wants us to call scroll(options) here. - // The only difference is that would invoke the viewport calculations that scroll() - // is not actually doing yet, so this is the same for now. - perform_a_scroll(page, left, top, nullptr, behavior); - return JS::js_undefined(); -} - JS_DEFINE_NATIVE_FUNCTION(Window::screen_left_getter) { auto* impl = TRY(impl_from(vm)); diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index 3e89d3ae28c..96fcabad917 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -177,6 +177,8 @@ public: double scroll_y() const; void scroll(ScrollToOptions const&); void scroll(double x, double y); + void scroll_by(ScrollToOptions); + void scroll_by(double x, double y); WebIDL::ExceptionOr> performance(); @@ -246,8 +248,6 @@ private: JS_DECLARE_NATIVE_FUNCTION(device_pixel_ratio_getter); - JS_DECLARE_NATIVE_FUNCTION(scroll_by); - JS_DECLARE_NATIVE_FUNCTION(screen_x_getter); JS_DECLARE_NATIVE_FUNCTION(screen_y_getter); JS_DECLARE_NATIVE_FUNCTION(screen_left_getter); diff --git a/Userland/Libraries/LibWeb/HTML/Window.idl b/Userland/Libraries/LibWeb/HTML/Window.idl index b7ba3282332..be6e2b4cef1 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.idl +++ b/Userland/Libraries/LibWeb/HTML/Window.idl @@ -61,6 +61,8 @@ interface Window : EventTarget { undefined scroll(unrestricted double x, unrestricted double y); [ImplementedAs=scroll] undefined scrollTo(optional ScrollToOptions options = {}); [ImplementedAs=scroll] undefined scrollTo(unrestricted double x, unrestricted double y); + undefined scrollBy(optional ScrollToOptions options = {}); + undefined scrollBy(unrestricted double x, unrestricted double y); // FIXME: Everything from here on should be shared through WindowOrWorkerGlobalScope // https://w3c.github.io/hr-time/#the-performance-attribute