Explorar o código

LibWeb: Don't delay document "load" event for unclosed script tags

We previously had a bug where markup with unclosed script tags caused
the document load event to be delayed indefinitely. Fix this by only
marking script elements as delaying the load event once we encounter
the script end tag.
Andreas Kling %!s(int64=3) %!d(string=hai) anos
pai
achega
2c9dfadb21

+ 3 - 0
Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp

@@ -29,7 +29,10 @@ HTMLScriptElement::~HTMLScriptElement() = default;
 void HTMLScriptElement::set_parser_document(Badge<HTMLParser>, DOM::Document& document)
 {
     m_parser_document = document;
+}
 
+void HTMLScriptElement::begin_delaying_document_load_event(Badge<HTMLParser>, 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);

+ 2 - 0
Userland/Libraries/LibWeb/HTML/HTMLScriptElement.h

@@ -42,6 +42,8 @@ public:
 
     void set_source_line_number(Badge<HTMLParser>, size_t source_line_number) { m_source_line_number = source_line_number; }
 
+    void begin_delaying_document_load_event(Badge<HTMLParser>, DOM::Document&);
+
 private:
     void prepare_script();
     void script_became_ready();

+ 5 - 0
Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp

@@ -2093,6 +2093,11 @@ void HTMLParser::handle_text(HTMLToken& token)
         flush_character_insertions();
 
         NonnullRefPtr<HTMLScriptElement> script = verify_cast<HTMLScriptElement>(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.