Add a ton of properties and improve a bunch of serialization.

This commit is contained in:
Josh Matthews 2014-10-01 00:40:55 -04:00
parent 755ebd6528
commit 5ca61c00b9
8 changed files with 923 additions and 63 deletions

View file

@ -9,6 +9,7 @@ use self::UnsignedIntegerAttribute::*;
use self::SimpleColorAttribute::*;
use node::{TElement, TElementAttributes, TNode};
use properties::common_types::specified::CSSColor;
use properties::DeclaredValue::SpecifiedValue;
use properties::PropertyDeclaration::*;
use properties::{CSSFloat, specified};
@ -214,7 +215,7 @@ impl PresentationalHintSynthesis for Stylist {
None => {}
Some(color) => {
matching_rules_list.vec_push(DeclarationBlock::from_declaration(
BackgroundColorDeclaration(SpecifiedValue(Color::RGBA(color)))));
BackgroundColorDeclaration(SpecifiedValue(CSSColor { parsed: Color::RGBA(color), authored: None }))));
*shareable = false
}
}

View file

@ -13,6 +13,7 @@ pub type CSSFloat = f64;
pub mod specified {
use std::ascii::AsciiExt;
use std::f64::consts::PI;
use std::fmt;
use std::fmt::{Formatter, FormatError, Show};
use url::Url;
use cssparser;
@ -20,7 +21,7 @@ pub mod specified {
use cssparser::ast::*;
use parsing_utils::{mod, BufferedIter, ParserIter};
use super::{Au, CSSFloat};
#[deriving(Clone)]
#[deriving(Clone, PartialEq)]
pub struct CSSColor {
pub parsed: cssparser::Color,
pub authored: Option<String>,
@ -30,7 +31,7 @@ pub mod specified {
let parsed = cssparser::Color::parse(component_value);
parsed.map(|parsed| {
let authored = match component_value {
&Ident(ref s) | &QuotedString(ref s) => Some(s.clone()),
&Ident(ref s) => Some(s.clone()),
_ => None,
};
CSSColor {
@ -40,8 +41,8 @@ pub mod specified {
})
}
}
impl Show for CSSColor {
fn fmt(&self, f: &mut Formatter) -> Result<(), FormatError> {
impl fmt::Show for CSSColor {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.authored {
Some(ref s) => write!(f, "{}", s),
None => write!(f, "{}", self.parsed),
@ -50,6 +51,32 @@ pub mod specified {
}
#[deriving(Clone)]
pub struct CSSRGBA {
pub parsed: cssparser::RGBA,
pub authored: Option<String>,
}
impl fmt::Show for CSSRGBA {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.authored {
Some(ref s) => write!(f, "{}", s),
None => write!(f, "{}", self.parsed),
}
}
}
#[deriving(Clone, PartialEq)]
pub struct CSSImage(pub Option<Image>);
impl fmt::Show for CSSImage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &CSSImage(ref url) = self;
match url {
&Some(ref image) => write!(f, "{}", image),
&None => write!(f, "none"),
}
}
}
#[deriving(Clone, PartialEq)]
pub enum Length {
Au(Au), // application units
Em(CSSFloat),
@ -69,8 +96,8 @@ pub mod specified {
// Vmin(CSSFloat),
// Vmax(CSSFloat),
}
impl Show for Length {
fn fmt(&self, f: &mut Formatter) -> Result<(), FormatError> {
impl fmt::Show for Length {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
&Length::Au(length) => write!(f, "{}", length),
&Length::Em(length) => write!(f, "{}em", length),
@ -123,16 +150,16 @@ pub mod specified {
}
}
#[deriving(Clone)]
#[deriving(Clone, PartialEq)]
pub enum LengthOrPercentage {
Length(Length),
Percentage(CSSFloat), // [0 .. 100%] maps to [0.0 .. 1.0]
}
impl Show for LengthOrPercentage {
fn fmt(&self, f: &mut Formatter) -> Result<(), FormatError> {
impl fmt::Show for LengthOrPercentage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&LengthOrPercentage::Length(length) => write!(f, "{}", length),
&LengthOrPercentage::Percentage(percentage) => write!(f, "{}%", percentage),
&LengthOrPercentage::Percentage(percentage) => write!(f, "{}%", percentage * 100.),
}
}
}
@ -167,11 +194,11 @@ pub mod specified {
Percentage(CSSFloat), // [0 .. 100%] maps to [0.0 .. 1.0]
Auto,
}
impl Show for LengthOrPercentageOrAuto {
fn fmt(&self, f: &mut Formatter) -> Result<(), FormatError> {
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::Percentage(percentage) => write!(f, "{}%", percentage * 100.),
&LengthOrPercentageOrAuto::Auto => write!(f, "auto"),
}
}
@ -207,11 +234,11 @@ pub mod specified {
Percentage(CSSFloat), // [0 .. 100%] maps to [0.0 .. 1.0]
None,
}
impl Show for LengthOrPercentageOrNone {
fn fmt(&self, f: &mut Formatter) -> Result<(), FormatError> {
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::Percentage(percentage) => write!(f, "{}%", percentage * 100.),
&LengthOrPercentageOrNone::None => write!(f, "none"),
}
}
@ -319,7 +346,7 @@ pub mod specified {
}
/// Specified values for an image according to CSS-IMAGES.
#[deriving(Clone)]
#[deriving(Clone, PartialEq)]
pub enum Image {
Url(Url),
LinearGradient(LinearGradient),
@ -328,7 +355,7 @@ pub mod specified {
impl Show for Image {
fn fmt(&self, f: &mut Formatter) -> Result<(), FormatError> {
match self {
&Image::Url(ref url) => write!(f, "url({})", url),
&Image::Url(ref url) => write!(f, "url(\"{}\")", url),
&Image::LinearGradient(ref grad) => write!(f, "linear-gradient({})", grad),
}
}
@ -368,7 +395,7 @@ pub mod specified {
}
/// Specified values for a CSS linear gradient.
#[deriving(Clone)]
#[deriving(Clone, PartialEq)]
pub struct LinearGradient {
/// The angle or corner of the gradient.
pub angle_or_corner: AngleOrCorner,
@ -404,7 +431,7 @@ pub mod specified {
}
/// Specified values for one color stop in a linear gradient.
#[deriving(Clone)]
#[deriving(Clone, PartialEq)]
pub struct ColorStop {
/// The color of this stop.
pub color: CSSColor,
@ -661,7 +688,7 @@ pub mod computed {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&LengthOrPercentage::Length(length) => write!(f, "{}", length),
&LengthOrPercentage::Percentage(percentage) => write!(f, "{}%", percentage),
&LengthOrPercentage::Percentage(percentage) => write!(f, "{}%", percentage * 100.),
}
}
}
@ -687,7 +714,7 @@ pub mod computed {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&LengthOrPercentageOrAuto::Length(length) => write!(f, "{}", length),
&LengthOrPercentageOrAuto::Percentage(percentage) => write!(f, "{}%", percentage),
&LengthOrPercentageOrAuto::Percentage(percentage) => write!(f, "{}%", percentage * 100.),
&LengthOrPercentageOrAuto::Auto => write!(f, "auto"),
}
}
@ -715,7 +742,7 @@ pub mod computed {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&LengthOrPercentageOrNone::Length(length) => write!(f, "{}", length),
&LengthOrPercentageOrNone::Percentage(percentage) => write!(f, "{}%", percentage),
&LengthOrPercentageOrNone::Percentage(percentage) => write!(f, "{}%", percentage * 100.),
&LengthOrPercentageOrNone::None => write!(f, "none"),
}
}
@ -743,7 +770,7 @@ pub mod computed {
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::Url(ref url) => write!(f, "url(\"{}\")", url),
&Image::LinearGradient(ref grad) => write!(f, "linear-gradient({})", grad),
}
}

View file

@ -564,14 +564,15 @@ pub mod longhands {
Normal,
Length(specified::Length),
Number(CSSFloat),
// percentage are the same as em.
Percentage(CSSFloat),
}
impl fmt::Show for SpecifiedValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&SpecifiedValue::Normal => write!(f, "normal"),
&SpecifiedValue::Length(length) => write!(f, "{}%", length),
&SpecifiedValue::Length(length) => write!(f, "{}", length),
&SpecifiedValue::Number(number) => write!(f, "{}", number),
&SpecifiedValue::Percentage(number) => write!(f, "{}%", number * 100.),
}
}
}
@ -582,7 +583,7 @@ pub mod longhands {
&ast::Number(ref value) if value.value >= 0. =>
Ok(SpecifiedValue::Number(value.value)),
&ast::Percentage(ref value) if value.value >= 0. =>
Ok(SpecifiedValue::Length(specified::Length::Em(value.value / 100.))),
Ok(SpecifiedValue::Percentage(value.value / 100.)),
&Dimension(ref value, ref unit) if value.value >= 0. =>
specified::Length::parse_dimension(value.value, unit.as_slice())
.map(SpecifiedValue::Length),
@ -619,6 +620,7 @@ pub mod longhands {
SpecifiedValue::Normal => T::Normal,
SpecifiedValue::Length(value) => T::Length(computed::compute_Au(value, context)),
SpecifiedValue::Number(value) => T::Number(value),
SpecifiedValue::Percentage(value) => T::Length(computed::compute_Au(specified::Length::Em(value), context)),
}
}
</%self:single_component_value>
@ -731,7 +733,7 @@ pub mod longhands {
impl fmt::Show for ContentItem {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&ContentItem::StringContent(ref s) => write!(f, "{}", s),
&ContentItem::StringContent(ref s) => write!(f, "\"{}\"", s),
}
}
}
@ -749,9 +751,9 @@ pub mod longhands {
&T::none => write!(f, "none"),
&T::Content(ref content) => {
for c in content.iter() {
let _ = write!(f, "{} ", c);
let _ = write!(f, "{}", c);
}
Ok(())
Ok(())
}
}
}
@ -833,13 +835,15 @@ pub mod longhands {
<%self:single_component_value name="background-image">
use super::common_types::specified as common_specified;
use super::super::common_types::specified::CSSImage as CSSImage;
pub mod computed_value {
use super::super::super::common_types::computed;
#[deriving(Clone, PartialEq)]
pub type T = Option<computed::Image>;
//#[deriving(Clone, PartialEq)]
//pub type T = super::SpecifiedValue;
}
#[deriving(Clone)]
pub type SpecifiedValue = Option<common_specified::Image>;
pub type SpecifiedValue = common_specified::CSSImage;
#[inline]
pub fn get_initial_value() -> computed_value::T {
None
@ -848,13 +852,13 @@ pub mod longhands {
-> Result<SpecifiedValue, ()> {
match component_value {
&ast::Ident(ref value) if value.as_slice().eq_ignore_ascii_case("none") => {
Ok(None)
Ok(CSSImage(None))
}
_ => {
match common_specified::Image::from_component_value(component_value,
base_url) {
Err(err) => Err(err),
Ok(result) => Ok(Some(result)),
Ok(result) => Ok(CSSImage(Some(result))),
}
}
}
@ -862,8 +866,8 @@ pub mod longhands {
pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> computed_value::T {
match value {
None => None,
Some(image) => Some(image.to_computed_value(context)),
CSSImage(None) => None,
CSSImage(Some(image)) => Some(image.to_computed_value(context)),
}
}
</%self:single_component_value>
@ -882,9 +886,7 @@ pub mod longhands {
}
impl fmt::Show for T {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let _ = write!(f, "{}", self.horizontal);
let _ = write!(f, "{}", self.vertical);
Ok(())
write!(f, "{} {}", self.horizontal, self.vertical)
}
}
}
@ -896,9 +898,7 @@ pub mod longhands {
}
impl fmt::Show for SpecifiedValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let _ = write!(f, "{}", self.horizontal);
let _ = write!(f, "{}", self.vertical);
Ok(())
write!(f, "{} {}", self.horizontal, self.vertical)
}
}
@ -1004,19 +1004,32 @@ pub mod longhands {
${new_style_struct("Color", is_inherited=True)}
<%self:raw_longhand name="color">
pub use super::computed_as_specified as to_computed_value;
pub type SpecifiedValue = RGBA;
use super::super::common_types::specified::{CSSColor, CSSRGBA};
#[inline]
pub fn to_computed_value(value: SpecifiedValue, _context: &computed::Context)
-> computed_value::T {
value.parsed
}
pub type SpecifiedValue = CSSRGBA;
pub mod computed_value {
pub type T = super::SpecifiedValue;
use cssparser;
pub type T = cssparser::RGBA;
}
#[inline] pub fn get_initial_value() -> computed_value::T {
RGBA { red: 0., green: 0., blue: 0., alpha: 1. } /* black */
}
pub fn parse_specified(input: &[ComponentValue], _base_url: &Url)
-> Result<DeclaredValue<SpecifiedValue>, ()> {
match one_component_value(input).and_then(Color::parse) {
Ok(Color::RGBA(rgba)) => Ok(DeclaredValue::SpecifiedValue(rgba)),
Ok(Color::CurrentColor) => Ok(DeclaredValue::Inherit),
match one_component_value(input).and_then(CSSColor::parse) {
Ok(CSSColor { parsed: Color::RGBA(rgba), authored }) => {
let rgba = CSSRGBA {
parsed: rgba,
authored: authored,
};
Ok(DeclaredValue::SpecifiedValue(rgba))
}
Ok(CSSColor { parsed: Color::CurrentColor, .. }) => Ok(DeclaredValue::Inherit),
Err(()) => Err(()),
}
}
@ -1058,7 +1071,7 @@ pub mod longhands {
/*impl fmt::Show for T {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for font in self.iter() {
write!(f, "{} ", font);
write!(f, "{}", font);
}
Ok(())
}
@ -1344,14 +1357,23 @@ pub mod longhands {
}
impl fmt::Show for SpecifiedValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut space = false;
if self.underline {
let _ = write!(f, "underline ");
let _ = write!(f, "underline");
space = true;
}
if self.overline {
let _ = write!(f, "overline ");
if space {
let _ = write!(f, " ");
}
let _ = write!(f, "overline");
space = true;
}
if self.line_through {
let _ = write!(f, "line-through ");
if space {
let _ = write!(f, " ");
}
let _ = write!(f, "line-through");
}
Ok(())
}
@ -1753,7 +1775,7 @@ pub mod longhands {
offset_y: computed::compute_Au(value.offset_y, context),
blur_radius: computed::compute_Au(value.blur_radius, context),
spread_radius: computed::compute_Au(value.spread_radius, context),
color: value.color.unwrap_or(cssparser::Color::CurrentColor),
color: value.color.map(|color| color.parsed).unwrap_or(cssparser::Color::CurrentColor),
inset: value.inset,
}
}).collect()
@ -1809,8 +1831,8 @@ pub mod longhands {
// Try to parse a color.
match specified::CSSColor::parse(value) {
Ok(the_color) if color.is_none() => {
color = Some(the_color);
Ok(ref the_color) if color.is_none() => {
color = Some(the_color.clone());
continue
}
Ok(_) => return Err(()),
@ -3013,7 +3035,13 @@ pub fn cascade(applicable_declarations: &[DeclarationBlock],
}
}
PropertyDeclaration::ColorDeclaration(ref value) => {
context.color = get_specified!(get_color, color, value);
context.color = match *value {
DeclaredValue::SpecifiedValue(ref specified_value) => specified_value.parsed,
DeclaredValue::Initial => longhands::color::get_initial_value(),
DeclaredValue::Inherit => inherited_style.get_color().color.clone(),
};
//get_specified!(get_color, color, value);
}
PropertyDeclaration::DisplayDeclaration(ref value) => {
context.display = get_specified!(get_box, display, value);