Skip to content

Commit

Permalink
Auto merge of #19093 - cbrewster:font_weight_cleanup, r=emilio
Browse files Browse the repository at this point in the history
style: Cleanup font-weight parsing.

Followup for #19086

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19093)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Nov 2, 2017
2 parents cc53189 + f378593 commit edb2db5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
12 changes: 1 addition & 11 deletions components/style/values/computed/font.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
49 changes: 28 additions & 21 deletions components/style/values/specified/font.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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!(),
}
}

Expand Down

0 comments on commit edb2db5

Please sign in to comment.