diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index 435a4dafc6a..4f13603a0fb 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -411,15 +411,22 @@ ${helpers.single_keyword("font-variant-position", impl ToCss for FeatureTagValue { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - let s = format!("{} {}", self.tag, self.value); + let s = format!("\"{}\" {}", self.tag, self.value); dest.write_str(&s) } } impl FeatureTagValue { + /// [ on | off | ] pub fn parse(input: &mut Parser) -> Result { let tag = try!(input.expect_string()).into_owned(); + // allowed strings of length 4 containing chars: + if tag.len() != 4 || + tag.chars().any(|c| c < ' ' || c > '~') { + return Err(()) + } + if let Ok(value) = input.try(|input| input.expect_integer()) { // handle integer, throw if it is negative if value >= 0 { @@ -427,11 +434,14 @@ ${helpers.single_keyword("font-variant-position", } else { Err(()) } + } else if let Ok(_) = input.try(|input| input.expect_ident_matching("on")) { + // on is an alias for '1' + Ok(FeatureTagValue{ tag: tag, value: 1 }) } else if let Ok(_) = input.try(|input| input.expect_ident_matching("off")) { // off is an alias for '0' Ok(FeatureTagValue{ tag: tag, value: 0 }) } else { - // remainders (empty value and "on" keyword) are aliases for '1' + // empty value is an alias for '1' Ok(FeatureTagValue{ tag:tag, value: 1 }) } } @@ -443,13 +453,13 @@ ${helpers.single_keyword("font-variant-position", computed_value::T::Normal } + /// normal | # pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result { if input.try(|input| input.expect_ident_matching("normal")).is_ok() { Ok(computed_value::T::Normal) } else { - input.parse_comma_separated(|input| { - computed_value::FeatureTagValue::parse(input) - }).map(computed_value::T::Computed) + input.parse_comma_separated(computed_value::FeatureTagValue::parse) + .map(computed_value::T::Computed) } } diff --git a/tests/unit/style/parsing/font.rs b/tests/unit/style/parsing/font.rs new file mode 100644 index 00000000000..68eda7b901b --- /dev/null +++ b/tests/unit/style/parsing/font.rs @@ -0,0 +1,43 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use cssparser::Parser; +use media_queries::CSSErrorReporterTest; +use style::parser::ParserContext; +use style::stylesheets::Origin; +use url::Url; + +#[test] +fn font_feature_settings_should_parse_properly() { + use style::properties::longhands::font_feature_settings; + use style::properties::longhands::font_feature_settings::computed_value; + use style::properties::longhands::font_feature_settings::computed_value::FeatureTagValue; + + let normal = parse_longhand!(font_feature_settings, "normal"); + let normal_computed = computed_value::T::Normal; + assert_eq!(normal, normal_computed); + + let on = parse_longhand!(font_feature_settings, "\"abcd\" on"); + let on_computed = computed_value::T::Computed(vec![FeatureTagValue{ tag:String::from("abcd"), value: 1 }]); + assert_eq!(on, on_computed); + + let off = parse_longhand!(font_feature_settings, "\"abcd\" off"); + let off_computed = computed_value::T::Computed(vec![FeatureTagValue{ tag:String::from("abcd"), value: 0 }]); + assert_eq!(off, off_computed); + + let empty = parse_longhand!(font_feature_settings, "\"abcd\""); + let empty_computed = computed_value::T::Computed(vec![FeatureTagValue{ tag:String::from("abcd"), value: 1 }]); + assert_eq!(empty, empty_computed); + + let pos_integer = parse_longhand!(font_feature_settings, "\"abcd\" 100"); + let pos_integer_computed = computed_value::T::Computed(vec![FeatureTagValue{ tag:String::from("abcd"), value: 100 }]); + assert_eq!(pos_integer, pos_integer_computed); + + let pos_integer = parse_longhand!(font_feature_settings, "\"abcd\" off, \"efgh\""); + let pos_integer_computed = computed_value::T::Computed(vec![ + FeatureTagValue{ tag:String::from("abcd"), value: 0 }, + FeatureTagValue{ tag:String::from("efgh"), value: 1 } + ]); + assert_eq!(pos_integer, pos_integer_computed); +} diff --git a/tests/unit/style/parsing/mod.rs b/tests/unit/style/parsing/mod.rs index c481ea865da..f9d932b8346 100644 --- a/tests/unit/style/parsing/mod.rs +++ b/tests/unit/style/parsing/mod.rs @@ -62,6 +62,7 @@ macro_rules! parse_longhand { } mod basic_shape; +mod font; mod image; mod mask; mod position;