mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-30 00:31:14 +00:00
LibWeb: Ensure lazy WindowObject creation when activating event handler
This commit is contained in:
parent
f9e8f02451
commit
192f4b0258
Notes:
sideshowbarker
2024-07-17 16:39:02 +09:00
|
@ -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.
|
// 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 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.
|
// 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;
|
JS::GlobalObject* global_object = nullptr;
|
||||||
if (is_attribute == IsAttribute::Yes) {
|
if (is_attribute == IsAttribute::Yes) {
|
||||||
if (is<HTML::Window>(this)) {
|
if (is<HTML::Window>(this)) {
|
||||||
auto* window_global_object = verify_cast<HTML::Window>(this)->wrapper();
|
auto& window = verify_cast<HTML::Window>(*this);
|
||||||
global_object = static_cast<JS::GlobalObject*>(window_global_object);
|
// 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 {
|
} else {
|
||||||
auto* html_element = verify_cast<HTML::HTMLElement>(this);
|
auto& html_element = verify_cast<HTML::HTMLElement>(*this);
|
||||||
global_object = &html_element->document().realm().global_object();
|
global_object = &html_element.document().realm().global_object();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
global_object = &event_handler.value.get<Bindings::CallbackType>().callback.cell()->global_object();
|
global_object = &event_handler.value.get<Bindings::CallbackType>().callback.cell()->global_object();
|
||||||
|
|
Loading…
Reference in a new issue