From 086c044dc34dfc0f74fbe41f4ecb402b2cd34884 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:33 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- servo/components/style/values/specified/calc.rs | 57 ++++++------------------- 1 file changed, 14 insertions(+), 43 deletions(-) (limited to 'servo/components/style/values/specified/calc.rs') diff --git a/servo/components/style/values/specified/calc.rs b/servo/components/style/values/specified/calc.rs index 2660864319..17f043ac58 100644 --- a/servo/components/style/values/specified/calc.rs +++ b/servo/components/style/values/specified/calc.rs @@ -6,7 +6,6 @@ //! //! [calc]: https://drafts.csswg.org/css-values/#calc-notation -use crate::color::parsing::{AngleOrNumber, NumberOrPercentage}; use crate::parser::ParserContext; use crate::values::generics::calc::{ self as generic, CalcNodeLeaf, CalcUnits, MinMaxOp, ModRemOp, PositivePercentageBasis, @@ -185,7 +184,11 @@ impl generic::CalcNodeLeaf for Leaf { let self_negative = self.is_negative(); if self_negative != other.is_negative() { - return Some(if self_negative { cmp::Ordering::Less } else { cmp::Ordering::Greater }); + return Some(if self_negative { + cmp::Ordering::Less + } else { + cmp::Ordering::Greater + }); } match (self, other) { @@ -482,7 +485,7 @@ impl CalcNode { /// Parse a top-level `calc` expression, with all nested sub-expressions. /// /// This is in charge of parsing, for example, `2 + 3 * 100%`. - fn parse<'i, 't>( + pub fn parse<'i, 't>( context: &ParserContext, input: &mut Parser<'i, 't>, function: MathFunction, @@ -524,13 +527,18 @@ impl CalcNode { } let value = Self::parse_argument(context, input, allowed_units)?; - input.expect_comma()?; - let step = Self::parse_argument(context, input, allowed_units)?; + + // defaults to the number 1 if not provided + // https://drafts.csswg.org/css-values-4/#funcdef-round + let step = input.try_parse(|input| { + input.expect_comma()?; + Self::parse_argument(context, input, allowed_units) + }); Ok(Self::Round { strategy: strategy.unwrap_or(RoundingStrategy::Nearest), value: Box::new(value), - step: Box::new(step), + step: Box::new(step.unwrap_or(Self::Leaf(Leaf::Number(1.0)))), }) }, MathFunction::Mod | MathFunction::Rem => { @@ -1046,41 +1054,4 @@ impl CalcNode { .to_resolution() .map_err(|()| input.new_custom_error(StyleParseErrorKind::UnspecifiedError)) } - - /// Convenience parsing function for `` or ``. - pub fn parse_number_or_percentage<'i, 't>( - context: &ParserContext, - input: &mut Parser<'i, 't>, - function: MathFunction, - ) -> Result> { - let node = Self::parse(context, input, function, CalcUnits::PERCENTAGE)?; - - if let Ok(value) = node.to_number() { - return Ok(NumberOrPercentage::Number { value }); - } - - match node.to_percentage() { - Ok(unit_value) => Ok(NumberOrPercentage::Percentage { unit_value }), - Err(()) => Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)), - } - } - - /// Convenience parsing function for `` or ``. - pub fn parse_angle_or_number<'i, 't>( - context: &ParserContext, - input: &mut Parser<'i, 't>, - function: MathFunction, - ) -> Result> { - let node = Self::parse(context, input, function, CalcUnits::ANGLE)?; - - if let Ok(angle) = node.to_angle() { - let degrees = angle.degrees(); - return Ok(AngleOrNumber::Angle { degrees }); - } - - match node.to_number() { - Ok(value) => Ok(AngleOrNumber::Number { value }), - Err(()) => Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)), - } - } } -- cgit v1.2.3