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

@ -69,7 +69,7 @@ impl Time {
/// Returns a `Time` value from a CSS `calc()` expression.
pub fn from_calc(seconds: CSSFloat) -> Self {
Time {
seconds: seconds,
seconds,
unit: TimeUnit::Second,
was_calc: true,
}
@ -95,11 +95,20 @@ impl Time {
Time::parse_dimension(value, unit, /* from_calc = */ false)
.map_err(|()| location.new_custom_error(StyleParseErrorKind::UnspecifiedError))
},
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
match input.parse_nested_block(|i| CalcNode::parse_time(context, i)) {
Ok(time) if clamping_mode.is_ok(ParsingMode::DEFAULT, time.seconds) => Ok(time),
_ => Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)),
Token::Function(ref name) => {
let function = CalcNode::math_function(name, location)?;
let time = CalcNode::parse_time(context, input, function)?;
// FIXME(emilio): Rejecting calc() at parse time is wrong,
// was_calc should probably be replaced by calc_clamping_mode or
// something like we do for numbers, or we should do the
// clamping here instead (simpler, but technically incorrect,
// though still more correct than this!).
if !clamping_mode.is_ok(ParsingMode::DEFAULT, time.seconds) {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}
Ok(time)
},
ref t => return Err(location.new_unexpected_token_error(t.clone())),
}