style: Add percentage for opacity (i.e. <alpha-value>).

The following properties accept <alpha-value> [1], which is
"<number> | <percentage>", so we update the parser, spec links, and
their web-platform-tests.
1. opacity
2. flood-opacity
3. fill-opacity
4. stroke-opacity
5. stop-opacity
6. -moz-window-opacity

Besides, shape-image-threshold [2] still only accepts <number>, so we need
to support a different version of `Opacity::parse()`.

[1] https://drafts.csswg.org/css-color/#typedef-alpha-value
[2] https://drafts.csswg.org/css-shapes/#shape-image-threshold-property

Differential Revision: https://phabricator.services.mozilla.com/D37493
This commit is contained in:
Boris Chiou 2019-07-12 19:49:32 +00:00 committed by Emilio Cobos Álvarez
parent 65bf72f131
commit 87231e676d
No known key found for this signature in database
GPG key ID: E1152D0994E4BF8A
5 changed files with 43 additions and 14 deletions

View file

@ -142,8 +142,8 @@ fn parse_number_with_clamping_mode<'i, 't>(
value: value.min(f32::MAX).max(f32::MIN),
calc_clamping_mode: None,
});
},
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {},
}
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {}
ref t => return Err(location.new_unexpected_token_error(t.clone())),
}
@ -402,14 +402,18 @@ impl Parse for NonNegativeNumberOrPercentage {
}
}
#[allow(missing_docs)]
/// The value of Opacity is <alpha-value>, which is "<number> | <percentage>".
/// However, we serialize the specified value as number, so it's ok to store
/// the Opacity as Number.
#[derive(
Clone, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, SpecifiedValueInfo, ToCss, ToShmem,
)]
pub struct Opacity(Number);
impl Parse for Opacity {
fn parse<'i, 't>(
impl Opacity {
/// Parse number value only.
#[inline]
pub fn parse_number<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
@ -417,6 +421,29 @@ impl Parse for Opacity {
}
}
impl Parse for Opacity {
/// Opacity accepts <number> | <percentage>, so we parse it as NumberOrPercentage,
/// and then convert into an Number if it's a Percentage.
/// https://drafts.csswg.org/cssom/#serializing-css-values
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let number = match NumberOrPercentage::parse(context, input)? {
NumberOrPercentage::Percentage(p) => Number {
value: p.get(),
calc_clamping_mode: if p.is_calc() {
Some(AllowedNumericType::All)
} else {
None
},
},
NumberOrPercentage::Number(n) => n,
};
Ok(Opacity(number))
}
}
impl ToComputedValue for Opacity {
type ComputedValue = CSSFloat;
@ -510,7 +537,7 @@ impl Parse for Integer {
Token::Number {
int_value: Some(v), ..
} => return Ok(Integer::new(v)),
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {},
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {}
ref t => return Err(location.new_unexpected_token_error(t.clone())),
}
@ -786,7 +813,7 @@ impl Attr {
None => {
return Err(location
.new_custom_error(StyleParseErrorKind::UnspecifiedError));
},
}
};
Some((prefix, ns))
} else {
@ -796,10 +823,10 @@ impl Attr {
namespace: prefix_and_ns,
attribute: Atom::from(second_token.as_ref()),
});
},
}
// In the case of attr(foobar ) we don't want to error out
// because of the trailing whitespace
Token::WhiteSpace(..) => {},
Token::WhiteSpace(..) => {}
ref t => return Err(input.new_unexpected_token_error(t.clone())),
}
}