diff --git a/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp index e7c10dc074..84d3736220 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp @@ -29,7 +29,10 @@ HTMLScriptElement::~HTMLScriptElement() = default; void HTMLScriptElement::set_parser_document(Badge, DOM::Document& document) { m_parser_document = document; +} +void HTMLScriptElement::begin_delaying_document_load_event(Badge, DOM::Document& document) +{ // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-script // The user agent must delay the load event of the element's node document until the script is ready. m_document_load_event_delayer.emplace(document); diff --git a/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h b/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h index 97ef29e642..a104e41ece 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h @@ -42,6 +42,8 @@ public: void set_source_line_number(Badge, size_t source_line_number) { m_source_line_number = source_line_number; } + void begin_delaying_document_load_event(Badge, DOM::Document&); + private: void prepare_script(); void script_became_ready(); diff --git a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp index 2541298c7b..a2b41c4540 100644 --- a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp +++ b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp @@ -2093,6 +2093,11 @@ void HTMLParser::handle_text(HTMLToken& token) flush_character_insertions(); NonnullRefPtr script = verify_cast(current_node()); + + // The document's "load" event is delayed until every script becomes ready. + // See HTMLScriptElement internals for how readiness is determined. + script->begin_delaying_document_load_event({}, *m_document); + (void)m_stack_of_open_elements.pop(); m_insertion_mode = m_original_insertion_mode; // Let the old insertion point have the same value as the current insertion point.