Skip to content

Truly independent web browser

LibWeb: Wait for @import rules before a link's style sheet loads

Andreas Klinge46f8ad4789e
diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
index ae29906a2b1..1a64afdbac8 100644
--- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
@@ -182,7 +182,25 @@ RefPtr<CSS::StyleSheetState> HTMLLinkElement::sheet() const
void HTMLLinkElement::finished_loading_critical_style_subresources(AnyFailed)
{
+ // NB: The style sheet of an older fetch stays associated until a newer fetch's response is processed. That newer
+ // fetch owns the document load event delayer, so the old sheet's imports must not release it.
+ if (m_loaded_style_sheet_fetch_generation != m_current_fetch_generation)
+ return;
+
m_document_load_event_delayer.clear();
+
+ auto fetch_generation = exchange(m_fetch_generation_waiting_for_critical_subresources, {});
+ if (!fetch_generation.has_value())
+ return;
+
+ queue_an_element_task(Task::Source::Networking, [link = GC::Ref { *this }, fetch_generation = *fetch_generation] {
+ if (fetch_generation != link->m_current_fetch_generation || !link->m_stylesheet_processing_pending)
+ return;
+
+ // 2. Fire an event named load at el.
+ link->dispatch_event(create_event_for_element(*link, HTML::EventNames::load));
+ link->finish_processing_stylesheet_resource(fetch_generation);
+ });
}
bool HTMLLinkElement::has_loaded_icon() const
@@ -1495,6 +1513,13 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastru
sheet->set_base_url(move(base_url));
link->associate_style_sheet(*sheet, origin_clean);
+ // NB: The sheet has to be associated before its critical subresources (@import rules)
+ // get fetched. Wait for them before firing load and unblocking scripts.
+ if (sheet->loading_state() == CSS::StyleSheetState::LoadingState::Loading) {
+ link->m_fetch_generation_waiting_for_critical_subresources = fetch_generation;
+ return;
+ }
+
// 2. Fire an event named load at el.
link->dispatch_event(create_event_for_element(*link, HTML::EventNames::load));
link->finish_processing_stylesheet_resource(fetch_generation);
@@ -1522,6 +1547,7 @@ void HTMLLinkElement::associate_style_sheet(CSS::StyleSheetState& sheet, CSS::St
(m_relationship & Relationship::Alternate && !m_explicitly_enabled) ? CSS::StyleScope::Alternate::Yes : CSS::StyleScope::Alternate::No,
origin_clean, nullptr, nullptr);
m_loaded_style_sheet = sheet;
+ m_loaded_style_sheet_fetch_generation = m_current_fetch_generation;
// NB: Removing disabled explicitly enables the sheet, even if its title would disable it.
if (m_explicitly_enabled)
@@ -1543,6 +1569,7 @@ void HTMLLinkElement::associate_empty_style_sheet(Fetch::Infrastructure::Respons
void HTMLLinkElement::cancel_pending_stylesheet_processing()
{
+ m_fetch_generation_waiting_for_critical_subresources = {};
if (!m_stylesheet_processing_pending)
return;
m_stylesheet_processing_pending = false;
diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.h b/Libraries/LibWeb/HTML/HTMLLinkElement.h
index d05ada02c23..bf96209f3bd 100644
--- a/Libraries/LibWeb/HTML/HTMLLinkElement.h
+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.h
@@ -223,6 +223,8 @@ private:
unsigned m_relationship { 0 };
u64 m_current_fetch_generation { 0 };
bool m_stylesheet_processing_pending { false };
+ Optional<u64> m_fetch_generation_waiting_for_critical_subresources;
+ u64 m_loaded_style_sheet_fetch_generation { 0 };
// https://html.spec.whatwg.org/multipage/semantics.html#explicitly-enabled
bool m_explicitly_enabled { false };
diff --git a/Tests/LibWeb/Text/expected/HTML/link-element-href-change-while-import-pending.txt b/Tests/LibWeb/Text/expected/HTML/link-element-href-change-while-import-pending.txt
new file mode 100644
index 00000000000..c618ddf51f9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/link-element-href-change-while-import-pending.txt
@@ -0,0 +1 @@
+Replacement style sheet applied before load event: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-defer-import.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-defer-import.txt
index 673064d0b87..bba96f90bdd 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-defer-import.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-defer-import.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail scheduler: stylesheets blocking defer scripts
\ No newline at end of file
+1 Pass
+Pass scheduler: stylesheets blocking defer scripts
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-external-import.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-external-import.txt
index 581897296b1..e7d39242786 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-external-import.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-external-import.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail scheduler: stylesheets blocking external parser-blocking scripts
\ No newline at end of file
+1 Pass
+Pass scheduler: stylesheets blocking external parser-blocking scripts
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-external-module-import.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-external-module-import.txt
index 0efcb439009..528a92d06d0 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-external-module-import.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-external-module-import.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail scheduler: stylesheets blocking external module scripts
\ No newline at end of file
+1 Pass
+Pass scheduler: stylesheets blocking external module scripts
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-import.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-import.txt
index 8846cdab20b..2411b705a91 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-import.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-import.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail scheduler: stylesheets blocking scripts
\ No newline at end of file
+1 Pass
+Pass scheduler: stylesheets blocking scripts
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-module-import.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-module-import.txt
index 835a26834b2..1d08674aa82 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-module-import.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-module-import.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail scheduler: stylesheets blocking module scripts
\ No newline at end of file
+1 Pass
+Pass scheduler: stylesheets blocking module scripts
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/150-import.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/150-import.txt
index 9d03ead581f..fc5588fc8ec 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/150-import.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/150-import.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Stylesheet in BODY with @import blocking scripts
\ No newline at end of file
+1 Pass
+Pass Stylesheet in BODY with @import blocking scripts
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/link-element-href-change-while-import-pending.html b/Tests/LibWeb/Text/input/HTML/link-element-href-change-while-import-pending.html
new file mode 100644
index 00000000000..909f71b3965
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/link-element-href-change-while-import-pending.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<div id="target"></div>
+<script>
+function registerEcho(path, body, delay_ms) {
+ const xhr = new XMLHttpRequest();
+ xhr.open("POST", "/echo", false);
+ xhr.setRequestHeader("Content-Type", "application/json");
+ xhr.send(JSON.stringify({
+ method: "GET",
+ path,
+ status: 200,
+ headers: { "Content-Type": "text/css", "Cache-Control": "no-store" },
+ body,
+ delay_ms,
+ }));
+}
+
+registerEcho("/echo/link-href-change-old.css", '@import url("/echo/link-href-change-old-import.css");', 0);
+registerEcho("/echo/link-href-change-old-import.css", "#target { width: 10px; }", 300);
+registerEcho("/echo/link-href-change-new.css", "#target { width: 20px; }", 1000);
+
+const link = document.createElement("link");
+link.rel = "stylesheet";
+link.href = "/echo/link-href-change-old.css";
+document.head.appendChild(link);
+
+function changeHrefOnceOldSheetIsAssociated() {
+ if (!link.sheet) {
+ setTimeout(changeHrefOnceOldSheetIsAssociated, 0);
+ return;
+ }
+ link.href = "/echo/link-href-change-new.css";
+}
+changeHrefOnceOldSheetIsAssociated();
+
+asyncTest(done => {
+ window.addEventListener("load", () => {
+ println(`Replacement style sheet applied before load event: ${getComputedStyle(document.getElementById("target")).width === "20px"}`);
+ done();
+ });
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/link-element-href-change-while-import-pending.html.headers b/Tests/LibWeb/Text/input/HTML/link-element-href-change-while-import-pending.html.headers
new file mode 100644
index 00000000000..156209f9c81
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/link-element-href-change-while-import-pending.html.headers
@@ -0,0 +1 @@
+Content-Type: text/html