diff options
author | Luke Wilde <lukew@serenityos.org> | 2022-06-27 19:48:54 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-06-29 21:21:50 +0100 |
commit | 3fe66bddf4f1b9def517c403e58beb39921c61f6 (patch) | |
tree | b6b9ccc9340dfe6fbdbcc324237c88f7ef579acd /Userland/Libraries/LibWeb/HTML | |
parent | ebf21846369745618e3749fb3c111bf354b0abf7 (diff) | |
download | serenity-3fe66bddf4f1b9def517c403e58beb39921c61f6.zip |
LibWeb: Implement WindowEventHandlers
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML')
11 files changed, 157 insertions, 8 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/AttributeNames.h b/Userland/Libraries/LibWeb/HTML/AttributeNames.h index 00656d9e7f..e30d9ddb79 100644 --- a/Userland/Libraries/LibWeb/HTML/AttributeNames.h +++ b/Userland/Libraries/LibWeb/HTML/AttributeNames.h @@ -98,7 +98,10 @@ namespace AttributeNames { __ENUMERATE_HTML_ATTRIBUTE(novalidate) \ __ENUMERATE_HTML_ATTRIBUTE(nowrap) \ __ENUMERATE_HTML_ATTRIBUTE(onabort) \ + __ENUMERATE_HTML_ATTRIBUTE(onafterprint) \ __ENUMERATE_HTML_ATTRIBUTE(onauxclick) \ + __ENUMERATE_HTML_ATTRIBUTE(onbeforeprint) \ + __ENUMERATE_HTML_ATTRIBUTE(onbeforeunload) \ __ENUMERATE_HTML_ATTRIBUTE(onblur) \ __ENUMERATE_HTML_ATTRIBUTE(oncancel) \ __ENUMERATE_HTML_ATTRIBUTE(oncanplay) \ @@ -122,15 +125,19 @@ namespace AttributeNames { __ENUMERATE_HTML_ATTRIBUTE(onerror) \ __ENUMERATE_HTML_ATTRIBUTE(onfocus) \ __ENUMERATE_HTML_ATTRIBUTE(onformdata) \ + __ENUMERATE_HTML_ATTRIBUTE(onhashchange) \ __ENUMERATE_HTML_ATTRIBUTE(oninput) \ __ENUMERATE_HTML_ATTRIBUTE(oninvalid) \ __ENUMERATE_HTML_ATTRIBUTE(onkeydown) \ __ENUMERATE_HTML_ATTRIBUTE(onkeypress) \ __ENUMERATE_HTML_ATTRIBUTE(onkeyup) \ + __ENUMERATE_HTML_ATTRIBUTE(onlanguagechange) \ __ENUMERATE_HTML_ATTRIBUTE(onload) \ __ENUMERATE_HTML_ATTRIBUTE(onloadeddata) \ __ENUMERATE_HTML_ATTRIBUTE(onloadedmetadata) \ __ENUMERATE_HTML_ATTRIBUTE(onloadstart) \ + __ENUMERATE_HTML_ATTRIBUTE(onmessage) \ + __ENUMERATE_HTML_ATTRIBUTE(onmessageerror) \ __ENUMERATE_HTML_ATTRIBUTE(onmousedown) \ __ENUMERATE_HTML_ATTRIBUTE(onmouseenter) \ __ENUMERATE_HTML_ATTRIBUTE(onmouseleave) \ @@ -138,11 +145,17 @@ namespace AttributeNames { __ENUMERATE_HTML_ATTRIBUTE(onmouseout) \ __ENUMERATE_HTML_ATTRIBUTE(onmouseover) \ __ENUMERATE_HTML_ATTRIBUTE(onmouseup) \ + __ENUMERATE_HTML_ATTRIBUTE(onoffline) \ + __ENUMERATE_HTML_ATTRIBUTE(ononline) \ + __ENUMERATE_HTML_ATTRIBUTE(onpagehide) \ + __ENUMERATE_HTML_ATTRIBUTE(onpageshow) \ __ENUMERATE_HTML_ATTRIBUTE(onpause) \ __ENUMERATE_HTML_ATTRIBUTE(onplay) \ __ENUMERATE_HTML_ATTRIBUTE(onplaying) \ + __ENUMERATE_HTML_ATTRIBUTE(onpopstate) \ __ENUMERATE_HTML_ATTRIBUTE(onprogress) \ __ENUMERATE_HTML_ATTRIBUTE(onratechange) \ + __ENUMERATE_HTML_ATTRIBUTE(onrejectionhandled) \ __ENUMERATE_HTML_ATTRIBUTE(onreset) \ __ENUMERATE_HTML_ATTRIBUTE(onresize) \ __ENUMERATE_HTML_ATTRIBUTE(onscroll) \ @@ -152,10 +165,13 @@ namespace AttributeNames { __ENUMERATE_HTML_ATTRIBUTE(onselect) \ __ENUMERATE_HTML_ATTRIBUTE(onslotchange) \ __ENUMERATE_HTML_ATTRIBUTE(onstalled) \ + __ENUMERATE_HTML_ATTRIBUTE(onstorage) \ __ENUMERATE_HTML_ATTRIBUTE(onsubmit) \ __ENUMERATE_HTML_ATTRIBUTE(onsuspend) \ __ENUMERATE_HTML_ATTRIBUTE(ontimeupdate) \ __ENUMERATE_HTML_ATTRIBUTE(ontoggle) \ + __ENUMERATE_HTML_ATTRIBUTE(onunhandledrejection) \ + __ENUMERATE_HTML_ATTRIBUTE(onunload) \ __ENUMERATE_HTML_ATTRIBUTE(onvolumechange) \ __ENUMERATE_HTML_ATTRIBUTE(onwaiting) \ __ENUMERATE_HTML_ATTRIBUTE(onwebkitanimationend) \ diff --git a/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp b/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp index c45a016023..fb39485202 100644 --- a/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp +++ b/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp @@ -4,11 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include <LibJS/Interpreter.h> -#include <LibJS/Parser.h> -#include <LibWeb/DOM/Document.h> -#include <LibWeb/DOM/IDLEventListener.h> -#include <LibWeb/HTML/EventHandler.h> +#include <LibWeb/DOM/EventTarget.h> #include <LibWeb/HTML/EventNames.h> #include <LibWeb/HTML/GlobalEventHandlers.h> #include <LibWeb/UIEvents/EventNames.h> diff --git a/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.cpp index b3b64a6625..9ff7ae3a03 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.cpp @@ -55,6 +55,14 @@ void HTMLBodyElement::parse_attribute(FlyString const& name, String const& value } else if (name.equals_ignoring_case("background")) { m_background_style_value = CSS::ImageStyleValue::create(document().parse_url(value)); } + +#undef __ENUMERATE +#define __ENUMERATE(attribute_name, event_name) \ + if (name == HTML::AttributeNames::attribute_name) { \ + element_event_handler_attribute_changed(event_name, value); \ + } + ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE) +#undef __ENUMERATE } DOM::EventTarget& HTMLBodyElement::global_event_handlers_to_event_target(FlyString const& event_name) @@ -67,4 +75,11 @@ DOM::EventTarget& HTMLBodyElement::global_event_handlers_to_event_target(FlyStri return *this; } +DOM::EventTarget& HTMLBodyElement::window_event_handlers_to_event_target() +{ + // All WindowEventHandlers on HTMLFrameSetElement (e.g. document.body.onrejectionhandled) are mapped to window.on{event}. + // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping. + return document().window(); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.h b/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.h index 21e9b3a2ef..e916ecad46 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.h @@ -7,10 +7,13 @@ #pragma once #include <LibWeb/HTML/HTMLElement.h> +#include <LibWeb/HTML/WindowEventHandlers.h> namespace Web::HTML { -class HTMLBodyElement final : public HTMLElement { +class HTMLBodyElement final + : public HTMLElement + , public WindowEventHandlers { public: using WrapperType = Bindings::HTMLBodyElementWrapper; @@ -24,6 +27,9 @@ private: // ^HTML::GlobalEventHandlers virtual EventTarget& global_event_handlers_to_event_target(FlyString const& event_name) override; + // ^HTML::WindowEventHandlers + virtual EventTarget& window_event_handlers_to_event_target() override; + RefPtr<CSS::ImageStyleValue> m_background_style_value; }; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.idl index bf24c5727f..758b9ded0e 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.idl @@ -1,3 +1,4 @@ +#import <DOM/EventHandler.idl> #import <HTML/HTMLElement.idl> interface HTMLBodyElement : HTMLElement { @@ -10,3 +11,5 @@ interface HTMLBodyElement : HTMLElement { [Reflect] attribute DOMString background; }; + +HTMLBodyElement includes WindowEventHandlers; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLFrameSetElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLFrameSetElement.cpp index 50aa75edf9..bf7d86accd 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLFrameSetElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLFrameSetElement.cpp @@ -4,7 +4,9 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include <LibWeb/DOM/Document.h> #include <LibWeb/HTML/HTMLFrameSetElement.h> +#include <LibWeb/HTML/Window.h> namespace Web::HTML { @@ -15,6 +17,19 @@ HTMLFrameSetElement::HTMLFrameSetElement(DOM::Document& document, DOM::Qualified HTMLFrameSetElement::~HTMLFrameSetElement() = default; +void HTMLFrameSetElement::parse_attribute(FlyString const& name, String const& value) +{ + HTMLElement::parse_attribute(name, value); + +#undef __ENUMERATE +#define __ENUMERATE(attribute_name, event_name) \ + if (name == HTML::AttributeNames::attribute_name) { \ + element_event_handler_attribute_changed(event_name, value); \ + } + ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE) +#undef __ENUMERATE +} + DOM::EventTarget& HTMLFrameSetElement::global_event_handlers_to_event_target(FlyString const& event_name) { // NOTE: This is a little weird, but IIUC document.body.onload actually refers to window.onload @@ -25,4 +40,11 @@ DOM::EventTarget& HTMLFrameSetElement::global_event_handlers_to_event_target(Fly return *this; } +DOM::EventTarget& HTMLFrameSetElement::window_event_handlers_to_event_target() +{ + // All WindowEventHandlers on HTMLFrameSetElement (e.g. document.body.onrejectionhandled) are mapped to window.on{event}. + // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping. + return document().window(); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLFrameSetElement.h b/Userland/Libraries/LibWeb/HTML/HTMLFrameSetElement.h index e6170a5607..3a4088d997 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLFrameSetElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLFrameSetElement.h @@ -7,20 +7,28 @@ #pragma once #include <LibWeb/HTML/HTMLElement.h> +#include <LibWeb/HTML/WindowEventHandlers.h> namespace Web::HTML { // NOTE: This element is marked as obsolete, but is still listed as required by the specification. -class HTMLFrameSetElement final : public HTMLElement { +class HTMLFrameSetElement final + : public HTMLElement + , public WindowEventHandlers { public: using WrapperType = Bindings::HTMLFrameSetElementWrapper; HTMLFrameSetElement(DOM::Document&, DOM::QualifiedName); virtual ~HTMLFrameSetElement() override; + virtual void parse_attribute(FlyString const&, String const&) override; + private: // ^HTML::GlobalEventHandlers virtual EventTarget& global_event_handlers_to_event_target(FlyString const& event_name) override; + + // ^HTML::WindowEventHandlers + virtual EventTarget& window_event_handlers_to_event_target() override; }; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLFrameSetElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLFrameSetElement.idl index 996c734d7b..650be3c737 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLFrameSetElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLFrameSetElement.idl @@ -1,3 +1,4 @@ +#import <DOM/EventHandler.idl> #import <HTML/HTMLElement.idl> interface HTMLFrameSetElement : HTMLElement { @@ -6,3 +7,5 @@ interface HTMLFrameSetElement : HTMLElement { [Reflect] attribute DOMString rows; }; + +HTMLFrameSetElement includes WindowEventHandlers; diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index df4b56fa9b..41766c024e 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -21,6 +21,7 @@ #include <LibWeb/HTML/AnimationFrameCallbackDriver.h> #include <LibWeb/HTML/BrowsingContext.h> #include <LibWeb/HTML/GlobalEventHandlers.h> +#include <LibWeb/HTML/WindowEventHandlers.h> namespace Web::HTML { @@ -30,7 +31,8 @@ class Window final : public RefCounted<Window> , public Weakable<Window> , public DOM::EventTarget - , public HTML::GlobalEventHandlers { + , public HTML::GlobalEventHandlers + , public HTML::WindowEventHandlers { public: static NonnullRefPtr<Window> create_with_document(DOM::Document&); ~Window(); @@ -130,6 +132,9 @@ private: // ^HTML::GlobalEventHandlers virtual DOM::EventTarget& global_event_handlers_to_event_target(FlyString const&) override { return *this; } + // ^HTML::WindowEventHandlers + virtual DOM::EventTarget& window_event_handlers_to_event_target() override { return *this; } + enum class Repeat { Yes, No, diff --git a/Userland/Libraries/LibWeb/HTML/WindowEventHandlers.cpp b/Userland/Libraries/LibWeb/HTML/WindowEventHandlers.cpp new file mode 100644 index 0000000000..bd99461a9d --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/WindowEventHandlers.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibWeb/DOM/EventTarget.h> +#include <LibWeb/HTML/EventNames.h> +#include <LibWeb/HTML/WindowEventHandlers.h> + +namespace Web::HTML { + +#undef __ENUMERATE +#define __ENUMERATE(attribute_name, event_name) \ + void WindowEventHandlers::set_##attribute_name(Optional<Bindings::CallbackType> value) \ + { \ + window_event_handlers_to_event_target().set_event_handler_attribute(event_name, move(value)); \ + } \ + Bindings::CallbackType* WindowEventHandlers::attribute_name() \ + { \ + return window_event_handlers_to_event_target().event_handler_attribute(event_name); \ + } +ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE) +#undef __ENUMERATE + +WindowEventHandlers::~WindowEventHandlers() = default; + +} diff --git a/Userland/Libraries/LibWeb/HTML/WindowEventHandlers.h b/Userland/Libraries/LibWeb/HTML/WindowEventHandlers.h new file mode 100644 index 0000000000..9c9291dc88 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/WindowEventHandlers.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/Forward.h> +#include <LibWeb/Forward.h> + +#define ENUMERATE_WINDOW_EVENT_HANDLERS(E) \ + E(onafterprint, HTML::EventNames::afterprint) \ + E(onbeforeprint, HTML::EventNames::beforeprint) \ + E(onbeforeunload, HTML::EventNames::beforeunload) \ + E(onhashchange, HTML::EventNames::hashchange) \ + E(onlanguagechange, HTML::EventNames::languagechange) \ + E(onmessage, HTML::EventNames::message) \ + E(onmessageerror, HTML::EventNames::messageerror) \ + E(onoffline, HTML::EventNames::offline) \ + E(ononline, HTML::EventNames::online) \ + E(onpagehide, HTML::EventNames::pagehide) \ + E(onpageshow, HTML::EventNames::pageshow) \ + E(onpopstate, HTML::EventNames::popstate) \ + E(onrejectionhandled, HTML::EventNames::rejectionhandled) \ + E(onstorage, HTML::EventNames::storage) \ + E(onunhandledrejection, HTML::EventNames::unhandledrejection) \ + E(onunload, HTML::EventNames::unload) + +namespace Web::HTML { + +class WindowEventHandlers { +public: + virtual ~WindowEventHandlers(); + +#undef __ENUMERATE +#define __ENUMERATE(attribute_name, event_name) \ + void set_##attribute_name(Optional<Bindings::CallbackType>); \ + Bindings::CallbackType* attribute_name(); + ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE) +#undef __ENUMERATE + +protected: + virtual DOM::EventTarget& window_event_handlers_to_event_target() = 0; +}; + +} |