Skip to content

Commit

Permalink
Fixed 'on' bug, to_css bug; added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rwakulszowa committed Oct 24, 2016
1 parent b9e03f3 commit 8902002
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
20 changes: 15 additions & 5 deletions components/style/properties/longhand/font.mako.rs
Expand Up @@ -411,27 +411,37 @@ ${helpers.single_keyword("font-variant-position",

impl ToCss for FeatureTagValue {
fn to_css<W>(&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 {
/// <string> [ on | off | <integer> ]
pub fn parse(input: &mut Parser) -> Result<FeatureTagValue, ()> {
let tag = try!(input.expect_string()).into_owned();

// allowed strings of length 4 containing chars: <U+20, U+7E>
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 {
Ok(FeatureTagValue{ tag: tag, value: value })
} 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 })
}
}
Expand All @@ -443,13 +453,13 @@ ${helpers.single_keyword("font-variant-position",
computed_value::T::Normal
}

/// normal | <feature-tag-value>#
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
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)
}
}
</%helpers:longhand>
43 changes: 43 additions & 0 deletions 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);
}
1 change: 1 addition & 0 deletions tests/unit/style/parsing/mod.rs
Expand Up @@ -62,6 +62,7 @@ macro_rules! parse_longhand {
}

mod basic_shape;
mod font;
mod image;
mod mask;
mod position;
Expand Down

0 comments on commit 8902002

Please sign in to comment.