Skip to content

Commit

Permalink
Implement drop-shadow CSS parser
Browse files Browse the repository at this point in the history
  • Loading branch information
shinglyu committed Nov 22, 2016
1 parent 3086700 commit 5248c58
Showing 1 changed file with 74 additions and 3 deletions.
77 changes: 74 additions & 3 deletions components/style/properties/longhand/effects.mako.rs
Expand Up @@ -402,10 +402,11 @@ ${helpers.predefined_type("opacity",
// FIXME: This prop should be animatable
<%helpers:longhand name="filter" animatable="False">
//pub use self::computed_value::T as SpecifiedValue;
use cssparser;
use std::fmt;
use style_traits::ToCss;
use style_traits::{self, ToCss};
use values::{CSSFloat, HasViewportPercentage};
use values::specified::{Angle, Length};
use values::specified::{Angle, CSSColor, Length};

impl HasViewportPercentage for SpecifiedValue {
fn has_viewport_percentage(&self) -> bool {
Expand All @@ -416,7 +417,7 @@ ${helpers.predefined_type("opacity",

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct SpecifiedValue(Vec<SpecifiedFilter>);
pub struct SpecifiedValue(pub Vec<SpecifiedFilter>);

impl HasViewportPercentage for SpecifiedFilter {
fn has_viewport_percentage(&self) -> bool {
Expand All @@ -440,11 +441,15 @@ ${helpers.predefined_type("opacity",
Opacity(CSSFloat),
Saturate(CSSFloat),
Sepia(CSSFloat),
% if product == "gecko":
DropShadow(Length, Length, Length, Option<CSSColor>),
% endif
}

pub mod computed_value {
use app_units::Au;
use values::CSSFloat;
use values::computed::CSSColor;
use values::specified::{Angle};

#[derive(Clone, PartialEq, Debug)]
Expand All @@ -459,6 +464,9 @@ ${helpers.predefined_type("opacity",
Opacity(CSSFloat),
Saturate(CSSFloat),
Sepia(CSSFloat),
% if product == "gecko":
DropShadow(Au, Au, Au, CSSColor),
% endif
}

#[derive(Clone, PartialEq, Debug)]
Expand Down Expand Up @@ -556,6 +564,19 @@ ${helpers.predefined_type("opacity",
computed_value::Filter::Opacity(value) => try!(write!(dest, "opacity({})", value)),
computed_value::Filter::Saturate(value) => try!(write!(dest, "saturate({})", value)),
computed_value::Filter::Sepia(value) => try!(write!(dest, "sepia({})", value)),
% if product == "gecko":
computed_value::Filter::DropShadow(offset_x, offset_y, blur_radius, ref color) => {
try!(dest.write_str("drop-shadow("));
try!(offset_x.to_css(dest));
try!(dest.write_str(", "));
try!(offset_y.to_css(dest));
try!(dest.write_str(", "));
try!(blur_radius.to_css(dest));
try!(dest.write_str(", "));
try!(color.to_css(dest));
try!(dest.write_str(")"));
}
% endif
}
Ok(())
}
Expand All @@ -581,6 +602,21 @@ ${helpers.predefined_type("opacity",
SpecifiedFilter::Opacity(value) => try!(write!(dest, "opacity({})", value)),
SpecifiedFilter::Saturate(value) => try!(write!(dest, "saturate({})", value)),
SpecifiedFilter::Sepia(value) => try!(write!(dest, "sepia({})", value)),
% if product == "gecko":
SpecifiedFilter::DropShadow(offset_x, offset_y, blur_radius, ref color) => {
try!(dest.write_str("drop-shadow("));
try!(offset_x.to_css(dest));
try!(dest.write_str(", "));
try!(offset_y.to_css(dest));
try!(dest.write_str(", "));
try!(blur_radius.to_css(dest));
if let &Some(ref color) = color {
try!(dest.write_str(", "));
try!(color.to_css(dest));
}
try!(dest.write_str(")"));
}
% endif
}
Ok(())
}
Expand Down Expand Up @@ -609,6 +645,9 @@ ${helpers.predefined_type("opacity",
"opacity" => parse_factor(input).map(SpecifiedFilter::Opacity),
"saturate" => parse_factor(input).map(SpecifiedFilter::Saturate),
"sepia" => parse_factor(input).map(SpecifiedFilter::Sepia),
% if product == "gecko":
"drop-shadow" => parse_drop_shadow(input),
% endif
_ => Err(())
}
})));
Expand All @@ -629,6 +668,16 @@ ${helpers.predefined_type("opacity",
}
}

% if product == "gecko":
fn parse_drop_shadow(input: &mut Parser) -> Result<SpecifiedFilter, ()> {
let offset_x = try!(specified::Length::parse(input));
let offset_y = try!(specified::Length::parse(input));
let blur_radius = input.try(specified::Length::parse).unwrap_or(specified::Length::from_px(0.0));
let color = input.try(specified::CSSColor::parse).ok();
Ok(SpecifiedFilter::DropShadow(offset_x, offset_y, blur_radius, color))
}
% endif

impl ToComputedValue for SpecifiedValue {
type ComputedValue = computed_value::T;

Expand All @@ -645,6 +694,18 @@ ${helpers.predefined_type("opacity",
SpecifiedFilter::Opacity(factor) => computed_value::Filter::Opacity(factor),
SpecifiedFilter::Saturate(factor) => computed_value::Filter::Saturate(factor),
SpecifiedFilter::Sepia(factor) => computed_value::Filter::Sepia(factor),
% if product == "gecko":
SpecifiedFilter::DropShadow(offset_x, offset_y, blur_radius, ref color) => {
computed_value::Filter::DropShadow(
offset_x.to_computed_value(context),
offset_y.to_computed_value(context),
blur_radius.to_computed_value(context),
color.as_ref()
.map(|color| color.parsed)
.unwrap_or(cssparser::Color::CurrentColor),
)
}
% endif
}
}).collect() }
}
Expand All @@ -662,6 +723,16 @@ ${helpers.predefined_type("opacity",
computed_value::Filter::Opacity(factor) => SpecifiedFilter::Opacity(factor),
computed_value::Filter::Saturate(factor) => SpecifiedFilter::Saturate(factor),
computed_value::Filter::Sepia(factor) => SpecifiedFilter::Sepia(factor),
% if product == "gecko":
computed_value::Filter::DropShadow(offset_x, offset_y, blur_radius, color) => {
SpecifiedFilter::DropShadow(
ToComputedValue::from_computed_value(&offset_x),
ToComputedValue::from_computed_value(&offset_y),
ToComputedValue::from_computed_value(&blur_radius),
Some(ToComputedValue::from_computed_value(&color)),
)
}
% endif
}
}).collect())
}
Expand Down

0 comments on commit 5248c58

Please sign in to comment.