LibWeb: Implement Window.scroll{X,Y} JS properties

...and pageXOffset/pageYOffset too, since those are just aliases for the
same thing.
This commit is contained in:
Sam Atkins 2021-09-08 12:22:28 +01:00 committed by Linus Groh
parent 83414af9f3
commit 9588a377ec
Notes: sideshowbarker 2024-07-18 04:16:19 +09:00
3 changed files with 35 additions and 0 deletions

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -29,6 +30,7 @@
#include <LibWeb/DOM/Window.h>
#include <LibWeb/Origin.h>
#include <LibWeb/Page/BrowsingContext.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/WebAssembly/WebAssemblyObject.h>
#include <LibWeb/Bindings/WindowObjectHelper.h>
@ -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<ElementWrapper*>(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());
}
}

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
*
* 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);

View File

@ -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&);