Truly independent web browser
LibWeb: Support unbounded sides in clamp()
Andreas Kling1aec8f4d7ddf
diff --git a/Libraries/LibWeb/Rust/src/css/calc.rs b/Libraries/LibWeb/Rust/src/css/calc.rsindex c54dbbc5880..f0eea86cc51 100644--- a/Libraries/LibWeb/Rust/src/css/calc.rs+++ b/Libraries/LibWeb/Rust/src/css/calc.rs@@ -593,9 +593,9 @@ pub enum CalcNode {Min(Vec<Arc<CalcNode>>),Max(Vec<Arc<CalcNode>>),Clamp {- min: Arc<CalcNode>,+ min: Option<Arc<CalcNode>>,center: Arc<CalcNode>,- max: Arc<CalcNode>,+ max: Option<Arc<CalcNode>>,},/// https://drafts.csswg.org/css-values-5/#progress-funcProgress {@@ -684,9 +684,13 @@ impl CalcNode {| CalcNode::Sqrt(child)| CalcNode::Exp(child) => f(child),CalcNode::Clamp { min, center, max } => {- f(min);+ if let Some(min) = min {+ f(min);+ }f(center);- f(max);+ if let Some(max) = max {+ f(max);+ }}CalcNode::Progress { progress, from, to, .. } => {f(progress);@@ -782,7 +786,13 @@ impl CalcNode {// NOTE: `- foo` doesn't change the type, and neither does abs().CalcNode::Negate(child) | CalcNode::Abs(child) => child.numeric_type(percentage_leaf_type),CalcNode::Invert(child) => Some(child.numeric_type(percentage_leaf_type)?.inverted()),- CalcNode::Clamp { min, center, max } => add_children(&[min, center, max]),+ CalcNode::Clamp { min, center, max } => {+ let mut numeric_type = center.numeric_type(percentage_leaf_type)?;+ for bound in min.iter().chain(max.iter()) {+ numeric_type = numeric_type.added_to(&bound.numeric_type(percentage_leaf_type)?)?;+ }+ Some(numeric_type)+ }CalcNode::Progress { progress, from, to, .. } => {let sum = add_children(&[progress, from, to])?;CalcNumericType::default().made_consistent_with(&sum)@@ -1051,9 +1061,13 @@ pub(crate) fn map_calc_size_leaves(CalcNode::Negate(child) => shared(CalcNode::Negate(map_calc_size_leaves(child, replace)?)),CalcNode::Invert(child) => shared(CalcNode::Invert(map_calc_size_leaves(child, replace)?)),CalcNode::Clamp { min, center, max } => shared(CalcNode::Clamp {- min: map_calc_size_leaves(min, replace)?,+ min: min+ .as_ref()+ .map_or(Some(None), |min| map_calc_size_leaves(min, replace).map(Some))?,center: map_calc_size_leaves(center, replace)?,- max: map_calc_size_leaves(max, replace)?,+ max: max+ .as_ref()+ .map_or(Some(None), |max| map_calc_size_leaves(max, replace).map(Some))?,}),_ => {let mut needs_replacement = false;@@ -1285,9 +1299,9 @@ pub extern "C" fn rust_calc_node_create_clamp(max: OwnedArc<CalcNode>,) -> *const CalcNode {handle(CalcNode::Clamp {- min: min.into_arc(),+ min: Some(min.into_arc()),center: center.into_arc(),- max: max.into_arc(),+ max: Some(max.into_arc()),})}@@ -1450,9 +1464,21 @@ impl CalcNode {CalcNode::Max(children) => fold_min_or_max(children, false),// https://drafts.csswg.org/css-values-4/#funcdef-clampCalcNode::Clamp { min, center, max } => {- let min_result = min.try_canonical_result(context)?;let center_result = center.try_canonical_result(context)?;- let max_result = max.try_canonical_result(context)?;+ let min_result = match min {+ Some(min) => min.try_canonical_result(context)?,+ None => CalcResult {+ value: f64::NEG_INFINITY,+ numeric_type: center_result.numeric_type,+ },+ };+ let max_result = match max {+ Some(max) => max.try_canonical_result(context)?,+ None => CalcResult {+ value: f64::INFINITY,+ numeric_type: center_result.numeric_type,+ },+ };// The first consistency must hold; the second stays part of the// result, exactly as in the C++ implementation.let first = min_result.numeric_type?.added_to(¢er_result.numeric_type?)?;@@ -2505,9 +2531,9 @@ impl CalcNode {CalcNode::Sqrt(child) => CalcNode::Sqrt(simplify_child(child, &mut changed)),CalcNode::Exp(child) => CalcNode::Exp(simplify_child(child, &mut changed)),CalcNode::Clamp { min, center, max } => CalcNode::Clamp {- min: simplify_child(min, &mut changed),+ min: min.as_ref().map(|min| simplify_child(min, &mut changed)),center: simplify_child(center, &mut changed),- max: simplify_child(max, &mut changed),+ max: max.as_ref().map(|max| simplify_child(max, &mut changed)),},CalcNode::Progress {no_clamp,@@ -3893,6 +3919,21 @@ impl CalcSerializer<'_> {return;}+ if let CalcNode::Clamp { min, center, max } = &**node {+ self.literal("clamp(");+ for (index, child) in [min.as_ref(), Some(center), max.as_ref()].into_iter().enumerate() {+ if index != 0 {+ self.literal(", ");+ }+ match child {+ Some(child) => self.serialize_tree(child, false),+ None => self.literal("none"),+ }+ }+ self.literal(")");+ return;+ }+// AD-HOC: A non-math function has no calc() wrapper; serialize it directly.if matches!(&**node, CalcNode::NonMathFunction { .. }) {self.serialize_tree(node, false);@@ -4763,9 +4804,12 @@ impl CalcNode {max: b_max,},) => {- a_min.structurally_equals(b_min)- && a_center.structurally_equals(b_center)- && a_max.structurally_equals(b_max)+ let bounds_equal = |a: &Option<Arc<CalcNode>>, b: &Option<Arc<CalcNode>>| match (a, b) {+ (Some(a), Some(b)) => a.structurally_equals(b),+ (None, None) => true,+ _ => false,+ };+ bounds_equal(a_min, b_min) && a_center.structurally_equals(b_center) && bounds_equal(a_max, b_max)}(CalcNode::Progress {@@ -4986,7 +5030,11 @@ fn append_reification_node(| CalcNode::Invert(..)| CalcNode::Min(..)| CalcNode::Max(..)- | CalcNode::Clamp { .. }+ | CalcNode::Clamp {+ min: Some(_),+ max: Some(_),+ ..+ }) {return None;}diff --git a/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rsindex de1398df027..7dd7262aed5 100644--- a/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs@@ -462,7 +462,42 @@ pub(crate) fn parse_a_calc_function_node(},})}- MathFunction::Clamp | MathFunction::Progress => {+ MathFunction::Clamp => {+ if arguments.len() != 3 {+ return Err(CalcParseError::Invalid);+ }+ // https://drafts.csswg.org/css-values-4/#funcdef-clamp+ // Either the min or max calculations (or even both) can instead be the keyword none, which indicates+ // the value is not clamped from that side.+ let parse_bound = |values: &[ComponentValue]| {+ let mut significant = values.iter().filter(|value| !value.is_whitespace());+ if significant+ .next()+ .and_then(ComponentValue::ident)+ .is_some_and(|ident| equals_ascii_case_insensitive(ident, b"none"))+ && significant.next().is_none()+ {+ Ok(None)+ } else {+ parse_argument(values, context).map(Some)+ }+ };+ let min = parse_bound(arguments[0])?;+ let center = parse_argument(arguments[1], context)?;+ let max = parse_bound(arguments[2])?;+ let types = min+ .iter()+ .chain(std::iter::once(¢er))+ .chain(max.iter())+ .map(|node| argument_type(node, percentages_resolve_as))+ .collect::<Result<Vec<_>>>()?;+ if !types.iter().all(accepts_dimension_or_percentage) {+ return Err(CalcParseError::Invalid);+ }+ require_consistent(&types)?;+ Arc::new(CalcNode::Clamp { min, center, max })+ }+ MathFunction::Progress => {let nodes = parse_fixed_arguments(&arguments, 3, context)?;let types = nodes.iter()@@ -472,19 +507,11 @@ pub(crate) fn parse_a_calc_function_node(return Err(CalcParseError::Invalid);}require_consistent(&types)?;- Arc::new(if function == MathFunction::Clamp {- CalcNode::Clamp {- min: nodes[0].clone(),- center: nodes[1].clone(),- max: nodes[2].clone(),- }- } else {- CalcNode::Progress {- no_clamp: progress_no_clamp,- progress: nodes[0].clone(),- from: nodes[1].clone(),- to: nodes[2].clone(),- }+ Arc::new(CalcNode::Progress {+ no_clamp: progress_no_clamp,+ progress: nodes[0].clone(),+ from: nodes[1].clone(),+ to: nodes[2].clone(),})}MathFunction::Log => {diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-color-computed.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-color-computed.txtindex 043dcd840f4..509cd96b1e0 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-color-computed.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-color-computed.txt@@ -2,20 +2,19 @@ Harness status: OKFound 15 tests-6 Pass-9 Fail+15 PassPass Property color value 'rgb(clamp(10, 20, 30) 0 0)'-Fail Property color value 'rgb(clamp(none, 30, 33) 0 0)'-Fail Property color value 'rgb(clamp(none, 33, 30) 0 0)'-Fail Property color value 'rgb(clamp(30, 33, none) 0 0)'-Fail Property color value 'rgb(clamp(33, 30, none) 0 0)'-Fail Property color value 'rgb(clamp(none, 30, none) 0 0)'+Pass Property color value 'rgb(clamp(none, 30, 33) 0 0)'+Pass Property color value 'rgb(clamp(none, 33, 30) 0 0)'+Pass Property color value 'rgb(clamp(30, 33, none) 0 0)'+Pass Property color value 'rgb(clamp(33, 30, none) 0 0)'+Pass Property color value 'rgb(clamp(none, 30, none) 0 0)'Pass Property color value 'rgb(from red clamp(r, 30, 300) g b)'Pass Property color value 'rgb(from red clamp(30, 300, r) g b)'Pass Property color value 'rgb(from red clamp(3, r, 300) g b)'Pass Property color value 'rgb(from red clamp(3, r, r + 30) g b)'Pass Property color value 'rgb(from red clamp(r - 30, r, 300) g b)'-Fail Property color value 'rgb(from red clamp(3, r, none) g b)'-Fail Property color value 'rgb(from red clamp(r, 30, none) g b)'-Fail Property color value 'rgb(from red clamp(none, r, 300) g b)'-Fail Property color value 'rgb(from red clamp(none, r, r + 30) g b)'\ No newline at end of file+Pass Property color value 'rgb(from red clamp(3, r, none) g b)'+Pass Property color value 'rgb(from red clamp(r, 30, none) g b)'+Pass Property color value 'rgb(from red clamp(none, r, 300) g b)'+Pass Property color value 'rgb(from red clamp(none, r, r + 30) g b)'\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-integer-computed.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-integer-computed.txtindex d63802d6f8d..f33cde24323 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-integer-computed.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-integer-computed.txt@@ -2,11 +2,10 @@ Harness status: OKFound 6 tests-1 Pass-5 Fail+6 PassPass Property z-index value 'clamp(10, 20, 30)'-Fail Property z-index value 'clamp(none, 30, 33)'-Fail Property z-index value 'clamp(none, 33, 30)'-Fail Property z-index value 'clamp(30, 33, none)'-Fail Property z-index value 'clamp(33, 30, none)'-Fail Property z-index value 'clamp(none, 30, none)'\ No newline at end of file+Pass Property z-index value 'clamp(none, 30, 33)'+Pass Property z-index value 'clamp(none, 33, 30)'+Pass Property z-index value 'clamp(30, 33, none)'+Pass Property z-index value 'clamp(33, 30, none)'+Pass Property z-index value 'clamp(none, 30, none)'\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-length-computed.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-length-computed.txtindex 956d3c41e88..5f7bdd0d2a2 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-length-computed.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-length-computed.txt@@ -2,8 +2,7 @@ Harness status: OKFound 24 tests-17 Pass-7 Fail+24 PassPass Property letter-spacing value 'clamp(10px, 20px, 30px)'Pass Property letter-spacing value 'clamp(10px, 5px, 30px)'Pass Property letter-spacing value 'clamp(10px, 35px, 30px)'@@ -21,10 +20,10 @@ Pass Property letter-spacing value 'calc(0px + clamp(10px, 20px, 30px))'Pass Property letter-spacing value 'calc(0px - clamp(10px, 20px, 30px))'Pass Property letter-spacing value 'calc(0px + clamp(30px, 100px, 20px))'Pass Property letter-spacing value 'calc(0px - clamp(30px, 100px, 20px))'-Fail Property letter-spacing value 'clamp(none, 30px, 33px)'-Fail Property letter-spacing value 'clamp(none, 33px, 30px)'-Fail Property letter-spacing value 'clamp(30px, 33px, none)'-Fail Property letter-spacing value 'clamp(33px, 30px, none)'-Fail Property letter-spacing value 'clamp(none, 30px, none)'-Fail Property letter-spacing value 'clamp(1000px, 1em / 1rem * 1px, none)'-Fail Property letter-spacing value 'clamp(1600px / 1em * 1px, 1em / 1rem * 1px, none)'\ No newline at end of file+Pass Property letter-spacing value 'clamp(none, 30px, 33px)'+Pass Property letter-spacing value 'clamp(none, 33px, 30px)'+Pass Property letter-spacing value 'clamp(30px, 33px, none)'+Pass Property letter-spacing value 'clamp(33px, 30px, none)'+Pass Property letter-spacing value 'clamp(none, 30px, none)'+Pass Property letter-spacing value 'clamp(1000px, 1em / 1rem * 1px, none)'+Pass Property letter-spacing value 'clamp(1600px / 1em * 1px, 1em / 1rem * 1px, none)'\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-length-serialize.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-length-serialize.txtindex e0bb4992a92..87a5d69a7ae 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-length-serialize.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-length-serialize.txt@@ -2,8 +2,7 @@ Harness status: OKFound 50 tests-44 Pass-6 Fail+50 PassPass e.style['margin-top'] = "clamp(1px, 2px, 3px)" should set the property valuePass e.style['margin-top'] = "calc(clamp(1px, 2px, 3px))" should set the property valuePass e.style['margin-top'] = "clamp(1px, 2px, clamp(2px, 3px, 4px))" should set the property value@@ -48,9 +47,9 @@ Pass e.style['margin-top'] = "calc(0px + clamp(1px, 1em, 1vh))" should set the pPass e.style['margin-top'] = "calc(calc(0px + clamp(1px, 1em, 1vh)))" should set the property valuePass e.style['margin-top'] = "calc(0px - clamp(1px, 1em, 1vh))" should set the property valuePass e.style['margin-top'] = "calc(calc(0px - clamp(1px, 1em, 1vh)))" should set the property value-Fail e.style['margin-top'] = "clamp(none, 33px, 30px)" should set the property value-Fail e.style['margin-top'] = "calc(clamp(none, 33px, 30px))" should set the property value-Fail e.style['margin-top'] = "clamp(33px, 30px, none)" should set the property value-Fail e.style['margin-top'] = "calc(clamp(33px, 30px, none))" should set the property value-Fail e.style['margin-top'] = "clamp(none, 30px, none)" should set the property value-Fail e.style['margin-top'] = "calc(clamp(none, 30px, none))" should set the property value\ No newline at end of file+Pass e.style['margin-top'] = "clamp(none, 33px, 30px)" should set the property value+Pass e.style['margin-top'] = "calc(clamp(none, 33px, 30px))" should set the property value+Pass e.style['margin-top'] = "clamp(33px, 30px, none)" should set the property value+Pass e.style['margin-top'] = "calc(clamp(33px, 30px, none))" should set the property value+Pass e.style['margin-top'] = "clamp(none, 30px, none)" should set the property value+Pass e.style['margin-top'] = "calc(clamp(none, 30px, none))" should set the property value\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-none-whitespace.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-none-whitespace.txtindex 5591530e5dc..1c6a5c3af3c 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-none-whitespace.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/clamp-none-whitespace.txt@@ -2,7 +2,7 @@ Harness status: OKFound 3 tests-3 Fail-Fail clamp() accepts whitespace between a 'none' min argument and the following comma-Fail clamp() accepts whitespace after a trailing 'none' max argument-Fail clamp() accepts whitespace around 'none' for both bounds\ No newline at end of file+3 Pass+Pass clamp() accepts whitespace between a 'none' min argument and the following comma+Pass clamp() accepts whitespace after a trailing 'none' max argument+Pass clamp() accepts whitespace around 'none' for both bounds\ No newline at end of file