Truly independent web browser
LibWeb: Use the `type` of a link only to decide whether to fetch it
Tim Ledbetter70df302eeebc
diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cppindex 1c3a4bda30c..01c1882ea88 100644--- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp@@ -229,23 +229,21 @@ bool HTMLLinkElement::has_icon_keyword() constreturn m_relationship & Relationship::Icon;}+static bool is_supported_style_sheet_type_attribute(Optional<Utf16String> const& type)+{+ // The default type for resources given by the stylesheet keyword is text/css.+ if (!type.has_value())+ return true;+ auto mime_type = MimeSniff::MimeType::parse(type->utf16_view());+ return mime_type.has_value() && mime_type->essence() == "text/css"sv;+}+void 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-type- if (name == HTML::AttributeNames::type) {- if (value.has_value())- m_mime_type = value->to_ascii_lowercase();- else {- m_mime_type = {};- }-- return;- }-// 4.6.7 Link types - https://html.spec.whatwg.org/multipage/links.html#linkTypesauto old_relationship = m_relationship;if (name == HTML::AttributeNames::rel) {@@ -353,8 +351,12 @@ void HTMLLinkElement::attribute_changed(Utf16FlyString const& name, Optional<Utf// - When the disabled attribute of the link element of an external resource link that is already browsing-context connected is set, changed, or removed.name == AttributeNames::disabled- // FIXME: - When the type attribute of the link element of an external resource link that is already browsing-context connected is set or changed to a value that does not or no longer matches the Content-Type metadata of the previous obtained external resource, if any.- // FIXME: - When the type attribute of the link element of an external resource link that is already browsing-context connected, but was previously not obtained due to the type attribute specifying an unsupported type, is removed or changed.+ // - When the type attribute of the link element of an external resource link that is already browsing-context connected is set or changed to a value that does not or no longer matches the Content-Type metadata of the previous obtained external resource, if any.+ // - When the type attribute of the link element of an external resource link that is already browsing-context connected, but was previously not obtained due to the type attribute specifying an unsupported type, is removed or changed.+ // NB: Only the Content-Type metadata decides whether the resource applies, so a change between two+ // supported types needs no new fetch.+ || (!namespace_.has_value() && name == AttributeNames::type && old_value != value+ && !(is_supported_style_sheet_type_attribute(old_value) && is_supported_style_sheet_type_attribute(value)))// AD-HOC: When the link element's type becomes a stylesheet|| !(old_relationship & Relationship::Stylesheet);@@ -907,6 +909,19 @@ bool HTMLLinkElement::icon_linked_resource_fetch_setup_steps(Fetch::Infrastructu// https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet:linked-resource-fetch-setup-stepsbool HTMLLinkElement::stylesheet_linked_resource_fetch_setup_steps(Fetch::Infrastructure::Request& request){+ // https://html.spec.whatwg.org/multipage/semantics.html#processing-the-type-attribute+ // If the type attribute is present, then the user agent must assume that the resource is of the given type (even+ // if that is not a valid MIME type string, e.g. the empty string). [...] If the UA does not support the given MIME+ // type for the given link relationship, then the UA should not fetch and process the linked resource+ if (!is_supported_style_sheet_type_attribute(get_attribute_ns({}, AttributeNames::type))) {+ // AD-HOC: The style sheet of an earlier fetch no longer applies.+ if (m_loaded_style_sheet) {+ document_or_shadow_root_style_scope().remove_a_css_style_sheet(*m_loaded_style_sheet);+ m_loaded_style_sheet = nullptr;+ }+ return false;+ }+// 1. If el's disabled attribute is set, then return false.if (has_attribute(AttributeNames::disabled))return false;@@ -1473,12 +1488,9 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastruauto fetch_generation = m_current_fetch_generation;// 1. If the resource's Content-Type metadata is not text/css, then set success to false.- auto mime_type_string = m_mime_type;Optional<StringView> mime_type_charset;auto extracted_mime_type = Fetch::Infrastructure::extract_mime_type(response.header_list());if (extracted_mime_type.has_value()) {- if (!mime_type_string.has_value())- mime_type_string = Utf16String::from_ascii_without_validation(extracted_mime_type->essence().bytes());if (auto charset = extracted_mime_type->parameters().get("charset"sv); charset.has_value())mime_type_charset = charset->bytes_as_string_view();}@@ -1489,7 +1501,8 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastruauto resource_url = response.url();auto assume_css_in_quirks_mode = document().in_quirks_mode() && resource_url.has_value()&& document().origin().is_same_origin(resource_url->origin());- if (mime_type_string.has_value() && mime_type_string != u"text/css"sv && !assume_css_in_quirks_mode)+ // AD-HOC: A response without a valid Content-Type is used as CSS, matching other engines.+ if (extracted_mime_type.has_value() && extracted_mime_type->essence() != "text/css"sv && !assume_css_in_quirks_mode)success = false;// 2. If el no longer creates an external resource link that contributes to the styling processing model, ordiff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.h b/Libraries/LibWeb/HTML/HTMLLinkElement.hindex 8a193b77874..6418bfa10d2 100644--- a/Libraries/LibWeb/HTML/HTMLLinkElement.h+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.h@@ -245,8 +245,6 @@ private:bool m_was_enabled_when_created_by_parser { false };- Optional<Utf16String> m_mime_type;-SourceSet m_source_set;GC::Weak<DOM::Document> m_parser_document;diff --git a/Tests/LibWeb/Text/expected/HTML/link-stylesheet-namespaced-type-attribute.txt b/Tests/LibWeb/Text/expected/HTML/link-stylesheet-namespaced-type-attribute.txtnew file mode 100644index 00000000000..adcefe970d0--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/link-stylesheet-namespaced-type-attribute.txt@@ -0,0 +1,2 @@+Namespaced type attributes: same sheet true, loads 0, rules 2+New href: load, color rgb(0, 0, 128)diff --git a/Tests/LibWeb/Text/expected/HTML/link-stylesheet-type-change-fetches-again.txt b/Tests/LibWeb/Text/expected/HTML/link-stylesheet-type-change-fetches-again.txtnew file mode 100644index 00000000000..fdf4424be26--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/link-stylesheet-type-change-fetches-again.txt@@ -0,0 +1,3 @@+Inserted: load, sheet true, color rgb(0, 128, 0)+type="text/plain": sheet false, color rgb(0, 0, 0)+type removed: load, sheet true, color rgb(0, 128, 0)diff --git a/Tests/LibWeb/Text/expected/HTML/link-stylesheet-type-does-not-override-content-type.txt b/Tests/LibWeb/Text/expected/HTML/link-stylesheet-type-does-not-override-content-type.txtnew file mode 100644index 00000000000..fc632c85f86--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/link-stylesheet-type-does-not-override-content-type.txt@@ -0,0 +1,3 @@+type="text/css", Content-Type: text/plain -> error, applied nothing+type="text/css", Content-Type: text/css -> load, applied "text/css"+type="text/css; charset=utf-8", Content-Type: text/css -> load, applied "text/css"diff --git a/Tests/LibWeb/Text/expected/XHTML/link-stylesheet-unsupported-type-after-href.txt b/Tests/LibWeb/Text/expected/XHTML/link-stylesheet-unsupported-type-after-href.txtnew file mode 100644index 00000000000..8d688733fd3--- /dev/null+++ b/Tests/LibWeb/Text/expected/XHTML/link-stylesheet-unsupported-type-after-href.txt@@ -0,0 +1,2 @@+type-after-href: rgb(0, 0, 0)+type-before-href: rgb(0, 0, 0)diff --git a/Tests/LibWeb/Text/input/HTML/link-stylesheet-namespaced-type-attribute.html b/Tests/LibWeb/Text/input/HTML/link-stylesheet-namespaced-type-attribute.htmlnew file mode 100644index 00000000000..27ea36cf46b--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/link-stylesheet-namespaced-type-attribute.html@@ -0,0 +1,43 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<div id="probe"></div>+<script>+ function loadStyleSheet(link) {+ return new Promise(resolve => {+ link.onload = () => resolve("load");+ link.onerror = () => resolve("error");+ });+ }++ function probeColor() {+ return getComputedStyle(document.getElementById("probe")).color;+ }++ promiseTest(async () => {+ let loads = 0;+ const link = document.createElement("link");+ link.rel = "stylesheet";+ link.href = "data:text/css,%23probe { color: rgb(0, 128, 0); }";+ let event = loadStyleSheet(link);+ document.head.appendChild(link);+ await event;+ link.addEventListener("load", () => ++loads);+ const sheet = link.sheet;+ sheet.insertRule("#probe { background-color: rgb(0, 0, 255); }", 1);++ link.setAttributeNS("urn:test", "test:type", "text/plain");+ link.setAttributeNS("urn:other", "type", "text/plain");++ const sentinel = document.createElement("link");+ sentinel.rel = "stylesheet";+ sentinel.href = "data:text/css,";+ event = loadStyleSheet(sentinel);+ document.head.appendChild(sentinel);+ await event;+ println(`Namespaced type attributes: same sheet ${link.sheet === sheet}, loads ${loads}, rules ${link.sheet?.cssRules.length}`);++ event = loadStyleSheet(link);+ link.href = "data:text/css,%23probe { color: rgb(0, 0, 128); }";+ println(`New href: ${await event}, color ${probeColor()}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/HTML/link-stylesheet-type-change-fetches-again.html b/Tests/LibWeb/Text/input/HTML/link-stylesheet-type-change-fetches-again.htmlnew file mode 100644index 00000000000..188e7ced0dd--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/link-stylesheet-type-change-fetches-again.html@@ -0,0 +1,31 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<div id="probe"></div>+<script>+ function nextLoadOrError(link) {+ return new Promise(resolve => {+ link.onload = () => resolve("load");+ link.onerror = () => resolve("error");+ });+ }++ function probeColor() {+ return getComputedStyle(document.getElementById("probe")).color;+ }++ promiseTest(async () => {+ const link = document.createElement("link");+ link.rel = "stylesheet";+ link.href = "data:text/css,%23probe { color: rgb(0, 128, 0); }";+ let event = nextLoadOrError(link);+ document.head.appendChild(link);+ println(`Inserted: ${await event}, sheet ${link.sheet !== null}, color ${probeColor()}`);++ link.type = "text/plain";+ println(`type="text/plain": sheet ${link.sheet !== null}, color ${probeColor()}`);++ event = nextLoadOrError(link);+ link.removeAttribute("type");+ println(`type removed: ${await event}, sheet ${link.sheet !== null}, color ${probeColor()}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/HTML/link-stylesheet-type-does-not-override-content-type.html b/Tests/LibWeb/Text/input/HTML/link-stylesheet-type-does-not-override-content-type.htmlnew file mode 100644index 00000000000..76c63884eee--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/link-stylesheet-type-does-not-override-content-type.html@@ -0,0 +1,37 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<div id="probe"></div>+<script>+ function loadStyleSheet(url, type) {+ return new Promise(resolve => {+ const link = document.createElement("link");+ link.rel = "stylesheet";+ link.type = type;+ link.href = url;+ link.onload = () => resolve({ link, event: "load" });+ link.onerror = () => resolve({ link, event: "error" });+ document.head.appendChild(link);+ });+ }++ promiseTest(async () => {+ const server = httpTestServer();+ const responses = [+ { contentType: "text/plain", type: "text/css" },+ { contentType: "text/css", type: "text/css" },+ { contentType: "text/css", type: "text/css; charset=utf-8" },+ ];+ const probe = document.getElementById("probe");+ for (const [index, { contentType, type }] of responses.entries()) {+ const url = await server.createEcho("GET", `/link-stylesheet-type-does-not-override-content-type-${index}`, {+ status: 200,+ headers: { "Content-Type": contentType },+ body: `#probe { --applied: "${contentType}"; }`,+ });+ const { link, event } = await loadStyleSheet(url, type);+ const applied = getComputedStyle(probe).getPropertyValue("--applied") || "nothing";+ println(`type="${type}", Content-Type: ${contentType} -> ${event}, applied ${applied}`);+ link.remove();+ }+ });+</script>diff --git a/Tests/LibWeb/Text/input/XHTML/link-stylesheet-unsupported-type-after-href.xhtml b/Tests/LibWeb/Text/input/XHTML/link-stylesheet-unsupported-type-after-href.xhtmlnew file mode 100644index 00000000000..dcf440f7e8a--- /dev/null+++ b/Tests/LibWeb/Text/input/XHTML/link-stylesheet-unsupported-type-after-href.xhtml@@ -0,0 +1,20 @@+<html xmlns="http://www.w3.org/1999/xhtml">+<head>+<link rel="stylesheet" href="data:text/css,%23type-after-href%20%7B%20color%3A%20rgb(255%2C%200%2C%200)%3B%20%7D" type="text/plain"/>+<link rel="stylesheet" type="text/plain" href="data:text/css,%23type-before-href%20%7B%20color%3A%20rgb(255%2C%200%2C%200)%3B%20%7D"/>+</head>+<body>+<div id="type-after-href"></div>+<div id="type-before-href"></div>+<script src="../include.js"></script>+<script>+ asyncTest(done => {+ window.addEventListener("load", () => {+ for (const id of ["type-after-href", "type-before-href"])+ println(`${id}: ${getComputedStyle(document.getElementById(id)).color}`);+ done();+ });+ });+</script>+</body>+</html>