From 63d971d33b1b7fc7fb22ef18cf1b1ec4e01b6a70 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 29 Sep 2021 00:15:26 +0200 Subject: [PATCH] LibWeb: Support window.screen{X,Y,Left,Top} Some sites query these properties for whatever reason, so let's support them. (But let's always pretend the screen coordinates are (0, 0)) --- .../LibWeb/Bindings/WindowObject.cpp | 37 +++++++++++++++++++ .../Libraries/LibWeb/Bindings/WindowObject.h | 5 +++ Userland/Libraries/LibWeb/DOM/Window.cpp | 16 ++++++++ Userland/Libraries/LibWeb/DOM/Window.h | 3 ++ 4 files changed, 61 insertions(+) diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index 74ca7db523..18a46e07db 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -96,6 +96,11 @@ void WindowObject::initialize_global_object() define_native_function("scrollTo", scroll, 2, attr); define_native_function("scrollBy", scroll_by, 2, attr); + define_native_accessor("screenX", screen_x_getter, {}, attr); + define_native_accessor("screenY", screen_y_getter, {}, attr); + define_native_accessor("screenLeft", screen_left_getter, {}, attr); + define_native_accessor("screenTop", screen_top_getter, {}, attr); + // Legacy define_native_accessor("event", event_getter, event_setter, JS::Attribute::Enumerable); @@ -717,6 +722,38 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::history_getter) return wrap(global_object, impl->associated_document().history()); } +JS_DEFINE_NATIVE_GETTER(WindowObject::screen_left_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + return JS::Value(impl->screen_x()); +} + +JS_DEFINE_NATIVE_GETTER(WindowObject::screen_top_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + return JS::Value(impl->screen_y()); +} + +JS_DEFINE_NATIVE_GETTER(WindowObject::screen_x_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + return JS::Value(impl->screen_x()); +} + +JS_DEFINE_NATIVE_GETTER(WindowObject::screen_y_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + return JS::Value(impl->screen_y()); +} + #define __ENUMERATE(attribute, event_name) \ JS_DEFINE_NATIVE_FUNCTION(WindowObject::attribute##_getter) \ { \ diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.h b/Userland/Libraries/LibWeb/Bindings/WindowObject.h index ffb21a6d50..1c29fbf788 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.h @@ -88,6 +88,11 @@ private: JS_DECLARE_NATIVE_FUNCTION(scroll); JS_DECLARE_NATIVE_FUNCTION(scroll_by); + JS_DECLARE_NATIVE_GETTER(screen_x_getter); + JS_DECLARE_NATIVE_GETTER(screen_y_getter); + JS_DECLARE_NATIVE_GETTER(screen_left_getter); + JS_DECLARE_NATIVE_GETTER(screen_top_getter); + JS_DECLARE_NATIVE_FUNCTION(alert); JS_DECLARE_NATIVE_FUNCTION(confirm); JS_DECLARE_NATIVE_FUNCTION(prompt); diff --git a/Userland/Libraries/LibWeb/DOM/Window.cpp b/Userland/Libraries/LibWeb/DOM/Window.cpp index a93eb58885..7ca96a76c9 100644 --- a/Userland/Libraries/LibWeb/DOM/Window.cpp +++ b/Userland/Libraries/LibWeb/DOM/Window.cpp @@ -315,4 +315,20 @@ float Window::device_pixel_ratio() const return 1.0f; } +// https://drafts.csswg.org/cssom-view/#dom-window-screenx +int Window::screen_x() const +{ + // The screenX and screenLeft attributes must return the x-coordinate, relative to the origin of the Web-exposed screen area, + // of the left of the client window as number of CSS pixels, or zero if there is no such thing. + return 0; +} + +// https://drafts.csswg.org/cssom-view/#dom-window-screeny +int Window::screen_y() const +{ + // The screenY and screenTop attributes must return the y-coordinate, relative to the origin of the screen of the Web-exposed screen area, + // of the top of the client window as number of CSS pixels, or zero if there is no such thing. + return 0; +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Window.h b/Userland/Libraries/LibWeb/DOM/Window.h index 7c4152ac64..5199fa3bf6 100644 --- a/Userland/Libraries/LibWeb/DOM/Window.h +++ b/Userland/Libraries/LibWeb/DOM/Window.h @@ -89,6 +89,9 @@ public: float device_pixel_ratio() const; + int screen_x() const; + int screen_y() const; + private: explicit Window(Document&);