summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-09-22 16:39:15 +0200
committerAndreas Kling <kling@serenityos.org>2021-09-22 16:40:24 +0200
commit635ab6db0bd6e7f0ef493835e33edfd6d05ee0f1 (patch)
tree738ed4111a7834d696314af845b5f46bbffe110a /Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
parentfb2f7c7b9ca4fc9e879ad08cbc448ba59ca3da48 (diff)
downloadserenity-635ab6db0bd6e7f0ef493835e33edfd6d05ee0f1.zip
LibWeb: Expose the GlobalEventHandlers mixin on the Window object
We now expose all the `onfoo` event handler attributes on the window object. This makes `window.onload = ...` actually work. :^)
Diffstat (limited to 'Userland/Libraries/LibWeb/Bindings/WindowObject.cpp')
-rw-r--r--Userland/Libraries/LibWeb/Bindings/WindowObject.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
index 01e82d0d09..3369221024 100644
--- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
+++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp
@@ -19,6 +19,7 @@
#include <LibWeb/Bindings/EventTargetPrototype.h>
#include <LibWeb/Bindings/EventWrapper.h>
#include <LibWeb/Bindings/EventWrapperFactory.h>
+#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/HistoryWrapper.h>
#include <LibWeb/Bindings/LocationObject.h>
#include <LibWeb/Bindings/MediaQueryListWrapper.h>
@@ -31,6 +32,7 @@
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/Window.h>
+#include <LibWeb/HTML/EventHandler.h>
#include <LibWeb/Origin.h>
#include <LibWeb/Page/BrowsingContext.h>
#include <LibWeb/Page/Page.h>
@@ -104,6 +106,12 @@ void WindowObject::initialize_global_object()
// WebAssembly "namespace"
define_direct_property("WebAssembly", heap().allocate<WebAssemblyObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
+ // HTML::GlobalEventHandlers
+#define __ENUMERATE(attribute, event_name) \
+ define_native_accessor(#attribute, attribute##_getter, attribute##_setter, attr);
+ ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE);
+#undef __ENUMERATE
+
ADD_WINDOW_OBJECT_INTERFACES;
}
@@ -674,4 +682,37 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::history_getter)
return wrap(global_object, impl->associated_document().history());
}
+#define __ENUMERATE(attribute, event_name) \
+ JS_DEFINE_NATIVE_FUNCTION(WindowObject::attribute##_getter) \
+ { \
+ auto* impl = impl_from(vm, global_object); \
+ if (!impl) \
+ return {}; \
+ auto retval = impl->attribute(); \
+ if (retval.callback.is_null()) \
+ return JS::js_null(); \
+ return retval.callback.cell(); \
+ } \
+ JS_DEFINE_NATIVE_FUNCTION(WindowObject::attribute##_setter) \
+ { \
+ auto* impl = impl_from(vm, global_object); \
+ if (!impl) \
+ return {}; \
+ auto value = vm.argument(0); \
+ HTML::EventHandler cpp_value; \
+ if (value.is_function()) { \
+ cpp_value.callback = JS::make_handle(&value.as_function()); \
+ } else if (value.is_string()) { \
+ cpp_value.string = value.as_string().string(); \
+ } else { \
+ return {}; \
+ } \
+ (void)throw_dom_exception_if_needed(vm, global_object, [&] { \
+ return impl->set_##attribute(cpp_value); \
+ }); \
+ return {}; \
+ }
+ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE)
+#undef __ENUMERATE
+
}