Truly independent web browser
LibWeb: Absolutize the fallback values of anchor() functions
Andreas Klingde4e2ddb56d1
diff --git a/Libraries/LibWeb/Rust/src/css/absolutize.rs b/Libraries/LibWeb/Rust/src/css/absolutize.rsindex 365da17a06f..0d89570661f 100644--- a/Libraries/LibWeb/Rust/src/css/absolutize.rs+++ b/Libraries/LibWeb/Rust/src/css/absolutize.rs@@ -1922,6 +1922,24 @@ pub(crate) fn absolutize(value: &StyleValueData, context: &AbsolutizationContext}}StyleValueData::Edge { .. } => absolutize_edge(value, context),+ StyleValueData::Anchor { .. } => {+ let mut tracked = false;+ let mut calc_length_context = *context.length;+ calc_length_context.resolved_viewport_relative_length = &raw mut tracked;+ let outcome = crate::css::calc::absolutize_anchor_function(+ value,+ &calc_length_context,+ context.tree_counting,+ context.random_base_values,+ );+ if tracked {+ context.resolved_viewport_relative_length.set(true);+ }+ match outcome? {+ None => Some(Absolutized::Unchanged),+ Some(absolutized) => Some(Absolutized::Changed(retain_new(absolutized))),+ }+ }StyleValueData::Position { edge_x, edge_y } => {let mut changed = false;let edge_x = absolutize_child(edge_x, context, &mut changed)?;diff --git a/Libraries/LibWeb/Rust/src/css/calc.rs b/Libraries/LibWeb/Rust/src/css/calc.rsindex 83973dd50d8..9322d675772 100644--- a/Libraries/LibWeb/Rust/src/css/calc.rs+++ b/Libraries/LibWeb/Rust/src/css/calc.rs@@ -4186,6 +4186,63 @@ pub(crate) enum AbsolutizedCalculation {Value(crate::css::style_value::StyleValueData),}+/// Absolutizes the fallback value of an anchor() function, which keeps its anchor query+/// until style and layout interleaving resolves it. Returns the rebuilt function, none when+/// nothing changed, or None when the fallback value cannot absolutize.+pub(crate) fn absolutize_anchor_function(+ value: &crate::css::style_value::StyleValueData,+ length_context: &crate::css::style_compute::FfiLengthResolutionContext,+ tree_counting: Option<(u64, u64)>,+ random_base_values: &[crate::css::style_compute::FfiRandomBaseValue],+) -> Option<Option<crate::css::style_value::StyleValueData>> {+ use crate::css::style_value::StyleValueData;+ let StyleValueData::Anchor {+ has_anchor_name,+ anchor_name,+ anchor_side,+ fallback_value,+ } = value+ else {+ return None;+ };+ let absolutized_fallback = match fallback_value.optional_data() {+ None => return Some(None),+ Some(StyleValueData::Length { value, unit }) => {+ let result = crate::css::style_compute::absolutize_length_for_calc(*value, *unit as usize, length_context);+ if !result.handled {+ return None;+ }+ if !result.changed {+ return Some(None);+ }+ StyleValueData::Length {+ value: result.px,+ unit: crate::css::style_compute::px_length_unit(),+ }+ }+ Some(fallback @ StyleValueData::Calculated { .. }) => {+ match absolutize_calculation_value(fallback, length_context, tree_counting, random_base_values)? {+ AbsolutizedCalculation::Unchanged => return Some(None),+ AbsolutizedCalculation::Percentage(value) => StyleValueData::Percentage { value },+ AbsolutizedCalculation::Value(value) => value,+ }+ }+ Some(fallback @ StyleValueData::Anchor { .. }) => {+ match absolutize_anchor_function(fallback, length_context, tree_counting, random_base_values)? {+ Some(value) => value,+ None => return Some(None),+ }+ }+ Some(_) => return Some(None),+ };+ Some(Some(StyleValueData::Anchor {+ has_anchor_name: *has_anchor_name,+ anchor_name: anchor_name.clone(),+ anchor_side: anchor_side.clone_retained(),+ fallback_value: RetainedStyleValueData::from_arc(Arc::new(absolutized_fallback)),+ }))+}+/// Absolutizes a Calculated style value against a length resolution context, with no/// percentage basis. Tree-counting functions resolve from the supplied sibling facts, lengths/// resolve from the immutable context, and fixed random sharing resolves without external state.@@ -4275,6 +4332,16 @@ pub(crate) fn absolutize_calculation_value(if matches!(source, StyleValueData::Anchor { .. }) {// anchor() stays unresolved until style and layout interleaving supplies// an anchor resolver, matching the context-free computed-value pass.+ if let Some(absolutized) =+ absolutize_anchor_function(source, length_context, tree_counting, random_base_values)?+ {+ let mut length_type = CalcNumericType::default();+ length_type.exponents[BASE_TYPE_LENGTH] = Some(1);+ external.resolved_node = Some(Arc::new(CalcNode::NonMathFunction {+ value: RetainedStyleValueData::from_arc(Arc::new(absolutized)),+ numeric_type: length_type,+ }));+ }continue;}return None;diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rsindex b293ddddd17..8ab14e899aa 100644--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs@@ -1531,6 +1531,7 @@ pub(crate) fn collect_external_value_dependencies(value: &StyleValueData) -> Extcollect_values(&[numerator, denominator], dependencies);}StyleValueData::Edge { offset, .. } => collect_optional(offset, dependencies),+ StyleValueData::Anchor { fallback_value, .. } => collect_optional(fallback_value, dependencies),StyleValueData::Ray { angle, position, .. } => {collect_optional(angle, dependencies);collect_optional(position, dependencies);diff --git a/Tests/LibWeb/Text/expected/css/anchor-function-relative-length-fallback.txt b/Tests/LibWeb/Text/expected/css/anchor-function-relative-length-fallback.txtnew file mode 100644index 00000000000..c19e2842bf4--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/anchor-function-relative-length-fallback.txt@@ -0,0 +1,7 @@+left: anchor(--missing left, 2em): 20px+left: calc(anchor(--missing left, 2em) + 1px): 21px+left: anchor(--missing left, calc(1em + 10%)): 30px+left: anchor(--missing left, anchor(--also-missing left, 3em)): 30px+left: anchor(--missing left, 10cqw): 20px+left: calc(anchor(--missing left, 10cqw) + 1px): 21px+left: anchor(--missing left, calc(10cqh + 1em)): 30pxdiff --git a/Tests/LibWeb/Text/input/css/anchor-function-relative-length-fallback.html b/Tests/LibWeb/Text/input/css/anchor-function-relative-length-fallback.htmlnew file mode 100644index 00000000000..0ac08a02ae1--- /dev/null+++ b/Tests/LibWeb/Text/input/css/anchor-function-relative-length-fallback.html@@ -0,0 +1,31 @@+<!DOCTYPE html>+<style>+#cb {+ position: relative;+ width: 200px;+ height: 200px;+ font-size: 10px;+ container-type: size;+}+#target {+ position: absolute;+}+</style>+<div id="cb"><div id="target"></div></div>+<script src="../include.js"></script>+<script>+ test(() => {+ for (const style of [+ "left: anchor(--missing left, 2em)",+ "left: calc(anchor(--missing left, 2em) + 1px)",+ "left: anchor(--missing left, calc(1em + 10%))",+ "left: anchor(--missing left, anchor(--also-missing left, 3em))",+ "left: anchor(--missing left, 10cqw)",+ "left: calc(anchor(--missing left, 10cqw) + 1px)",+ "left: anchor(--missing left, calc(10cqh + 1em))",+ ]) {+ target.style = style;+ println(`${style}: ${getComputedStyle(target).left}`);+ }+ });+</script>