diff options
author | Andreas Kling <kling@serenityos.org> | 2021-09-19 01:38:14 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-19 01:43:27 +0200 |
commit | 824be02cb0a31d264fc45bc3056232fee5b833ee (patch) | |
tree | 058389edbe1f8165446339c8851a573a0dbe6949 /Userland | |
parent | ae59d8a72814d7c0552d93461b321798d075875c (diff) | |
download | serenity-824be02cb0a31d264fc45bc3056232fee5b833ee.zip |
LibWeb: Move `onfoo` attribute handling to EventTarget
This logic was kept in the GlobalEventHandlers mixing for sharing
between Document and HTMLElement, but there are other interfaces who
need to support `onfoo` attribute event listeners as well.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/EventTarget.cpp | 45 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/EventTarget.h | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp | 59 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.h | 3 |
4 files changed, 55 insertions, 55 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/EventTarget.cpp b/Userland/Libraries/LibWeb/DOM/EventTarget.cpp index 0d0acbacb5..1789ca3e43 100644 --- a/Userland/Libraries/LibWeb/DOM/EventTarget.cpp +++ b/Userland/Libraries/LibWeb/DOM/EventTarget.cpp @@ -1,13 +1,18 @@ /* - * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ +#include <AK/StringBuilder.h> +#include <LibJS/Interpreter.h> +#include <LibJS/Parser.h> +#include <LibJS/Runtime/OrdinaryFunctionObject.h> #include <LibWeb/Bindings/ScriptExecutionContext.h> #include <LibWeb/DOM/Event.h> #include <LibWeb/DOM/EventListener.h> #include <LibWeb/DOM/EventTarget.h> +#include <LibWeb/HTML/EventHandler.h> namespace Web::DOM { @@ -68,4 +73,42 @@ ExceptionOr<bool> EventTarget::dispatch_event_binding(NonnullRefPtr<Event> event return dispatch_event(event); } +HTML::EventHandler EventTarget::event_handler_attribute(FlyString const& name) +{ + for (auto& listener : listeners()) { + if (listener.event_name == name && listener.listener->is_attribute()) { + return HTML::EventHandler { JS::make_handle(&listener.listener->function()) }; + } + } + return {}; +} + +void EventTarget::set_event_handler_attribute(FlyString const& name, HTML::EventHandler value) +{ + RefPtr<DOM::EventListener> listener; + if (!value.callback.is_null()) { + listener = adopt_ref(*new DOM::EventListener(move(value.callback))); + } else { + StringBuilder builder; + builder.appendff("function {}(event) {{\n{}\n}}", name, value.string); + auto parser = JS::Parser(JS::Lexer(builder.string_view())); + auto program = parser.parse_function_node<JS::FunctionExpression>(); + if (parser.has_errors()) { + dbgln("Failed to parse script in event handler attribute '{}'", name); + return; + } + auto* function = JS::OrdinaryFunctionObject::create(script_execution_context()->interpreter().global_object(), name, program->body(), program->parameters(), program->function_length(), nullptr, JS::FunctionKind::Regular, false, false); + VERIFY(function); + listener = adopt_ref(*new DOM::EventListener(JS::make_handle(static_cast<JS::FunctionObject*>(function)))); + } + if (listener) { + for (auto& registered_listener : listeners()) { + if (registered_listener.event_name == name && registered_listener.listener->is_attribute()) { + remove_event_listener(name, registered_listener.listener); + break; + } + } + add_event_listener(name, listener.release_nonnull()); + } +} } diff --git a/Userland/Libraries/LibWeb/DOM/EventTarget.h b/Userland/Libraries/LibWeb/DOM/EventTarget.h index c85f65ee84..a21c414970 100644 --- a/Userland/Libraries/LibWeb/DOM/EventTarget.h +++ b/Userland/Libraries/LibWeb/DOM/EventTarget.h @@ -53,6 +53,9 @@ public: Function<void()> legacy_pre_activation_behavior; Function<void()> legacy_cancelled_activation_behavior; + HTML::EventHandler event_handler_attribute(FlyString const& name); + void set_event_handler_attribute(FlyString const& name, HTML::EventHandler); + protected: explicit EventTarget(Bindings::ScriptExecutionContext&); diff --git a/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp b/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp index d1ff701b95..b6a2bf1a6c 100644 --- a/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp +++ b/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.cpp @@ -17,14 +17,14 @@ namespace Web::HTML { #undef __ENUMERATE -#define __ENUMERATE(attribute_name, event_name) \ - void GlobalEventHandlers::set_##attribute_name(HTML::EventHandler value) \ - { \ - set_event_handler_attribute(event_name, move(value)); \ - } \ - HTML::EventHandler GlobalEventHandlers::attribute_name() \ - { \ - return get_event_handler_attribute(event_name); \ +#define __ENUMERATE(attribute_name, event_name) \ + void GlobalEventHandlers::set_##attribute_name(HTML::EventHandler value) \ + { \ + global_event_handlers_to_event_target().set_event_handler_attribute(event_name, move(value)); \ + } \ + HTML::EventHandler GlobalEventHandlers::attribute_name() \ + { \ + return global_event_handlers_to_event_target().event_handler_attribute(event_name); \ } ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE) #undef __ENUMERATE @@ -33,47 +33,4 @@ GlobalEventHandlers::~GlobalEventHandlers() { } -void GlobalEventHandlers::set_event_handler_attribute(const FlyString& name, HTML::EventHandler value) -{ - auto& self = global_event_handlers_to_event_target(); - - RefPtr<DOM::EventListener> listener; - if (!value.callback.is_null()) { - listener = adopt_ref(*new DOM::EventListener(move(value.callback))); - } else { - StringBuilder builder; - builder.appendff("function {}(event) {{\n{}\n}}", name, value.string); - auto parser = JS::Parser(JS::Lexer(builder.string_view())); - auto program = parser.parse_function_node<JS::FunctionExpression>(); - if (parser.has_errors()) { - dbgln("Failed to parse script in event handler attribute '{}'", name); - return; - } - auto* function = JS::OrdinaryFunctionObject::create(self.script_execution_context()->interpreter().global_object(), name, program->body(), program->parameters(), program->function_length(), nullptr, JS::FunctionKind::Regular, false, false); - VERIFY(function); - listener = adopt_ref(*new DOM::EventListener(JS::make_handle(static_cast<JS::FunctionObject*>(function)))); - } - if (listener) { - for (auto& registered_listener : self.listeners()) { - if (registered_listener.event_name == name && registered_listener.listener->is_attribute()) { - self.remove_event_listener(name, registered_listener.listener); - break; - } - } - self.add_event_listener(name, listener.release_nonnull()); - } -} - -HTML::EventHandler GlobalEventHandlers::get_event_handler_attribute(const FlyString& name) -{ - auto& self = global_event_handlers_to_event_target(); - for (auto& listener : self.listeners()) { - if (listener.event_name == name && listener.listener->is_attribute()) { - return HTML::EventHandler { JS::make_handle(&listener.listener->function()) }; - } - } - - return {}; -} - } diff --git a/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.h b/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.h index 5dd2edeca0..9d8ee80e00 100644 --- a/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.h +++ b/Userland/Libraries/LibWeb/HTML/GlobalEventHandlers.h @@ -90,9 +90,6 @@ public: ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE) #undef __ENUMERATE - void set_event_handler_attribute(const FlyString& name, HTML::EventHandler); - HTML::EventHandler get_event_handler_attribute(const FlyString& name); - protected: virtual DOM::EventTarget& global_event_handlers_to_event_target() = 0; }; |