diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index e643a9b0e0a..bab9e02e3ca 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020-2021, Andreas Kling + * Copyright (c) 2021, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -29,6 +30,7 @@ #include #include #include +#include #include #include @@ -74,6 +76,12 @@ void WindowObject::initialize_global_object() define_native_function("getComputedStyle", get_computed_style, 1, attr); + // FIXME: These properties should be [Replaceable] according to the spec, but [Writable+Configurable] is the closest we have. + define_native_accessor("scrollX", scroll_x_getter, {}, attr); + define_native_accessor("pageXOffset", scroll_x_getter, {}, attr); + define_native_accessor("scrollY", scroll_y_getter, {}, attr); + define_native_accessor("pageYOffset", scroll_y_getter, {}, attr); + // Legacy define_native_accessor("event", event_getter, {}, JS::Attribute::Enumerable); @@ -458,4 +466,26 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::get_computed_style) return wrap(global_object, impl->get_computed_style(static_cast(object)->impl())); } +// https://www.w3.org/TR/cssom-view/#dom-window-scrollx +JS_DEFINE_NATIVE_GETTER(WindowObject::scroll_x_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + if (!impl->page()) + return JS::Value(0); + return JS::Value(impl->page()->top_level_browsing_context().viewport_scroll_offset().x()); +} + +// https://www.w3.org/TR/cssom-view/#dom-window-scrolly +JS_DEFINE_NATIVE_GETTER(WindowObject::scroll_y_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + if (!impl->page()) + return JS::Value(0); + return JS::Value(impl->page()->top_level_browsing_context().viewport_scroll_offset().y()); +} + } diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.h b/Userland/Libraries/LibWeb/Bindings/WindowObject.h index c2e7ead848d..66b91b3fc5b 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2020-2021, Andreas Kling + * Copyright (c) 2021, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -72,6 +73,9 @@ private: JS_DECLARE_NATIVE_FUNCTION(parent_getter); + JS_DECLARE_NATIVE_GETTER(scroll_x_getter); + JS_DECLARE_NATIVE_GETTER(scroll_y_getter); + JS_DECLARE_NATIVE_FUNCTION(alert); JS_DECLARE_NATIVE_FUNCTION(confirm); JS_DECLARE_NATIVE_FUNCTION(prompt); diff --git a/Userland/Libraries/LibWeb/Page/BrowsingContext.h b/Userland/Libraries/LibWeb/Page/BrowsingContext.h index a252a6b5a22..80b10ac0bf7 100644 --- a/Userland/Libraries/LibWeb/Page/BrowsingContext.h +++ b/Userland/Libraries/LibWeb/Page/BrowsingContext.h @@ -52,6 +52,7 @@ public: void set_needs_display(Gfx::IntRect const&); + Gfx::IntPoint const& viewport_scroll_offset() const { return m_viewport_scroll_offset; } void set_viewport_scroll_offset(Gfx::IntPoint const&); Gfx::IntRect viewport_rect() const { return { m_viewport_scroll_offset, m_size }; } void set_viewport_rect(Gfx::IntRect const&);