diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 51e68f80b9..3ff49702dd 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -51,10 +51,10 @@ #include #include #include -#include #include #include #include +#include #include #include @@ -429,4 +429,24 @@ NonnullRefPtr Document::take_pending_parsing_blocking_script( return m_pending_parsing_blocking_script.release_nonnull(); } +void Document::add_script_to_execute_when_parsing_has_finished(Badge, HTMLScriptElement& script) +{ + m_scripts_to_execute_when_parsing_has_finished.append(script); +} + +NonnullRefPtrVector Document::take_scripts_to_execute_when_parsing_has_finished(Badge) +{ + return move(m_scripts_to_execute_when_parsing_has_finished); +} + +void Document::add_script_to_execute_as_soon_as_possible(Badge, HTMLScriptElement& script) +{ + m_scripts_to_execute_as_soon_as_possible.append(script); +} + +NonnullRefPtrVector Document::take_scripts_to_execute_as_soon_as_possible(Badge) +{ + return move(m_scripts_to_execute_as_soon_as_possible); +} + } diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h index ee72d54c1a..37db176127 100644 --- a/Libraries/LibWeb/DOM/Document.h +++ b/Libraries/LibWeb/DOM/Document.h @@ -133,6 +133,12 @@ public: HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script; } NonnullRefPtr take_pending_parsing_blocking_script(Badge); + void add_script_to_execute_when_parsing_has_finished(Badge, HTMLScriptElement&); + NonnullRefPtrVector take_scripts_to_execute_when_parsing_has_finished(Badge); + + void add_script_to_execute_as_soon_as_possible(Badge, HTMLScriptElement&); + NonnullRefPtrVector take_scripts_to_execute_as_soon_as_possible(Badge); + bool in_quirks_mode() const { return m_quirks_mode; } void set_quirks_mode(bool mode) { m_quirks_mode = mode; } @@ -161,6 +167,8 @@ private: OwnPtr m_interpreter; RefPtr m_pending_parsing_blocking_script; + NonnullRefPtrVector m_scripts_to_execute_when_parsing_has_finished; + NonnullRefPtrVector m_scripts_to_execute_as_soon_as_possible; bool m_quirks_mode { false }; }; diff --git a/Libraries/LibWeb/DOM/HTMLScriptElement.cpp b/Libraries/LibWeb/DOM/HTMLScriptElement.cpp index df9e1df5f6..f0fe745486 100644 --- a/Libraries/LibWeb/DOM/HTMLScriptElement.cpp +++ b/Libraries/LibWeb/DOM/HTMLScriptElement.cpp @@ -206,10 +206,7 @@ void HTMLScriptElement::prepare_script(Badge) // FIXME: Check classic vs. module if (has_attribute("src") && has_attribute("defer") && m_parser_inserted && !has_attribute("async")) { - // FIXME: Add the element to the end of the list of scripts that will execute - // when the document has finished parsing associated with the Document - // of the parser that created the element. - ASSERT_NOT_REACHED(); + document().add_script_to_execute_when_parsing_has_finished({}, *this); } else if (has_attribute("src") && m_parser_inserted && !has_attribute("async")) { @@ -225,7 +222,7 @@ void HTMLScriptElement::prepare_script(Badge) } else if (has_attribute("src")) { - ASSERT_NOT_REACHED(); + m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this); } else { diff --git a/Libraries/LibWeb/Parser/HTMLDocumentParser.cpp b/Libraries/LibWeb/Parser/HTMLDocumentParser.cpp index e524fe0edd..a985f846d1 100644 --- a/Libraries/LibWeb/Parser/HTMLDocumentParser.cpp +++ b/Libraries/LibWeb/Parser/HTMLDocumentParser.cpp @@ -80,7 +80,17 @@ void HTMLDocumentParser::run(const URL& url) // "The end" + auto scripts_to_execute_when_parsing_has_finished = m_document->take_scripts_to_execute_when_parsing_has_finished({}); + for (auto& script : scripts_to_execute_when_parsing_has_finished) { + script.execute_script(); + } + m_document->dispatch_event(Event::create("DOMContentLoaded")); + + auto scripts_to_execute_as_soon_as_possible = m_document->take_scripts_to_execute_as_soon_as_possible({}); + for (auto& script : scripts_to_execute_as_soon_as_possible) { + script.execute_script(); + } } void HTMLDocumentParser::process_using_the_rules_for(InsertionMode mode, HTMLToken& token)