Derive ToCss for font-face sources

This commit is contained in:
Anthony Ramine 2017-06-17 02:59:46 +02:00
parent 3ee6598abb
commit 3217d1404e
3 changed files with 11 additions and 30 deletions

View file

@ -24,33 +24,16 @@ use style_traits::{ToCss, OneOrMoreCommaSeparated, ParseError, StyleParseError};
use values::specified::url::SpecifiedUrl;
/// A source for a font-face rule.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Debug, Eq, PartialEq, ToCss)]
pub enum Source {
/// A `url()` source.
Url(UrlSource),
/// A `local()` source.
#[css(function)]
Local(FamilyName),
}
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("url(\""));
try!(url.to_css(dest));
},
Source::Local(ref family) => {
try!(dest.write_str("local(\""));
try!(family.to_css(dest));
},
}
dest.write_str("\")")
}
}
impl OneOrMoreCommaSeparated for Source {}
/// A `UrlSource` represents a font-face source that has been specified with a
@ -70,7 +53,7 @@ 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())
self.url.to_css(dest)
}
}

View file

@ -4,12 +4,11 @@
//! Common handling for the specified value CSS url() values.
use cssparser::CssStringWriter;
use gecko_bindings::structs::{ServoBundledURI, URLExtraData};
use gecko_bindings::structs::root::mozilla::css::ImageValue;
use gecko_bindings::sugar::refptr::RefPtr;
use parser::ParserContext;
use std::fmt::{self, Write};
use std::fmt;
use style_traits::{ToCss, ParseError};
use stylearc::Arc;
@ -100,8 +99,8 @@ impl SpecifiedUrl {
impl ToCss for SpecifiedUrl {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("url(\""));
try!(CssStringWriter::new(dest).write_str(&*self.serialization));
dest.write_str("\")")
dest.write_str("url(")?;
self.serialization.to_css(dest)?;
dest.write_str(")")
}
}

View file

@ -4,10 +4,9 @@
//! Common handling for the specified value CSS url() values.
use cssparser::CssStringWriter;
use parser::ParserContext;
use servo_url::ServoUrl;
use std::fmt::{self, Write};
use std::fmt;
// Note: We use std::sync::Arc rather than stylearc::Arc here because the
// nonzero optimization is important in keeping the size of SpecifiedUrl below
// the threshold.
@ -111,7 +110,6 @@ impl PartialEq for SpecifiedUrl {
impl ToCss for SpecifiedUrl {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("url(\""));
let string = match self.original {
Some(ref original) => &**original,
None => match self.resolved {
@ -123,7 +121,8 @@ impl ToCss for SpecifiedUrl {
}
};
try!(CssStringWriter::new(dest).write_str(string));
dest.write_str("\")")
dest.write_str("url(")?;
string.to_css(dest)?;
dest.write_str(")")
}
}