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

Kill define_numbered_css_keyword_enum

  • Loading branch information
nox committed Feb 1, 2018
commit bac8781cc782978a68443f20726e762f50a7eeeb
@@ -65,45 +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.
///
@@ -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",
@@ -9,9 +9,13 @@ ${helpers.four_sides_shorthand("border-color", "border-%s-color", "specified::Co
spec="https://drafts.csswg.org/css-backgrounds/#border-color",
allow_quirks=True)}

${helpers.four_sides_shorthand("border-style", "border-%s-style",
"specified::BorderStyle::parse",
spec="https://drafts.csswg.org/css-backgrounds/#border-style")}
${helpers.four_sides_shorthand(
"border-style",
"border-%s-style",
"specified::BorderStyle::parse",
needs_context=False,
spec="https://drafts.csswg.org/css-backgrounds/#border-style",
)}

<%helpers:shorthand name="border-width" sub_properties="${
' '.join('border-%s-width' % side
@@ -63,7 +67,7 @@ pub fn parse_border<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
}
}
if style.is_none() {
if let Ok(value) = input.try(|i| BorderStyle::parse(context, i)) {
if let Ok(value) = input.try(BorderStyle::parse) {
style = Some(value);
any = true;
continue
@@ -161,20 +161,23 @@ fn parse_number_with_clamping_mode<'i, 't>(
// 17.6.2.1. Higher values override lower values.
//
// FIXME(emilio): Should move to border.rs
define_numbered_css_keyword_enum! { BorderStyle:
"none" => None = -1,
"solid" => Solid = 6,
"double" => Double = 7,
"dotted" => Dotted = 4,
"dashed" => Dashed = 5,
"hidden" => Hidden = -2,
"groove" => Groove = 1,
"ridge" => Ridge = 3,
"inset" => Inset = 0,
"outset" => Outset = 2,
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Ord, Parse, PartialEq)]
#[derive(PartialOrd, ToCss)]
pub enum BorderStyle {
None = -1,
Solid = 6,
Double = 7,
Dotted = 4,
Dashed = 5,
Hidden = -2,
Groove = 1,
Ridge = 3,
Inset = 0,
Outset = 2,
}


impl BorderStyle {
/// Whether this border style is either none or hidden.
pub fn none_or_hidden(&self) -> bool {
@@ -39,10 +39,10 @@ impl OutlineStyle {

impl Parse for OutlineStyle {
fn parse<'i, 't>(
context: &ParserContext,
_context: &ParserContext,
input: &mut Parser<'i, 't>
) -> Result<OutlineStyle, ParseError<'i>> {
if let Ok(border_style) = input.try(|i| BorderStyle::parse(context, i)) {
if let Ok(border_style) = input.try(BorderStyle::parse) {
if let BorderStyle::Hidden = border_style {
return Err(input.new_custom_error(SelectorParseErrorKind::UnexpectedIdent("hidden".into())));
}
@@ -168,16 +168,16 @@ fn border_image_outset_should_return_length_on_length_zero() {
fn test_border_style() {
use style::values::specified::BorderStyle;

assert_roundtrip_with_context!(BorderStyle::parse, r#"none"#);
assert_roundtrip_with_context!(BorderStyle::parse, r#"hidden"#);
assert_roundtrip_with_context!(BorderStyle::parse, r#"solid"#);
assert_roundtrip_with_context!(BorderStyle::parse, r#"double"#);
assert_roundtrip_with_context!(BorderStyle::parse, r#"dotted"#);
assert_roundtrip_with_context!(BorderStyle::parse, r#"dashed"#);
assert_roundtrip_with_context!(BorderStyle::parse, r#"groove"#);
assert_roundtrip_with_context!(BorderStyle::parse, r#"ridge"#);
assert_roundtrip_with_context!(BorderStyle::parse, r#"inset"#);
assert_roundtrip_with_context!(BorderStyle::parse, r#"outset"#);
assert_roundtrip_with_context!(<BorderStyle as Parse>::parse, r#"none"#);
assert_roundtrip_with_context!(<BorderStyle as Parse>::parse, r#"hidden"#);
assert_roundtrip_with_context!(<BorderStyle as Parse>::parse, r#"solid"#);
assert_roundtrip_with_context!(<BorderStyle as Parse>::parse, r#"double"#);
assert_roundtrip_with_context!(<BorderStyle as Parse>::parse, r#"dotted"#);
assert_roundtrip_with_context!(<BorderStyle as Parse>::parse, r#"dashed"#);
assert_roundtrip_with_context!(<BorderStyle as Parse>::parse, r#"groove"#);
assert_roundtrip_with_context!(<BorderStyle as Parse>::parse, r#"ridge"#);
assert_roundtrip_with_context!(<BorderStyle as Parse>::parse, r#"inset"#);
assert_roundtrip_with_context!(<BorderStyle as Parse>::parse, r#"outset"#);
}

#[test]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.