From 0090fbb3c8278b2616e9dd164dd932740d879d91 Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Thu, 8 Mar 2018 21:31:08 +1100 Subject: [PATCH] Move ComputedUrl into their impl mods. --- components/style/gecko/url.rs | 5 ++ components/style/servo/url.rs | 43 +++++++++++++++- components/style/values/computed/mod.rs | 63 ++++-------------------- components/style/values/specified/mod.rs | 4 +- 4 files changed, 57 insertions(+), 58 deletions(-) diff --git a/components/style/gecko/url.rs b/components/style/gecko/url.rs index 70a6ca4878d..9da274a3848 100644 --- a/components/style/gecko/url.rs +++ b/components/style/gecko/url.rs @@ -196,3 +196,8 @@ impl MallocSizeOf for SpecifiedImageUrl { n } } + +/// The computed value of a CSS `url()`. +pub type ComputedUrl = SpecifiedUrl; +/// The computed value of a CSS `url()` for image. +pub type ComputedImageUrl = SpecifiedImageUrl; diff --git a/components/style/servo/url.rs b/components/style/servo/url.rs index be3c31f84ef..fc461955cea 100644 --- a/components/style/servo/url.rs +++ b/components/style/servo/url.rs @@ -4,7 +4,8 @@ //! Common handling for the specified value CSS url() values. -use parser::ParserContext; +use cssparser::Parser; +use parser::{Parse, ParserContext}; use servo_url::ServoUrl; use std::fmt::{self, Write}; // Note: We use std::sync::Arc rather than servo_arc::Arc here because the @@ -12,7 +13,7 @@ use std::fmt::{self, Write}; // the threshold. use std::sync::Arc; use style_traits::{CssWriter, ParseError, ToCss}; -use values::computed::{Context, ToComputedValue, ComputedUrl}; +use values::computed::{Context, ToComputedValue}; /// A specified url() value for servo. /// @@ -174,3 +175,41 @@ impl ToComputedValue for SpecifiedUrl { /// A specified image url() value for servo. pub type SpecifiedImageUrl = SpecifiedUrl; + +/// The computed value of a CSS `url()`, resolved relative to the stylesheet URL. +#[derive(Clone, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)] +pub enum ComputedUrl { + /// The `url()` was invalid or it wasn't specified by the user. + Invalid(#[ignore_malloc_size_of = "Arc"] Arc), + /// The resolved `url()` relative to the stylesheet URL. + Valid(ServoUrl), +} + +impl ComputedUrl { + /// Returns the resolved url if it was valid. + pub fn url(&self) -> Option<&ServoUrl> { + match *self { + ComputedUrl::Valid(ref url) => Some(url), + _ => None, + } + } +} + +impl ToCss for ComputedUrl { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { + let string = match *self { + ComputedUrl::Valid(ref url) => url.as_str(), + ComputedUrl::Invalid(ref invalid_string) => invalid_string, + }; + + dest.write_str("url(")?; + string.to_css(dest)?; + dest.write_str(")") + } +} + +/// The computed value of a CSS `url()` for image. +pub type ComputedImageUrl = ComputedUrl; diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index be9985d8cb4..1667f7ec8cf 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -15,14 +15,10 @@ use media_queries::Device; use properties; use properties::{ComputedValues, LonghandId, StyleBuilder}; use rule_cache::RuleCacheConditions; -#[cfg(feature = "servo")] -use servo_url::ServoUrl; use std::cell::RefCell; use std::cmp; use std::f32; use std::fmt::{self, Write}; -#[cfg(feature = "servo")] -use std::sync::Arc; use style_traits::{CssWriter, ToCss}; use style_traits::cursor::CursorKind; use super::{CSSFloat, CSSInteger}; @@ -84,6 +80,7 @@ pub use self::time::Time; pub use self::transform::{Rotate, Scale, TimingFunction, Transform, TransformOperation}; pub use self::transform::{TransformOrigin, TransformStyle, Translate}; pub use self::ui::MozForceBrokenImageIcon; +pub use self::url::{ComputedUrl, ComputedImageUrl}; #[cfg(feature = "gecko")] pub mod align; @@ -117,6 +114,14 @@ pub mod time; pub mod transform; pub mod ui; +/// Common handling for the computed value CSS url() values. +pub mod url { +#[cfg(feature = "servo")] +pub use ::servo::url::{ComputedUrl, ComputedImageUrl}; +#[cfg(feature = "gecko")] +pub use ::gecko::url::{ComputedUrl, ComputedImageUrl}; +} + /// A `Context` is all the data a specified value could ever need to compute /// itself and be transformed to a computed value. pub struct Context<'a> { @@ -636,56 +641,6 @@ impl ClipRectOrAuto { } } -/// The computed value of a CSS `url()`, resolved relative to the stylesheet URL. -#[cfg(feature = "servo")] -#[derive(Clone, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)] -pub enum ComputedUrl { - /// The `url()` was invalid or it wasn't specified by the user. - Invalid(#[ignore_malloc_size_of = "Arc"] Arc), - /// The resolved `url()` relative to the stylesheet URL. - Valid(ServoUrl), -} - -/// The computed value of a CSS `url()` for image. -#[cfg(feature = "servo")] -pub type ComputedImageUrl = ComputedUrl; - -// TODO: Properly build ComputedUrl for gecko -/// The computed value of a CSS `url()`. -#[cfg(feature = "gecko")] -pub type ComputedUrl = specified::url::SpecifiedUrl; -/// The computed value of a CSS `url()` for image. -#[cfg(feature = "gecko")] -pub type ComputedImageUrl = specified::url::SpecifiedImageUrl; - -#[cfg(feature = "servo")] -impl ComputedUrl { - /// Returns the resolved url if it was valid. - pub fn url(&self) -> Option<&ServoUrl> { - match *self { - ComputedUrl::Valid(ref url) => Some(url), - _ => None, - } - } -} - -#[cfg(feature = "servo")] -impl ToCss for ComputedUrl { - fn to_css(&self, dest: &mut CssWriter) -> fmt::Result - where - W: Write, - { - let string = match *self { - ComputedUrl::Valid(ref url) => url.as_str(), - ComputedUrl::Invalid(ref invalid_string) => invalid_string, - }; - - dest.write_str("url(")?; - string.to_css(dest)?; - dest.write_str(")") - } -} - /// | pub type UrlOrNone = Either; diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index 7a3d51964a3..192c22a0a13 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -117,9 +117,9 @@ pub mod ui; /// Common handling for the specified value CSS url() values. pub mod url { #[cfg(feature = "servo")] -pub use ::servo::url::*; +pub use ::servo::url::{SpecifiedUrl, SpecifiedImageUrl}; #[cfg(feature = "gecko")] -pub use ::gecko::url::*; +pub use ::gecko::url::{SpecifiedUrl, SpecifiedImageUrl}; } /// Parse a `` value, with a given clamping mode.