Skip to content

Truly independent web browser

LibWeb: Resolve computed image URLs during CSSOM serialization

Andreas Kling8ffe7026e9ad
diff --git a/Libraries/LibWeb/Rust/src/css/serialize.rs b/Libraries/LibWeb/Rust/src/css/serialize.rs
index b7c52015cdc..7a7ce930f57 100644
--- a/Libraries/LibWeb/Rust/src/css/serialize.rs
+++ b/Libraries/LibWeb/Rust/src/css/serialize.rs
@@ -1124,10 +1124,27 @@ pub(crate) fn serialize_style_value(sink: &mut TextSink, value: &StyleValueData,
url,
url_type,
url_modifiers,
- ..
+ resource_context,
} => {
sink.push_ascii(if *url_type == 0 { "url(" } else { "src(" });
- serialize_url_text(sink, url);
+ // NB: Image values retain their base URL separately from their source text. CSSOM exposes the resolved URL.
+ let resolved_url = if mode.is_resolved()
+ && resource_context.has_base_url
+ && !url.is_empty()
+ && !url.is_fragment()
+ && liburl_rust::basic_parse(url.url_input(), liburl_rust::BasicParseOptions::new()).is_none()
+ {
+ std::str::from_utf8(resource_context.base_url.ascii_bytes())
+ .ok()
+ .and_then(|base| liburl_rust::basic_parse(base, liburl_rust::BasicParseOptions::new()))
+ .and_then(|base| {
+ liburl_rust::basic_parse(url.url_input(), liburl_rust::BasicParseOptions::new().base_url(&base))
+ })
+ .map(|url| RetainedString::from_ascii(url.into()))
+ } else {
+ None
+ };
+ serialize_url_text(sink, resolved_url.as_ref().unwrap_or(url));
for modifier in url_modifiers.as_slice() {
sink.push_ascii(" ");
serialize_request_url_modifier(sink, modifier);
diff --git a/Tests/LibWeb/Text/expected/css/computed-image-set-fragment-url.txt b/Tests/LibWeb/Text/expected/css/computed-image-set-fragment-url.txt
new file mode 100644
index 00000000000..8d1265c7fc2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/computed-image-set-fragment-url.txt
@@ -0,0 +1,2 @@
+#image: image-set(url("#image") 1dppx)
+image.svg#image: image-set(url("[absolute-image-url]") 1dppx)
diff --git a/Tests/LibWeb/Text/expected/css/inline-relative-css-image-base-url.txt b/Tests/LibWeb/Text/expected/css/inline-relative-css-image-base-url.txt
index 8d4adc8916d..46cfcffccf8 100644
--- a/Tests/LibWeb/Text/expected/css/inline-relative-css-image-base-url.txt
+++ b/Tests/LibWeb/Text/expected/css/inline-relative-css-image-base-url.txt
@@ -1,4 +1,4 @@
-Computed URL before base change: url("../wpt-import/images/anim-gr.gif")
-Computed URL after base change: url("../wpt-import/images/anim-gr.gif")
+Computed URL before base change: url("[image-url]")
+Computed URL after base change: url("[image-url]")
timer active before base change: true
timer active after base change: true
diff --git a/Tests/LibWeb/Text/expected/css/src-function.txt b/Tests/LibWeb/Text/expected/css/src-function.txt
index c1c4f9a6c86..fa94f8eef55 100644
--- a/Tests/LibWeb/Text/expected/css/src-function.txt
+++ b/Tests/LibWeb/Text/expected/css/src-function.txt
@@ -1,5 +1,5 @@
Before: none
-Using url('cool.png'): url("cool.png")
+Using url('cool.png'): url("[test-directory]/cool.png")
Using url(var(--some-url)): none
-Using src('cool.png'): src("cool.png")
-Using src(var(--some-url)): src("awesome.png")
+Using src('cool.png'): src("[test-directory]/cool.png")
+Using src(var(--some-url)): src("[test-directory]/awesome.png")
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/url-resolution.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/url-resolution.txt
index 54ec70e8379..c2ac6c5ffd4 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/url-resolution.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/url-resolution.txt
@@ -2,9 +2,10 @@ Harness status: OK
Found 18 tests
-18 Fail
-Fail Unregistered property resolves against document (URL token)
-Fail Unregistered property resolves against document (URL function)
+2 Pass
+16 Fail
+Pass Unregistered property resolves against document (URL token)
+Pass Unregistered property resolves against document (URL function)
Fail Registered non-inherited <url> resolves against sheet (URL token)
Fail Registered non-inherited <url> resolves against sheet (URL function)
Fail Registered inherited <url> resolves against sheet (URL token)
diff --git a/Tests/LibWeb/Text/input/css/computed-image-set-fragment-url.html b/Tests/LibWeb/Text/input/css/computed-image-set-fragment-url.html
new file mode 100644
index 00000000000..e4508a5b867
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/computed-image-set-fragment-url.html
@@ -0,0 +1,14 @@
+<!doctype html>
+<script src="../include.js"></script>
+<div id="target"></div>
+<script>
+ test(() => {
+ for (const url of ["#image", "image.svg#image"]) {
+ target.style.backgroundImage = `image-set("${url}" 1x)`;
+ const computed = getComputedStyle(target).backgroundImage;
+ println(
+ `${url}: ${computed.replace(new URL("image.svg#image", document.baseURI).href, "[absolute-image-url]")}`
+ );
+ }
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/inline-relative-css-image-base-url.html b/Tests/LibWeb/Text/input/css/inline-relative-css-image-base-url.html
index 71aa817b103..18b2a51784e 100644
--- a/Tests/LibWeb/Text/input/css/inline-relative-css-image-base-url.html
+++ b/Tests/LibWeb/Text/input/css/inline-relative-css-image-base-url.html
@@ -17,11 +17,11 @@
const timerActiveBeforeBaseChange =
internals.imageAnimationStateForURL(imageURL).timerActive;
- const beforeBaseChange = getComputedStyle(target).backgroundImage;
+ const beforeBaseChange = getComputedStyle(target).backgroundImage.replace(imageURL, "[image-url]");
const base = document.createElement("base");
base.href = "../wpt-import/";
document.head.append(base);
- const afterBaseChange = getComputedStyle(target).backgroundImage;
+ const afterBaseChange = getComputedStyle(target).backgroundImage.replace(imageURL, "[image-url]");
internals.dumpLayoutTree(document);
const timerActiveAfterBaseChange =
diff --git a/Tests/LibWeb/Text/input/css/src-function.html b/Tests/LibWeb/Text/input/css/src-function.html
index 6759c32060d..725b6055ff1 100644
--- a/Tests/LibWeb/Text/input/css/src-function.html
+++ b/Tests/LibWeb/Text/input/css/src-function.html
@@ -4,22 +4,24 @@
<script>
test(() => {
let target = document.getElementById("target");
- println(`Before: ${getComputedStyle(target).backgroundImage}`);
+ const directory = new URL(".", location.href).href;
+ const computedImage = () => getComputedStyle(target).backgroundImage.replace(directory, "[test-directory]/");
+ println(`Before: ${computedImage()}`);
target.style.backgroundImage = "url('cool.png')";
- println(`Using url('cool.png'): ${getComputedStyle(target).backgroundImage}`);
+ println(`Using url('cool.png'): ${computedImage()}`);
target.style.backgroundImage = "";
target.style.backgroundImage = "url(var(--some-url))";
- println(`Using url(var(--some-url)): ${getComputedStyle(target).backgroundImage}`);
+ println(`Using url(var(--some-url)): ${computedImage()}`);
target.style.backgroundImage = "";
target.style.backgroundImage = "src('cool.png')";
- println(`Using src('cool.png'): ${getComputedStyle(target).backgroundImage}`);
+ println(`Using src('cool.png'): ${computedImage()}`);
target.style.backgroundImage = "";
target.style.backgroundImage = "src(var(--some-url))";
- println(`Using src(var(--some-url)): ${getComputedStyle(target).backgroundImage}`);
+ println(`Using src(var(--some-url)): ${computedImage()}`);
});
</script>