mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
Introduce traits for style structs and computed values.
This commit is contained in:
parent
16d2e9af65
commit
5c749127cc
7 changed files with 492 additions and 274 deletions
|
@ -41,7 +41,7 @@ use std::ops::{Deref, DerefMut};
|
|||
use std::sync::Arc;
|
||||
use style::computed_values::{border_style, cursor, filter, image_rendering, mix_blend_mode};
|
||||
use style::computed_values::{pointer_events};
|
||||
use style::properties::ComputedValues;
|
||||
use style::properties::{ComputedValues, TComputedValues};
|
||||
use style_traits::cursor::Cursor;
|
||||
use text::TextRun;
|
||||
use text::glyph::CharIndex;
|
||||
|
|
|
@ -7,7 +7,6 @@ use bezier::Bezier;
|
|||
use cssparser::{Color, RGBA};
|
||||
use dom::{OpaqueNode, TRestyleDamage};
|
||||
use euclid::point::Point2D;
|
||||
use properties::ComputedValues;
|
||||
use properties::longhands::background_position::computed_value::T as BackgroundPosition;
|
||||
use properties::longhands::border_spacing::computed_value::T as BorderSpacing;
|
||||
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::visibility::computed_value::T as Visibility;
|
||||
use properties::longhands::z_index::computed_value::T as ZIndex;
|
||||
use properties::{ComputedValues, TComputedValues};
|
||||
use std::cmp::Ordering;
|
||||
use std::iter::repeat;
|
||||
use std::sync::mpsc::Sender;
|
||||
|
|
|
@ -8,7 +8,7 @@ use animation::{self, Animation};
|
|||
use context::SharedStyleContext;
|
||||
use data::PrivateStyleData;
|
||||
use dom::{TElement, TNode, TRestyleDamage};
|
||||
use properties::{ComputedValues, PropertyDeclaration, cascade};
|
||||
use properties::{ComputedValues, PropertyDeclaration, TComputedValues, cascade};
|
||||
use selector_impl::SelectorImplExt;
|
||||
use selector_matching::{DeclarationBlock, Stylist};
|
||||
use selectors::Element;
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -100,6 +100,7 @@ pub mod specified {
|
|||
use std::ops::Mul;
|
||||
use style_traits::values::specified::AllowedNumericType;
|
||||
use super::AuExtensionMethods;
|
||||
use super::computed::{TContext, ToComputedValue};
|
||||
use super::{CSSFloat, FONT_MEDIUM_PX};
|
||||
use url::Url;
|
||||
|
||||
|
@ -1418,11 +1419,11 @@ pub mod specified {
|
|||
}
|
||||
}
|
||||
|
||||
impl super::computed::ToComputedValue for Time {
|
||||
impl ToComputedValue for Time {
|
||||
type ComputedValue = Time;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, _: &super::computed::Context) -> Time {
|
||||
fn to_computed_value<Cx: TContext>(&self, _: &Cx) -> Time {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
@ -1437,7 +1438,8 @@ pub mod specified {
|
|||
pub mod computed {
|
||||
use app_units::Au;
|
||||
use euclid::size::Size2D;
|
||||
use properties::ComputedValues;
|
||||
use properties::TComputedValues;
|
||||
use properties::style_struct_traits::TFont;
|
||||
use std::fmt;
|
||||
use super::AuExtensionMethods;
|
||||
use super::specified::AngleOrCorner;
|
||||
|
@ -1446,21 +1448,39 @@ pub mod computed {
|
|||
pub use cssparser::Color as CSSColor;
|
||||
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 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":
|
||||
/// 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 {
|
||||
type ComputedValue;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, _context: &Context) -> Self::ComputedValue;
|
||||
fn to_computed_value<Cx: TContext>(&self, _context: &Cx) -> Self::ComputedValue;
|
||||
}
|
||||
|
||||
pub trait ComputedValueAsSpecified {}
|
||||
|
@ -1469,7 +1489,7 @@ pub mod computed {
|
|||
type ComputedValue = T;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, _context: &Context) -> T {
|
||||
fn to_computed_value<Cx: TContext>(&self, _context: &Cx) -> T {
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
|
@ -1478,7 +1498,7 @@ pub mod computed {
|
|||
type ComputedValue = CSSColor;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, _context: &Context) -> CSSColor {
|
||||
fn to_computed_value<Cx: TContext>(&self, _context: &Cx) -> CSSColor {
|
||||
self.parsed
|
||||
}
|
||||
}
|
||||
|
@ -1489,17 +1509,17 @@ pub mod computed {
|
|||
type ComputedValue = Au;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> Au {
|
||||
fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> Au {
|
||||
match *self {
|
||||
specified::Length::Absolute(length) => length,
|
||||
specified::Length::Calc(calc) => calc.to_computed_value(context).length(),
|
||||
specified::Length::FontRelative(length) =>
|
||||
length.to_computed_value(context.style.get_font().font_size,
|
||||
context.style.root_font_size),
|
||||
length.to_computed_value(context.style().get_font().clone_font_size(),
|
||||
context.style().root_font_size()),
|
||||
specified::Length::ViewportPercentage(length) =>
|
||||
length.to_computed_value(context.viewport_size),
|
||||
length.to_computed_value(context.viewport_size()),
|
||||
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 {
|
||||
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;
|
||||
|
||||
if let Some(absolute) = self.absolute {
|
||||
|
@ -1593,13 +1613,13 @@ pub mod computed {
|
|||
for val in &[self.vw, self.vh, self.vmin, self.vmax] {
|
||||
if let Some(val) = *val {
|
||||
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] {
|
||||
if let Some(val) = *val {
|
||||
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;
|
||||
|
||||
#[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 w = s.width.to_computed_value(context);
|
||||
let h = s.height.to_computed_value(context);
|
||||
|
@ -1664,7 +1684,7 @@ pub mod computed {
|
|||
impl ToComputedValue for specified::LengthOrPercentage {
|
||||
type ComputedValue = LengthOrPercentage;
|
||||
|
||||
fn to_computed_value(&self, context: &Context) -> LengthOrPercentage {
|
||||
fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> LengthOrPercentage {
|
||||
match *self {
|
||||
specified::LengthOrPercentage::Length(value) => {
|
||||
LengthOrPercentage::Length(value.to_computed_value(context))
|
||||
|
@ -1712,7 +1732,7 @@ pub mod computed {
|
|||
type ComputedValue = LengthOrPercentageOrAuto;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> LengthOrPercentageOrAuto {
|
||||
fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> LengthOrPercentageOrAuto {
|
||||
match *self {
|
||||
specified::LengthOrPercentageOrAuto::Length(value) => {
|
||||
LengthOrPercentageOrAuto::Length(value.to_computed_value(context))
|
||||
|
@ -1764,7 +1784,7 @@ pub mod computed {
|
|||
type ComputedValue = LengthOrPercentageOrNone;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> LengthOrPercentageOrNone {
|
||||
fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> LengthOrPercentageOrNone {
|
||||
match *self {
|
||||
specified::LengthOrPercentageOrNone::Length(value) => {
|
||||
LengthOrPercentageOrNone::Length(value.to_computed_value(context))
|
||||
|
@ -1812,7 +1832,7 @@ pub mod computed {
|
|||
type ComputedValue = LengthOrNone;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> LengthOrNone {
|
||||
fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> LengthOrNone {
|
||||
match *self {
|
||||
specified::LengthOrNone::Length(specified::Length::Calc(calc)) => {
|
||||
LengthOrNone::Length(calc.to_computed_value(context).length())
|
||||
|
@ -1840,7 +1860,7 @@ pub mod computed {
|
|||
type ComputedValue = Image;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> Image {
|
||||
fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> Image {
|
||||
match *self {
|
||||
specified::Image::Url(ref url) => Image::Url(url.clone()),
|
||||
specified::Image::LinearGradient(ref linear_gradient) => {
|
||||
|
@ -1936,7 +1956,7 @@ pub mod computed {
|
|||
type ComputedValue = LinearGradient;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> LinearGradient {
|
||||
fn to_computed_value<Cx: TContext>(&self, context: &Cx) -> LinearGradient {
|
||||
let specified::LinearGradient {
|
||||
angle_or_corner,
|
||||
ref stops
|
||||
|
|
|
@ -8,7 +8,7 @@ use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser,
|
|||
use euclid::scale_factor::ScaleFactor;
|
||||
use euclid::size::{Size2D, TypedSize2D};
|
||||
use parser::{ParserContext, log_css_error};
|
||||
use properties::INITIAL_VALUES;
|
||||
use properties::{ComputedValues, TComputedValues};
|
||||
use std::ascii::AsciiExt;
|
||||
use std::collections::hash_map::{Entry, HashMap};
|
||||
use std::fmt;
|
||||
|
@ -594,8 +594,8 @@ impl MaybeNew for ViewportConstraints {
|
|||
let context = Context {
|
||||
is_root_element: false,
|
||||
viewport_size: initial_viewport,
|
||||
inherited_style: &*INITIAL_VALUES,
|
||||
style: INITIAL_VALUES.clone(),
|
||||
inherited_style: ComputedValues::initial_values(),
|
||||
style: ComputedValues::initial_values().clone(),
|
||||
};
|
||||
|
||||
// DEVICE-ADAPT § 9.3 Resolving 'extend-to-zoom'
|
||||
|
|
|
@ -25,10 +25,10 @@ extern crate util;
|
|||
|
||||
#[cfg(test)] mod writing_modes {
|
||||
use style::logical_geometry::WritingMode;
|
||||
use style::properties::{INITIAL_VALUES, get_writing_mode};
|
||||
use style::properties::{INITIAL_SERVO_VALUES, TComputedValues, get_writing_mode};
|
||||
|
||||
#[test]
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue