Auto merge of #18359 - emilio:bye-color-mako, r=nox

style: Move color to use predefined_type.

I want to play with autogenerating style structs, and color is the smallest
struct out there.

However, moving it out of the mako file is a requirement. This patch does that.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18359)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-09-04 05:01:39 -05:00 committed by GitHub
commit bcddb19eb8
5 changed files with 125 additions and 120 deletions

View file

@ -8,48 +8,22 @@
<% from data import to_rust_ident %> <% from data import to_rust_ident %>
<%helpers:longhand name="color" need_clone="True" ${helpers.predefined_type(
animation_value_type="AnimatedRGBA" "color",
ignored_when_colors_disabled="True" "ColorPropertyValue",
flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER" "::cssparser::RGBA::new(0, 0, 0, 255)",
spec="https://drafts.csswg.org/css-color/#color"> animation_value_type="AnimatedRGBA",
use cssparser::RGBA; flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER",
use values::specified::{AllowQuirks, Color}; ignored_when_colors_disabled="True",
spec="https://drafts.csswg.org/css-color/#color",
need_clone="True"
)}
impl ToComputedValue for SpecifiedValue { // FIXME(#15973): Add servo support for system colors
type ComputedValue = computed_value::T; //
// FIXME(emilio): Move outside of mako.
#[inline] % if product == "gecko":
fn to_computed_value(&self, context: &Context) -> computed_value::T { pub mod system_colors {
self.0.to_computed_value(context)
.to_rgba(context.builder.get_parent_color().clone_color())
}
#[inline]
fn from_computed_value(computed: &computed_value::T) -> Self {
SpecifiedValue(Color::rgba(*computed).into())
}
}
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, ToCss)]
pub struct SpecifiedValue(pub Color);
pub mod computed_value {
use cssparser;
pub type T = cssparser::RGBA;
}
#[inline]
pub fn get_initial_value() -> computed_value::T {
RGBA::new(0, 0, 0, 255) // black
}
pub fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<SpecifiedValue, ParseError<'i>> {
Color::parse_quirky(context, input, AllowQuirks::Yes).map(SpecifiedValue)
}
// FIXME(#15973): Add servo support for system colors
% if product == "gecko":
<% <%
# These are actually parsed. See nsCSSProps::kColorKTable # These are actually parsed. See nsCSSProps::kColorKTable
system_colors = """activeborder activecaption appworkspace background buttonface system_colors = """activeborder activecaption appworkspace background buttonface
@ -84,10 +58,12 @@
IMESelectedConvertedTextBackground IMESelectedConvertedTextForeground IMESelectedConvertedTextBackground IMESelectedConvertedTextForeground
IMESelectedConvertedTextUnderline SpellCheckerUnderline""".split() IMESelectedConvertedTextUnderline SpellCheckerUnderline""".split()
%> %>
use cssparser::Parser;
use gecko_bindings::bindings::Gecko_GetLookAndFeelSystemColor; use gecko_bindings::bindings::Gecko_GetLookAndFeelSystemColor;
use gecko_bindings::structs::root::mozilla::LookAndFeel_ColorID; use gecko_bindings::structs::root::mozilla::LookAndFeel_ColorID;
use std::fmt; use std::fmt;
use style_traits::ToCss; use style_traits::ToCss;
use values::computed::{Context, ToComputedValue};
pub type SystemColor = LookAndFeel_ColorID; pub type SystemColor = LookAndFeel_ColorID;
@ -105,6 +81,7 @@
impl ToComputedValue for SystemColor { impl ToComputedValue for SystemColor {
type ComputedValue = u32; // nscolor type ComputedValue = u32; // nscolor
#[inline] #[inline]
fn to_computed_value(&self, cx: &Context) -> Self::ComputedValue { fn to_computed_value(&self, cx: &Context) -> Self::ComputedValue {
unsafe { unsafe {
@ -120,7 +97,7 @@
} }
impl SystemColor { impl SystemColor {
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> { pub fn parse<'i, 't>(input: &mut Parser<'i, 't>,) -> Result<Self, ()> {
ascii_case_insensitive_phf_map! { ascii_case_insensitive_phf_map! {
color_name -> SystemColor = { color_name -> SystemColor = {
% for color in system_colors: % for color in system_colors:
@ -129,13 +106,9 @@
} }
} }
let ident = input.expect_ident()?; let ident = input.expect_ident().map_err(|_| ())?;
if let Some(color) = color_name(&ident) { color_name(ident).cloned().ok_or(())
Ok(*color)
} else {
Err(SelectorParseError::UnexpectedIdent(ident.clone()).into())
} }
} }
} }
% endif % endif
</%helpers:longhand>

View file

@ -26,6 +26,9 @@ pub struct Color {
/// Computed value type for the specified RGBAColor. /// Computed value type for the specified RGBAColor.
pub type RGBAColor = RGBA; pub type RGBAColor = RGBA;
/// The computed value of the `color` property.
pub type ColorPropertyValue = RGBA;
impl Color { impl Color {
/// Returns a numeric color representing the given RGBA value. /// Returns a numeric color representing the given RGBA value.
pub fn rgba(rgba: RGBA) -> Color { pub fn rgba(rgba: RGBA) -> Color {

View file

@ -35,7 +35,7 @@ pub use self::background::BackgroundSize;
pub use self::border::{BorderImageSlice, BorderImageWidth, BorderImageSideWidth}; pub use self::border::{BorderImageSlice, BorderImageWidth, BorderImageSideWidth};
pub use self::border::{BorderRadius, BorderCornerRadius}; pub use self::border::{BorderRadius, BorderCornerRadius};
pub use self::box_::VerticalAlign; pub use self::box_::VerticalAlign;
pub use self::color::{Color, RGBAColor}; pub use self::color::{Color, ColorPropertyValue, RGBAColor};
pub use self::effects::{BoxShadow, Filter, SimpleShadow}; pub use self::effects::{BoxShadow, Filter, SimpleShadow};
pub use self::flex::FlexBasis; pub use self::flex::FlexBasis;
pub use self::image::{Gradient, GradientItem, Image, ImageLayer, LineDirection, MozImageRect}; pub use self::image::{Gradient, GradientItem, Image, ImageLayer, LineDirection, MozImageRect};

View file

@ -10,7 +10,7 @@ use gecko_bindings::structs::nscolor;
use itoa; use itoa;
use parser::{ParserContext, Parse}; use parser::{ParserContext, Parse};
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
use properties::longhands::color::SystemColor; use properties::longhands::system_colors::SystemColor;
use std::fmt; use std::fmt;
use std::io::Write; use std::io::Write;
use style_traits::{ToCss, ParseError, StyleParseError, ValueParseError}; use style_traits::{ToCss, ParseError, StyleParseError, ValueParseError};
@ -329,3 +329,32 @@ impl From<Color> for RGBAColor {
RGBAColor(color) RGBAColor(color)
} }
} }
/// Specified value for the "color" property, which resolves the `currentcolor`
/// keyword to the parent color instead of self's color.
#[derive(Clone, Debug, PartialEq, ToCss)]
pub struct ColorPropertyValue(pub Color);
impl ToComputedValue for ColorPropertyValue {
type ComputedValue = RGBA;
#[inline]
fn to_computed_value(&self, context: &Context) -> RGBA {
self.0.to_computed_value(context)
.to_rgba(context.builder.get_parent_color().clone_color())
}
#[inline]
fn from_computed_value(computed: &RGBA) -> Self {
ColorPropertyValue(Color::rgba(*computed).into())
}
}
impl Parse for ColorPropertyValue {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
Color::parse_quirky(context, input, AllowQuirks::Yes).map(ColorPropertyValue)
}
}

View file

@ -32,7 +32,7 @@ pub use self::background::BackgroundSize;
pub use self::border::{BorderCornerRadius, BorderImageSlice, BorderImageWidth}; pub use self::border::{BorderCornerRadius, BorderImageSlice, BorderImageWidth};
pub use self::border::{BorderImageSideWidth, BorderRadius, BorderSideWidth}; pub use self::border::{BorderImageSideWidth, BorderRadius, BorderSideWidth};
pub use self::box_::VerticalAlign; pub use self::box_::VerticalAlign;
pub use self::color::{Color, RGBAColor}; pub use self::color::{Color, ColorPropertyValue, RGBAColor};
pub use self::effects::{BoxShadow, Filter, SimpleShadow}; pub use self::effects::{BoxShadow, Filter, SimpleShadow};
pub use self::flex::FlexBasis; pub use self::flex::FlexBasis;
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]