Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive more Parse implementations (fixes #19827) #19903

Merged
merged 7 commits into from Feb 1, 2018
@@ -67,13 +67,17 @@ impl ToCss for UrlSource {
/// A font-display value for a @font-face rule.
/// The font-display descriptor determines how a font face is displayed based
/// on whether and when it is downloaded and ready to use.
define_css_keyword_enum!(FontDisplay:
"auto" => Auto,
"block" => Block,
"swap" => Swap,
"fallback" => Fallback,
"optional" => Optional);
add_impls_for_keyword_enum!(FontDisplay);
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq)]
#[derive(ToComputedValue, ToCss)]
pub enum FontDisplay {
Auto,
Block,
Swap,
Fallback,
Optional,
}

/// A font-weight value for a @font-face rule.
/// The font-weight CSS property specifies the weight or boldness of the font.
@@ -707,9 +707,9 @@ pub mod basic_shape {
}
StyleBasicShapeType::Polygon => {
let fill_rule = if other.mFillRule == StyleFillRule::Evenodd {
FillRule::EvenOdd
FillRule::Evenodd
} else {
FillRule::NonZero
FillRule::Nonzero
};
let mut coords = Vec::with_capacity(other.mCoordinates.len() / 2);
for i in 0..(other.mCoordinates.len() / 2) {
@@ -348,10 +348,10 @@ impl GeckoStyleCoordConvertible for ExtremumLength {
use gecko_bindings::structs::{NS_STYLE_WIDTH_MAX_CONTENT, NS_STYLE_WIDTH_MIN_CONTENT};
coord.set_value(CoordDataValue::Enumerated(
match *self {
ExtremumLength::MaxContent => NS_STYLE_WIDTH_MAX_CONTENT,
ExtremumLength::MinContent => NS_STYLE_WIDTH_MIN_CONTENT,
ExtremumLength::FitContent => NS_STYLE_WIDTH_FIT_CONTENT,
ExtremumLength::FillAvailable => NS_STYLE_WIDTH_AVAILABLE,
ExtremumLength::MozMaxContent => NS_STYLE_WIDTH_MAX_CONTENT,
ExtremumLength::MozMinContent => NS_STYLE_WIDTH_MIN_CONTENT,
ExtremumLength::MozFitContent => NS_STYLE_WIDTH_FIT_CONTENT,
ExtremumLength::MozAvailable => NS_STYLE_WIDTH_AVAILABLE,
}
))
}
@@ -361,12 +361,12 @@ impl GeckoStyleCoordConvertible for ExtremumLength {
use gecko_bindings::structs::{NS_STYLE_WIDTH_MAX_CONTENT, NS_STYLE_WIDTH_MIN_CONTENT};
match coord.as_value() {
CoordDataValue::Enumerated(NS_STYLE_WIDTH_MAX_CONTENT) =>
Some(ExtremumLength::MaxContent),
Some(ExtremumLength::MozMaxContent),
CoordDataValue::Enumerated(NS_STYLE_WIDTH_MIN_CONTENT) =>
Some(ExtremumLength::MinContent),
Some(ExtremumLength::MozMinContent),
CoordDataValue::Enumerated(NS_STYLE_WIDTH_FIT_CONTENT) =>
Some(ExtremumLength::FitContent),
CoordDataValue::Enumerated(NS_STYLE_WIDTH_AVAILABLE) => Some(ExtremumLength::FillAvailable),
Some(ExtremumLength::MozFitContent),
CoordDataValue::Enumerated(NS_STYLE_WIDTH_AVAILABLE) => Some(ExtremumLength::MozAvailable),
_ => None,
}
}
@@ -25,8 +25,6 @@

#![deny(missing_docs)]

#![recursion_limit = "500"] // For define_css_keyword_enum! in -moz-appearance

extern crate app_units;
extern crate arrayvec;
extern crate atomic_refcell;
@@ -72,7 +70,6 @@ extern crate smallbitvec;
extern crate smallvec;
#[macro_use]
extern crate style_derive;
#[macro_use]
extern crate style_traits;
extern crate time;
extern crate uluru;
@@ -65,67 +65,6 @@ macro_rules! try_match_ident_ignore_ascii_case {
}}
}

macro_rules! define_numbered_css_keyword_enum {
($name: ident: $( $css: expr => $variant: ident = $value: expr ),+,) => {
define_numbered_css_keyword_enum!($name: $( $css => $variant = $value ),+);
};
($name: ident: $( $css: expr => $variant: ident = $value: expr ),+) => {
#[allow(non_camel_case_types, missing_docs)]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
pub enum $name {
$( $variant = $value ),+
}

impl $crate::parser::Parse for $name {
fn parse<'i, 't>(
_context: &$crate::parser::ParserContext,
input: &mut ::cssparser::Parser<'i, 't>,
) -> Result<$name, ::style_traits::ParseError<'i>> {
try_match_ident_ignore_ascii_case! { input,
$( $css => Ok($name::$variant), )+
}
}
}

impl ::style_traits::ToCss for $name {
fn to_css<W>(
&self,
dest: &mut ::style_traits::CssWriter<W>,
) -> ::std::fmt::Result
where
W: ::std::fmt::Write,
{
match *self {
$( $name::$variant => dest.write_str($css) ),+
}
}
}
}
}

/// A macro for implementing `ToComputedValue`, and `Parse` traits for
/// the enums defined using `define_css_keyword_enum` macro.
///
/// NOTE: We should either move `Parse` trait to `style_traits`
/// or `define_css_keyword_enum` macro to this crate, but that
/// may involve significant cleanup in both the crates.
macro_rules! add_impls_for_keyword_enum {
($name:ident) => {
impl $crate::parser::Parse for $name {
#[inline]
fn parse<'i, 't>(
_context: &$crate::parser::ParserContext,
input: &mut ::cssparser::Parser<'i, 't>,
) -> Result<Self, ::style_traits::ParseError<'i>> {
$name::parse(input)
}
}

trivial_to_computed_value!($name);
};
}

macro_rules! define_keyword_type {
($name: ident, $css: expr) => {
#[allow(missing_docs)]
@@ -62,6 +62,7 @@ use values::computed::{NonNegativeLength, ToComputedValue, Percentage};
use values::computed::font::{FontSize, SingleFontFamily};
use values::computed::effects::{BoxShadow, Filter, SimpleShadow};
use values::computed::outline::OutlineStyle;
use values::generics::transform::TransformStyle;
use computed_values::border_style;

pub mod style_structs {
@@ -3345,6 +3346,25 @@ fn static_assert() {
self.copy_transition_property_from(other)
}

// Hand-written because the Mako helpers transform `Preserve3d` into `PRESERVE3D`.
pub fn set_transform_style(&mut self, v: TransformStyle) {
self.gecko.mTransformStyle = match v {
TransformStyle::Flat => structs::NS_STYLE_TRANSFORM_STYLE_FLAT as u8,
TransformStyle::Preserve3d => structs::NS_STYLE_TRANSFORM_STYLE_PRESERVE_3D as u8,
};
}

// Hand-written because the Mako helpers transform `Preserve3d` into `PRESERVE3D`.
pub fn clone_transform_style(&self) -> TransformStyle {
match self.gecko.mTransformStyle as u32 {
structs::NS_STYLE_TRANSFORM_STYLE_FLAT => TransformStyle::Flat,
structs::NS_STYLE_TRANSFORM_STYLE_PRESERVE_3D => TransformStyle::Preserve3d,
_ => panic!("illegal transform style"),
}
}

${impl_simple_copy('transform_style', 'mTransformStyle')}

${impl_transition_count('property', 'Property')}

pub fn animations_equals(&self, other: &Self) -> bool {
@@ -5081,7 +5101,7 @@ fn static_assert() {
coord.0.to_gecko_style_coord(&mut shape.mCoordinates[2 * i]);
coord.1.to_gecko_style_coord(&mut shape.mCoordinates[2 * i + 1]);
}
shape.mFillRule = if poly.fill == FillRule::EvenOdd {
shape.mFillRule = if poly.fill == FillRule::Evenodd {
StyleFillRule::Evenodd
} else {
StyleFillRule::Nonzero
@@ -424,20 +424,13 @@
use properties::longhands::system_font::SystemFont;

pub mod computed_value {
use cssparser::Parser;
use parser::{Parse, ParserContext};

use style_traits::ParseError;
define_css_keyword_enum! { T:
% for value in keyword.values_for(product):
"${value}" => ${to_camel_case(value)},
% endfor
}

impl Parse for T {
fn parse<'i, 't>(_: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
T::parse(input)
}
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, Hash, MallocSizeOf, Parse)]
#[derive(PartialEq, ToCss)]
pub enum T {
% for value in keyword.values_for(product):
${to_camel_case(value)},
% endfor
}

${gecko_keyword_conversion(keyword, keyword.values_for(product), type="T", cast_to="i32")}
@@ -604,26 +597,32 @@

<%def name="inner_body(keyword, extra_specified=None, needs_conversion=False)">
% if extra_specified or keyword.aliases_for(product):
define_css_keyword_enum! { SpecifiedValue:
values {
% for value in keyword.values_for(product) + (extra_specified or "").split():
"${value}" => ${to_camel_case(value)},
% endfor
}
aliases {
% for alias, value in keyword.aliases_for(product).iteritems():
"${alias}" => ${to_camel_case(value)},
% endfor
}
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, ToCss)]
pub enum SpecifiedValue {
% for value in keyword.values_for(product) + (extra_specified or "").split():
<%
aliases = []
for alias, v in keyword.aliases_for(product).iteritems():
if value == v:
aliases.append(alias)
%>
% if aliases:
#[css(aliases = "${','.join(aliases)}")]
% endif
${to_camel_case(value)},
% endfor
}
% else:
pub use self::computed_value::T as SpecifiedValue;
% endif
pub mod computed_value {
define_css_keyword_enum! { T:
% for value in data.longhands_by_name[name].keyword.values_for(product):
"${value}" => ${to_camel_case(value)},
% endfor
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, ToCss)]
pub enum T {
% for value in data.longhands_by_name[name].keyword.values_for(product):
${to_camel_case(value)},
% endfor
}
}
#[inline]
@@ -639,13 +638,6 @@
-> Result<SpecifiedValue, ParseError<'i>> {
SpecifiedValue::parse(input)
}
impl Parse for SpecifiedValue {
#[inline]
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<SpecifiedValue, ParseError<'i>> {
SpecifiedValue::parse(input)
}
}

% if needs_conversion:
<%
@@ -32,13 +32,16 @@
ignored_when_colors_disabled=True,
)}

${helpers.predefined_type("border-%s-style" % side_name, "BorderStyle",
"specified::BorderStyle::None",
alias=maybe_moz_logical_alias(product, side, "-moz-border-%s-style"),
spec=maybe_logical_spec(side, "style"),
flags="APPLIES_TO_FIRST_LETTER",
animation_value_type="discrete" if not is_logical else "none",
logical=is_logical)}
${helpers.predefined_type(
"border-%s-style" % side_name, "BorderStyle",
"specified::BorderStyle::None",
alias=maybe_moz_logical_alias(product, side, "-moz-border-%s-style"),
spec=maybe_logical_spec(side, "style"),
flags="APPLIES_TO_FIRST_LETTER",
animation_value_type="discrete" if not is_logical else "none",
logical=is_logical,
needs_context=False,
)}

${helpers.predefined_type("border-%s-width" % side_name,
"BorderSideWidth",
@@ -112,11 +115,14 @@ ${helpers.predefined_type("border-image-outset", "LengthOrNumberRect",
pub struct SpecifiedValue(pub RepeatKeyword,
pub Option<RepeatKeyword>);

define_css_keyword_enum!(RepeatKeyword:
"stretch" => Stretch,
"repeat" => Repeat,
"round" => Round,
"space" => Space);
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, ToCss)]
pub enum RepeatKeyword {
Stretch,
Repeat,
Round,
Space,
}

#[inline]
pub fn get_initial_value() -> computed_value::T {
@@ -526,14 +526,16 @@ ${helpers.single_keyword("transform-box",
gecko_inexhaustive="True",
animation_value_type="discrete")}

// `auto` keyword is not supported in gecko yet.
${helpers.single_keyword("transform-style",
"auto flat preserve-3d" if product == "servo" else
"flat preserve-3d",
spec="https://drafts.csswg.org/css-transforms/#transform-style-property",
extra_prefixes="moz webkit",
flags="CREATES_STACKING_CONTEXT FIXPOS_CB",
animation_value_type="discrete")}
${helpers.predefined_type(
"transform-style",
"TransformStyle",
"computed::TransformStyle::" + ("Auto" if product == "servo" else "Flat"),

This comment has been minimized.

spec="https://drafts.csswg.org/css-transforms-2/#transform-style-property",
needs_context=False,
extra_prefixes="moz webkit",
flags="CREATES_STACKING_CONTEXT FIXPOS_CB",
animation_value_type="discrete",
)}

${helpers.predefined_type("transform-origin",
"TransformOrigin",
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.