From f4cfdd704b6be50fd8395f284d0f44f8c9185b21 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 10 Apr 2025 10:26:22 +0100 Subject: [PATCH] LibWeb/CSS: Make fetch_a_style_resource() take URLs not strings The spec has this parameter as "a url or ``", so let's match that. --- Libraries/LibWeb/CSS/CSSImportRule.cpp | 2 +- Libraries/LibWeb/CSS/Fetch.cpp | 9 ++++++--- Libraries/LibWeb/CSS/Fetch.h | 9 ++++++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Libraries/LibWeb/CSS/CSSImportRule.cpp b/Libraries/LibWeb/CSS/CSSImportRule.cpp index 56dc2764712..13cdf57750f 100644 --- a/Libraries/LibWeb/CSS/CSSImportRule.cpp +++ b/Libraries/LibWeb/CSS/CSSImportRule.cpp @@ -116,7 +116,7 @@ void CSSImportRule::fetch() m_document_load_event_delayer.emplace(*m_document); // 4. Fetch a style resource from parsedUrl, with stylesheet parentStylesheet, destination "style", CORS mode "no-cors", and processResponse being the following steps given response response and byte stream, null or failure byteStream: - fetch_a_style_resource(parsed_url->to_string(), parent_style_sheet, Fetch::Infrastructure::Request::Destination::Style, CorsMode::NoCors, + fetch_a_style_resource(parsed_url.value(), parent_style_sheet, Fetch::Infrastructure::Request::Destination::Style, CorsMode::NoCors, [strong_this = GC::Ref { *this }, parent_style_sheet = GC::Ref { parent_style_sheet }, parsed_url = parsed_url.value()](auto response, auto maybe_byte_stream) { // AD-HOC: Stop delaying the load event. ScopeGuard guard = [strong_this] { diff --git a/Libraries/LibWeb/CSS/Fetch.cpp b/Libraries/LibWeb/CSS/Fetch.cpp index 66f81b176f1..b11db422bb1 100644 --- a/Libraries/LibWeb/CSS/Fetch.cpp +++ b/Libraries/LibWeb/CSS/Fetch.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024, Sam Atkins + * Copyright (c) 2024-2025, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -12,7 +12,7 @@ namespace Web::CSS { // https://drafts.csswg.org/css-values-4/#fetch-a-style-resource -void fetch_a_style_resource(String const& url_value, CSSStyleSheet const& sheet, Fetch::Infrastructure::Request::Destination destination, CorsMode cors_mode, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_response) +void fetch_a_style_resource(StyleResourceURL const& url_value, CSSStyleSheet const& sheet, Fetch::Infrastructure::Request::Destination destination, CorsMode cors_mode, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_response) { auto& vm = sheet.vm(); @@ -23,7 +23,10 @@ void fetch_a_style_resource(String const& url_value, CSSStyleSheet const& sheet, auto base = sheet.base_url().value_or(environment_settings.api_base_url()); // 3. Let parsedUrl be the result of the URL parser steps with urlValue’s url and base. If the algorithm returns an error, return. - auto parsed_url = ::URL::Parser::basic_parse(url_value, base); + auto url_string = url_value.visit( + [](::URL::URL const& url) { return url.to_string(); }, + [](CSS::URL const& url) { return url.url(); }); + auto parsed_url = ::URL::Parser::basic_parse(url_string, base); if (!parsed_url.has_value()) return; diff --git a/Libraries/LibWeb/CSS/Fetch.h b/Libraries/LibWeb/CSS/Fetch.h index 4ea81c023c1..f39d5b2d8b5 100644 --- a/Libraries/LibWeb/CSS/Fetch.h +++ b/Libraries/LibWeb/CSS/Fetch.h @@ -1,12 +1,13 @@ /* - * Copyright (c) 2024, Sam Atkins + * Copyright (c) 2024-2025, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once -#include +#include +#include #include #include @@ -17,7 +18,9 @@ enum class CorsMode { Cors, }; +using StyleResourceURL = Variant<::URL::URL, CSS::URL>; + // https://drafts.csswg.org/css-values-4/#fetch-a-style-resource -void fetch_a_style_resource(String const& url, CSSStyleSheet const&, Fetch::Infrastructure::Request::Destination, CorsMode, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_response); +void fetch_a_style_resource(StyleResourceURL const& url, CSSStyleSheet const&, Fetch::Infrastructure::Request::Destination, CorsMode, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_response); }