Introduce traits for style structs and computed values.

This commit is contained in:
Bobby Holley 2016-03-04 16:53:57 -08:00
parent 16d2e9af65
commit 5c749127cc
7 changed files with 492 additions and 274 deletions

View file

@ -41,7 +41,7 @@ use std::ops::{Deref, DerefMut};
use std::sync::Arc; use std::sync::Arc;
use style::computed_values::{border_style, cursor, filter, image_rendering, mix_blend_mode}; use style::computed_values::{border_style, cursor, filter, image_rendering, mix_blend_mode};
use style::computed_values::{pointer_events}; use style::computed_values::{pointer_events};
use style::properties::ComputedValues; use style::properties::{ComputedValues, TComputedValues};
use style_traits::cursor::Cursor; use style_traits::cursor::Cursor;
use text::TextRun; use text::TextRun;
use text::glyph::CharIndex; use text::glyph::CharIndex;

View file

@ -7,7 +7,6 @@ use bezier::Bezier;
use cssparser::{Color, RGBA}; use cssparser::{Color, RGBA};
use dom::{OpaqueNode, TRestyleDamage}; use dom::{OpaqueNode, TRestyleDamage};
use euclid::point::Point2D; use euclid::point::Point2D;
use properties::ComputedValues;
use properties::longhands::background_position::computed_value::T as BackgroundPosition; use properties::longhands::background_position::computed_value::T as BackgroundPosition;
use properties::longhands::border_spacing::computed_value::T as BorderSpacing; use properties::longhands::border_spacing::computed_value::T as BorderSpacing;
use properties::longhands::clip::computed_value::ClipRect; use properties::longhands::clip::computed_value::ClipRect;
@ -25,6 +24,7 @@ use properties::longhands::transition_timing_function::computed_value::{Transiti
use properties::longhands::vertical_align::computed_value::T as VerticalAlign; use properties::longhands::vertical_align::computed_value::T as VerticalAlign;
use properties::longhands::visibility::computed_value::T as Visibility; use properties::longhands::visibility::computed_value::T as Visibility;
use properties::longhands::z_index::computed_value::T as ZIndex; use properties::longhands::z_index::computed_value::T as ZIndex;
use properties::{ComputedValues, TComputedValues};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::iter::repeat; use std::iter::repeat;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;

View file

@ -8,7 +8,7 @@ use animation::{self, Animation};
use context::SharedStyleContext; use context::SharedStyleContext;
use data::PrivateStyleData; use data::PrivateStyleData;
use dom::{TElement, TNode, TRestyleDamage}; use dom::{TElement, TNode, TRestyleDamage};
use properties::{ComputedValues, PropertyDeclaration, cascade}; use properties::{ComputedValues, PropertyDeclaration, TComputedValues, cascade};
use selector_impl::SelectorImplExt; use selector_impl::SelectorImplExt;
use selector_matching::{DeclarationBlock, Stylist}; use selector_matching::{DeclarationBlock, Stylist};
use selectors::Element; use selectors::Element;

File diff suppressed because it is too large Load diff

View file

@ -100,6 +100,7 @@ pub mod specified {
use std::ops::Mul; use std::ops::Mul;
use style_traits::values::specified::AllowedNumericType; use style_traits::values::specified::AllowedNumericType;
use super::AuExtensionMethods; use super::AuExtensionMethods;
use super::computed::{TContext, ToComputedValue};
use super::{CSSFloat, FONT_MEDIUM_PX}; use super::{CSSFloat, FONT_MEDIUM_PX};
use url::Url; use url::Url;
@ -1418,11 +1419,11 @@ pub mod specified {
} }
} }
impl super::computed::ToComputedValue for Time { impl ToComputedValue for Time {
type ComputedValue = Time; type ComputedValue = Time;
#[inline] #[inline]
fn to_computed_value(&self, _: &super::computed::Context) -> Time { fn to_computed_value<Cx: TContext>(&self, _: &Cx) -> Time {
*self *self
} }
} }
@ -1437,7 +1438,8 @@ pub mod specified {
pub mod computed { pub mod computed {
use app_units::Au; use app_units::Au;
use euclid::size::Size2D; use euclid::size::Size2D;
use properties::ComputedValues; use properties::TComputedValues;
use properties::style_struct_traits::TFont;
use std::fmt; use std::fmt;
use super::AuExtensionMethods; use super::AuExtensionMethods;
use super::specified::AngleOrCorner; use super::specified::AngleOrCorner;
@ -1446,21 +1448,39 @@ pub mod computed {
pub use cssparser::Color as CSSColor; pub use cssparser::Color as CSSColor;
pub use super::specified::{Angle, BorderStyle, Time}; pub use super::specified::{Angle, BorderStyle, Time};
pub struct Context<'a> { pub trait TContext {
type ConcreteComputedValues: TComputedValues;
fn is_root_element(&self) -> bool;
fn viewport_size(&self) -> Size2D<Au>;
fn inherited_style(&self) -> &Self::ConcreteComputedValues;
fn style(&self) -> &Self::ConcreteComputedValues;
fn mutate_style(&mut self) -> &mut Self::ConcreteComputedValues;
}
pub struct Context<'a, C: TComputedValues> {
pub is_root_element: bool, pub is_root_element: bool,
pub viewport_size: Size2D<Au>, pub viewport_size: Size2D<Au>,
pub inherited_style: &'a ComputedValues, pub inherited_style: &'a C,
/// Values access through this need to be in the properties "computed early": /// Values access through this need to be in the properties "computed early":
/// color, text-decoration, font-size, display, position, float, border-*-style, outline-style /// color, text-decoration, font-size, display, position, float, border-*-style, outline-style
pub style: ComputedValues, pub style: C,
}
impl<'a, C: TComputedValues> TContext for Context<'a, C> {
type ConcreteComputedValues = C;
fn is_root_element(&self) -> bool { self.is_root_element }
fn viewport_size(&self) -> Size2D<Au> { self.viewport_size }
fn inherited_style(&self) -> &C { &self.inherited_style }
fn style(&self) -> &C { &self.style }
fn mutate_style(&mut self) -> &mut C { &mut self.style }
} }
pub trait ToComputedValue { pub trait ToComputedValue {
type ComputedValue; type ComputedValue;
#[inline] #[inline]
fn to_computed_value(&self, _context: &Context) -> Self::ComputedValue; fn to_computed_value<Cx: TContext>(&self, _context: &Cx) -> Self::ComputedValue;
} }
pub trait ComputedValueAsSpecified {} pub trait ComputedValueAsSpecified {}
@ -1469,7 +1489,7 @@ pub mod computed {
type ComputedValue = T; type ComputedValue = T;
#[inline] #[inline]
fn to_computed_value(&self, _context: &Context) -> T { fn to_computed_value<Cx: TContext>(&self, _context: &Cx) -> T {
self.clone() self.clone()
} }
} }
@ -1478,7 +1498,7 @@ pub mod computed {
type ComputedValue = CSSColor; type ComputedValue = CSSColor;
#[inline] #[inline]
fn to_computed_value(&self, _context: &Context) -> CSSColor { fn to_computed_value<Cx: TContext>(&self, _context: &Cx) -> CSSColor {
self.parsed self.parsed
} }
} }
@ -1489,17 +1509,17 @@ pub mod computed {
type ComputedValue = Au; type ComputedValue = Au;
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> Au { fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> Au {
match *self { match *self {
specified::Length::Absolute(length) => length, specified::Length::Absolute(length) => length,
specified::Length::Calc(calc) => calc.to_computed_value(context).length(), specified::Length::Calc(calc) => calc.to_computed_value(context).length(),
specified::Length::FontRelative(length) => specified::Length::FontRelative(length) =>
length.to_computed_value(context.style.get_font().font_size, length.to_computed_value(context.style().get_font().clone_font_size(),
context.style.root_font_size), context.style().root_font_size()),
specified::Length::ViewportPercentage(length) => specified::Length::ViewportPercentage(length) =>
length.to_computed_value(context.viewport_size), length.to_computed_value(context.viewport_size()),
specified::Length::ServoCharacterWidth(length) => specified::Length::ServoCharacterWidth(length) =>
length.to_computed_value(context.style.get_font().font_size) length.to_computed_value(context.style().get_font().clone_font_size())
} }
} }
} }
@ -1583,7 +1603,7 @@ pub mod computed {
impl ToComputedValue for specified::CalcLengthOrPercentage { impl ToComputedValue for specified::CalcLengthOrPercentage {
type ComputedValue = CalcLengthOrPercentage; type ComputedValue = CalcLengthOrPercentage;
fn to_computed_value(&self, context: &Context) -> CalcLengthOrPercentage { fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> CalcLengthOrPercentage {
let mut length = None; let mut length = None;
if let Some(absolute) = self.absolute { if let Some(absolute) = self.absolute {
@ -1593,13 +1613,13 @@ pub mod computed {
for val in &[self.vw, self.vh, self.vmin, self.vmax] { for val in &[self.vw, self.vh, self.vmin, self.vmax] {
if let Some(val) = *val { if let Some(val) = *val {
length = Some(length.unwrap_or(Au(0)) + length = Some(length.unwrap_or(Au(0)) +
val.to_computed_value(context.viewport_size)); val.to_computed_value(context.viewport_size()));
} }
} }
for val in &[self.ch, self.em, self.ex, self.rem] { for val in &[self.ch, self.em, self.ex, self.rem] {
if let Some(val) = *val { if let Some(val) = *val {
length = Some(length.unwrap_or(Au(0)) + val.to_computed_value( length = Some(length.unwrap_or(Au(0)) + val.to_computed_value(
context.style.get_font().font_size, context.style.root_font_size)); context.style().get_font().clone_font_size(), context.style().root_font_size()));
} }
} }
@ -1621,7 +1641,7 @@ pub mod computed {
type ComputedValue = BorderRadiusSize; type ComputedValue = BorderRadiusSize;
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> BorderRadiusSize { fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> BorderRadiusSize {
let specified::BorderRadiusSize(s) = *self; let specified::BorderRadiusSize(s) = *self;
let w = s.width.to_computed_value(context); let w = s.width.to_computed_value(context);
let h = s.height.to_computed_value(context); let h = s.height.to_computed_value(context);
@ -1664,7 +1684,7 @@ pub mod computed {
impl ToComputedValue for specified::LengthOrPercentage { impl ToComputedValue for specified::LengthOrPercentage {
type ComputedValue = LengthOrPercentage; type ComputedValue = LengthOrPercentage;
fn to_computed_value(&self, context: &Context) -> LengthOrPercentage { fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> LengthOrPercentage {
match *self { match *self {
specified::LengthOrPercentage::Length(value) => { specified::LengthOrPercentage::Length(value) => {
LengthOrPercentage::Length(value.to_computed_value(context)) LengthOrPercentage::Length(value.to_computed_value(context))
@ -1712,7 +1732,7 @@ pub mod computed {
type ComputedValue = LengthOrPercentageOrAuto; type ComputedValue = LengthOrPercentageOrAuto;
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> LengthOrPercentageOrAuto { fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> LengthOrPercentageOrAuto {
match *self { match *self {
specified::LengthOrPercentageOrAuto::Length(value) => { specified::LengthOrPercentageOrAuto::Length(value) => {
LengthOrPercentageOrAuto::Length(value.to_computed_value(context)) LengthOrPercentageOrAuto::Length(value.to_computed_value(context))
@ -1764,7 +1784,7 @@ pub mod computed {
type ComputedValue = LengthOrPercentageOrNone; type ComputedValue = LengthOrPercentageOrNone;
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> LengthOrPercentageOrNone { fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> LengthOrPercentageOrNone {
match *self { match *self {
specified::LengthOrPercentageOrNone::Length(value) => { specified::LengthOrPercentageOrNone::Length(value) => {
LengthOrPercentageOrNone::Length(value.to_computed_value(context)) LengthOrPercentageOrNone::Length(value.to_computed_value(context))
@ -1812,7 +1832,7 @@ pub mod computed {
type ComputedValue = LengthOrNone; type ComputedValue = LengthOrNone;
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> LengthOrNone { fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> LengthOrNone {
match *self { match *self {
specified::LengthOrNone::Length(specified::Length::Calc(calc)) => { specified::LengthOrNone::Length(specified::Length::Calc(calc)) => {
LengthOrNone::Length(calc.to_computed_value(context).length()) LengthOrNone::Length(calc.to_computed_value(context).length())
@ -1840,7 +1860,7 @@ pub mod computed {
type ComputedValue = Image; type ComputedValue = Image;
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> Image { fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> Image {
match *self { match *self {
specified::Image::Url(ref url) => Image::Url(url.clone()), specified::Image::Url(ref url) => Image::Url(url.clone()),
specified::Image::LinearGradient(ref linear_gradient) => { specified::Image::LinearGradient(ref linear_gradient) => {
@ -1936,7 +1956,7 @@ pub mod computed {
type ComputedValue = LinearGradient; type ComputedValue = LinearGradient;
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> LinearGradient { fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> LinearGradient {
let specified::LinearGradient { let specified::LinearGradient {
angle_or_corner, angle_or_corner,
ref stops ref stops

View file

@ -8,7 +8,7 @@ use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser,
use euclid::scale_factor::ScaleFactor; use euclid::scale_factor::ScaleFactor;
use euclid::size::{Size2D, TypedSize2D}; use euclid::size::{Size2D, TypedSize2D};
use parser::{ParserContext, log_css_error}; use parser::{ParserContext, log_css_error};
use properties::INITIAL_VALUES; use properties::{ComputedValues, TComputedValues};
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
use std::collections::hash_map::{Entry, HashMap}; use std::collections::hash_map::{Entry, HashMap};
use std::fmt; use std::fmt;
@ -594,8 +594,8 @@ impl MaybeNew for ViewportConstraints {
let context = Context { let context = Context {
is_root_element: false, is_root_element: false,
viewport_size: initial_viewport, viewport_size: initial_viewport,
inherited_style: &*INITIAL_VALUES, inherited_style: ComputedValues::initial_values(),
style: INITIAL_VALUES.clone(), style: ComputedValues::initial_values().clone(),
}; };
// DEVICE-ADAPT § 9.3 Resolving 'extend-to-zoom' // DEVICE-ADAPT § 9.3 Resolving 'extend-to-zoom'

View file

@ -25,10 +25,10 @@ extern crate util;
#[cfg(test)] mod writing_modes { #[cfg(test)] mod writing_modes {
use style::logical_geometry::WritingMode; use style::logical_geometry::WritingMode;
use style::properties::{INITIAL_VALUES, get_writing_mode}; use style::properties::{INITIAL_SERVO_VALUES, TComputedValues, get_writing_mode};
#[test] #[test]
fn initial_writing_mode_is_empty() { fn initial_writing_mode_is_empty() {
assert_eq!(get_writing_mode(INITIAL_VALUES.get_inheritedbox()), WritingMode::empty()) assert_eq!(get_writing_mode(INITIAL_SERVO_VALUES.get_inheritedbox()), WritingMode::empty())
} }
} }