style: Centralize calc function parsing.

So that extending it to support other math functions like min / max / etc is
simpler.

There should be no behavior change with this patch, though I added a comment to
some places where we don't do calc() clamping correctly (though other browsers
don't either so...).

Differential Revision: https://phabricator.services.mozilla.com/D59939
This commit is contained in:
Emilio Cobos Álvarez 2020-01-15 00:46:01 +00:00
parent 9026720f04
commit d74f90e3a7
No known key found for this signature in database
GPG key ID: E1152D0994E4BF8A
7 changed files with 116 additions and 58 deletions

View file

@ -599,11 +599,11 @@ impl Length {
value,
))))
},
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => input
.parse_nested_block(|input| {
CalcNode::parse_length(context, input, num_context)
.map(|calc| Length::Calc(Box::new(calc)))
}),
Token::Function(ref name) => {
let function = CalcNode::math_function(name, location)?;
let calc = CalcNode::parse_length(context, input, num_context, function)?;
Ok(Length::Calc(Box::new(calc)))
},
ref token => return Err(location.new_unexpected_token_error(token.clone())),
}
}
@ -822,10 +822,9 @@ impl LengthPercentage {
return Ok(LengthPercentage::Length(NoCalcLength::from_px(value)));
}
},
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
let calc = input.parse_nested_block(|i| {
CalcNode::parse_length_or_percentage(context, i, num_context)
})?;
Token::Function(ref name) => {
let function = CalcNode::math_function(name, location)?;
let calc = CalcNode::parse_length_or_percentage(context, input, num_context, function)?;
Ok(LengthPercentage::Calc(Box::new(calc)))
},
_ => return Err(location.new_unexpected_token_error(token.clone())),