diff options
author | Linus Groh <mail@linusgroh.de> | 2022-03-27 20:12:45 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-03-27 20:14:25 +0100 |
commit | 192f4b0258b04197a3aba5eaa2cbedb34dce672c (patch) | |
tree | 2b3e4ac72c3ce61a3222e614508bb5219b049907 /Userland/Libraries/LibWeb/DOM | |
parent | f9e8f0245103bf36b62d959a27f51b02d4c3e934 (diff) | |
download | serenity-192f4b0258b04197a3aba5eaa2cbedb34dce672c.zip |
LibWeb: Ensure lazy WindowObject creation when activating event handler
Diffstat (limited to 'Userland/Libraries/LibWeb/DOM')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/EventTarget.cpp | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/EventTarget.cpp b/Userland/Libraries/LibWeb/DOM/EventTarget.cpp index 95c74d0090..ae84bd87bb 100644 --- a/Userland/Libraries/LibWeb/DOM/EventTarget.cpp +++ b/Userland/Libraries/LibWeb/DOM/EventTarget.cpp @@ -519,15 +519,18 @@ void EventTarget::activate_event_handler(FlyString const& name, HTML::EventHandl // returning the element's document's global object, which is the HTML::Window object. // For any other HTMLElement who just had an element attribute set, `this` will be that HTMLElement, so the global object is this's document's realm's global object. // For anything else, it came from JavaScript, so use the global object of the provided callback function. - // Sadly, this doesn't work if an element attribute is set on a <body> element before any script is run, as Window::wrapper() will be null. JS::GlobalObject* global_object = nullptr; if (is_attribute == IsAttribute::Yes) { if (is<HTML::Window>(this)) { - auto* window_global_object = verify_cast<HTML::Window>(this)->wrapper(); - global_object = static_cast<JS::GlobalObject*>(window_global_object); + auto& window = verify_cast<HTML::Window>(*this); + // If an element attribute is set on a <body> element before any script is run, Window::wrapper() will be null. + // Force creation of the global object via the Document::interpreter() lazy initialization mechanism. + if (window.wrapper() == nullptr) + window.associated_document().interpreter(); + global_object = static_cast<JS::GlobalObject*>(window.wrapper()); } else { - auto* html_element = verify_cast<HTML::HTMLElement>(this); - global_object = &html_element->document().realm().global_object(); + auto& html_element = verify_cast<HTML::HTMLElement>(*this); + global_object = &html_element.document().realm().global_object(); } } else { global_object = &event_handler.value.get<Bindings::CallbackType>().callback.cell()->global_object(); |