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

@ -10,8 +10,9 @@
namespace Web::CSS {
URL::URL(String url, Vector<RequestURLModifier> request_url_modifiers)
: m_url(move(url))
URL::URL(String url, Type type, Vector<RequestURLModifier> request_url_modifiers)
: m_type(type)
, m_url(move(url))
, m_request_url_modifiers(move(request_url_modifiers))
{
}
@ -19,9 +20,18 @@ URL::URL(String url, Vector<RequestURLModifier> request_url_modifiers)
// https://drafts.csswg.org/cssom-1/#serialize-a-url
String URL::to_string() const
{
// To serialize a URL means to create a string represented by "url(", followed by the serialization of the URL as a string, followed by ")".
// To serialize a URL means to create a string represented by "url(", followed by the serialization of the URL as a
// string, followed by ")".
// AD-HOC: Serialize as src() if it was declared as that.
StringBuilder builder;
builder.append("url("sv);
switch (m_type) {
case Type::Url:
builder.append("url("sv);
break;
case Type::Src:
builder.append("src("sv);
break;
}
serialize_a_string(builder, m_url);
// AD-HOC: Serialize the RequestURLModifiers