From 4a305d1e626d1af4af57bdd1dc9d27d92dd53d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Thu, 3 Sep 2015 21:05:44 +0100 Subject: [PATCH] Add style_traits crate to improve crate separation, closes #7353 --- components/compositing/Cargo.toml | 7 +- components/compositing/compositor.rs | 2 +- components/compositing/compositor_task.rs | 2 +- components/compositing/constellation.rs | 2 +- components/compositing/lib.rs | 3 +- components/msg/Cargo.toml | 7 +- components/msg/constellation_msg.rs | 2 +- components/msg/lib.rs | 2 +- components/servo/Cargo.lock | 24 ++++- components/style/Cargo.toml | 4 +- components/style/lib.rs | 2 + components/style/selector_matching.rs | 3 +- components/style/values.rs | 47 +--------- components/style/viewport.rs | 102 +++------------------- components/style_traits/Cargo.toml | 35 ++++++++ components/style_traits/lib.rs | 27 ++++++ components/style_traits/values.rs | 55 ++++++++++++ components/style_traits/viewport.rs | 99 +++++++++++++++++++++ tests/unit/style/Cargo.toml | 3 + tests/unit/style/lib.rs | 1 + tests/unit/style/viewport.rs | 1 + 21 files changed, 274 insertions(+), 156 deletions(-) create mode 100644 components/style_traits/Cargo.toml create mode 100644 components/style_traits/lib.rs create mode 100644 components/style_traits/values.rs create mode 100644 components/style_traits/viewport.rs diff --git a/components/compositing/Cargo.toml b/components/compositing/Cargo.toml index fe8f444891ca..417e8a0ca62a 100644 --- a/components/compositing/Cargo.toml +++ b/components/compositing/Cargo.toml @@ -19,6 +19,9 @@ path = "../layout_traits" [dependencies.script_traits] path = "../script_traits" +[dependencies.style_traits] +path = "../style_traits" + [dependencies.msg] path = "../msg" @@ -28,9 +31,6 @@ path = "../profile_traits" [dependencies.net_traits] path = "../net_traits" -[dependencies.style] -path = "../style" - [dependencies.util] path = "../util" @@ -79,4 +79,3 @@ core-graphics = "0.1" [target.x86_64-apple-darwin.dependencies.core-text] git = "https://github.com/servo/core-text-rs" - diff --git a/components/compositing/compositor.rs b/components/compositing/compositor.rs index dcb45f40791b..b5dfd8d4efbd 100644 --- a/components/compositing/compositor.rs +++ b/components/compositing/compositor.rs @@ -46,7 +46,7 @@ use std::mem as std_mem; use std::rc::Rc; use std::slice::bytes::copy_memory; use std::sync::mpsc::Sender; -use style::viewport::ViewportConstraints; +use style_traits::viewport::ViewportConstraints; use time::{precise_time_ns, precise_time_s}; use url::Url; use util::geometry::{Au, PagePx, ScreenPx, ViewportPx}; diff --git a/components/compositing/compositor_task.rs b/components/compositing/compositor_task.rs index 161d8861a9dc..1721e303c023 100644 --- a/components/compositing/compositor_task.rs +++ b/components/compositing/compositor_task.rs @@ -25,7 +25,7 @@ use profile_traits::time; use std::fmt::{Error, Formatter, Debug}; use std::rc::Rc; use std::sync::mpsc::{channel, Sender, Receiver}; -use style::viewport::ViewportConstraints; +use style_traits::viewport::ViewportConstraints; use url::Url; use util::cursor::Cursor; diff --git a/components/compositing/constellation.rs b/components/compositing/constellation.rs index 8d0c2fee4c63..f4344d7a2fa3 100644 --- a/components/compositing/constellation.rs +++ b/components/compositing/constellation.rs @@ -50,7 +50,7 @@ use std::marker::PhantomData; use std::mem::replace; use std::process; use std::sync::mpsc::{Receiver, Sender, channel}; -use style::viewport::ViewportConstraints; +use style_traits::viewport::ViewportConstraints; use url::Url; use util::cursor::Cursor; use util::geometry::PagePx; diff --git a/components/compositing/lib.rs b/components/compositing/lib.rs index 92a633674b40..c4d9746587aa 100644 --- a/components/compositing/lib.rs +++ b/components/compositing/lib.rs @@ -30,7 +30,7 @@ extern crate num; extern crate profile_traits; extern crate net_traits; extern crate gfx_traits; -extern crate style; +extern crate style_traits; #[macro_use] extern crate util; extern crate gleam; @@ -58,4 +58,3 @@ mod scrolling; pub mod pipeline; pub mod constellation; pub mod windowing; - diff --git a/components/msg/Cargo.toml b/components/msg/Cargo.toml index fc4480318a59..32819c5faf94 100644 --- a/components/msg/Cargo.toml +++ b/components/msg/Cargo.toml @@ -7,15 +7,15 @@ authors = ["The Servo Project Developers"] name = "msg" path = "lib.rs" -[dependencies.style] -path = "../style" - [dependencies.util] path = "../util" [dependencies.canvas_traits] path = "../canvas_traits" +[dependencies.style_traits] +path = "../style_traits" + [dependencies.azure] git = "https://github.com/servo/rust-azure" @@ -56,4 +56,3 @@ core-foundation = "0.1" [target.x86_64-apple-darwin.dependencies.io-surface] git = "https://github.com/servo/io-surface-rs" - diff --git a/components/msg/constellation_msg.rs b/components/msg/constellation_msg.rs index 96c8b5a6bbb0..dc42e2deca56 100644 --- a/components/msg/constellation_msg.rs +++ b/components/msg/constellation_msg.rs @@ -18,7 +18,7 @@ use offscreen_gl_context::GLContextAttributes; use png::Image; use std::collections::HashMap; use std::sync::mpsc::{channel, Sender, Receiver}; -use style::viewport::ViewportConstraints; +use style_traits::viewport::ViewportConstraints; use url::Url; use util::cursor::Cursor; use util::geometry::{PagePx, ViewportPx}; diff --git a/components/msg/lib.rs b/components/msg/lib.rs index 34a87cfd8057..0fd0b59c304f 100644 --- a/components/msg/lib.rs +++ b/components/msg/lib.rs @@ -18,7 +18,7 @@ extern crate rustc_serialize; extern crate serde; extern crate util; extern crate url; -extern crate style; +extern crate style_traits; #[cfg(target_os = "macos")] extern crate core_foundation; diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index 9ab67dbf83d9..59389a78d717 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -242,7 +242,7 @@ dependencies = [ "png 0.1.0 (git+https://github.com/servo/rust-png)", "profile_traits 0.0.1", "script_traits 0.0.1", - "style 0.0.1", + "style_traits 0.0.1", "time 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "url 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", "util 0.0.1", @@ -1036,7 +1036,7 @@ dependencies = [ "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde_macros 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "style 0.0.1", + "style_traits 0.0.1", "url 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", "util 0.0.1", ] @@ -1589,6 +1589,7 @@ dependencies = [ "smallvec 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "string_cache 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "string_cache_plugin 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "style_traits 0.0.1", "url 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", "util 0.0.1", ] @@ -1603,6 +1604,25 @@ dependencies = [ "string_cache 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "string_cache_plugin 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "style 0.0.1", + "style_traits 0.0.1", + "url 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", + "util 0.0.1", +] + +[[package]] +name = "style_traits" +version = "0.0.1" +dependencies = [ + "cssparser 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "euclid 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", + "plugins 0.0.1", + "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", + "serde 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_macros 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "url 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", "util 0.0.1", ] diff --git a/components/style/Cargo.toml b/components/style/Cargo.toml index 12e2a44656ef..4bbc4762da90 100644 --- a/components/style/Cargo.toml +++ b/components/style/Cargo.toml @@ -15,6 +15,9 @@ path = "../plugins" [dependencies.util] path = "../util" +[dependencies.style_traits] +path = "../style_traits" + [dependencies.selectors] git = "https://github.com/servo/rust-selectors" features = ["unstable"] @@ -42,4 +45,3 @@ string_cache_plugin = "0.1" euclid = "0.1" serde = "0.5" serde_macros = "0.5" - diff --git a/components/style/lib.rs b/components/style/lib.rs index 1d7622ce0884..60e137a1ab0a 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -42,6 +42,8 @@ extern crate lazy_static; extern crate num; extern crate util; +#[macro_use] +extern crate style_traits; mod custom_properties; pub mod stylesheets; diff --git a/components/style/selector_matching.rs b/components/style/selector_matching.rs index 4cf0f7f2846e..ce4a3658a1af 100644 --- a/components/style/selector_matching.rs +++ b/components/style/selector_matching.rs @@ -18,8 +18,9 @@ use legacy::PresentationalHintSynthesis; use media_queries::Device; use node::TElementAttributes; use properties::{PropertyDeclaration, PropertyDeclarationBlock}; +use style_traits::viewport::ViewportConstraints; use stylesheets::{Stylesheet, CSSRuleIteratorExt, Origin}; -use viewport::{ViewportConstraints, ViewportRuleCascade}; +use viewport::{MaybeNew, ViewportRuleCascade}; pub type DeclarationBlock = GenericDeclarationBlock>; diff --git a/components/style/values.rs b/components/style/values.rs index 659c66dc9e08..7e71ef7354b4 100644 --- a/components/style/values.rs +++ b/components/style/values.rs @@ -6,37 +6,6 @@ pub use cssparser::RGBA; -macro_rules! define_css_keyword_enum { - ($name: ident: $( $css: expr => $variant: ident ),+,) => { - define_css_keyword_enum!($name: $( $css => $variant ),+); - }; - ($name: ident: $( $css: expr => $variant: ident ),+) => { - #[allow(non_camel_case_types)] - #[derive(Clone, Eq, PartialEq, Copy, Hash, RustcEncodable, Debug, HeapSizeOf)] - #[derive(Deserialize, Serialize)] - pub enum $name { - $( $variant ),+ - } - - impl $name { - pub fn parse(input: &mut ::cssparser::Parser) -> Result<$name, ()> { - match_ignore_ascii_case! { try!(input.expect_ident()), - $( $css => Ok($name::$variant) ),+ - _ => Err(()) - } - } - } - - impl ::cssparser::ToCss for $name { - fn to_css(&self, dest: &mut W) -> ::std::fmt::Result - where W: ::std::fmt::Write { - match *self { - $( $name::$variant => dest.write_str($css) ),+ - } - } - } - } -} macro_rules! define_numbered_css_keyword_enum { ($name: ident: $( $css: expr => $variant: ident = $value: expr ),+,) => { @@ -83,25 +52,11 @@ pub mod specified { use std::fmt; use std::fmt::Write; use std::ops::Mul; + use style_traits::values::specified::AllowedNumericType; use super::CSSFloat; use url::Url; use util::geometry::Au; - #[derive(Clone, Copy, Debug, PartialEq, Eq)] - pub enum AllowedNumericType { - All, - NonNegative - } - - impl AllowedNumericType { - #[inline] - pub fn is_ok(&self, value: f32) -> bool { - match *self { - AllowedNumericType::All => true, - AllowedNumericType::NonNegative => value >= 0., - } - } - } #[derive(Clone, PartialEq, Debug, HeapSizeOf)] pub struct CSSColor { diff --git a/components/style/viewport.rs b/components/style/viewport.rs index bdc9b6176f5c..2f5e5e3c21b7 100644 --- a/components/style/viewport.rs +++ b/components/style/viewport.rs @@ -2,19 +2,20 @@ * 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 cssparser::{Parser, DeclarationListParser, AtRuleParser, DeclarationParser, ToCss, parse_important}; +use cssparser::{Parser, DeclarationListParser, AtRuleParser, DeclarationParser, parse_important}; use euclid::scale_factor::ScaleFactor; use euclid::size::{Size2D, TypedSize2D}; use parser::{ParserContext, log_css_error}; use properties::longhands; +use style_traits::viewport::{UserZoom, Zoom, Orientation, ViewportConstraints}; use stylesheets::Origin; -use util::geometry::{Au, PagePx, ViewportPx}; +use util::geometry::{Au, ViewportPx}; use values::computed::{Context, ToComputedValue}; -use values::specified::{AllowedNumericType, LengthOrPercentageOrAuto}; +use values::specified::LengthOrPercentageOrAuto; + use std::ascii::AsciiExt; use std::collections::hash_map::{Entry, HashMap}; -use std::fmt; use std::intrinsics; #[derive(Copy, Clone, Debug, PartialEq)] @@ -33,61 +34,6 @@ pub enum ViewportDescriptor { Orientation(Orientation) } -/// Zoom is a number | percentage | auto -/// See http://dev.w3.org/csswg/css-device-adapt/#descdef-viewport-zoom -#[derive(Copy, Clone, Debug, PartialEq)] -pub enum Zoom { - Number(f32), - Percentage(f32), - Auto, -} - -impl ToCss for Zoom { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write - { - match *self { - Zoom::Number(number) => write!(dest, "{}", number), - Zoom::Percentage(percentage) => write!(dest, "{}%", percentage * 100.), - Zoom::Auto => write!(dest, "auto") - } - } -} - -impl Zoom { - pub fn parse(input: &mut Parser) -> Result { - use cssparser::Token; - - match try!(input.next()) { - Token::Percentage(ref value) if AllowedNumericType::NonNegative.is_ok(value.unit_value) => - Ok(Zoom::Percentage(value.unit_value)), - Token::Number(ref value) if AllowedNumericType::NonNegative.is_ok(value.value) => - Ok(Zoom::Number(value.value)), - Token::Ident(ref value) if value.eq_ignore_ascii_case("auto") => - Ok(Zoom::Auto), - _ => Err(()) - } - } - - #[inline] - pub fn to_f32(&self) -> Option { - match *self { - Zoom::Number(number) => Some(number as f32), - Zoom::Percentage(percentage) => Some(percentage as f32), - Zoom::Auto => None - } - } -} - -define_css_keyword_enum!(UserZoom: - "zoom" => Zoom, - "fixed" => Fixed); - -define_css_keyword_enum!(Orientation: - "auto" => Auto, - "portrait" => Portrait, - "landscape" => Landscape); - struct ViewportRuleParser<'a, 'b: 'a> { context: &'a ParserContext<'b> } @@ -306,40 +252,14 @@ impl<'a, I> ViewportDescriptorDeclarationCascade for I } } -#[derive(Debug, PartialEq, Deserialize, Serialize)] -pub struct ViewportConstraints { - pub size: TypedSize2D, - - pub initial_zoom: ScaleFactor, - pub min_zoom: Option>, - pub max_zoom: Option>, - - pub user_zoom: UserZoom, - pub orientation: Orientation -} - -impl ToCss for ViewportConstraints { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write - { - try!(write!(dest, "@viewport {{")); - try!(write!(dest, " width: {}px;", self.size.width.get())); - try!(write!(dest, " height: {}px;", self.size.height.get())); - try!(write!(dest, " zoom: {};", self.initial_zoom.get())); - if let Some(min_zoom) = self.min_zoom { - try!(write!(dest, " min-zoom: {};", min_zoom.get())); - } - if let Some(max_zoom) = self.max_zoom { - try!(write!(dest, " max-zoom: {};", max_zoom.get())); - } - try!(write!(dest, " user-zoom: ")); try!(self.user_zoom.to_css(dest)); - try!(write!(dest, "; orientation: ")); try!(self.orientation.to_css(dest)); - write!(dest, "; }}") - } +pub trait MaybeNew { + fn maybe_new(initial_viewport: TypedSize2D, + rule: &ViewportRule) + -> Option; } -impl ViewportConstraints { - pub fn maybe_new(initial_viewport: TypedSize2D, +impl MaybeNew for ViewportConstraints { + fn maybe_new(initial_viewport: TypedSize2D, rule: &ViewportRule) -> Option { diff --git a/components/style_traits/Cargo.toml b/components/style_traits/Cargo.toml new file mode 100644 index 000000000000..316ce73f462c --- /dev/null +++ b/components/style_traits/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "style_traits" +version = "0.0.1" +authors = ["The Servo Project Developers"] + +[lib] +name = "style_traits" +path = "lib.rs" + +[dependencies.util] +path = "../util" + +[dependencies.cssparser] +version = "0.3" +features = [ "serde-serialization" ] + +[dependencies.plugins] +path = "../plugins" + +[dependencies.selectors] +git = "https://github.com/servo/rust-selectors" +features = ["unstable"] + +[dependencies.url] +version = "0.2" +features = [ "serde_serialization" ] + +[dependencies] +euclid = "0.1" +log = "0.3" +lazy_static = "0.1.10" +num = "0.1.24" +rustc-serialize = "0.3" +serde = "0.5" +serde_macros = "0.5" diff --git a/components/style_traits/lib.rs b/components/style_traits/lib.rs new file mode 100644 index 000000000000..f47eac39baeb --- /dev/null +++ b/components/style_traits/lib.rs @@ -0,0 +1,27 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +//! This module contains shared types and messages for use by devtools/script. +//! The traits are here instead of in script so that the devtools crate can be +//! modified independently of the rest of Servo. + +#![crate_name = "style_traits"] +#![crate_type = "rlib"] +#![feature(custom_derive)] +#![feature(plugin)] +#![plugin(serde_macros)] +#![deny(unsafe_code)] + +extern crate euclid; +extern crate rustc_serialize; +extern crate serde; +extern crate util; + +#[macro_use] +extern crate cssparser; + +#[macro_use] +pub mod values; + +pub mod viewport; diff --git a/components/style_traits/values.rs b/components/style_traits/values.rs new file mode 100644 index 000000000000..6bca2601502a --- /dev/null +++ b/components/style_traits/values.rs @@ -0,0 +1,55 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +#[macro_export] +macro_rules! define_css_keyword_enum { + ($name: ident: $( $css: expr => $variant: ident ),+,) => { + define_css_keyword_enum!($name: $( $css => $variant ),+); + }; + ($name: ident: $( $css: expr => $variant: ident ),+) => { + #[allow(non_camel_case_types)] + #[derive(Clone, Eq, PartialEq, Copy, Hash, RustcEncodable, Debug, HeapSizeOf)] + #[derive(Deserialize, Serialize)] + pub enum $name { + $( $variant ),+ + } + + impl $name { + pub fn parse(input: &mut ::cssparser::Parser) -> Result<$name, ()> { + match_ignore_ascii_case! { try!(input.expect_ident()), + $( $css => Ok($name::$variant) ),+ + _ => Err(()) + } + } + } + + impl ::cssparser::ToCss for $name { + fn to_css(&self, dest: &mut W) -> ::std::fmt::Result + where W: ::std::fmt::Write { + match *self { + $( $name::$variant => dest.write_str($css) ),+ + } + } + } + } +} + + +pub mod specified { + #[derive(Clone, Copy, Debug, PartialEq, Eq)] + pub enum AllowedNumericType { + All, + NonNegative + } + + impl AllowedNumericType { + #[inline] + pub fn is_ok(&self, value: f32) -> bool { + match *self { + AllowedNumericType::All => true, + AllowedNumericType::NonNegative => value >= 0., + } + } + } +} diff --git a/components/style_traits/viewport.rs b/components/style_traits/viewport.rs new file mode 100644 index 000000000000..e08fc3909c61 --- /dev/null +++ b/components/style_traits/viewport.rs @@ -0,0 +1,99 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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 cssparser::{Parser, ToCss}; +use euclid::scale_factor::ScaleFactor; +use euclid::size::TypedSize2D; +use std::ascii::AsciiExt; +use std::fmt; +use util::geometry::{PagePx, ViewportPx}; +use values::specified::AllowedNumericType; + +define_css_keyword_enum!(UserZoom: + "zoom" => Zoom, + "fixed" => Fixed); + +define_css_keyword_enum!(Orientation: + "auto" => Auto, + "portrait" => Portrait, + "landscape" => Landscape); + + +#[derive(Debug, PartialEq, Deserialize, Serialize)] +pub struct ViewportConstraints { + pub size: TypedSize2D, + + pub initial_zoom: ScaleFactor, + pub min_zoom: Option>, + pub max_zoom: Option>, + + pub user_zoom: UserZoom, + pub orientation: Orientation +} + +impl ToCss for ViewportConstraints { + fn to_css(&self, dest: &mut W) -> fmt::Result + where W: fmt::Write + { + try!(write!(dest, "@viewport {{")); + try!(write!(dest, " width: {}px;", self.size.width.get())); + try!(write!(dest, " height: {}px;", self.size.height.get())); + try!(write!(dest, " zoom: {};", self.initial_zoom.get())); + if let Some(min_zoom) = self.min_zoom { + try!(write!(dest, " min-zoom: {};", min_zoom.get())); + } + if let Some(max_zoom) = self.max_zoom { + try!(write!(dest, " max-zoom: {};", max_zoom.get())); + } + try!(write!(dest, " user-zoom: ")); try!(self.user_zoom.to_css(dest)); + try!(write!(dest, "; orientation: ")); try!(self.orientation.to_css(dest)); + write!(dest, "; }}") + } +} + +/// Zoom is a number | percentage | auto +/// See http://dev.w3.org/csswg/css-device-adapt/#descdef-viewport-zoom +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum Zoom { + Number(f32), + Percentage(f32), + Auto, +} + +impl ToCss for Zoom { + fn to_css(&self, dest: &mut W) -> fmt::Result + where W: fmt::Write + { + match *self { + Zoom::Number(number) => write!(dest, "{}", number), + Zoom::Percentage(percentage) => write!(dest, "{}%", percentage * 100.), + Zoom::Auto => write!(dest, "auto") + } + } +} + +impl Zoom { + pub fn parse(input: &mut Parser) -> Result { + use cssparser::Token; + + match try!(input.next()) { + Token::Percentage(ref value) if AllowedNumericType::NonNegative.is_ok(value.unit_value) => + Ok(Zoom::Percentage(value.unit_value)), + Token::Number(ref value) if AllowedNumericType::NonNegative.is_ok(value.value) => + Ok(Zoom::Number(value.value)), + Token::Ident(ref value) if value.eq_ignore_ascii_case("auto") => + Ok(Zoom::Auto), + _ => Err(()) + } + } + + #[inline] + pub fn to_f32(&self) -> Option { + match *self { + Zoom::Number(number) => Some(number as f32), + Zoom::Percentage(percentage) => Some(percentage as f32), + Zoom::Auto => None + } + } +} diff --git a/tests/unit/style/Cargo.toml b/tests/unit/style/Cargo.toml index b8ad8c9a5e08..2b1a9e0b3be0 100644 --- a/tests/unit/style/Cargo.toml +++ b/tests/unit/style/Cargo.toml @@ -11,6 +11,9 @@ doctest = false [dependencies.style] path = "../../../components/style" +[dependencies.style_traits] +path = "../../../components/style_traits" + [dependencies.util] path = "../../../components/util" diff --git a/tests/unit/style/lib.rs b/tests/unit/style/lib.rs index 7744c802d98e..70af5f2f62c6 100644 --- a/tests/unit/style/lib.rs +++ b/tests/unit/style/lib.rs @@ -10,6 +10,7 @@ extern crate euclid; extern crate selectors; extern crate string_cache; extern crate style; +extern crate style_traits; extern crate url; extern crate util; diff --git a/tests/unit/style/viewport.rs b/tests/unit/style/viewport.rs index 283a453109b3..994908b3cfb9 100644 --- a/tests/unit/style/viewport.rs +++ b/tests/unit/style/viewport.rs @@ -10,6 +10,7 @@ use style::parser::ParserContext; use style::stylesheets::{Origin, Stylesheet, CSSRuleIteratorExt}; use style::values::specified::{Length, LengthOrPercentageOrAuto}; use style::viewport::*; +use style_traits::viewport::*; use url::Url; macro_rules! stylesheet {