diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index 6ce154ce44c..1033f68db11 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -538,3 +538,49 @@ ${helpers.single_keyword("font-variant-position", } } + +// https://www.w3.org/TR/css-fonts-3/#propdef-font-language-override +<%helpers:longhand name="font-language-override" products="none" animatable="False"> + use values::NoViewportPercentage; + use values::computed::ComputedValueAsSpecified; + pub use self::computed_value::T as SpecifiedValue; + + impl ComputedValueAsSpecified for SpecifiedValue {} + impl NoViewportPercentage for SpecifiedValue {} + + pub mod computed_value { + use cssparser::ToCss; + use std::fmt; + + impl ToCss for T { + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + match *self { + T::Normal => dest.write_str("normal"), + T::Override(ref lang) => write!(dest, "\"{}\"", lang), + } + } + } + + #[derive(Clone, Debug, PartialEq)] + #[cfg_attr(feature = "servo", derive(HeapSizeOf))] + pub enum T { + Normal, + Override(String), + } + } + + #[inline] + pub fn get_initial_value() -> computed_value::T { + computed_value::T::Normal + } + + pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result { + if input.try(|input| input.expect_ident_matching("normal")).is_ok() { + Ok(SpecifiedValue::Normal) + } else { + input.expect_string().map(|cow| { + SpecifiedValue::Override(cow.into_owned()) + }) + } + } + diff --git a/tests/unit/style/parsing/font.rs b/tests/unit/style/parsing/font.rs index 962e081ee20..1873314dc89 100644 --- a/tests/unit/style/parsing/font.rs +++ b/tests/unit/style/parsing/font.rs @@ -76,3 +76,31 @@ fn font_feature_settings_to_css() { assert_roundtrip_with_context!(font_feature_settings::parse, "\"abcd\" 4"); assert_roundtrip_with_context!(font_feature_settings::parse, "\"abcd\", \"efgh\""); } + +#[test] +fn font_language_override_should_parse_properly() { + use style::properties::longhands::font_language_override::{self, SpecifiedValue}; + + let normal = parse_longhand!(font_language_override, "normal"); + assert_eq!(normal, SpecifiedValue::Normal); + + let empty_str = parse_longhand!(font_language_override, "\"\""); + assert_eq!(empty_str, SpecifiedValue::Override("".to_string())); + + let normal_str = parse_longhand!(font_language_override, "\"normal\""); + assert_eq!(normal_str, SpecifiedValue::Override("normal".to_string())); + + let turkic = parse_longhand!(font_language_override, "\"TRK\""); + assert_eq!(turkic, SpecifiedValue::Override("TRK".to_string())); + + let danish = parse_longhand!(font_language_override, "\"DAN\""); + assert_eq!(danish, SpecifiedValue::Override("DAN".to_string())); +} + +#[test] +#[should_panic] +fn font_language_override_should_fail_on_empty_str() { + use style::properties::longhands::font_language_override::{self, SpecifiedValue}; + + parse_longhand!(font_language_override, ""); +}