Truly independent web browser
LibWeb: Implement resource render blocking
Andreas Kling4e62e22491e7
diff --git a/Libraries/LibWeb/CSS/StyleSheetImport.cpp b/Libraries/LibWeb/CSS/StyleSheetImport.cppindex 1f90f7ec4b3..fb7b2cf5d33 100644--- a/Libraries/LibWeb/CSS/StyleSheetImport.cpp+++ b/Libraries/LibWeb/CSS/StyleSheetImport.cpp@@ -146,7 +146,7 @@ void StyleSheetImport::fetch()return;}- // AD-HOC: Track pending import rules to block rendering until they are done.+ // NB: Track pending import rules for stylesheet load accounting.m_document->add_pending_css_import_rule({}, *this);set_loading_state(StyleSheetState::LoadingState::Loading);diff --git a/Libraries/LibWeb/DOM/DOMTokenList.cpp b/Libraries/LibWeb/DOM/DOMTokenList.cppindex 7458d8170ad..b14023b15ba 100644--- a/Libraries/LibWeb/DOM/DOMTokenList.cpp+++ b/Libraries/LibWeb/DOM/DOMTokenList.cpp@@ -262,6 +262,11 @@ WebIDL::ExceptionOr<bool> DOMTokenList::run_validation_steps(Utf16View token){ { HTML::TagNames::form, HTML::AttributeNames::rel },{ u"noreferrer"sv, u"noopener"sv, u"opener"sv } },+ // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#blocking-attribute+ { { HTML::TagNames::link, HTML::AttributeNames::blocking }, { u"render"sv } },+ { { HTML::TagNames::script, HTML::AttributeNames::blocking }, { u"render"sv } },+ { { HTML::TagNames::style, HTML::AttributeNames::blocking }, { u"render"sv } },+// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-sandbox{ { HTML::TagNames::iframe, HTML::AttributeNames::sandbox },{ u"allow-downloads"sv, u"allow-forms"sv, u"allow-modals"sv, u"allow-orientation-lock"sv, u"allow-pointer-lock"sv, u"allow-popups"sv, u"allow-popups-to-escape-sandbox"sv, u"allow-presentation"sv, u"allow-same-origin"sv, u"allow-scripts"sv, u"allow-top-navigation"sv, u"allow-top-navigation-by-user-activation"sv, u"allow-top-navigation-to-custom-protocols"sv } },diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex aade86c7084..85c63371839 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -6786,6 +6786,12 @@ void Document::abort()// 3. Make document unsalvageable given document and "parser-aborted".make_unsalvageable("parser-aborted"_utf16);}++ // NB: Stopped fetches discard their completion tasks, so their elements cannot release rendering themselves.+ while (!m_render_blocking_elements.is_empty()) {+ auto element = *m_render_blocking_elements.begin();+ element->unblock_rendering();+ }}// https://html.spec.whatwg.org/multipage/document-lifecycle.html#abort-a-document-and-its-descendants@@ -10482,10 +10488,6 @@ bool Document::is_render_blocked() constif (now > max_time_to_block_rendering_in_ms)return false;- // AD-HOC: Consider pending CSS @import rules as render-blocking- if (!m_pending_css_import_rules.is_empty())- return true;-if (!m_render_blocking_elements.is_empty())return true;diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex 51c62ff01e5..05495f767b4 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -6612,6 +6612,12 @@ void Element::attribute_changed(Utf16FlyString const& local_name, Optional<Utf16return;}+ // https://html.spec.whatwg.org/multipage/dom.html#render-blocking-mechanism+ // Whenever a render-blocking element el becomes browsing-context disconnected, or el's blocking attribute's+ // value is changed so that el is no longer potentially render-blocking, then unblock rendering on el.+ if (local_name == HTML::AttributeNames::blocking && document().is_render_blocking_element(*this) && !is_potentially_render_blocking())+ unblock_rendering();+// https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-change-ext①// 1. If localName is slot and namespace is null, then:if (local_name == HTML::AttributeNames::slot) {@@ -7413,10 +7419,25 @@ void Element::unblock_rendering()// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#potentially-render-blockingbool Element::is_potentially_render_blocking(){- // An element is potentially render-blocking if- // FIXME: its blocking tokens set contains "render",- // or if it is implicitly potentially render-blocking, which will be defined at the individual elements.- return is_implicitly_potentially_render_blocking();+ // An element is potentially render-blocking if its blocking tokens set contains "render", or if it is+ // implicitly potentially render-blocking, which will be defined at the individual elements.+ if (is_implicitly_potentially_render_blocking())+ return true;+ if (namespace_uri() != Namespace::HTML || !local_name().is_one_of(HTML::TagNames::link, HTML::TagNames::script, HTML::TagNames::style))+ return false;+ auto value = attribute(HTML::AttributeNames::blocking);+ if (!value.has_value())+ return false;+ auto tokens = value->utf16_view();+ size_t start = 0;+ for (size_t i = 0; i <= tokens.length_in_code_units(); ++i) {+ if (i != tokens.length_in_code_units() && !Infra::is_ascii_whitespace(tokens.code_unit_at(i)))+ continue;+ if (i > start && tokens.substring_view(start, i - start).equals_ignoring_ascii_case(u"render"sv))+ return true;+ start = i + 1;+ }+ return false;}}diff --git a/Libraries/LibWeb/DOM/StyleElementBase.cpp b/Libraries/LibWeb/DOM/StyleElementBase.cppindex 7ef7e207419..a480b33b46e 100644--- a/Libraries/LibWeb/DOM/StyleElementBase.cpp+++ b/Libraries/LibWeb/DOM/StyleElementBase.cpp@@ -6,6 +6,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/ScopeGuard.h>#include <LibWeb/CSS/Parser/Parser.h>#include <LibWeb/CSS/StyleComputer.h>#include <LibWeb/CSS/StyleEngineInput.h>@@ -67,6 +68,11 @@ void StyleElementBase::style_element_moved()m_style_sheet_scope = &destination;}+bool StyleElementBase::style_element_was_created_by_parser() const+{+ return m_parser_document == &as_element().document();+}+// The user agent must run the "update a style block" algorithm whenever one of the following conditions occur:// The element is popped off the stack of open elements of an HTML parser or XML parser.//@@ -114,6 +120,13 @@ void StyleElementBase::update_a_style_block(UpdateSource update_source)// replaced before that sheet's critical subresources finish loading. Keep that set and any queued// completions in sync with the sheet removed above.++m_style_sheet_update_generation;+ // NB: Keep an existing render-blocking entry while replacing its sheet. Once the body exists,+ // removing the entry would prevent the new sheet from blocking rendering again.+ bool keep_render_blocking = false;+ ScopeGuard unblock_replaced_sheet = [&] {+ if (!keep_render_blocking && style_element.document().is_render_blocking_element(style_element))+ style_element.unblock_rendering();+ };remove_from_script_blocking_style_sheet_set_if_needed();clear_associated_css_style_sheet_parser_blocking_state();@@ -204,7 +217,11 @@ void StyleElementBase::update_a_style_block(UpdateSource update_source)m_associated_css_style_sheet_is_blocking_scripts = true;}- // FIXME: 8. If element's media attribute's value matches the environment and element is potentially render-blocking, then block rendering on element.+ // 8. If element's media attribute's value matches the environment and element is potentially render-blocking, then block rendering on element.+ if (m_associated_css_style_sheet_media_matches_environment && style_element.is_potentially_render_blocking()) {+ style_element.block_rendering();+ keep_render_blocking = true;+ }// AD-HOC: Check if we have already loaded the sheet's resources.auto loading_state = m_associated_css_style_sheet->loading_state();@@ -231,7 +248,7 @@ void StyleElementBase::finished_loading_critical_subresources(AnyFailed any_failsuccess = false;// 4. Queue an element task on the networking task source given element and the following steps:- element.queue_an_element_task(HTML::Task::Source::UserInteraction, [&element, success, style_sheet_update_generation] {+ element.queue_an_element_task(HTML::Task::Source::Networking, [&element, success, style_sheet_update_generation] {// AD-HOC: If the associated stylesheet has been replaced or removed since this task was queued, ignore it.auto* style_element_base = as_if<StyleElementBase>(element);if (!style_element_base || style_element_base->m_style_sheet_update_generation != style_sheet_update_generation)@@ -248,6 +265,10 @@ void StyleElementBase::finished_loading_critical_subresources(AnyFailed any_failelement.dispatch_event(DOM::Event::create(HTML::EventNames::error,HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(element))));+ // NB: A load or error handler can replace the sheet and start blocking for its new resources.+ if (style_element_base->m_style_sheet_update_generation != style_sheet_update_generation)+ return;+// 3. If element contributes a script-blocking style sheet:if (element.contributes_a_script_blocking_style_sheet()) {// 1. Assert: element's node document's script-blocking style sheet set contains element.diff --git a/Libraries/LibWeb/DOM/StyleElementBase.h b/Libraries/LibWeb/DOM/StyleElementBase.hindex 091b0bcdfbc..91ce799a730 100644--- a/Libraries/LibWeb/DOM/StyleElementBase.h+++ b/Libraries/LibWeb/DOM/StyleElementBase.h@@ -58,6 +58,7 @@ public:protected:bool style_element_contributes_a_script_blocking_style_sheet() const;+ bool style_element_was_created_by_parser() const;private:void evaluate_associated_style_sheet_media_queries();diff --git a/Libraries/LibWeb/HTML/AttributeNames.h b/Libraries/LibWeb/HTML/AttributeNames.hindex f38879c43e5..6011ddf3ae2 100644--- a/Libraries/LibWeb/HTML/AttributeNames.h+++ b/Libraries/LibWeb/HTML/AttributeNames.h@@ -36,6 +36,7 @@ namespace AttributeNames {__ENUMERATE_HTML_ATTRIBUTE(background, "background") \__ENUMERATE_HTML_ATTRIBUTE(behavior, "behavior") \__ENUMERATE_HTML_ATTRIBUTE(bgcolor, "bgcolor") \+ __ENUMERATE_HTML_ATTRIBUTE(blocking, "blocking") \__ENUMERATE_HTML_ATTRIBUTE(border, "border") \__ENUMERATE_HTML_ATTRIBUTE(bordercolor, "bordercolor") \__ENUMERATE_HTML_ATTRIBUTE(bottommargin, "bottommargin") \diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cppindex c0c5b840d2d..35b5624fbe1 100644--- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp@@ -9,6 +9,7 @@#include <AK/ByteBuffer.h>#include <AK/Debug.h>+#include <AK/ScopeGuard.h>#include <LibCore/ImmutableBytes.h>#include <LibGC/Heap.h>#include <LibGfx/Bitmap.h>@@ -66,9 +67,18 @@ HTMLLinkElement::HTMLLinkElement(DOM::Document& document, DOM::QualifiedName quaHTMLLinkElement::~HTMLLinkElement() = default;+// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#blocking-attribute+GC::Ref<DOM::DOMTokenList> HTMLLinkElement::blocking()+{+ if (!m_blocking)+ m_blocking = DOM::DOMTokenList::create(*this, AttributeNames::blocking);+ return *m_blocking;+}+void HTMLLinkElement::visit_edges(Cell::Visitor& visitor){Base::visit_edges(visitor);+ visitor.visit(m_blocking);visitor.visit(m_fetch_controller);visitor.visit(m_loaded_style_sheet);visitor.visit(m_rel_list);@@ -220,6 +230,8 @@ bool HTMLLinkElement::has_icon_keyword() constvoid HTMLLinkElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_){Base::attribute_changed(name, old_value, value, namespace_);+ if (!namespace_.has_value() && name == AttributeNames::blocking && m_blocking)+ m_blocking->associated_attribute_changed(value.has_value() ? value->utf16_view() : u""sv);// https://html.spec.whatwg.org/multipage/semantics.html#processing-the-type-attribute:attr-link-typeif (name == HTML::AttributeNames::type) {@@ -270,6 +282,10 @@ void HTMLLinkElement::attribute_changed(Utf16FlyString const& name, Optional<Utfif (m_rel_list)m_rel_list->associated_attribute_changed(value.has_value() ? value->utf16_view() : u""sv);++ // NB: A link that no longer represents a stylesheet will not run stylesheet completion steps.+ if ((old_relationship & Relationship::Stylesheet) && !(m_relationship & Relationship::Stylesheet))+ cancel_pending_stylesheet_processing();}// https://html.spec.whatwg.org/multipage/semantics.html#the-link-element:explicitly-enabled@@ -487,7 +503,14 @@ void HTMLLinkElement::fetch_and_process_linked_resource(){auto fetch_generation = ++m_current_fetch_generation;- cancel_pending_stylesheet_processing();+ // NB: Replacing a pending stylesheet must preserve its render-blocking entry even after the body was inserted.+ cancel_pending_stylesheet_processing(UnblockRendering::No);+ ScopeGuard unblock_if_no_blocking_stylesheet = [this] {+ if (document().is_render_blocking_element(*this)+ && ((m_relationship & Relationship::Icon) || !(m_relationship & Relationship::Stylesheet) || !m_fetch_controller || !is_potentially_render_blocking()+ || !CSS::RustMediaList::parse(media()).evaluate(document())))+ unblock_rendering();+ };if (m_fetch_controller) {m_fetch_controller->stop_fetch();@@ -869,8 +892,7 @@ bool HTMLLinkElement::stylesheet_linked_resource_fetch_setup_steps(Fetch::Infrasdocument().script_blocking_style_sheet_set().set(*this);// 3. If el's media attribute's value matches the environment and el is potentially render-blocking, then block rendering on el.- // FIXME: Check media attribute value.- if (is_potentially_render_blocking())+ if (is_potentially_render_blocking() && CSS::RustMediaList::parse(media()).evaluate(document()))block_rendering();m_document_load_event_delayer.emplace(document(), DOM::DocumentLoadEventDelayerReason::StyleSheetRequest);@@ -1584,24 +1606,29 @@ void HTMLLinkElement::associate_empty_style_sheet(Fetch::Infrastructure::Responsassociate_style_sheet(*parse_css_stylesheet(CSS::Parser::ParsingParams { document() }, u""sv, move(location)), origin_clean);}-void HTMLLinkElement::cancel_pending_stylesheet_processing()+void HTMLLinkElement::cancel_pending_stylesheet_processing(UnblockRendering unblock_rendering){m_fetch_generation_waiting_for_critical_subresources = {};- if (!m_stylesheet_processing_pending)+ if (!m_stylesheet_processing_pending && !m_document_load_event_delayer.has_value())return;m_stylesheet_processing_pending = false;- stop_blocking_on_style_sheet();+ stop_blocking_on_style_sheet(unblock_rendering);}-void HTMLLinkElement::stop_blocking_on_style_sheet()+void HTMLLinkElement::stop_blocking_on_style_sheet(UnblockRendering unblock_rendering){document().remove_from_script_blocking_style_sheet_set(*this);- unblock_rendering();+ if (unblock_rendering == UnblockRendering::Yes)+ this->unblock_rendering();m_document_load_event_delayer.clear();}void HTMLLinkElement::finish_processing_stylesheet_resource(u64 fetch_generation){+ // NB: A load or error handler can start another fetch, which owns its own blocking state.+ if (fetch_generation != m_current_fetch_generation)+ return;+// 6. If el contributes a script-blocking style sheet, then:if (document().script_blocking_style_sheet_set().contains(*this)) {// 1. Assert: el's node document's script-blocking style sheet set contains el.@@ -1614,10 +1641,7 @@ void HTMLLinkElement::finish_processing_stylesheet_resource(u64 fetch_generation// 7. Unblock rendering on el.unblock_rendering();- // NB: Loading completion is element-based, but a newer parse may already be pending after- // event dispatch. Do not mark that worker result as canceled.- if (fetch_generation == m_current_fetch_generation)- m_stylesheet_processing_pending = false;+ m_stylesheet_processing_pending = false;if (m_loaded_style_sheet) {auto style_sheet_loading_state = m_loaded_style_sheet->loading_state();diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.h b/Libraries/LibWeb/HTML/HTMLLinkElement.hindex 0779eee8762..61f3864a851 100644--- a/Libraries/LibWeb/HTML/HTMLLinkElement.h+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.h@@ -32,6 +32,8 @@ class WEB_API HTMLLinkElement final : public HTMLElement {public:virtual ~HTMLLinkElement() override;+ GC::Ref<DOM::DOMTokenList> blocking();+virtual void inserted() override;virtual void removed_from(IsSubtreeRoot, Node* old_ancestor, Node& old_root) override;virtual void moved_from(IsSubtreeRoot, GC::Ptr<Node> old_ancestor) override;@@ -159,6 +161,8 @@ private:};HTMLLinkElement(DOM::Document&, DOM::QualifiedName);++ GC::Ptr<DOM::DOMTokenList> m_blocking;virtual void visit_edges(Cell::Visitor&) override;virtual void adopted_from(DOM::Document&) override;@@ -196,8 +200,12 @@ private:void associate_style_sheet(CSS::StyleSheetState&, CSS::StyleScope::OriginClean);void associate_empty_style_sheet(Fetch::Infrastructure::ResponseView const&, CSS::StyleScope::OriginClean);void finish_processing_stylesheet_resource(u64 fetch_generation);- void cancel_pending_stylesheet_processing();- void stop_blocking_on_style_sheet();+ enum class UnblockRendering : u8 {+ No,+ Yes,+ };+ void cancel_pending_stylesheet_processing(UnblockRendering = UnblockRendering::Yes);+ void stop_blocking_on_style_sheet(UnblockRendering = UnblockRendering::Yes);bool should_fetch_and_process_resource_type() const;diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.idl b/Libraries/LibWeb/HTML/HTMLLinkElement.idlindex 7f967cc33b8..7419cbb53f5 100644--- a/Libraries/LibWeb/HTML/HTMLLinkElement.idl+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.idl@@ -46,7 +46,7 @@ interface HTMLLinkElement : HTMLElement {[CEReactions, Reflect=imagesrcset] attribute USVString imageSrcset;[CEReactions, Reflect=imagesizes] attribute DOMString imageSizes;[CEReactions, Reflect=referrerpolicy, Enumerated=ReferrerPolicy] attribute DOMString referrerPolicy;- [FIXME, SameObject, PutForwards=value] readonly attribute DOMTokenList blocking;+ [SameObject, PutForwards=value] readonly attribute DOMTokenList blocking;[CEReactions, Reflect] attribute boolean disabled;[CEReactions, Enumerated=FetchPriorityAttribute, Reflect=fetchpriority] attribute DOMString fetchPriority;diff --git a/Libraries/LibWeb/HTML/HTMLScriptElement.cpp b/Libraries/LibWeb/HTML/HTMLScriptElement.cppindex 9f75899d258..d26095ef48d 100644--- a/Libraries/LibWeb/HTML/HTMLScriptElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLScriptElement.cpp@@ -9,6 +9,7 @@#include <AK/Utf16StringBuilder.h>#include <LibTextCodec/Decoder.h>#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>+#include <LibWeb/DOM/DOMTokenList.h>#include <LibWeb/DOM/Document.h>#include <LibWeb/DOM/Event.h>#include <LibWeb/DOM/ShadowRoot.h>@@ -44,9 +45,18 @@ HTMLScriptElement::HTMLScriptElement(DOM::Document& document, DOM::QualifiedNameHTMLScriptElement::~HTMLScriptElement() = default;+// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#blocking-attribute+GC::Ref<DOM::DOMTokenList> HTMLScriptElement::blocking()+{+ if (!m_blocking)+ m_blocking = DOM::DOMTokenList::create(*this, AttributeNames::blocking);+ return *m_blocking;+}+void HTMLScriptElement::visit_edges(Cell::Visitor& visitor){Base::visit_edges(visitor);+ visitor.visit(m_blocking);m_result.visit([](ResultState::Uninitialized) { },[](ResultState::Null) { },@@ -66,6 +76,8 @@ void HTMLScriptElement::adopted_from(DOM::Document& old_document)void HTMLScriptElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_){Base::attribute_changed(name, old_value, value, namespace_);+ if (!namespace_.has_value() && name == AttributeNames::blocking && m_blocking)+ m_blocking->associated_attribute_changed(value.has_value() ? value->utf16_view() : u""sv);if (!namespace_.has_value() && name == HTML::AttributeNames::crossorigin) {m_crossorigin = cors_setting_attribute_from_keyword(value.map([](auto const& value) { return value.utf16_view(); }));@@ -461,7 +473,9 @@ void HTMLScriptElement::prepare_script()// 8. Set el's delaying the load event to true.begin_delaying_document_load_event(*m_preparation_time_document);- // FIXME: 9. If el is currently render-blocking, then set options's render-blocking to true.+ // 9. If el is currently render-blocking, then set options's render-blocking to true.+ if (document().is_render_blocking_element(*this))+ options.render_blocking = true;// 10. Let onComplete given result be the following steps:OnFetchScriptComplete on_complete = create_on_fetch_script_complete(heap(), [this](auto result) {@@ -522,6 +536,14 @@ void HTMLScriptElement::prepare_script()// 1. Set el's delaying the load event to true.begin_delaying_document_load_event(*m_preparation_time_document);+ // 2. If el is potentially render-blocking:+ if (is_potentially_render_blocking()) {+ // 1. Block rendering on el.+ block_rendering();+ // 2. Set options's render-blocking to true.+ options.render_blocking = true;+ }+auto steps = create_on_fetch_script_complete(heap(), [this](auto result) {// 1. Queue an element task on the networking task source given el to perform the following steps:queue_an_element_task(Task::Source::Networking, [this, result = move(result)] {@@ -533,7 +555,7 @@ void HTMLScriptElement::prepare_script()});});- // 2. Fetch an inline module script graph, given source text, base URL, settings object, options, and with the following steps given result:+ // 3. Fetch an inline module script graph, given source text, base URL, settings object, options, and with the following steps given result:fetch_inline_module_script_graph(HTML::relevant_realm(*this), m_document->url().to_byte_string(), source_text.utf16_view(), base_url, document().relevant_settings_object(), options, m_source_line_number, steps);}// -> "importmap"@@ -611,7 +633,8 @@ void HTMLScriptElement::prepare_script()// 1. Set el's parser document's pending parsing-blocking script to el.m_parser_document->set_pending_parsing_blocking_script(this);- // FIXME: 2. Block rendering on el.+ // 2. Block rendering on el.+ block_rendering();// 3. Set el's steps to run when the result is ready to the following:m_steps_to_run_when_the_result_is_ready = [this] {diff --git a/Libraries/LibWeb/HTML/HTMLScriptElement.h b/Libraries/LibWeb/HTML/HTMLScriptElement.hindex 40f595b9627..c9fe38c4d51 100644--- a/Libraries/LibWeb/HTML/HTMLScriptElement.h+++ b/Libraries/LibWeb/HTML/HTMLScriptElement.h@@ -26,6 +26,8 @@ class HTMLScriptElement final : public HTMLElement {public:virtual ~HTMLScriptElement() override;+ GC::Ref<DOM::DOMTokenList> blocking();+bool is_force_async() const { return m_force_async; }bool is_ready_to_be_parser_executed() const { return m_ready_to_be_parser_executed; }bool failed_to_load() const { return m_failed_to_load; }@@ -81,6 +83,8 @@ protected:private:HTMLScriptElement(DOM::Document&, DOM::QualifiedName);+ GC::Ptr<DOM::DOMTokenList> m_blocking;+virtual bool is_html_script_element() const override { return true; }virtual void visit_edges(Cell::Visitor&) override;virtual void adopted_from(DOM::Document&) override;diff --git a/Libraries/LibWeb/HTML/HTMLScriptElement.idl b/Libraries/LibWeb/HTML/HTMLScriptElement.idlindex 04d86f81427..f0d236a79ca 100644--- a/Libraries/LibWeb/HTML/HTMLScriptElement.idl+++ b/Libraries/LibWeb/HTML/HTMLScriptElement.idl@@ -7,7 +7,7 @@ interface HTMLScriptElement : HTMLElement {[CEReactions, Reflect=nomodule] attribute boolean noModule;[CEReactions] attribute boolean async;[CEReactions, Reflect] attribute boolean defer;- [FIXME, SameObject, PutForwards=value] readonly attribute DOMTokenList blocking;+ [SameObject, PutForwards=value] readonly attribute DOMTokenList blocking;[CEReactions, Reflect=crossorigin, Enumerated=CORSSettingsAttribute] attribute DOMString? crossOrigin;[CEReactions, Reflect=referrerpolicy, Enumerated=ReferrerPolicy] attribute DOMString referrerPolicy;[CEReactions, Reflect] attribute DOMString integrity;diff --git a/Libraries/LibWeb/HTML/HTMLStyleElement.cpp b/Libraries/LibWeb/HTML/HTMLStyleElement.cppindex 91cefebfb18..bf3c61950c6 100644--- a/Libraries/LibWeb/HTML/HTMLStyleElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLStyleElement.cpp@@ -7,6 +7,7 @@*/#include <LibWeb/CSS/StyleComputer.h>+#include <LibWeb/DOM/DOMTokenList.h>#include <LibWeb/DOM/Document.h>#include <LibWeb/HTML/HTMLStyleElement.h>@@ -21,9 +22,18 @@ HTMLStyleElement::HTMLStyleElement(DOM::Document& document, DOM::QualifiedName qHTMLStyleElement::~HTMLStyleElement() = default;+// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#blocking-attribute+GC::Ref<DOM::DOMTokenList> HTMLStyleElement::blocking()+{+ if (!m_blocking)+ m_blocking = DOM::DOMTokenList::create(*this, AttributeNames::blocking);+ return *m_blocking;+}+void HTMLStyleElement::visit_edges(Cell::Visitor& visitor){Base::visit_edges(visitor);+ visitor.visit(m_blocking);visit_style_element_edges(visitor);}@@ -61,9 +71,18 @@ void HTMLStyleElement::moved_from(IsSubtreeRoot is_subtree_root, GC::Ptr<Node> ovoid HTMLStyleElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_){Base::attribute_changed(name, old_value, value, namespace_);+ if (!namespace_.has_value() && name == AttributeNames::blocking && m_blocking)+ m_blocking->associated_attribute_changed(value.has_value() ? value->utf16_view() : u""sv);style_element_attribute_changed(name, value);}+// https://html.spec.whatwg.org/multipage/semantics.html#the-style-element+bool HTMLStyleElement::is_implicitly_potentially_render_blocking() const+{+ // A style element is implicitly potentially render-blocking if the element was created by its node document's parser.+ return style_element_was_created_by_parser();+}+// https://html.spec.whatwg.org/multipage/semantics.html#contributes-a-script-blocking-style-sheetbool HTMLStyleElement::contributes_a_script_blocking_style_sheet() const{diff --git a/Libraries/LibWeb/HTML/HTMLStyleElement.h b/Libraries/LibWeb/HTML/HTMLStyleElement.hindex 9b3d43f38e7..ebbda5cd933 100644--- a/Libraries/LibWeb/HTML/HTMLStyleElement.h+++ b/Libraries/LibWeb/HTML/HTMLStyleElement.h@@ -21,6 +21,8 @@ class HTMLStyleElement finalpublic:virtual ~HTMLStyleElement() override;+ GC::Ref<DOM::DOMTokenList> blocking();+virtual void children_changed(ChildrenChangedMetadata const&) override;virtual void inserted() override;virtual void removed_from(IsSubtreeRoot, Node* old_ancestor, Node& old_root) override;@@ -28,10 +30,13 @@ public:virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;virtual bool contributes_a_script_blocking_style_sheet() const final;+ virtual bool is_implicitly_potentially_render_blocking() const override;private:HTMLStyleElement(DOM::Document&, DOM::QualifiedName);+ GC::Ptr<DOM::DOMTokenList> m_blocking;+// ^DOM::Nodevirtual bool is_html_style_element() const override { return true; }diff --git a/Libraries/LibWeb/HTML/HTMLStyleElement.idl b/Libraries/LibWeb/HTML/HTMLStyleElement.idlindex 1400b9c1f3e..e608f4b9eb0 100644--- a/Libraries/LibWeb/HTML/HTMLStyleElement.idl+++ b/Libraries/LibWeb/HTML/HTMLStyleElement.idl@@ -5,7 +5,7 @@ interface HTMLStyleElement : HTMLElement {attribute boolean disabled;[CEReactions, Reflect] attribute DOMString media;- [FIXME, SameObject, PutForwards=value] readonly attribute DOMTokenList blocking;+ [SameObject, PutForwards=value] readonly attribute DOMTokenList blocking;// Obsolete[Reflect] attribute DOMString type;diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex 92babce62e2..05d5c4cf1bc 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -13,6 +13,8 @@ Text/input/wpt-import/permissions/; Their harness scripts are served from the WPT root, which a file: page cannot reach.Crash/wpt-import/permissions/; Render-blocking tests need delayed HTTP responses and module imports.+Text/input/HTML/render-blocking-resource-cancellation.html+Text/input/HTML/render-blocking-resource-load-replacement.htmlText/input/wpt-import/html/dom/render-blocking/blocking-idl-attr.htmlText/input/wpt-import/html/dom/render-blocking/script-inserted-inline-module-with-import.htmlText/input/wpt-import/html/dom/render-blocking/parser-inserted-module-script.htmldiff --git a/Tests/LibWeb/Text/expected/HTML/blocking-tokens-reflection.txt b/Tests/LibWeb/Text/expected/HTML/blocking-tokens-reflection.txtindex 1f1e5132711..43374ff3d80 100644--- a/Tests/LibWeb/Text/expected/HTML/blocking-tokens-reflection.txt+++ b/Tests/LibWeb/Text/expected/HTML/blocking-tokens-reflection.txt@@ -1,3 +1,21 @@-link: blocking is undefined-script: blocking is undefined-style: blocking is undefined+link: same object true+attribute to tokens: render extra, 2+tokens to attribute: RENDER+supported: true, true, false+namespaced attribute leaves tokens: RENDER+added token: RENDER other+removed attribute empties tokens: 0+script: same object true+attribute to tokens: render extra, 2+tokens to attribute: RENDER+supported: true, true, false+namespaced attribute leaves tokens: RENDER+added token: RENDER other+removed attribute empties tokens: 0+style: same object true+attribute to tokens: render extra, 2+tokens to attribute: RENDER+supported: true, true, false+namespaced attribute leaves tokens: RENDER+added token: RENDER other+removed attribute empties tokens: 0diff --git a/Tests/LibWeb/Text/expected/HTML/render-blocking-resource-cancellation.txt b/Tests/LibWeb/Text/expected/HTML/render-blocking-resource-cancellation.txtnew file mode 100644index 00000000000..4b6ad25d51a--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/render-blocking-resource-cancellation.txt@@ -0,0 +1,21 @@+script: rendering resumes after stop+script: rendering resumes after remove+script: rendering resumes after remove token+script: rendering resumes after navigate+script: rendering resumes after fail+module: rendering resumes after stop+module: rendering resumes after remove+module: rendering resumes after remove token+module: rendering resumes after navigate+module: rendering resumes after fail+style: rendering resumes after stop+style: rendering resumes after remove+style: rendering resumes after remove token+style: rendering resumes after navigate+style: rendering resumes after fail+link: rendering resumes after stop+link: rendering resumes after remove+link: rendering resumes after remove token+link: rendering resumes after navigate+link: rendering resumes after fail+link: rendering resumes after remove reldiff --git a/Tests/LibWeb/Text/expected/HTML/render-blocking-resource-load-replacement.txt b/Tests/LibWeb/Text/expected/HTML/render-blocking-resource-load-replacement.txtnew file mode 100644index 00000000000..37db3d84eee--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/render-blocking-resource-load-replacement.txt@@ -0,0 +1,2 @@+style: first frame includes replacement from load handler: rgb(4, 5, 6)+link: first frame includes replacement from load handler: rgb(4, 5, 6)diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/blocking-idl-attr.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/blocking-idl-attr.txtindex c98f20c9837..188062b534d 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/blocking-idl-attr.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/blocking-idl-attr.txt@@ -2,10 +2,10 @@ Harness status: OKFound 6 tests-6 Fail-Fail Supported tokens of the 'blocking' IDL attribute of the link element-Fail Setting the 'blocking' IDL attribute of the link element-Fail Supported tokens of the 'blocking' IDL attribute of the script element-Fail Setting the 'blocking' IDL attribute of the script element-Fail Supported tokens of the 'blocking' IDL attribute of the style element-Fail Setting the 'blocking' IDL attribute of the style element\ No newline at end of file+6 Pass+Pass Supported tokens of the 'blocking' IDL attribute of the link element+Pass Setting the 'blocking' IDL attribute of the link element+Pass Supported tokens of the 'blocking' IDL attribute of the script element+Pass Setting the 'blocking' IDL attribute of the script element+Pass Supported tokens of the 'blocking' IDL attribute of the style element+Pass Setting the 'blocking' IDL attribute of the style element\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-async-inline-module-with-import.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-async-inline-module-with-import.txtindex 6e9b7b7f31a..07b5d6a5895 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-async-inline-module-with-import.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-async-inline-module-with-import.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail Parser-inserted async inline module script elements with "blocking=render" are render-blocking\ No newline at end of file+1 Pass+Pass Parser-inserted async inline module script elements with "blocking=render" are render-blocking\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-async-script.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-async-script.txtindex 97b28fcac73..66a96ae35d1 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-async-script.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-async-script.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail Rendering is blocked before render-blocking resources are loaded+2 Pass+Pass Rendering is blocked before render-blocking resources are loadedPass Parser-inserted render-blocking async script is evaluated\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-defer-script.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-defer-script.txtindex eeace5f939b..75a67f52e7b 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-defer-script.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-defer-script.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail Rendering is blocked before render-blocking resources are loaded+2 Pass+Pass Rendering is blocked before render-blocking resources are loadedPass Parser-inserted render-blocking defer script is evaluated\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-inline-module-with-import.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-inline-module-with-import.txtindex d307f6eece9..e097de93502 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-inline-module-with-import.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-inline-module-with-import.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail Parser-inserted module script elements with "blocking=render" are render-blocking\ No newline at end of file+1 Pass+Pass Parser-inserted module script elements with "blocking=render" are render-blocking\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-module-script.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-module-script.txtindex fece26ac744..1322210bcaa 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-module-script.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/parser-inserted-module-script.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail Rendering is blocked before render-blocking resources are loaded+2 Pass+Pass Rendering is blocked before render-blocking resources are loadedPass Parser-inserted render-blocking module script is evaluated\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-inline-module-with-import.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-inline-module-with-import.txtindex d6089b394e8..19a6bc6680c 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-inline-module-with-import.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-inline-module-with-import.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail Script-inserted module script elements with "blocking=render" are render-blocking\ No newline at end of file+1 Pass+Pass Script-inserted module script elements with "blocking=render" are render-blocking\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-module-script.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-module-script.txtindex 02384e62001..189cf3943af 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-module-script.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-module-script.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail Rendering is blocked before render-blocking resources are loaded+2 Pass+Pass Rendering is blocked before render-blocking resources are loadedPass Script-inserted render-blocking module script is evaluated\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-script.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-script.txtindex 11274f78b4e..d6a9366d9e7 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-script.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-script.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail Rendering is blocked before render-blocking resources are loaded+2 Pass+Pass Rendering is blocked before render-blocking resources are loadedPass Script-inserted render-blocking script is evaluated\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-stylesheet-link.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-stylesheet-link.txtindex e0d8b05e0e4..4235474be8f 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-stylesheet-link.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/script-inserted-stylesheet-link.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail-Fail Rendering is blocked before render-blocking resources are loaded+2 Pass+Pass Rendering is blocked before render-blocking resources are loadedPass Render-blocking stylesheet is applied\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/stylesheet-link-href-change-keeps-blocking.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/stylesheet-link-href-change-keeps-blocking.txtindex e1d18367a80..6833db26321 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/stylesheet-link-href-change-keeps-blocking.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/render-blocking/stylesheet-link-href-change-keeps-blocking.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail The stylesheet with the new href applies in the first rendering update\ No newline at end of file+1 Pass+Pass The stylesheet with the new href applies in the first rendering update\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/HTML/render-blocking-resource-cancellation.html b/Tests/LibWeb/Text/input/HTML/render-blocking-resource-cancellation.htmlnew file mode 100644index 00000000000..b8fa3852596--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/render-blocking-resource-cancellation.html@@ -0,0 +1,64 @@+<!doctype html>+<script src="../include.js"></script>+<script>+promiseTest(async () => {+ const server = httpTestServer();+ for (const kind of ["script", "module", "style", "link"]) {+ for (const action of ["stop", "remove", "remove token", "navigate", "fail", ...(kind === "link" ? ["remove rel"] : [])]) {+ const token = crypto.randomUUID();+ const resource = await server.createEcho("GET", `/render-blocking-cancel-${token}`, {+ status: action === "fail" ? 404 : 200,+ headers: { "Content-Type": kind === "script" || kind === "module" ? "text/javascript" : "text/css" },+ body: "",+ wait_for_unblock: token,+ });+ const frame = document.createElement("iframe");+ const ready = new Promise(resolve => {+ window.addEventListener("message", function receive(event) {+ if (event.source !== frame.contentWindow || event.data !== token) return;+ window.removeEventListener("message", receive);+ resolve();+ });+ });+ frame.src = await server.createEcho("GET", `/render-blocking-cancel-document-${token}`, {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: `<!doctype html><head><script>+ const blocker = document.createElement(${JSON.stringify(kind === "module" ? "script" : kind)});+ blocker.id = "blocker";+ blocker.blocking = "render";+ ${kind === "script" ? `blocker.src = ${JSON.stringify(resource)};` : ""}+ ${kind === "module" ? `blocker.type = "module"; blocker.textContent = ${JSON.stringify(`import "${resource}";`)};` : ""}+ ${kind === "style" ? `blocker.textContent = ${JSON.stringify(`@import url("${resource}");`)};` : ""}+ ${kind === "link" ? `blocker.rel = "stylesheet"; blocker.href = ${JSON.stringify(resource)};` : ""}+ document.head.append(blocker);+ <\/script></head><body>Some text<script>+ parent.postMessage(${JSON.stringify(token)}, "*");+ <\/script>`,+ });+ document.body.append(frame);+ await ready;+ while (!(await fetch(server.recordedRequestHeadersURL(resource))).ok) {}+ let renderedWhileBlocked = false;+ frame.contentWindow.requestAnimationFrame(() => (renderedWhileBlocked = true));+ for (let i = 0; i < 3; ++i) await new Promise(resolve => requestAnimationFrame(resolve));+ if (renderedWhileBlocked) println(`FAIL: ${kind}: rendering was not blocked before ${action}`);+ const blocker = frame.contentDocument.getElementById("blocker");+ if (action === "stop") frame.contentWindow.stop();+ else if (action === "remove") blocker.remove();+ else if (action === "remove token") blocker.blocking = "";+ else if (action === "remove rel") blocker.rel = "";+ else if (action === "fail") await fetch(new URL(`/unblock/${token}`, resource));+ else {+ const navigated = new Promise(resolve => frame.addEventListener("load", resolve, { once: true }));+ frame.src = "about:blank";+ await navigated;+ }+ await new Promise(resolve => frame.contentWindow.requestAnimationFrame(resolve));+ println(`${kind}: rendering resumes after ${action}`);+ frame.remove();+ await fetch(new URL(`/unblock/${token}`, resource));+ }+ }+});+</script>diff --git a/Tests/LibWeb/Text/input/HTML/render-blocking-resource-load-replacement.html b/Tests/LibWeb/Text/input/HTML/render-blocking-resource-load-replacement.htmlnew file mode 100644index 00000000000..b5d3eb2ce4a--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/render-blocking-resource-load-replacement.html@@ -0,0 +1,63 @@+<!doctype html>+<script src="../include.js"></script>+<script>+promiseTest(async () => {+ const server = httpTestServer();+ for (const kind of ["style", "link"]) {+ const firstToken = crypto.randomUUID();+ const secondToken = crypto.randomUUID();+ async function styleSheet(path, token, color) {+ return await server.createEcho("GET", path, {+ status: 200,+ headers: { "Content-Type": "text/css", "Cache-Control": "no-store" },+ body: `body { color: ${color}; }`,+ wait_for_unblock: token,+ });+ }+ const first = await styleSheet(`/render-blocking-${kind}-first.css`, firstToken, "rgb(1, 2, 3)");+ const second = await styleSheet(`/render-blocking-${kind}-second.css`, secondToken, "rgb(4, 5, 6)");+ const frame = document.createElement("iframe");+ let replaced;+ const replacement = new Promise(resolve => replaced = resolve);+ let painted;+ const firstFrame = new Promise(resolve => painted = resolve);+ window.addEventListener("message", function receive(event) {+ if (event.source !== frame.contentWindow) return;+ if (event.data === "replaced") replaced();+ else if (event.data.color) {+ window.removeEventListener("message", receive);+ painted(event.data.color);+ }+ });+ frame.src = await server.createEcho("GET", `/render-blocking-${kind}-load-replacement`, {+ status: 200,+ headers: { "Content-Type": "text/html", "Cache-Control": "no-store" },+ body: `<!doctype html><head><script>+ const style = document.createElement(${JSON.stringify(kind)});+ style.rel = "stylesheet";+ style.blocking = "render";+ ${kind === "style" ? `style.textContent = ${JSON.stringify(`@import url("${first}");`)};` : `style.href = ${JSON.stringify(first)};`}+ style.onload = () => {+ style.onload = null;+ ${kind === "style" ? `style.textContent = ${JSON.stringify(`@import url("${second}");`)};` : `style.href = ${JSON.stringify(second)};`}+ parent.postMessage("replaced", "*");+ };+ document.head.append(style);+ requestAnimationFrame(() => parent.postMessage({ color: getComputedStyle(document.body).color }, "*"));+ <\/script></head><body>Some text</body>`,+ });+ const loaded = new Promise(resolve => frame.addEventListener("load", resolve, { once: true }));+ document.body.append(frame);+ while (!(await fetch(server.recordedRequestHeadersURL(first))).ok) {}+ await fetch(new URL(`/unblock/${firstToken}`, first));+ await replacement;+ while (!(await fetch(server.recordedRequestHeadersURL(second))).ok) {}+ // Give an unblocked child a rendering opportunity while the replacement is unavailable.+ await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));+ await fetch(new URL(`/unblock/${secondToken}`, second));+ println(`${kind}: first frame includes replacement from load handler: ${await firstFrame}`);+ await loaded;+ frame.remove();+ }+});+</script>diff --git a/Tests/LibWeb/Text/input/link-re-enable-crash.html b/Tests/LibWeb/Text/input/link-re-enable-crash.htmlindex 8e73e978978..5581479119f 100644--- a/Tests/LibWeb/Text/input/link-re-enable-crash.html+++ b/Tests/LibWeb/Text/input/link-re-enable-crash.html@@ -3,7 +3,7 @@<script>var scriptOnloadWasInvoked = false;</script>-<link id="link" rel="stylesheet" href="valid.css" onload="link.disabled = true; link.disabled = false;">+<link id="link" rel="stylesheet" href="valid.css" onload="this.onload = null; link.disabled = true; link.disabled = false;"><script>test(() => {println("PASS (didn't crash)");