Przeglądaj źródła

LibWeb: Only delay "load" event for script elements that load something

We shouldn't delay the load event for scripts that we're completely
refusing to run anyway. Also, for scripts that have inline text content,
we don't need to delay them either, as they will become ready before
returning from "prepare script".

This makes the "load" event finally fire on lots of websites, including
Wikipedia. :^)
Andreas Kling 3 lat temu
rodzic
commit
cbd343dced

+ 4 - 1
Userland/Libraries/LibWeb/HTML/HTMLScriptElement.cpp

@@ -31,7 +31,7 @@ void HTMLScriptElement::set_parser_document(Badge<HTMLParser>, DOM::Document& do
     m_parser_document = document;
 }
 
-void HTMLScriptElement::begin_delaying_document_load_event(Badge<HTMLParser>, DOM::Document& document)
+void HTMLScriptElement::begin_delaying_document_load_event(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.
@@ -295,6 +295,9 @@ void HTMLScriptElement::prepare_script()
             //    Fetch a classic script given url, settings object, options, classic script CORS setting, and encoding.
             auto request = LoadRequest::create_for_url_on_page(url, document().page());
 
+            if (parser_document)
+                begin_delaying_document_load_event(*parser_document);
+
             ResourceLoader::the().load(
                 request,
                 [this, url](auto data, auto&, auto) {

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

@@ -42,12 +42,11 @@ 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();
     void when_the_script_is_ready(Function<void()>);
+    void begin_delaying_document_load_event(DOM::Document&);
 
     WeakPtr<DOM::Document> m_parser_document;
     WeakPtr<DOM::Document> m_preparation_time_document;

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

@@ -2094,10 +2094,6 @@ void HTMLParser::handle_text(HTMLToken& token)
 
         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.