Skip to content
Permalink
Browse files

style: Cleanup font-weight parsing.

  • Loading branch information
cbrewster committed Nov 1, 2017
1 parent 1e0fd7d commit f37859375e68fa0df8b1c55705ed1d6a8f6d19e8
Showing with 29 additions and 32 deletions.
  1. +1 −11 components/style/values/computed/font.rs
  2. +28 −21 components/style/values/specified/font.rs
@@ -5,10 +5,8 @@
//! Computed values for font properties

use app_units::Au;
use cssparser::Parser;
use parser::{Parse, ParserContext};
use std::fmt;
use style_traits::{ParseError, StyleParseErrorKind, ToCss};
use style_traits::ToCss;
use values::animated::ToAnimatedValue;
use values::computed::{Context, NonNegativeLength, ToComputedValue};
use values::specified::font as specified;
@@ -136,14 +134,6 @@ impl FontWeight {
}
}

impl Parse for FontWeight {
fn parse<'i, 't>(_: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<FontWeight, ParseError<'i>> {
FontWeight::from_int(input.expect_integer()?)
.map_err(|_| input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
}
}

impl FontSize {
/// The actual computed font size.
pub fn size(self) -> Au {
@@ -7,7 +7,7 @@
#[cfg(feature = "gecko")]
use Atom;
use app_units::Au;
use cssparser::Parser;
use cssparser::{Parser, Token};
use parser::{Parse, ParserContext};
use properties::longhands::system_font::SystemFont;
use std::fmt;
@@ -58,19 +58,24 @@ impl FontWeight {
}

impl Parse for FontWeight {
fn parse<'i, 't>(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),
"bolder" => Ok(FontWeight::Bolder),
"lighter" => Ok(FontWeight::Lighter),
_ => Err(())
fn parse<'i, 't>(_: &ParserContext, input: &mut Parser<'i, 't>) -> Result<FontWeight, ParseError<'i>> {
let result = match *input.next()? {
Token::Ident(ref ident) => {
match_ignore_ascii_case! { ident,
"normal" => Ok(FontWeight::Normal),
"bold" => Ok(FontWeight::Bold),
"bolder" => Ok(FontWeight::Bolder),
"lighter" => Ok(FontWeight::Lighter),
_ => Err(()),
}
}
});
result.or_else(|_| computed::FontWeight::parse(context, input).map(FontWeight::Weight))
Token::Number { int_value: Some(value), .. } => {
computed::FontWeight::from_int(value).map(FontWeight::Weight)
},
_ => Err(()),
};

result.map_err(|_| input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
}
}

@@ -83,16 +88,18 @@ impl ToComputedValue for FontWeight {
FontWeight::Weight(weight) => weight,
FontWeight::Normal => computed::FontWeight::normal(),
FontWeight::Bold => computed::FontWeight::bold(),
FontWeight::Bolder =>
context.builder.get_parent_font().clone_font_weight().bolder(),
FontWeight::Lighter =>
context.builder.get_parent_font().clone_font_weight().lighter(),
FontWeight::Bolder => {
context.builder.get_parent_font().clone_font_weight().bolder()
},
FontWeight::Lighter => {
context.builder.get_parent_font().clone_font_weight().lighter()
},
#[cfg(feature = "gecko")]
FontWeight::System(_) =>
context.cached_system_font.as_ref().unwrap().font_weight.clone(),
FontWeight::System(_) => {
context.cached_system_font.as_ref().unwrap().font_weight.clone()
},
#[cfg(not(feature = "gecko"))]
FontWeight::System(_) =>
unreachable!(),
FontWeight::System(_) => unreachable!(),
}
}

0 comments on commit f378593

Please sign in to comment.
You can’t perform that action at this time.