Truly independent web browser
LibWeb: Keep a link’s sheet when it’s disabled and enabled again
sideshowbarker76ab4e99ce5d
diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cppindex 35b5624fbe1..98f49854daf 100644--- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp@@ -81,6 +81,7 @@ void HTMLLinkElement::visit_edges(Cell::Visitor& visitor)visitor.visit(m_blocking);visitor.visit(m_fetch_controller);visitor.visit(m_loaded_style_sheet);+ visitor.visit(m_style_sheet_kept_while_disabled);visitor.visit(m_rel_list);visitor.visit(m_sizes);}@@ -112,6 +113,7 @@ void HTMLLinkElement::removed_from(IsSubtreeRoot is_subtree_root, Node* old_ance{Base::removed_from(is_subtree_root, old_ancestor, old_root);cancel_pending_stylesheet_processing();+ m_style_sheet_kept_while_disabled = nullptr;if (m_loaded_style_sheet) {// NB: We can't use `old_root` here. When this link element is nested@@ -296,12 +298,26 @@ void HTMLLinkElement::attribute_changed(Utf16FlyString const& name, Optional<Utfif ((m_relationship & Relationship::Stylesheet) && m_loaded_style_sheet) {if (name == HTML::AttributeNames::disabled) {document_or_shadow_root_style_scope().remove_a_css_style_sheet(*m_loaded_style_sheet);+ // AD-HOC: The link keeps the sheet aside while it's disabled, so that enabling it again before anything+ // else about it changes puts the sheet back at once, rather than leaving the link without a sheet+ // until a new fetch completes. Only a sheet from the link's latest fetch is kept.+ if (m_loaded_style_sheet_fetch_generation == m_current_fetch_generation)+ m_style_sheet_kept_while_disabled = m_loaded_style_sheet;m_loaded_style_sheet = nullptr;} else if (name == HTML::AttributeNames::media) {m_loaded_style_sheet->set_media(value.has_value() ? value->utf16_view() : u""sv);}}+ if (name == HTML::AttributeNames::disabled && !value.has_value() && m_style_sheet_kept_while_disabled) {+ auto sheet = m_style_sheet_kept_while_disabled.release_nonnull();+ if ((m_relationship & Relationship::Stylesheet) && is_browsing_context_connected()+ && m_style_sheet_kept_while_disabled_fetch_generation == m_current_fetch_generation) {+ associate_style_sheet(sheet, sheet->is_origin_clean() ? CSS::StyleScope::OriginClean::Yes : CSS::StyleScope::OriginClean::No);+ return;+ }+ }+if (should_fetch_and_process_resource_type() && is_browsing_context_connected()) {// The appropriate times to fetch and process this type of link are:// - When the href attribute of the link element of an external resource link that is already browsing-context connected is changed.@@ -347,6 +363,11 @@ void HTMLLinkElement::attribute_changed(Utf16FlyString const& name, Optional<Utfif (fetch)fetch_and_process_linked_resource();}++ // NB: Disabling the link starts a fetch, which its setup steps end right away, so the kept sheet belongs with the+ // generation that leaves.+ if (name == HTML::AttributeNames::disabled && value.has_value() && m_style_sheet_kept_while_disabled)+ m_style_sheet_kept_while_disabled_fetch_generation = m_current_fetch_generation;}// https://html.spec.whatwg.org/multipage/semantics.html#contributes-a-script-blocking-style-sheetdiff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.h b/Libraries/LibWeb/HTML/HTMLLinkElement.hindex 61f3864a851..3dd47126440 100644--- a/Libraries/LibWeb/HTML/HTMLLinkElement.h+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.h@@ -227,6 +227,10 @@ private:RefPtr<Gfx::Bitmap const> m_loaded_icon;RefPtr<CSS::StyleSheetState> m_loaded_style_sheet;+ // The style sheet the link had when it was disabled, and the fetch generation then: enabling the link again before+ // it starts another fetch puts the sheet back.+ RefPtr<CSS::StyleSheetState> m_style_sheet_kept_while_disabled;+ u64 m_style_sheet_kept_while_disabled_fetch_generation { 0 };GC::Ptr<DOM::DOMTokenList> m_rel_list;GC::Ptr<DOM::DOMTokenList> m_sizes;diff --git a/Tests/LibWeb/Text/expected/HTML/link-stylesheet-disabled-and-enabled-again.txt b/Tests/LibWeb/Text/expected/HTML/link-stylesheet-disabled-and-enabled-again.txtnew file mode 100644index 00000000000..90781848624--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/link-stylesheet-disabled-and-enabled-again.txt@@ -0,0 +1,5 @@+after load: 2 sheets, background rgb(0, 128, 0)+right after: 1 sheets, background rgb(0, 128, 0)+next frame: 1 sheets, background rgb(0, 128, 0)+requests for the enabled sheet: 1+after its href changed while disabled: 1 sheets, background rgb(0, 0, 255)diff --git a/Tests/LibWeb/Text/input/HTML/link-stylesheet-disabled-and-enabled-again.html b/Tests/LibWeb/Text/input/HTML/link-stylesheet-disabled-and-enabled-again.htmlnew file mode 100644index 00000000000..36264551a1a--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/link-stylesheet-disabled-and-enabled-again.html@@ -0,0 +1,56 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<div id="target"></div>+<script>+ // A theme switcher disables every titled stylesheet link and then enables the one it wants. The link enabled again+ // keeps its sheet the whole time, without fetching it again. A link whose href changed while it was disabled+ // fetches its new sheet.+ asyncTest(async done => {+ const httpServer = httpTestServer();+ async function stylesheet(name, color) {+ return httpServer.createEcho("GET", `/link-stylesheet-disabled-and-enabled-again-${name}.css`, {+ status: 200,+ headers: { "Content-Type": "text/css" },+ body: `#target { background-color: ${color} }`,+ });+ }+ const bright = await stylesheet("bright", "rgb(0, 128, 0)");+ const dark = await stylesheet("dark", "rgb(40, 40, 40)");+ const blue = await stylesheet("blue", "rgb(0, 0, 255)");++ const target = document.getElementById("target");+ const state = () => `${document.styleSheets.length} sheets, background ${getComputedStyle(target).backgroundColor}`;+ const links = [];+ for (const [href, title, rel] of [[bright, "Bright", "stylesheet"], [dark, "Dark", "alternate stylesheet"]]) {+ const link = document.createElement("link");+ link.rel = rel;+ link.href = href;+ link.title = title;+ const loaded = new Promise(resolve => (link.onload = resolve));+ document.head.append(link);+ links.push(link);+ await loaded;+ }+ println(`after load: ${state()}`);++ for (const link of links) {+ link.disabled = true;+ if (link.title === "Bright")+ link.disabled = false;+ }+ println(`right after: ${state()}`);+ await new Promise(resolve => requestAnimationFrame(resolve));+ println(`next frame: ${state()}`);+ const requests = await (await fetch(httpServer.recordedRequestCountURL(bright))).text();+ println(`requests for the enabled sheet: ${requests}`);++ const [brightLink] = links;+ brightLink.disabled = true;+ brightLink.href = blue;+ const loaded = new Promise(resolve => (brightLink.onload = resolve));+ brightLink.disabled = false;+ await loaded;+ println(`after its href changed while disabled: ${state()}`);+ done();+ });+</script>