LibWeb/CSS: Add alternative src() syntax for URLs

url() has some limitations because of allowing unquoted URLs as its
contents. For example, it can't use `var()`. To get around this, there's
an alternative `src()` function which behaves the same as `url()` except
that it is parsed as a regular function, which makes `var()` and friends
work properly.

There's no WPT test for this as far as I can tell, so I added our own.
This commit is contained in:
Sam Atkins 2025-06-09 17:20:04 +01:00 committed by Andreas Kling
parent ea0bfda1b9
commit 00f76ccbf4
Notes: github-actions[bot] 2025-06-11 14:27:18 +00:00
5 changed files with 63 additions and 8 deletions

View file

@ -2710,7 +2710,6 @@ Optional<URL> Parser::parse_url_function(TokenStream<ComponentValue>& tokens)
// <url> = <url()> | <src()>
// <url()> = url( <string> <url-modifier>* ) | <url-token>
// <src()> = src( <string> <url-modifier>* )
// FIXME: Also parse src() function
auto transaction = tokens.begin_transaction();
auto const& component_value = tokens.consume_a_token();
@ -2721,7 +2720,17 @@ Optional<URL> Parser::parse_url_function(TokenStream<ComponentValue>& tokens)
}
// <url()> = url( <string> <url-modifier>* )
if (component_value.is_function("url"sv)) {
// <src()> = src( <string> <url-modifier>* )
if (component_value.is_function()) {
URL::Type function_type;
if (component_value.is_function("url"sv)) {
function_type = URL::Type::Url;
} else if (component_value.is_function("src"sv)) {
function_type = URL::Type::Src;
} else {
return {};
}
auto const& function_values = component_value.function().value;
TokenStream url_tokens { function_values };
@ -2802,7 +2811,7 @@ Optional<URL> Parser::parse_url_function(TokenStream<ComponentValue>& tokens)
});
transaction.commit();
return URL { url_string.token().string().to_string(), move(request_url_modifiers) };
return URL { url_string.token().string().to_string(), function_type, move(request_url_modifiers) };
}
return {};