Skip to content

Truly independent web browser

LibWeb: Resolve a calc() that divides by a compound unit

sideshowbarker8788984cf223
diff --git a/Libraries/LibWeb/Rust/src/css/calc.rs b/Libraries/LibWeb/Rust/src/css/calc.rs
index 1170a08d709..a3586e1e685 100644
--- a/Libraries/LibWeb/Rust/src/css/calc.rs
+++ b/Libraries/LibWeb/Rust/src/css/calc.rs
@@ -337,6 +337,7 @@ pub(crate) fn simplify_parsed_calculation(
percentage_basis: None,
length_resolution: LengthResolution::default(),
random_base_value: None,
+ evaluates_arithmetic: false,
};
let callbacks = CalcSimplifyCallbacks {
resolve_non_math_function: &|_| None,
@@ -1184,6 +1185,7 @@ fn simplify_combined_calculation(
percentage_basis: None,
length_resolution: LengthResolution::default(),
random_base_value: None,
+ evaluates_arithmetic: false,
};
let callbacks = CalcSimplifyCallbacks {
resolve_non_math_function: &|_| None,
@@ -1312,6 +1314,9 @@ pub(crate) struct CalcEvaluationContext<'a> {
/// Produces the random base value for a random() node's sharing options,
/// when the resolution context can (it needs per-element state).
pub random_base_value: Option<RandomBaseValueResolver<'a>>,
+ /// Whether a product that simplification can't reduce, but whose value is fully determined, becomes that value.
+ /// Set when computing or resolving a value; a specified value keeps the form its author wrote.
+ pub evaluates_arithmetic: bool,
}
/// Produces the random base value for a random() node's sharing options.
@@ -1369,6 +1374,53 @@ impl CalcNode {
})
}
+ /// The value of a tree that combines canonical numeric leaves with sums, negations, products and inversions only.
+ /// Simplification leaves some of those trees unreduced although their value is fully determined: It never
+ /// distributes an Invert node over a product, so 64px / (300px / 1s) stays a product holding an inverted product,
+ /// rather than becoming the time it is.
+ fn try_evaluate_arithmetic(&self, context: &CalcEvaluationContext) -> Option<CalcResult> {
+ match self {
+ CalcNode::Numeric(..) => self.try_canonical_result(context),
+ CalcNode::Negate(child) => {
+ let mut result = child.try_evaluate_arithmetic(context)?;
+ result.value = -result.value;
+ Some(result)
+ }
+ CalcNode::Invert(child) => {
+ let mut result = child.try_evaluate_arithmetic(context)?;
+ result.invert();
+ Some(result)
+ }
+ CalcNode::Product(children) => {
+ let mut result: Option<CalcResult> = None;
+ for child in children {
+ let child_result = child.try_evaluate_arithmetic(context)?;
+ match &mut result {
+ Some(result) => result.multiply_by(&child_result),
+ None => result = Some(child_result),
+ }
+ result.as_ref()?.numeric_type?;
+ }
+ result
+ }
+ CalcNode::Sum(children) => {
+ let mut result: Option<CalcResult> = None;
+ for child in children {
+ let child_result = child.try_evaluate_arithmetic(context)?;
+ result = Some(match result {
+ None => child_result,
+ Some(previous) => CalcResult {
+ value: previous.value + child_result.value,
+ numeric_type: Some(previous.numeric_type?.added_to(&child_result.numeric_type?)?),
+ },
+ });
+ }
+ result
+ }
+ _ => None,
+ }
+ }
+
/// Mirrors try_get_number: a plain number leaf.
fn try_number(&self) -> Option<f64> {
match self {
@@ -2383,6 +2435,7 @@ impl CalcNode {
..context.length_resolution
},
random_base_value: context.random_base_value,
+ evaluates_arithmetic: context.evaluates_arithmetic,
};
return shared(CalcNode::Sum(multiplied)).simplify(&neutral_context, callbacks);
}
@@ -2445,7 +2498,19 @@ impl CalcNode {
{
return shared(node);
}
- shared(CalcNode::Product(flattened))
+ let product = shared(CalcNode::Product(flattened));
+ // NB: The steps above never look into an Invert node holding a product, so 64px / (300px / 1s) stays a
+ // product here although its value is the time 0.213333s. Where the value is computed or resolved,
+ // take that value, so that the math functions around the product can fold it like any other leaf.
+ if context.evaluates_arithmetic
+ && !product.contains_percentage()
+ && let Some(node) = product
+ .try_evaluate_arithmetic(context)
+ .and_then(|result| make_calc_result_node(&result, context.resolve_as))
+ {
+ return shared(node);
+ }
+ product
}
_ => root,
}
@@ -2952,6 +3017,7 @@ fn with_ffi_evaluation<R>(
fallback: Some(&resolve_length_fallback),
},
random_base_value: Some(&random_base_value),
+ evaluates_arithmetic: true,
};
let absolutize_random_sharing = |sharing: &RetainedStyleValueData| -> Option<RetainedStyleValueData> {
external_resolutions
@@ -3114,6 +3180,7 @@ fn resolve_calculated_with_channels_options(
percentage_basis,
length_resolution,
random_base_value: Some(&fixed_random_base_value),
+ evaluates_arithmetic: true,
};
let callbacks = CalcSimplifyCallbacks {
absolutize_random_sharing: &|_| None,
diff --git a/Tests/LibWeb/Text/expected/css/calc-division-by-compound-unit.txt b/Tests/LibWeb/Text/expected/css/calc-division-by-compound-unit.txt
new file mode 100644
index 00000000000..e619126649a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/calc-division-by-compound-unit.txt
@@ -0,0 +1,30 @@
+transition-duration: calc(64px / (300px / 1s))
+ specified: calc(64px / (300px / 1s))
+ computed: 0.213333s
+animation-duration: calc(64px / (320px / 1s))
+ specified: calc(64px / (320px / 1s))
+ computed: 0.2s
+transition-delay: calc(1s + 64px / (320px / 1s))
+ specified: calc(1s + (64px / (320px / 1s)))
+ computed: 1.2s
+transition-duration: calc(-1 * (64px / (320px / 1s)))
+ specified: calc(-1 * 64px / (320px / 1s))
+ computed: 0s
+width: calc(64px / (320px / 1s) * 100px / 1s)
+ specified: calc(64px * 100px / (320px / 1s) / 1s)
+ computed: 20px
+width: calc(2rem / (16px / 10px))
+ specified: calc(0.625 * 2rem)
+ computed: 20px
+transition-duration: min(1s, calc(64px / (320px / 1s)))
+ specified: min(1s, 64px / (320px / 1s))
+ computed: 0.2s
+transition-delay: max(1s, 640px / (320px / 1s))
+ specified: max(1s, 640px / (320px / 1s))
+ computed: 2s
+animation-duration: clamp(0s, 64px / (320px / 1s), 1s)
+ specified: clamp(0s, 64px / (320px / 1s), 1s)
+ computed: 0.2s
+width: round(up, 64px / (320px / 1s) * 100px / 1s, 15px)
+ specified: round(up, 64px * 100px / (320px / 1s) / 1s, 15px)
+ computed: 30px
diff --git a/Tests/LibWeb/Text/input/css/calc-division-by-compound-unit.html b/Tests/LibWeb/Text/input/css/calc-division-by-compound-unit.html
new file mode 100644
index 00000000000..3eded86673b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/calc-division-by-compound-unit.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<div id="target"></div>
+<script>
+ test(() => {
+ const cases = [
+ ["transition-duration", "calc(64px / (300px / 1s))"],
+ ["animation-duration", "calc(64px / (320px / 1s))"],
+ ["transition-delay", "calc(1s + 64px / (320px / 1s))"],
+ ["transition-duration", "calc(-1 * (64px / (320px / 1s)))"],
+ ["width", "calc(64px / (320px / 1s) * 100px / 1s)"],
+ ["width", "calc(2rem / (16px / 10px))"],
+ ["transition-duration", "min(1s, calc(64px / (320px / 1s)))"],
+ ["transition-delay", "max(1s, 640px / (320px / 1s))"],
+ ["animation-duration", "clamp(0s, 64px / (320px / 1s), 1s)"],
+ ["width", "round(up, 64px / (320px / 1s) * 100px / 1s, 15px)"],
+ ];
+ for (const [property, value] of cases) {
+ target.style.setProperty(property, value);
+ println(`${property}: ${value}`);
+ println(` specified: ${target.style.getPropertyValue(property)}`);
+ println(` computed: ${getComputedStyle(target).getPropertyValue(property)}`);
+ target.style.removeProperty(property);
+ }
+ });
+</script>