diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index 54d72a5f4d8..7a2534ffb38 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -2429,6 +2429,7 @@ dependencies = [ "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/components/style/values/mod.rs b/components/style/values/mod.rs index e926685f11b..79be8f7bd50 100644 --- a/components/style/values/mod.rs +++ b/components/style/values/mod.rs @@ -7,11 +7,7 @@ //! [values]: https://drafts.csswg.org/css-values/ pub use cssparser::RGBA; - -use app_units::Au; -use cssparser::CssStringWriter; -use std::fmt::{self, Write}; -use url::Url; +pub use style_traits::ToCss as LocalToCss; macro_rules! define_numbered_css_keyword_enum { ($name: ident: $( $css: expr => $variant: ident = $value: expr ),+,) => { @@ -48,38 +44,6 @@ macro_rules! define_numbered_css_keyword_enum { pub mod computed; pub mod specified; -/// The real ToCss trait can't be implemented for types in crates that don't -/// depend on each other. -pub trait LocalToCss { - /// Serialize `self` in CSS syntax, writing to `dest`. - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write; - - /// Serialize `self` in CSS syntax and return a string. - /// - /// (This is a convenience wrapper for `to_css` and probably should not be overridden.) - #[inline] - fn to_css_string(&self) -> String { - let mut s = String::new(); - self.to_css(&mut s).unwrap(); - s - } -} - -impl LocalToCss for Au { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - write!(dest, "{}px", self.to_f64_px()) - } -} - -impl LocalToCss for Url { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - try!(dest.write_str("url(\"")); - try!(write!(CssStringWriter::new(dest), "{}", self)); - try!(dest.write_str("\")")); - Ok(()) - } -} - pub type CSSFloat = f32; pub const FONT_MEDIUM_PX: i32 = 16; diff --git a/components/style_traits/Cargo.toml b/components/style_traits/Cargo.toml index af4c9314381..07bc967e664 100644 --- a/components/style_traits/Cargo.toml +++ b/components/style_traits/Cargo.toml @@ -22,3 +22,4 @@ heapsize_derive = {version = "0.1", optional = true} rustc-serialize = "0.3" serde = {version = "0.8", optional = true} serde_derive = {version = "0.8", optional = true} +url = "1.2" diff --git a/components/style_traits/lib.rs b/components/style_traits/lib.rs index bd060abf139..4ddb4e6a846 100644 --- a/components/style_traits/lib.rs +++ b/components/style_traits/lib.rs @@ -23,6 +23,7 @@ extern crate euclid; extern crate rustc_serialize; #[cfg(feature = "servo")] extern crate serde; #[cfg(feature = "servo")] #[macro_use] extern crate serde_derive; +extern crate url; /// Opaque type stored in type-unsafe work queues for parallel layout. /// Must be transmutable to and from TNode. @@ -61,3 +62,4 @@ pub mod cursor; pub mod values; pub mod viewport; +pub use values::ToCss; diff --git a/components/style_traits/values.rs b/components/style_traits/values.rs index 747c1481bd1..5982b8695e8 100644 --- a/components/style_traits/values.rs +++ b/components/style_traits/values.rs @@ -2,6 +2,53 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use app_units::Au; +use cssparser::CssStringWriter; +use std::fmt::{self, Write}; +use url::Url; + +/// The real ToCss trait can't be implemented for types in crates that don't +/// depend on each other. +pub trait ToCss { + /// Serialize `self` in CSS syntax, writing to `dest`. + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write; + + /// Serialize `self` in CSS syntax and return a string. + /// + /// (This is a convenience wrapper for `to_css` and probably should not be overridden.) + #[inline] + fn to_css_string(&self) -> String { + let mut s = String::new(); + self.to_css(&mut s).unwrap(); + s + } +} + +impl ToCss for Au { + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + write!(dest, "{}px", self.to_f64_px()) + } +} + +impl ToCss for Url { + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + try!(dest.write_str("url(\"")); + try!(write!(CssStringWriter::new(dest), "{}", self)); + try!(dest.write_str("\")")); + Ok(()) + } +} + +macro_rules! impl_to_css_for_predefined_type { + ($name: ty) => { + impl<'a> ToCss for $name { + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + ::cssparser::ToCss::to_css(self, dest) + } + } + }; +} + #[macro_export] macro_rules! define_css_keyword_enum { ($name: ident: $( $css: expr => $variant: ident ),+,) => { @@ -61,7 +108,6 @@ macro_rules! __define_css_keyword_enum__actual { } } - pub mod specified { use app_units::Au; diff --git a/ports/geckolib/Cargo.lock b/ports/geckolib/Cargo.lock index b2a72c41748..2970dba3b3f 100644 --- a/ports/geckolib/Cargo.lock +++ b/ports/geckolib/Cargo.lock @@ -373,6 +373,7 @@ dependencies = [ "cssparser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]]