diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 839fdeb1dc7..de2359193dc 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -2181,7 +2181,7 @@ fn static_assert() { } pub fn set_font_family(&mut self, v: longhands::font_family::computed_value::T) { - use properties::longhands::font_family::computed_value::FontFamily; + use properties::longhands::font_family::computed_value::{FontFamily, FamilyNameSyntax}; let list = &mut self.gecko.mFont.fontlist; unsafe { Gecko_FontFamilyList_Clear(list); } @@ -2191,7 +2191,8 @@ fn static_assert() { for family in &v.0 { match *family { FontFamily::FamilyName(ref f) => { - unsafe { Gecko_FontFamilyList_AppendNamed(list, f.name.as_ptr(), f.quoted); } + let quoted = matches!(f.syntax, FamilyNameSyntax::Quoted); + unsafe { Gecko_FontFamilyList_AppendNamed(list, f.name.as_ptr(), quoted); } } FontFamily::Generic(ref name) => { let (family_type, generic) = FontFamily::generic(name); @@ -2222,7 +2223,8 @@ fn static_assert() { } pub fn clone_font_family(&self) -> longhands::font_family::computed_value::T { - use properties::longhands::font_family::computed_value::{FontFamily, FamilyName}; + use cssparser::serialize_identifier; + use properties::longhands::font_family::computed_value::{FontFamily, FamilyName, FamilyNameSyntax}; use gecko_bindings::structs::FontFamilyType; use gecko_string_cache::Atom; @@ -2235,13 +2237,18 @@ fn static_assert() { FontFamilyType::eFamily_cursive => FontFamily::Generic(atom!("cursive")), FontFamilyType::eFamily_fantasy => FontFamily::Generic(atom!("fantasy")), FontFamilyType::eFamily_moz_fixed => FontFamily::Generic(Atom::from("-moz-fixed")), - FontFamilyType::eFamily_named => FontFamily::FamilyName(FamilyName { - name: (&*gecko_font_family_name.mName).into(), - quoted: false - }), + FontFamilyType::eFamily_named => { + let name = Atom::from(&*gecko_font_family_name.mName); + let mut serialization = String::new(); + serialize_identifier(&name.to_string(), &mut serialization).unwrap(); + FontFamily::FamilyName(FamilyName { + name: name.clone(), + syntax: FamilyNameSyntax::Identifiers(serialization), + }) + }, FontFamilyType::eFamily_named_quoted => FontFamily::FamilyName(FamilyName { name: (&*gecko_font_family_name.mName).into(), - quoted: true + syntax: FamilyNameSyntax::Quoted, }), x => panic!("Found unexpected font FontFamilyType: {:?}", x), } diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index cbf6df1f44e..0f7c50e4f17 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -96,7 +96,20 @@ macro_rules! impl_gecko_keyword_conversions { #[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))] pub struct FamilyName { pub name: Atom, - pub quoted: bool, + pub syntax: FamilyNameSyntax, + } + + #[derive(Debug, PartialEq, Eq, Clone, Hash)] + #[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))] + pub enum FamilyNameSyntax { + /// The family name was specified in a quoted form, e.g. "Font Name" + /// or 'Font Name'. + Quoted, + + /// The family name was specified in an unquoted form as a sequence of + /// identifiers. The `String` is the serialization of the sequence of + /// identifiers. + Identifiers(String), } impl FontFamily { @@ -139,7 +152,7 @@ macro_rules! impl_gecko_keyword_conversions { // quoted by default. FontFamily::FamilyName(FamilyName { name: input, - quoted: true, + syntax: FamilyNameSyntax::Quoted, }) } @@ -148,7 +161,7 @@ macro_rules! impl_gecko_keyword_conversions { if let Ok(value) = input.try(|i| i.expect_string_cloned()) { return Ok(FontFamily::FamilyName(FamilyName { name: Atom::from(&*value), - quoted: true, + syntax: FamilyNameSyntax::Quoted, })) } let first_ident = input.expect_ident()?.clone(); @@ -182,20 +195,28 @@ macro_rules! impl_gecko_keyword_conversions { } let mut value = first_ident.as_ref().to_owned(); + let mut serialization = String::new(); + serialize_identifier(&first_ident, &mut serialization).unwrap(); + // These keywords are not allowed by themselves. // The only way this value can be valid with with another keyword. if css_wide_keyword { let ident = input.expect_ident()?; - value.push_str(" "); + value.push(' '); value.push_str(&ident); + serialization.push(' '); + serialize_identifier(&ident, &mut serialization).unwrap(); } while let Ok(ident) = input.try(|i| i.expect_ident_cloned()) { - value.push_str(" "); + value.push(' '); value.push_str(&ident); + serialization.push(' '); + serialize_identifier(&ident, &mut serialization).unwrap(); } + println!("serialization: {}", serialization); Ok(FontFamily::FamilyName(FamilyName { name: Atom::from(value), - quoted: false, + syntax: FamilyNameSyntax::Identifiers(serialization), })) } @@ -229,12 +250,17 @@ macro_rules! impl_gecko_keyword_conversions { impl ToCss for FamilyName { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - if self.quoted { - dest.write_char('"')?; - write!(CssStringWriter::new(dest), "{}", self.name)?; - dest.write_char('"') - } else { - serialize_identifier(&*self.name.to_string(), dest) + match self.syntax { + FamilyNameSyntax::Quoted => { + dest.write_char('"')?; + write!(CssStringWriter::new(dest), "{}", self.name)?; + dest.write_char('"') + } + FamilyNameSyntax::Identifiers(ref serialization) => { + // Note that `serialization` is already escaped/ + // serialized appropriately. + dest.write_str(&*serialization) + } } } } @@ -2508,7 +2534,7 @@ ${helpers.single_keyword("-moz-math-variant", use properties::longhands::font_family::computed_value::*; FontFamily::FamilyName(FamilyName { name: (&*font.mName).into(), - quoted: true + syntax: FamilyNameSyntax::Quoted, }) }).collect::>(); let weight = longhands::font_weight::computed_value::T::from_gecko_weight(system.weight); diff --git a/tests/unit/gfx/font_cache_thread.rs b/tests/unit/gfx/font_cache_thread.rs index 91e5b712dc7..e60ca8b9ba4 100644 --- a/tests/unit/gfx/font_cache_thread.rs +++ b/tests/unit/gfx/font_cache_thread.rs @@ -5,7 +5,7 @@ use cssparser::SourceLocation; use gfx::font_cache_thread::FontCacheThread; use ipc_channel::ipc; -use style::computed_values::font_family::FamilyName; +use style::computed_values::font_family::{FamilyName, FamilyNameSyntax}; use style::font_face::{FontFaceRuleData, Source}; #[test] @@ -15,11 +15,11 @@ fn test_local_web_font() { let font_cache_thread = FontCacheThread::new(inp_chan, None); let family_name = FamilyName { name: From::from("test family"), - quoted: true, + syntax: FamilyNameSyntax::Quoted, }; let variant_name = FamilyName { name: From::from("test font face"), - quoted: true, + syntax: FamilyNameSyntax::Quoted, }; let font_face_rule = FontFaceRuleData { family: Some(family_name.clone()), diff --git a/tests/unit/style/stylesheets.rs b/tests/unit/style/stylesheets.rs index 16a9eca1a10..17833893cf4 100644 --- a/tests/unit/style/stylesheets.rs +++ b/tests/unit/style/stylesheets.rs @@ -14,7 +14,7 @@ use servo_url::ServoUrl; use std::borrow::ToOwned; use std::sync::Mutex; use std::sync::atomic::AtomicBool; -use style::computed_values::font_family::FamilyName; +use style::computed_values::font_family::{FamilyName, FamilyNameSyntax}; use style::context::QuirksMode; use style::error_reporting::{ParseErrorReporter, ContextualParseError}; use style::media_queries::MediaList; @@ -254,7 +254,7 @@ fn test_parse_stylesheet() { CssRule::FontFeatureValues(Arc::new(stylesheet.shared_lock.wrap(FontFeatureValuesRule { family_names: vec![FamilyName { name: Atom::from("test"), - quoted: false, + syntax: FamilyNameSyntax::Identifiers(String::from("test")), }], swash: vec![ FFVDeclaration {