style: Preserve font-family identifier sequence when serializing.

This commit is contained in:
Cameron McCormack 2017-07-30 13:32:40 +08:00
parent 097e36beb1
commit 4f525f6fa0
2 changed files with 47 additions and 19 deletions

View file

@ -1782,7 +1782,7 @@ fn static_assert() {
} }
pub fn set_font_family(&mut self, v: longhands::font_family::computed_value::T) { 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; let list = &mut self.gecko.mFont.fontlist;
unsafe { Gecko_FontFamilyList_Clear(list); } unsafe { Gecko_FontFamilyList_Clear(list); }
@ -1792,7 +1792,8 @@ fn static_assert() {
for family in &v.0 { for family in &v.0 {
match *family { match *family {
FontFamily::FamilyName(ref f) => { 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) => { FontFamily::Generic(ref name) => {
let (family_type, generic) = FontFamily::generic(name); let (family_type, generic) = FontFamily::generic(name);
@ -1819,7 +1820,7 @@ fn static_assert() {
} }
pub fn clone_font_family(&self) -> longhands::font_family::computed_value::T { pub fn clone_font_family(&self) -> longhands::font_family::computed_value::T {
use properties::longhands::font_family::computed_value::{FontFamily, FamilyName}; use properties::longhands::font_family::computed_value::{FontFamily, FamilyName, FamilyNameSyntax};
use gecko_bindings::structs::FontFamilyType; use gecko_bindings::structs::FontFamilyType;
use gecko_string_cache::Atom; use gecko_string_cache::Atom;
@ -1832,13 +1833,16 @@ fn static_assert() {
FontFamilyType::eFamily_cursive => FontFamily::Generic(atom!("cursive")), FontFamilyType::eFamily_cursive => FontFamily::Generic(atom!("cursive")),
FontFamilyType::eFamily_fantasy => FontFamily::Generic(atom!("fantasy")), FontFamilyType::eFamily_fantasy => FontFamily::Generic(atom!("fantasy")),
FontFamilyType::eFamily_moz_fixed => FontFamily::Generic(Atom::from("-moz-fixed")), FontFamilyType::eFamily_moz_fixed => FontFamily::Generic(Atom::from("-moz-fixed")),
FontFamilyType::eFamily_named => FontFamily::FamilyName(FamilyName { FontFamilyType::eFamily_named => {
name: (&*gecko_font_family_name.mName).into(), let name = Atom::from(&*gecko_font_family_name.mName);
quoted: false FontFamily::FamilyName(FamilyName {
}), name: name.clone(),
syntax: FamilyNameSyntax::Identifiers(vec![name]),
})
},
FontFamilyType::eFamily_named_quoted => FontFamily::FamilyName(FamilyName { FontFamilyType::eFamily_named_quoted => FontFamily::FamilyName(FamilyName {
name: (&*gecko_font_family_name.mName).into(), name: (&*gecko_font_family_name.mName).into(),
quoted: true syntax: FamilyNameSyntax::Quoted,
}), }),
x => panic!("Found unexpected font FontFamilyType: {:?}", x), x => panic!("Found unexpected font FontFamilyType: {:?}", x),
} }

View file

@ -96,7 +96,14 @@ macro_rules! impl_gecko_keyword_conversions {
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))] #[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct FamilyName { pub struct FamilyName {
pub name: Atom, 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 {
Quoted,
Identifiers(Vec<Atom>),
} }
impl FontFamily { impl FontFamily {
@ -139,7 +146,7 @@ macro_rules! impl_gecko_keyword_conversions {
// quoted by default. // quoted by default.
FontFamily::FamilyName(FamilyName { FontFamily::FamilyName(FamilyName {
name: input, name: input,
quoted: true, syntax: FamilyNameSyntax::Quoted,
}) })
} }
@ -148,10 +155,14 @@ macro_rules! impl_gecko_keyword_conversions {
if let Ok(value) = input.try(|i| i.expect_string_cloned()) { if let Ok(value) = input.try(|i| i.expect_string_cloned()) {
return Ok(FontFamily::FamilyName(FamilyName { return Ok(FontFamily::FamilyName(FamilyName {
name: Atom::from(&*value), name: Atom::from(&*value),
quoted: true, syntax: FamilyNameSyntax::Quoted,
})) }))
} }
let mut identifiers = vec![];
let first_ident = input.expect_ident()?.clone(); let first_ident = input.expect_ident()?.clone();
identifiers.push(Atom::from(&*first_ident));
// FIXME(bholley): The fast thing to do here would be to look up the // FIXME(bholley): The fast thing to do here would be to look up the
// string (as lowercase) in the static atoms table. We don't have an // string (as lowercase) in the static atoms table. We don't have an
@ -188,14 +199,16 @@ macro_rules! impl_gecko_keyword_conversions {
let ident = input.expect_ident()?; let ident = input.expect_ident()?;
value.push_str(" "); value.push_str(" ");
value.push_str(&ident); value.push_str(&ident);
identifiers.push(Atom::from(&*ident.clone()));
} }
while let Ok(ident) = input.try(|i| i.expect_ident_cloned()) { while let Ok(ident) = input.try(|i| i.expect_ident_cloned()) {
value.push_str(" "); value.push_str(" ");
value.push_str(&ident); value.push_str(&ident);
identifiers.push(Atom::from(&*ident));
} }
Ok(FontFamily::FamilyName(FamilyName { Ok(FontFamily::FamilyName(FamilyName {
name: Atom::from(value), name: Atom::from(value),
quoted: false, syntax: FamilyNameSyntax::Identifiers(identifiers),
})) }))
} }
@ -229,12 +242,23 @@ macro_rules! impl_gecko_keyword_conversions {
impl ToCss for FamilyName { impl ToCss for FamilyName {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
if self.quoted { match self.syntax {
dest.write_char('"')?; FamilyNameSyntax::Quoted => {
write!(CssStringWriter::new(dest), "{}", self.name)?; dest.write_char('"')?;
dest.write_char('"') write!(CssStringWriter::new(dest), "{}", self.name)?;
} else { dest.write_char('"')
serialize_identifier(&*self.name.to_string(), dest) }
FamilyNameSyntax::Identifiers(ref identifiers) => {
let mut first = true;
for identifier in identifiers {
if !first {
dest.write_char(' ')?;
}
serialize_identifier(&*identifier.to_string(), dest)?;
first = false;
}
Ok(())
}
} }
} }
} }
@ -2471,7 +2495,7 @@ ${helpers.single_keyword("-moz-math-variant",
use properties::longhands::font_family::computed_value::*; use properties::longhands::font_family::computed_value::*;
FontFamily::FamilyName(FamilyName { FontFamily::FamilyName(FamilyName {
name: (&*font.mName).into(), name: (&*font.mName).into(),
quoted: true syntax: FamilyNameSyntax::Quoted,
}) })
}).collect::<Vec<_>>(); }).collect::<Vec<_>>();
let weight = longhands::font_weight::computed_value::T::from_gecko_weight(system.weight); let weight = longhands::font_weight::computed_value::T::from_gecko_weight(system.weight);