Have shorthand parsing functions return values

Shorthands are responsible to set all its longhands to a proper value,
rather than returning None.

Fixes #15380.
This commit is contained in:
Xidorn Quan 2017-02-28 15:21:17 +11:00
parent 2e07ce7e84
commit f33b0b4ea3
18 changed files with 240 additions and 259 deletions

View file

@ -492,8 +492,7 @@
pub struct Longhands {
% for sub_property in shorthand.sub_properties:
pub ${sub_property.ident}:
Option<longhands::${sub_property.ident}::SpecifiedValue>,
pub ${sub_property.ident}: longhands::${sub_property.ident}::SpecifiedValue,
% endfor
}
@ -606,14 +605,11 @@
if let Ok(value) = value {
% for sub_property in shorthand.sub_properties:
declarations.push((PropertyDeclaration::${sub_property.camel_case}(
match value.${sub_property.ident} {
% if sub_property.boxed:
Some(value) => DeclaredValue::Value(Box::new(value)),
% else:
Some(value) => DeclaredValue::Value(value),
% endif
None => DeclaredValue::Initial,
}
% if sub_property.boxed:
DeclaredValue::Value(Box::new(value.${sub_property.ident}))
% else:
DeclaredValue::Value(value.${sub_property.ident})
% endif
), Importance::Normal));
% endfor
Ok(())
@ -660,7 +656,7 @@
% endif
Ok(Longhands {
% for side in ["top", "right", "bottom", "left"]:
${to_rust_ident(sub_property_pattern % side)}: Some(${side}),
${to_rust_ident(sub_property_pattern % side)}: ${side},
% endfor
})
}

View file

@ -88,6 +88,12 @@ pub mod longhands {
<%include file="/longhand/xul.mako.rs" />
}
macro_rules! unwrap_or_initial {
($prop: ident) => (unwrap_or_initial!($prop, $prop));
($prop: ident, $expr: expr) =>
($expr.unwrap_or_else(|| $prop::get_initial_specified_value()));
}
/// A module with code for all the shorthand css properties, and a few
/// serialization helpers.
#[allow(missing_docs)]
@ -343,13 +349,12 @@ impl PropertyDeclarationIdSet {
% if property in shorthand.sub_properties:
Some(ShorthandId::${shorthand.camel_case}) => {
shorthands::${shorthand.ident}::parse_value(&context, input)
.map(|result| match result.${property.ident} {
.map(|result| {
% if property.boxed:
Some(value) => DeclaredValue::Value(Box::new(value)),
DeclaredValue::Value(Box::new(result.${property.ident}))
% else:
Some(value) => DeclaredValue::Value(value),
DeclaredValue::Value(result.${property.ident})
% endif
None => DeclaredValue::Initial,
})
}
% endif

View file

@ -115,15 +115,15 @@
}));
Ok(Longhands {
background_color: background_color,
background_image: Some(background_image),
background_position_x: Some(background_position_x),
background_position_y: Some(background_position_y),
background_repeat: Some(background_repeat),
background_attachment: Some(background_attachment),
background_size: Some(background_size),
background_origin: Some(background_origin),
background_clip: Some(background_clip),
background_color: unwrap_or_initial!(background_color),
background_image: background_image,
background_position_x: background_position_x,
background_position_y: background_position_y,
background_repeat: background_repeat,
background_attachment: background_attachment,
background_size: background_size,
background_origin: background_origin,
background_clip: background_clip,
})
}
@ -246,8 +246,8 @@
}
Ok(Longhands {
background_position_x: Some(position_x),
background_position_y: Some(position_y),
background_position_x: position_x,
background_position_y: position_y,
})
}

View file

@ -24,7 +24,7 @@ ${helpers.four_sides_shorthand("border-style", "border-%s-style",
let (top, right, bottom, left) = try!(parse_four_sides(input, |i| specified::BorderWidth::parse(context, i)));
Ok(Longhands {
% for side in ["top", "right", "bottom", "left"]:
${to_rust_ident('border-%s-width' % side)}: Some(${side}),
${to_rust_ident('border-%s-width' % side)}: ${side},
% endfor
})
}
@ -42,10 +42,10 @@ ${helpers.four_sides_shorthand("border-style", "border-%s-style",
pub fn parse_border(context: &ParserContext, input: &mut Parser)
-> Result<(Option<specified::CSSColor>,
Option<specified::BorderStyle>,
Option<specified::BorderWidth>), ()> {
use values::specified;
-> Result<(specified::CSSColor,
specified::BorderStyle,
specified::BorderWidth), ()> {
use values::specified::{CSSColor, BorderStyle, BorderWidth};
let _unused = context;
let mut color = None;
let mut style = None;
@ -53,21 +53,21 @@ pub fn parse_border(context: &ParserContext, input: &mut Parser)
let mut any = false;
loop {
if color.is_none() {
if let Ok(value) = input.try(|i| specified::CSSColor::parse(context, i)) {
if let Ok(value) = input.try(|i| CSSColor::parse(context, i)) {
color = Some(value);
any = true;
continue
}
}
if style.is_none() {
if let Ok(value) = input.try(|i| specified::BorderStyle::parse(context, i)) {
if let Ok(value) = input.try(|i| BorderStyle::parse(context, i)) {
style = Some(value);
any = true;
continue
}
}
if width.is_none() {
if let Ok(value) = input.try(|i| specified::BorderWidth::parse(context, i)) {
if let Ok(value) = input.try(|i| BorderWidth::parse(context, i)) {
width = Some(value);
any = true;
continue
@ -75,7 +75,13 @@ pub fn parse_border(context: &ParserContext, input: &mut Parser)
}
break
}
if any { Ok((color, style, width)) } else { Err(()) }
if any {
Ok((color.unwrap_or_else(|| CSSColor::currentcolor()),
style.unwrap_or(BorderStyle::none),
width.unwrap_or(BorderWidth::Medium)))
} else {
Err(())
}
}
% for side, logical in ALL_SIDES:
@ -158,10 +164,10 @@ pub fn parse_border(context: &ParserContext, input: &mut Parser)
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let radii = try!(BorderRadius::parse(context, input));
Ok(Longhands {
border_top_left_radius: Some(radii.top_left),
border_top_right_radius: Some(radii.top_right),
border_bottom_right_radius: Some(radii.bottom_right),
border_bottom_left_radius: Some(radii.bottom_left),
border_top_left_radius: radii.top_left,
border_top_right_radius: radii.top_right,
border_bottom_right_radius: radii.bottom_right,
border_bottom_left_radius: radii.bottom_left,
})
}
@ -254,7 +260,7 @@ pub fn parse_border(context: &ParserContext, input: &mut Parser)
Ok(Longhands {
% for name in "outset repeat slice source width".split():
border_image_${name}: Some(border_image_${name}),
border_image_${name}: border_image_${name},
% endfor
})
}

View file

@ -11,8 +11,8 @@
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let overflow = try!(overflow_x::parse(context, input));
Ok(Longhands {
overflow_x: Some(overflow),
overflow_y: Some(overflow_y::SpecifiedValue(overflow)),
overflow_x: overflow,
overflow_y: overflow_y::SpecifiedValue(overflow),
})
}
@ -103,31 +103,25 @@ macro_rules! try_parse_one {
}
}
if input.try(|input| input.expect_ident_matching("none")).is_ok() {
return Ok(Longhands {
transition_property: None,
transition_duration: None,
transition_timing_function: None,
transition_delay: None,
})
}
let results = try!(input.parse_comma_separated(|i| parse_one_transition(context, i)));
let (mut properties, mut durations) = (Vec::new(), Vec::new());
let (mut timing_functions, mut delays) = (Vec::new(), Vec::new());
for result in results {
properties.push(result.transition_property);
durations.push(result.transition_duration);
timing_functions.push(result.transition_timing_function);
delays.push(result.transition_delay);
if input.try(|input| input.expect_ident_matching("none")).is_err() {
let results = try!(input.parse_comma_separated(|i| parse_one_transition(context, i)));
for result in results {
properties.push(result.transition_property);
durations.push(result.transition_duration);
timing_functions.push(result.transition_timing_function);
delays.push(result.transition_delay);
}
}
Ok(Longhands {
transition_property: Some(transition_property::SpecifiedValue(properties)),
transition_duration: Some(transition_duration::SpecifiedValue(durations)),
transition_property: transition_property::SpecifiedValue(properties),
transition_duration: transition_duration::SpecifiedValue(durations),
transition_timing_function:
Some(transition_timing_function::SpecifiedValue(timing_functions)),
transition_delay: Some(transition_delay::SpecifiedValue(delays)),
transition_timing_function::SpecifiedValue(timing_functions),
transition_delay: transition_delay::SpecifiedValue(delays),
})
}
@ -258,21 +252,6 @@ macro_rules! try_parse_one {
}
}
if input.try(|input| input.expect_ident_matching("none")).is_ok() {
return Ok(Longhands {
animation_name: None,
animation_duration: None,
animation_timing_function: None,
animation_delay: None,
animation_iteration_count: None,
animation_direction: None,
animation_fill_mode: None,
animation_play_state: None,
})
}
let results = try!(input.parse_comma_separated(|i| parse_one_animation(context, i)));
let mut names = vec![];
let mut durations = vec![];
let mut timing_functions = vec![];
@ -282,26 +261,29 @@ macro_rules! try_parse_one {
let mut fill_modes = vec![];
let mut play_states = vec![];
for result in results.into_iter() {
names.push(result.animation_name);
durations.push(result.animation_duration);
timing_functions.push(result.animation_timing_function);
delays.push(result.animation_delay);
iteration_counts.push(result.animation_iteration_count);
directions.push(result.animation_direction);
fill_modes.push(result.animation_fill_mode);
play_states.push(result.animation_play_state);
if input.try(|input| input.expect_ident_matching("none")).is_err() {
let results = try!(input.parse_comma_separated(|i| parse_one_animation(context, i)));
for result in results.into_iter() {
names.push(result.animation_name);
durations.push(result.animation_duration);
timing_functions.push(result.animation_timing_function);
delays.push(result.animation_delay);
iteration_counts.push(result.animation_iteration_count);
directions.push(result.animation_direction);
fill_modes.push(result.animation_fill_mode);
play_states.push(result.animation_play_state);
}
}
Ok(Longhands {
animation_name: Some(animation_name::SpecifiedValue(names)),
animation_duration: Some(animation_duration::SpecifiedValue(durations)),
animation_timing_function: Some(animation_timing_function::SpecifiedValue(timing_functions)),
animation_delay: Some(animation_delay::SpecifiedValue(delays)),
animation_iteration_count: Some(animation_iteration_count::SpecifiedValue(iteration_counts)),
animation_direction: Some(animation_direction::SpecifiedValue(directions)),
animation_fill_mode: Some(animation_fill_mode::SpecifiedValue(fill_modes)),
animation_play_state: Some(animation_play_state::SpecifiedValue(play_states)),
animation_name: animation_name::SpecifiedValue(names),
animation_duration: animation_duration::SpecifiedValue(durations),
animation_timing_function: animation_timing_function::SpecifiedValue(timing_functions),
animation_delay: animation_delay::SpecifiedValue(delays),
animation_iteration_count: animation_iteration_count::SpecifiedValue(iteration_counts),
animation_direction: animation_direction::SpecifiedValue(directions),
animation_fill_mode: animation_fill_mode::SpecifiedValue(fill_modes),
animation_play_state: animation_play_state::SpecifiedValue(play_states),
})
}
@ -364,8 +346,8 @@ macro_rules! try_parse_one {
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let result = try!(scroll_snap_type_x::parse(context, input));
Ok(Longhands {
scroll_snap_type_x: Some(result),
scroll_snap_type_y: Some(result),
scroll_snap_type_x: result,
scroll_snap_type_y: result,
})
}

View file

@ -43,8 +43,8 @@
Err(())
} else {
Ok(Longhands {
column_count: column_count,
column_width: column_width,
column_count: unwrap_or_initial!(column_count),
column_width: unwrap_or_initial!(column_width),
})
}
}
@ -87,11 +87,9 @@
}
if any {
Ok(Longhands {
% for name in "width style".split():
column_rule_${name}: column_rule_${name}
.or(Some(column_rule_${name}::get_initial_specified_value())),
% endfor
column_rule_color: column_rule_color,
column_rule_width: unwrap_or_initial!(column_rule_width),
column_rule_style: unwrap_or_initial!(column_rule_style),
column_rule_color: unwrap_or_initial!(column_rule_color),
})
} else {
Err(())

View file

@ -14,6 +14,12 @@
spec="https://drafts.csswg.org/css-fonts-3/#propdef-font">
use properties::longhands::{font_style, font_variant, font_weight, font_stretch};
use properties::longhands::{font_size, line_height};
% if product == "gecko":
use properties::longhands::{font_size_adjust, font_kerning, font_variant_caps, font_variant_position};
% endif
% if product == "none":
use properties::longhands::font_language_override;
% endif
use properties::longhands::font_family::SpecifiedValue as FontFamily;
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
@ -72,22 +78,19 @@
};
let family = FontFamily::parse(input)?;
Ok(Longhands {
font_style: style,
font_variant: variant,
font_weight: weight,
font_stretch: stretch,
font_size: size,
line_height: line_height,
font_family: Some(family),
% if product == "gecko":
font_size_adjust: None,
font_kerning: None,
font_variant_caps: None,
font_variant_position: None,
% endif
% if product == "none":
font_language_override: None,
% endif
% for name in "style variant weight stretch size".split():
font_${name}: unwrap_or_initial!(font_${name}, ${name}),
% endfor
line_height: unwrap_or_initial!(line_height),
font_family: family,
% if product == "gecko":
% for name in "size_adjust kerning variant_caps variant_position".split():
font_${name}: font_${name}::get_initial_specified_value(),
% endfor
% endif
% if product == "none":
font_language_override: font_language_override::get_initial_specified_value(),
% endif
})
}

View file

@ -14,9 +14,9 @@
let url = UrlOrNone::parse(context, input)?;
Ok(Longhands {
marker_start: Some(url.clone()),
marker_mid: Some(url.clone()),
marker_end: Some(url),
marker_start: url.clone(),
marker_mid: url.clone(),
marker_end: url,
})
}

View file

@ -29,13 +29,9 @@
break
}
if color.is_some() || style.is_some() {
if style.is_none() {
style = Some(text_emphasis_style::get_initial_specified_value());
}
Ok(Longhands {
text_emphasis_color: color,
text_emphasis_style: style,
text_emphasis_color: unwrap_or_initial!(text_emphasis_color, color),
text_emphasis_style: unwrap_or_initial!(text_emphasis_style, style),
})
} else {
Err(())
@ -68,13 +64,9 @@
-webkit-text-stroke-width"
products="gecko"
spec="https://compat.spec.whatwg.org/#the-webkit-text-stroke">
use cssparser::Color as CSSParserColor;
use properties::longhands::{_webkit_text_stroke_color, _webkit_text_stroke_width};
use values::specified::CSSColor;
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
use values::specified::{BorderWidth, Length};
let mut color = None;
let mut width = None;
loop {
@ -96,9 +88,8 @@
if color.is_some() || width.is_some() {
Ok(Longhands {
_webkit_text_stroke_color: color.or(Some(CSSColor { parsed: CSSParserColor::CurrentColor,
authored: None })),
_webkit_text_stroke_width: width.or(Some(BorderWidth::from_length(Length::zero()))),
_webkit_text_stroke_color: unwrap_or_initial!(_webkit_text_stroke_color, color),
_webkit_text_stroke_width: unwrap_or_initial!(_webkit_text_stroke_width, width),
})
} else {
Err(())

View file

@ -51,6 +51,8 @@
break
}
let position = unwrap_or_initial!(list_style_position, position);
// If there are two `none`s, then we can't have a type or image; if there is one `none`,
// then we can't have both a type *and* an image; if there is no `none` then we're fine as
// long as we parsed something.
@ -58,36 +60,36 @@
(true, 2, None, None) => {
Ok(Longhands {
list_style_position: position,
list_style_image: Some(Either::Second(None_)),
list_style_type: Some(list_style_type::SpecifiedValue::none),
list_style_image: Either::Second(None_),
list_style_type: list_style_type::SpecifiedValue::none,
})
}
(true, 1, None, Some(image)) => {
Ok(Longhands {
list_style_position: position,
list_style_image: Some(image),
list_style_type: Some(list_style_type::SpecifiedValue::none),
list_style_image: image,
list_style_type: list_style_type::SpecifiedValue::none,
})
}
(true, 1, Some(list_style_type), None) => {
Ok(Longhands {
list_style_position: position,
list_style_image: Some(Either::Second(None_)),
list_style_type: Some(list_style_type),
list_style_image: Either::Second(None_),
list_style_type: list_style_type,
})
}
(true, 1, None, None) => {
Ok(Longhands {
list_style_position: position,
list_style_image: Some(Either::Second(None_)),
list_style_type: Some(list_style_type::SpecifiedValue::none),
list_style_image: Either::Second(None_),
list_style_type: list_style_type::SpecifiedValue::none,
})
}
(true, 0, list_style_type, image) => {
Ok(Longhands {
list_style_position: position,
list_style_image: image,
list_style_type: list_style_type,
list_style_image: unwrap_or_initial!(list_style_image, image),
list_style_type: unwrap_or_initial!(list_style_type),
})
}
_ => Err(()),

View file

@ -115,7 +115,7 @@
Ok(Longhands {
% for name in "image mode position_x position_y size repeat origin clip composite".split():
mask_${name}: Some(mask_${name}),
mask_${name}: mask_${name},
% endfor
})
}
@ -263,8 +263,8 @@
}
Ok(Longhands {
mask_position_x: Some(position_x),
mask_position_y: Some(position_y),
mask_position_x: position_x,
mask_position_y: position_y,
})
}

View file

@ -6,7 +6,7 @@
<%helpers:shorthand name="outline" sub_properties="outline-color outline-style outline-width"
spec="https://drafts.csswg.org/css-ui/#propdef-outline">
use properties::longhands::{outline_width, outline_style};
use properties::longhands::{outline_color, outline_width, outline_style};
use values::specified;
use parser::Parse;
@ -42,9 +42,9 @@
}
if any {
Ok(Longhands {
outline_color: color,
outline_style: style,
outline_width: width,
outline_color: unwrap_or_initial!(outline_color, color),
outline_style: unwrap_or_initial!(outline_style, style),
outline_width: unwrap_or_initial!(outline_width, width),
})
} else {
Err(())

View file

@ -31,8 +31,8 @@
return Err(())
}
Ok(Longhands {
flex_direction: direction,
flex_wrap: wrap,
flex_direction: unwrap_or_initial!(flex_direction, direction),
flex_wrap: unwrap_or_initial!(flex_wrap, wrap),
})
}
@ -73,9 +73,9 @@
if input.try(|input| input.expect_ident_matching("none")).is_ok() {
return Ok(Longhands {
flex_grow: Some(Number(0.0)),
flex_shrink: Some(Number(0.0)),
flex_basis: Some(LengthOrPercentageOrAutoOrContent::Auto)
flex_grow: Number(0.0),
flex_shrink: Number(0.0),
flex_basis: LengthOrPercentageOrAutoOrContent::Auto
})
}
loop {
@ -99,9 +99,9 @@
return Err(())
}
Ok(Longhands {
flex_grow: grow.or(Some(Number(1.0))),
flex_shrink: shrink.or(Some(Number(1.0))),
flex_basis: basis.or(Some(LengthOrPercentageOrAutoOrContent::Length(NoCalcLength::zero())))
flex_grow: grow.unwrap_or(Number(1.0)),
flex_shrink: shrink.unwrap_or(Number(1.0)),
flex_basis: basis.unwrap_or(LengthOrPercentageOrAutoOrContent::Length(NoCalcLength::zero()))
})
}

View file

@ -13,7 +13,6 @@
spec="https://drafts.csswg.org/css-text-decor/#propdef-text-decoration">
use cssparser::Color as CSSParserColor;
use properties::longhands::{text_decoration_color, text_decoration_line, text_decoration_style};
use values::specified::CSSColor;
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let (mut color, mut line, mut style, mut any) = (None, None, None, false);
@ -41,10 +40,9 @@
}
Ok(Longhands {
text_decoration_color: color.or(Some(CSSColor { parsed: CSSParserColor::CurrentColor,
authored: None })),
text_decoration_line: line.or(Some(text_decoration_line::computed_value::none)),
text_decoration_style: style.or(Some(text_decoration_style::computed_value::T::solid)),
text_decoration_color: unwrap_or_initial!(text_decoration_color, color),
text_decoration_line: unwrap_or_initial!(text_decoration_line, line),
text_decoration_style: unwrap_or_initial!(text_decoration_style, style),
})
}