style: Update font-weight property and descriptor to css-fonts-4.

Bug: 1454596
Reviewed-by: xidorn
MozReview-Commit-ID: 27aS2UrgXjs
This commit is contained in:
Emilio Cobos Álvarez 2018-04-17 13:30:04 +02:00
parent 76a14d6a64
commit bc1126ee8c
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
10 changed files with 199 additions and 141 deletions

View file

@ -9,7 +9,7 @@
#![deny(missing_docs)]
#[cfg(feature = "gecko")]
use computed_values::{font_stretch, font_style, font_weight};
use computed_values::{font_stretch, font_style};
use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser};
use cssparser::{CowRcStr, SourceLocation};
#[cfg(feature = "gecko")]
@ -27,7 +27,7 @@ use style_traits::{StyleParseErrorKind, ToCss};
use style_traits::values::SequenceWriter;
use values::computed::font::FamilyName;
#[cfg(feature = "gecko")]
use values::specified::font::{SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings};
use values::specified::font::{AbsoluteFontWeight, SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings};
use values::specified::url::SpecifiedUrl;
/// A source for a font-face rule.
@ -92,38 +92,21 @@ pub enum FontDisplay {
Optional,
}
/// A font-weight value for a @font-face rule.
/// The font-weight CSS property specifies the weight or boldness of the font.
#[cfg(feature = "gecko")]
#[derive(Clone, Debug, Eq, PartialEq, ToCss)]
pub enum FontWeight {
/// Numeric font weights for fonts that provide more than just normal and bold.
Weight(font_weight::T),
/// Normal font weight. Same as 400.
Normal,
/// Bold font weight. Same as 700.
Bold,
}
/// The font-weight descriptor:
///
/// https://drafts.csswg.org/css-fonts-4/#descdef-font-face-font-weight
#[derive(Clone, Debug, PartialEq, ToCss)]
pub struct FontWeight(pub AbsoluteFontWeight, pub Option<AbsoluteFontWeight>);
#[cfg(feature = "gecko")]
impl Parse for FontWeight {
fn parse<'i, 't>(
_: &ParserContext,
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<FontWeight, ParseError<'i>> {
let result = input.try(|input| {
let ident = input.expect_ident().map_err(|_| ())?;
match_ignore_ascii_case! { &ident,
"normal" => Ok(FontWeight::Normal),
"bold" => Ok(FontWeight::Bold),
_ => Err(())
}
});
result.or_else(|_| {
font_weight::T::from_int(input.expect_integer()?)
.map(FontWeight::Weight)
.map_err(|()| input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
})
) -> Result<Self, ParseError<'i>> {
let first = AbsoluteFontWeight::parse(context, input)?;
let second =
input.try(|input| AbsoluteFontWeight::parse(context, input)).ok();
Ok(FontWeight(first, second))
}
}