Implement ToCss serialization for CSSRules

This commit is contained in:
Nazım Can Altınova 2016-11-15 23:25:03 +03:00
parent 22aebdf5d4
commit fdbadcdce2
10 changed files with 198 additions and 28 deletions

View file

@ -10,7 +10,9 @@ use computed_values::font_family::FontFamily;
use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser};
use parser::{ParserContext, log_css_error};
use properties::longhands::font_family::parse_one_family;
use std::fmt;
use std::iter;
use style_traits::ToCss;
use values::specified::url::SpecifiedUrl;
#[derive(Clone, Debug, PartialEq, Eq)]
@ -20,6 +22,22 @@ pub enum Source {
Local(FontFamily),
}
impl ToCss for Source {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
Source::Url(ref url) => {
try!(dest.write_str("local(\""));
try!(url.to_css(dest));
},
Source::Local(ref family) => {
try!(dest.write_str("url(\""));
try!(family.to_css(dest));
},
}
dest.write_str("\")")
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct UrlSource {
@ -27,6 +45,12 @@ pub struct UrlSource {
pub format_hints: Vec<String>,
}
impl ToCss for UrlSource {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
dest.write_str(self.url.as_str())
}
}
#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct FontFaceRule {
@ -34,6 +58,28 @@ pub struct FontFaceRule {
pub sources: Vec<Source>,
}
impl ToCss for FontFaceRule {
// Serialization of FontFaceRule is not specced.
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("@font-face { font-family: "));
try!(self.family.to_css(dest));
try!(dest.write_str(";"));
if self.sources.len() > 0 {
try!(dest.write_str(" src: "));
let mut iter = self.sources.iter();
try!(iter.next().unwrap().to_css(dest));
for source in iter {
try!(dest.write_str(", "));
try!(source.to_css(dest));
}
try!(dest.write_str(";"));
}
dest.write_str(" }")
}
}
pub fn parse_font_face_block(context: &ParserContext, input: &mut Parser)
-> Result<FontFaceRule, ()> {
let mut family = None;