Skip to content

Truly independent web browser

LibWeb: Compute absolute URLs for url() style values

Andreas Klingbe55c486e6b2
diff --git a/Libraries/LibWeb/Rust/src/css/absolutize.rs b/Libraries/LibWeb/Rust/src/css/absolutize.rs
index 5d0786050f8..75a4269dc82 100644
--- a/Libraries/LibWeb/Rust/src/css/absolutize.rs
+++ b/Libraries/LibWeb/Rust/src/css/absolutize.rs
@@ -2071,6 +2071,43 @@ pub(crate) fn absolutize(value: &StyleValueData, context: &AbsolutizationContext
}
})))
}
+ StyleValueData::Url {
+ url,
+ url_type,
+ modifiers,
+ } => {
+ // https://drafts.csswg.org/css-values-4/#local-urls
+ // When serializing a url() with the local url flag set, it must serialize as just the fragment.
+ if url.is_empty() || url.is_fragment() {
+ return Some(Absolutized::Unchanged);
+ }
+
+ // https://drafts.csswg.org/css-values-4/#relative-urls
+ // When a <url> appears in the computed value of a property, it is resolved to an absolute URL.
+ // The computed value of a URL that the UA cannot resolve to an absolute URL is the specified value.
+ let resolved_url = liburl_rust::basic_parse(url.url_input(), liburl_rust::BasicParseOptions::new())
+ .or_else(|| {
+ let base_url = context
+ .style_sheet_resource_context
+ .map(|resource_context| resource_context.base_url)
+ .filter(|base_url| !base_url.is_empty())
+ .unwrap_or(context.document_base_url);
+ let base_url = std::str::from_utf8(base_url).ok()?;
+ let base_url = liburl_rust::basic_parse(base_url, liburl_rust::BasicParseOptions::new())?;
+ liburl_rust::basic_parse(
+ url.url_input(),
+ liburl_rust::BasicParseOptions::new().base_url(&base_url),
+ )
+ });
+ let Some(resolved_url) = resolved_url else {
+ return Some(Absolutized::Unchanged);
+ };
+ Some(Absolutized::Changed(retain_new(StyleValueData::Url {
+ url: crate::css::style_value::RetainedString::from_ascii(resolved_url.into()),
+ url_type: *url_type,
+ modifiers: modifiers.clone(),
+ })))
+ }
StyleValueData::Image { .. } => absolutize_image(value, context),
// https://drafts.csswg.org/css-values-5/#random-caching
// If the random function's <random-value-sharing> is fixed <number>, the random base value is that number.
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/winner_store.rs b/Libraries/LibWeb/Rust/src/css/style/publication/winner_store.rs
index 657dd0a5a07..66b95ae580e 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication/winner_store.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication/winner_store.rs
@@ -503,6 +503,71 @@ mod tests {
assert!(!length_store(1.0, "cqw").environment_values_equal(&length_store(1.0, "cqw"), &engine.retained));
}
+ #[test]
+ fn environment_value_comparison_only_reuses_urls_without_resource_dependencies() {
+ use crate::css::style_value::{ImageResourceContext, RetainedRequestUrlModifierList, RetainedString};
+
+ let store = |property, value| {
+ WinnerStore::new(vec![WinnerDeclaration::new(
+ property,
+ false,
+ WinnerValue::Substituted {
+ value: RetainedStyleValueData::from_owned(value),
+ source: WinnerSource::Element(ElementDeclarationKind::InlineStyle),
+ },
+ )])
+ };
+ let engine = StyleEngine::new();
+ for (url, can_reuse) in [
+ ("https://example.com/paint.svg#fill", true),
+ ("data:image/svg+xml,paint", true),
+ ("paint.svg#fill", false),
+ ("/paint.svg#fill", false),
+ ("//example.com/paint.svg#fill", false),
+ ] {
+ let url_value = || StyleValueData::Url {
+ url: RetainedString::from_ascii(url.to_owned()),
+ url_type: 0,
+ modifiers: RetainedRequestUrlModifierList::from_retained_modifiers(Vec::new()),
+ };
+ let first = store(property_id::FILL, url_value());
+ let equal = store(property_id::FILL, url_value());
+ assert_eq!(
+ first.environment_values_equal(&equal, &engine.retained),
+ can_reuse,
+ "{url}"
+ );
+ assert_eq!(
+ first.reads_no_resource_contexts(&engine.retained).is_some(),
+ can_reuse,
+ "{url}"
+ );
+ }
+
+ let image_value = |base_url: &str, origin_clean| StyleValueData::Image {
+ url: RetainedString::from_ascii("https://example.com/image.png".to_owned()),
+ url_type: 0,
+ url_modifiers: RetainedRequestUrlModifierList::from_retained_modifiers(Vec::new()),
+ resource_context: ImageResourceContext {
+ base_url: RetainedString::from_ascii(base_url.to_owned()),
+ has_base_url: true,
+ has_parent_style_sheet_origin_clean: true,
+ parent_style_sheet_origin_clean: origin_clean,
+ should_absolutize_url_for_computed_value: true,
+ },
+ };
+ let first = store(
+ property_id::BACKGROUND_IMAGE,
+ image_value("https://example.com/first.css", true),
+ );
+ let equal = store(
+ property_id::BACKGROUND_IMAGE,
+ image_value("https://example.com/second.css", false),
+ );
+ assert!(!first.environment_values_equal(&equal, &engine.retained));
+ assert!(first.reads_no_resource_contexts(&engine.retained).is_none());
+ }
+
#[test]
fn winner_view_borrows_written_spelling_and_preserves_cascade_order() {
let mut engine = StyleEngine::new();
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index e7244075836..22745997bc5 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -1560,6 +1560,14 @@ pub(crate) fn collect_external_value_dependencies(value: &StyleValueData) -> Ext
dependencies.needs_document_base_url |= !url.is_empty() && !resource_context.has_base_url;
dependencies.may_need_style_sheet_resource_context |= !url.is_empty();
}
+ StyleValueData::Url { url, .. } => {
+ if !url.is_empty() && !url.is_fragment() {
+ let needs_base_url =
+ liburl_rust::basic_parse(url.url_input(), liburl_rust::BasicParseOptions::new()).is_none();
+ dependencies.needs_document_base_url |= needs_base_url;
+ dependencies.may_need_style_sheet_resource_context |= needs_base_url;
+ }
+ }
StyleValueData::Calculated { rust_calculation, .. } => {
collect_calculation(rust_calculation.node(), dependencies);
}
@@ -3061,8 +3069,8 @@ pub(crate) fn value_absolutization_is_identity(value: &StyleValueData) -> bool {
| StyleValueData::CustomIdent { .. }
| StyleValueData::Percentage { .. }
| StyleValueData::Flex { .. }
- | StyleValueData::UnicodeRange { .. }
- | StyleValueData::Url { .. } => true,
+ | StyleValueData::UnicodeRange { .. } => true,
+ StyleValueData::Url { url, .. } => url.is_empty() || url.is_fragment(),
_ => false,
}
}
@@ -8338,6 +8346,31 @@ pub(crate) mod ffi_test_stubs {
mod tests {
use super::*;
+ #[test]
+ fn url_dependencies_only_read_a_base_for_relative_urls() {
+ for (url, needs_base) in [
+ ("https://example.com/paint.svg#fill", false),
+ ("data:image/svg+xml,paint", false),
+ ("https://example.com/色.svg#fill", false),
+ ("paint.svg#fill", true),
+ ("色.svg#fill", true),
+ ("/paint.svg#fill", true),
+ ("//example.com/paint.svg#fill", true),
+ ("#fill", false),
+ ("", false),
+ ] {
+ let value = StyleValueData::Url {
+ url: crate::css::style_value::RetainedString::from_utf16(&url.encode_utf16().collect::<Vec<_>>())
+ .unwrap(),
+ url_type: 0,
+ modifiers: crate::css::style_value::RetainedRequestUrlModifierList::from_retained_modifiers(Vec::new()),
+ };
+ let dependencies = collect_external_value_dependencies(&value);
+ assert_eq!(dependencies.needs_document_base_url, needs_base, "{url}");
+ assert_eq!(dependencies.may_need_style_sheet_resource_context, needs_base, "{url}");
+ }
+ }
+
#[test]
fn calculated_random_values_record_their_document_dependency() {
let number = || {
diff --git a/Tests/LibWeb/Text/expected/svg-paint-computed-urls.txt b/Tests/LibWeb/Text/expected/svg-paint-computed-urls.txt
index 6b05743edc6..e926df05f2d 100644
--- a/Tests/LibWeb/Text/expected/svg-paint-computed-urls.txt
+++ b/Tests/LibWeb/Text/expected/svg-paint-computed-urls.txt
@@ -1,16 +1,16 @@
-external fill: false
-external stroke: false
-fill "paint.svg#gradient": false
-fill inherited "paint.svg#gradient": false
-fill fallback "paint.svg#gradient": false
+external fill: true
+external stroke: true
+fill "paint.svg#gradient": true
+fill inherited "paint.svg#gradient": true
+fill fallback "paint.svg#gradient": true
fill specified "paint.svg#gradient": true
-fill "../paint.svg#gradient": false
-fill inherited "../paint.svg#gradient": false
-fill fallback "../paint.svg#gradient": false
+fill "../paint.svg#gradient": true
+fill inherited "../paint.svg#gradient": true
+fill fallback "../paint.svg#gradient": true
fill specified "../paint.svg#gradient": true
-fill "é.svg#gradient": false
-fill inherited "é.svg#gradient": false
-fill fallback "é.svg#gradient": false
+fill "é.svg#gradient": true
+fill inherited "é.svg#gradient": true
+fill fallback "é.svg#gradient": true
fill specified "é.svg#gradient": true
fill "#gradient": true
fill inherited "#gradient": true
@@ -24,17 +24,17 @@ fill "https://example.test/paint.svg#gradient": true
fill inherited "https://example.test/paint.svg#gradient": true
fill fallback "https://example.test/paint.svg#gradient": true
fill specified "https://example.test/paint.svg#gradient": true
-stroke "paint.svg#gradient": false
-stroke inherited "paint.svg#gradient": false
-stroke fallback "paint.svg#gradient": false
+stroke "paint.svg#gradient": true
+stroke inherited "paint.svg#gradient": true
+stroke fallback "paint.svg#gradient": true
stroke specified "paint.svg#gradient": true
-stroke "../paint.svg#gradient": false
-stroke inherited "../paint.svg#gradient": false
-stroke fallback "../paint.svg#gradient": false
+stroke "../paint.svg#gradient": true
+stroke inherited "../paint.svg#gradient": true
+stroke fallback "../paint.svg#gradient": true
stroke specified "../paint.svg#gradient": true
-stroke "é.svg#gradient": false
-stroke inherited "é.svg#gradient": false
-stroke fallback "é.svg#gradient": false
+stroke "é.svg#gradient": true
+stroke inherited "é.svg#gradient": true
+stroke fallback "é.svg#gradient": true
stroke specified "é.svg#gradient": true
stroke "#gradient": true
stroke inherited "#gradient": true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/painting/parsing/fill-computed.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/painting/parsing/fill-computed.txt
index f3fe7f6da82..003d65687bb 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/painting/parsing/fill-computed.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/painting/parsing/fill-computed.txt
@@ -2,11 +2,10 @@ Harness status: OK
Found 6 tests
-5 Pass
-1 Fail
+6 Pass
Pass Property fill value 'none'
Pass Property fill value 'rgb(12, 34, 56)'
Pass Property fill value 'url("https://example.com/")'
Pass Property fill value 'url("https://example.com/") none'
Pass Property fill value 'url("https://example.com/") rgb(12, 34, 56)'
-Fail url values are made absolute
\ No newline at end of file
+Pass url values are made absolute
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/painting/parsing/stroke-computed.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/painting/parsing/stroke-computed.txt
index 6994bf7d4f8..0458d22f28f 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/painting/parsing/stroke-computed.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/painting/parsing/stroke-computed.txt
@@ -2,11 +2,10 @@ Harness status: OK
Found 6 tests
-5 Pass
-1 Fail
+6 Pass
Pass Property stroke value 'none'
Pass Property stroke value 'rgb(12, 34, 56)'
Pass Property stroke value 'url("https://example.com/")'
Pass Property stroke value 'url("https://example.com/") none'
Pass Property stroke value 'url("https://example.com/") rgb(12, 34, 56)'
-Fail url values are made absolute
\ No newline at end of file
+Pass url values are made absolute
\ No newline at end of file