Implement setting element style properties. Improve serialization to return initial values when unspecified.

This commit is contained in:
Josh Matthews 2014-09-20 20:37:05 -04:00
parent 505e1855a3
commit 6f8a9b6d46
6 changed files with 218 additions and 20 deletions

View file

@ -43,6 +43,7 @@ pub use selector_matching::{matches, matches_simple_selector, common_style_affec
pub use selector_matching::{rare_style_affecting_attributes};
pub use selector_matching::{RECOMMENDED_SELECTOR_BLOOM_FILTER_SIZE, SELECTOR_WHITESPACE};
pub use properties::{cascade, cascade_anonymous, computed, longhands_from_shorthand};
pub use properties::is_supported_property;
pub use properties::{PropertyDeclaration, ComputedValues, computed_values, style_structs};
pub use properties::{PropertyDeclarationBlock, parse_style_attribute}; // Style attributes
pub use properties::{CSSFloat, DeclaredValue, PropertyDeclarationParseResult};

View file

@ -650,6 +650,15 @@ pub mod computed {
Percentage(CSSFloat),
Auto,
}
impl fmt::Show for LengthOrPercentageOrAuto {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&LengthOrPercentageOrAuto::Length(length) => write!(f, "{}", length),
&LengthOrPercentageOrAuto::Percentage(percentage) => write!(f, "{}%", percentage),
&LengthOrPercentageOrAuto::Auto => write!(f, "auto"),
}
}
}
#[allow(non_snake_case)]
pub fn compute_LengthOrPercentageOrAuto(value: specified::LengthOrPercentageOrAuto,
context: &Context) -> LengthOrPercentageOrAuto {
@ -669,6 +678,15 @@ pub mod computed {
Percentage(CSSFloat),
None,
}
impl fmt::Show for LengthOrPercentageOrNone {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&LengthOrPercentageOrNone::Length(length) => write!(f, "{}", length),
&LengthOrPercentageOrNone::Percentage(percentage) => write!(f, "{}%", percentage),
&LengthOrPercentageOrNone::None => write!(f, "none"),
}
}
}
#[allow(non_snake_case)]
pub fn compute_LengthOrPercentageOrNone(value: specified::LengthOrPercentageOrNone,
context: &Context) -> LengthOrPercentageOrNone {
@ -689,6 +707,15 @@ pub mod computed {
LinearGradient(LinearGradient),
}
impl fmt::Show for Image {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Image::Url(ref url) => write!(f, "url({})", url),
&Image::LinearGradient(ref grad) => write!(f, "linear-gradient({})", grad),
}
}
}
/// Computed values for a CSS linear gradient.
#[deriving(Clone, PartialEq)]
pub struct LinearGradient {
@ -699,6 +726,16 @@ pub mod computed {
pub stops: Vec<ColorStop>,
}
impl fmt::Show for LinearGradient {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let _ = write!(f, "{}", self.angle_or_corner);
for stop in self.stops.iter() {
let _ = write!(f, ", {}", stop);
}
Ok(())
}
}
/// Computed values for one color stop in a linear gradient.
#[deriving(Clone, PartialEq)]
pub struct ColorStop {
@ -710,6 +747,16 @@ pub mod computed {
pub position: Option<LengthOrPercentage>,
}
impl fmt::Show for ColorStop {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let _ = write!(f, "{}", self.color);
self.position.map(|pos| {
let _ = write!(f, " {}", pos);
});
Ok(())
}
}
impl LinearGradient {
pub fn compute(value: specified::LinearGradient, context: &Context) -> LinearGradient {
let specified::LinearGradient {

View file

@ -593,12 +593,22 @@ pub mod longhands {
}
pub mod computed_value {
use super::super::{Au, CSSFloat};
use std::fmt;
#[deriving(PartialEq, Clone)]
pub enum T {
Normal,
Length(Au),
Number(CSSFloat),
}
impl fmt::Show for T {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&T::Normal => write!(f, "normal"),
&T::Length(length) => write!(f, "{}%", length),
&T::Number(number) => write!(f, "{}", number),
}
}
}
}
#[inline]
pub fn get_initial_value() -> computed_value::T { T::Normal }
@ -656,6 +666,7 @@ pub mod longhands {
}
pub mod computed_value {
use super::super::{Au, CSSFloat};
use std::fmt;
#[allow(non_camel_case_types)]
#[deriving(PartialEq, Clone)]
pub enum T {
@ -665,6 +676,17 @@ pub mod longhands {
Length(Au),
Percentage(CSSFloat),
}
impl fmt::Show for T {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
% for keyword in vertical_align_keywords:
&T::${to_rust_ident(keyword)} => write!(f, "${keyword}"),
% endfor
&T::Length(length) => write!(f, "{}", length),
&T::Percentage(number) => write!(f, "{}%", number),
}
}
}
}
#[inline]
pub fn get_initial_value() -> computed_value::T { T::baseline }
@ -1143,12 +1165,22 @@ pub mod longhands {
}
}
pub mod computed_value {
use std::fmt;
#[deriving(PartialEq, Clone)]
pub enum T {
% for weight in range(100, 901, 100):
Weight${weight},
% endfor
}
impl fmt::Show for T {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
% for weight in range(100, 901, 100):
&T::Weight${weight} => write!(f, "{}", ${weight}i),
% endfor
}
}
}
impl T {
pub fn is_bold(self) -> bool {
match self {
@ -1672,6 +1704,7 @@ pub mod longhands {
pub mod computed_value {
use super::super::Au;
use super::super::super::computed;
use std::fmt;
pub type T = Vec<BoxShadow>;
@ -1684,6 +1717,17 @@ pub mod longhands {
pub color: computed::CSSColor,
pub inset: bool,
}
impl fmt::Show for BoxShadow {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.inset {
let _ = write!(f, "inset ");
}
let _ = write!(f, "{} {} {} {} {}", self.offset_x, self.offset_y,
self.blur_radius, self.spread_radius, self.color);
Ok(())
}
}
}
#[inline]
@ -2456,7 +2500,8 @@ impl<T: Show> DeclaredValue<T> {
pub fn specified_value(&self) -> Option<String> {
match self {
&DeclaredValue::SpecifiedValue(ref inner) => Some(format!("{}", inner)),
_ => None,
&DeclaredValue::Initial => None,
&DeclaredValue::Inherit => Some("inherit".to_string()),
}
}
}
@ -2488,14 +2533,16 @@ impl PropertyDeclaration {
}
}
pub fn value(&self) -> Option<String> {
pub fn value(&self) -> String {
match self {
% for property in LONGHANDS:
% if property.derived_from is None:
&PropertyDeclaration::${property.camel_case}Declaration(ref value) => value.specified_value(),
&PropertyDeclaration::${property.camel_case}Declaration(ref value) =>
value.specified_value()
.unwrap_or_else(|| format!("{}", longhands::${property.ident}::get_initial_value())),
% endif
% endfor
_ => None,
decl => panic!("unsupported property declaration: {}", decl.name()),
}
}
@ -3144,6 +3191,18 @@ pub fn cascade_anonymous(parent_style: &ComputedValues) -> ComputedValues {
result
}
pub fn is_supported_property(property: &str) -> bool {
match property {
% for property in SHORTHANDS:
"${property.name}" => true,
% endfor
% for property in LONGHANDS:
"${property.name}" => true,
% endfor
_ => false,
}
}
pub fn longhands_from_shorthand(shorthand: &str) -> Option<Vec<String>> {
match shorthand {
% for property in SHORTHANDS: