Add style_traits crate to improve crate separation,

closes #7353
This commit is contained in:
João Oliveira 2015-09-03 21:05:44 +01:00
parent 8d21a79246
commit 4a305d1e62
21 changed files with 274 additions and 156 deletions

View file

@ -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"

View file

@ -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;

View file

@ -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<Vec<PropertyDeclaration>>;

View file

@ -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<W>(&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 {

View file

@ -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<W>(&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<Zoom, ()> {
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<f32> {
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<ViewportPx, f32>,
pub initial_zoom: ScaleFactor<PagePx, ViewportPx, f32>,
pub min_zoom: Option<ScaleFactor<PagePx, ViewportPx, f32>>,
pub max_zoom: Option<ScaleFactor<PagePx, ViewportPx, f32>>,
pub user_zoom: UserZoom,
pub orientation: Orientation
pub trait MaybeNew {
fn maybe_new(initial_viewport: TypedSize2D<ViewportPx, f32>,
rule: &ViewportRule)
-> Option<ViewportConstraints>;
}
impl ToCss for ViewportConstraints {
fn to_css<W>(&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, "; }}")
}
}
impl ViewportConstraints {
pub fn maybe_new(initial_viewport: TypedSize2D<ViewportPx, f32>,
impl MaybeNew for ViewportConstraints {
fn maybe_new(initial_viewport: TypedSize2D<ViewportPx, f32>,
rule: &ViewportRule)
-> Option<ViewportConstraints>
{