mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Fixed 'on' bug, to_css bug; added tests
This commit is contained in:
parent
b9e03f3f4b
commit
89020022a3
3 changed files with 59 additions and 5 deletions
|
@ -411,15 +411,22 @@ ${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 {
|
||||
|
@ -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 | <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
tests/unit/style/parsing/font.rs
Normal file
43
tests/unit/style/parsing/font.rs
Normal file
|
@ -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);
|
||||
}
|
|
@ -62,6 +62,7 @@ macro_rules! parse_longhand {
|
|||
}
|
||||
|
||||
mod basic_shape;
|
||||
mod font;
|
||||
mod image;
|
||||
mod mask;
|
||||
mod position;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue