From 40b4ee88de7193e38888de0bc40d011c17ec7070 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 6 Mar 2023 19:51:44 +0000 Subject: [PATCH] LibWeb/HTML: Port Window.scroll{X,Y} / Window.page{X,Y}Offset to IDL --- Userland/Libraries/LibWeb/DOM/Element.cpp | 8 ++-- Userland/Libraries/LibWeb/HTML/Window.cpp | 55 +++++++++-------------- Userland/Libraries/LibWeb/HTML/Window.h | 8 ++-- Userland/Libraries/LibWeb/HTML/Window.idl | 6 +++ 4 files changed, 33 insertions(+), 44 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 1bfccea90a..8975b21112 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -977,7 +977,7 @@ void Element::set_scroll_left(double x) if (document.document_element() == this) { // FIXME: Implement this in terms of invoking scroll() on window. if (auto* page = document.page()) - page->client().page_did_request_scroll_to({ static_cast(x), window->scroll_y() }); + page->client().page_did_request_scroll_to({ static_cast(x), static_cast(window->scroll_y()) }); return; } @@ -986,7 +986,7 @@ void Element::set_scroll_left(double x) if (document.body() == this && document.in_quirks_mode() && !is_potentially_scrollable()) { // FIXME: Implement this in terms of invoking scroll() on window. if (auto* page = document.page()) - page->client().page_did_request_scroll_to({ static_cast(x), window->scroll_y() }); + page->client().page_did_request_scroll_to({ static_cast(x), static_cast(window->scroll_y()) }); return; } @@ -1041,7 +1041,7 @@ void Element::set_scroll_top(double y) if (document.document_element() == this) { // FIXME: Implement this in terms of invoking scroll() on window. if (auto* page = document.page()) - page->client().page_did_request_scroll_to({ window->scroll_x(), static_cast(y) }); + page->client().page_did_request_scroll_to({ static_cast(window->scroll_x()), static_cast(y) }); return; } @@ -1050,7 +1050,7 @@ void Element::set_scroll_top(double y) if (document.body() == this && document.in_quirks_mode() && !is_potentially_scrollable()) { // FIXME: Implement this in terms of invoking scroll() on window. if (auto* page = document.page()) - page->client().page_did_request_scroll_to({ window->scroll_x(), static_cast(y) }); + page->client().page_did_request_scroll_to({ static_cast(window->scroll_x()), static_cast(y) }); return; } diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index 9faa74ccc8..b6ed58537c 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -712,22 +712,6 @@ Optional Window::query_media_feature(CSS::MediaFeatureID return {}; } -// https://www.w3.org/TR/cssom-view/#dom-window-scrollx -float Window::scroll_x() const -{ - if (auto* page = this->page()) - return page->top_level_browsing_context().viewport_scroll_offset().x().value(); - return 0; -} - -// https://www.w3.org/TR/cssom-view/#dom-window-scrolly -float Window::scroll_y() const -{ - if (auto* page = this->page()) - return page->top_level_browsing_context().viewport_scroll_offset().y().value(); - return 0; -} - // https://html.spec.whatwg.org/#fire-a-page-transition-event void Window::fire_a_page_transition_event(DeprecatedFlyString const& event_name, bool persisted) { @@ -1033,11 +1017,6 @@ WebIDL::ExceptionOr Window::initialize_web_interfaces(Badgepage()) + return page->top_level_browsing_context().viewport_scroll_offset().x().value(); + return 0; +} + +// https://w3c.github.io/csswg-drafts/cssom-view/#dom-window-scrolly +double Window::scroll_y() const +{ + // The scrollY attribute must return the y-coordinate, relative to the initial containing block origin, + // of the top of the viewport, or zero if there is no viewport. + if (auto* page = this->page()) + return page->top_level_browsing_context().viewport_scroll_offset().y().value(); + return 0; +} + // https://w3c.github.io/hr-time/#dom-windoworworkerglobalscope-performance WebIDL::ExceptionOr> Window::performance() { @@ -1553,20 +1552,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::get_selection) return impl->associated_document().get_selection(); } -// https://www.w3.org/TR/cssom-view/#dom-window-scrollx -JS_DEFINE_NATIVE_FUNCTION(Window::scroll_x_getter) -{ - auto* impl = TRY(impl_from(vm)); - return JS::Value(impl->scroll_x()); -} - -// https://www.w3.org/TR/cssom-view/#dom-window-scrolly -JS_DEFINE_NATIVE_FUNCTION(Window::scroll_y_getter) -{ - auto* impl = TRY(impl_from(vm)); - return JS::Value(impl->scroll_y()); -} - enum class ScrollBehavior { Auto, Smooth diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index 0137a0b787..7673e0a5c5 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -100,9 +100,6 @@ public: CSS::CSSStyleDeclaration* get_computed_style_impl(DOM::Element&) const; Optional query_media_feature(CSS::MediaFeatureID) const; - float scroll_x() const; - float scroll_y() const; - void fire_a_page_transition_event(DeprecatedFlyString const& event_name, bool persisted); float device_pixel_ratio() const; @@ -165,6 +162,9 @@ public: i32 inner_width() const; i32 inner_height() const; + double scroll_x() const; + double scroll_y() const; + WebIDL::ExceptionOr> performance(); WebIDL::ExceptionOr> crypto(); @@ -233,8 +233,6 @@ private: JS_DECLARE_NATIVE_FUNCTION(device_pixel_ratio_getter); - JS_DECLARE_NATIVE_FUNCTION(scroll_x_getter); - JS_DECLARE_NATIVE_FUNCTION(scroll_y_getter); JS_DECLARE_NATIVE_FUNCTION(scroll); JS_DECLARE_NATIVE_FUNCTION(scroll_by); diff --git a/Userland/Libraries/LibWeb/HTML/Window.idl b/Userland/Libraries/LibWeb/HTML/Window.idl index 7534866308..68766e1cb8 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.idl +++ b/Userland/Libraries/LibWeb/HTML/Window.idl @@ -52,6 +52,12 @@ interface Window : EventTarget { [Replaceable] readonly attribute long innerWidth; [Replaceable] readonly attribute long innerHeight; + // viewport scrolling + [Replaceable] readonly attribute double scrollX; + [Replaceable, ImplementedAs=scroll_x] readonly attribute double pageXOffset; + [Replaceable] readonly attribute double scrollY; + [Replaceable, ImplementedAs=scroll_y] readonly attribute double pageYOffset; + // FIXME: Everything from here on should be shared through WindowOrWorkerGlobalScope // https://w3c.github.io/hr-time/#the-performance-attribute [Replaceable] readonly attribute Performance performance;