diff --git a/components/style/properties/longhand/position.mako.rs b/components/style/properties/longhand/position.mako.rs index c39a7585215..1cf56b645ce 100644 --- a/components/style/properties/longhand/position.mako.rs +++ b/components/style/properties/longhand/position.mako.rs @@ -307,7 +307,9 @@ ${helpers.predefined_type("object-position", #[derive(PartialEq, Clone, Eq, Copy, Debug)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct T { - pub autoflow: AutoFlow, + // This needs to be an Option because we need an indicator to determine whether this property is + // parsed or not in `grid` shorthand. Otherwise we can't properly serialize it. + pub autoflow: Option, pub dense: bool, } } @@ -317,7 +319,7 @@ ${helpers.predefined_type("object-position", impl ToCss for computed_value::T { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - dest.write_str(match self.autoflow { + dest.write_str(match self.autoflow.unwrap_or(computed_value::AutoFlow::Row) { computed_value::AutoFlow::Column => "column", computed_value::AutoFlow::Row => "row" })?; @@ -330,7 +332,8 @@ ${helpers.predefined_type("object-position", #[inline] pub fn get_initial_value() -> computed_value::T { computed_value::T { - autoflow: computed_value::AutoFlow::Row, + // None should resolve to `computed_value::AutoFlow::Row` in layout. + autoflow: None, dense: false } } @@ -367,7 +370,7 @@ ${helpers.predefined_type("object-position", if value.is_some() || dense { Ok(computed_value::T { - autoflow: value.unwrap_or(AutoFlow::Row), + autoflow: value, dense: dense, }) } else { @@ -382,12 +385,12 @@ ${helpers.predefined_type("object-position", use self::computed_value::AutoFlow; SpecifiedValue { - autoflow: + autoflow: Some( if bits & structs::NS_STYLE_GRID_AUTO_FLOW_ROW as u8 != 0 { AutoFlow::Row } else { AutoFlow::Column - }, + }), dense: bits & structs::NS_STYLE_GRID_AUTO_FLOW_DENSE as u8 != 0, } @@ -400,7 +403,7 @@ ${helpers.predefined_type("object-position", use gecko_bindings::structs; use self::computed_value::AutoFlow; - let mut result: u8 = match v.autoflow { + let mut result: u8 = match v.autoflow.unwrap_or(AutoFlow::Row) { AutoFlow::Row => structs::NS_STYLE_GRID_AUTO_FLOW_ROW as u8, AutoFlow::Column => structs::NS_STYLE_GRID_AUTO_FLOW_COLUMN as u8, }; diff --git a/components/style/properties/shorthand/position.mako.rs b/components/style/properties/shorthand/position.mako.rs index d304f2bab6e..8b9b816bc68 100644 --- a/components/style/properties/shorthand/position.mako.rs +++ b/components/style/properties/shorthand/position.mako.rs @@ -437,7 +437,7 @@ auto_flow.map(|flow| { SpecifiedAutoFlow { - autoflow: flow, + autoflow: Some(flow), dense: dense, } }).ok_or(StyleParseError::UnspecifiedError.into()) @@ -450,10 +450,10 @@ } else if let Ok(rows) = input.try(|i| GridTemplateComponent::parse(context, i)) { temp_rows = rows; input.expect_delim('/')?; - flow = parse_auto_flow(input, false)?; + flow = parse_auto_flow(input, true)?; auto_cols = grid_auto_columns::parse(context, input).unwrap_or_default(); } else { - flow = parse_auto_flow(input, true)?; + flow = parse_auto_flow(input, false)?; auto_rows = input.try(|i| grid_auto_rows::parse(context, i)).unwrap_or_default(); input.expect_delim('/')?; temp_cols = GridTemplateComponent::parse(context, input)?; @@ -474,28 +474,39 @@ impl<'a> ToCss for LonghandsToSerialize<'a> { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - if let Either::First(_) = *self.grid_template_areas { - super::grid_template::serialize_grid_template(self.grid_template_rows, - self.grid_template_columns, - self.grid_template_areas, dest) - } else if self.grid_auto_flow.autoflow == AutoFlow::Row { + if self.grid_auto_flow.autoflow.is_none() { + return super::grid_template::serialize_grid_template(self.grid_template_rows, + self.grid_template_columns, + self.grid_template_areas, dest); + } + + // This unwrap is safe because we checked the None part in previous if condition. + if self.grid_auto_flow.autoflow.unwrap() == AutoFlow::Row { self.grid_template_rows.to_css(dest)?; dest.write_str(" / auto-flow")?; if self.grid_auto_flow.dense { dest.write_str(" dense")?; } - self.grid_auto_columns.to_css(dest) + if !self.grid_auto_columns.is_default() { + dest.write_str(" ")?; + self.grid_auto_columns.to_css(dest)?; + } } else { - dest.write_str("auto-flow ")?; + dest.write_str("auto-flow")?; if self.grid_auto_flow.dense { - dest.write_str("dense ")?; + dest.write_str(" dense")?; + } + + if !self.grid_auto_rows.is_default() { + dest.write_str(" ")?; + self.grid_auto_rows.to_css(dest)?; } - self.grid_auto_rows.to_css(dest)?; dest.write_str(" / ")?; - self.grid_template_columns.to_css(dest) + self.grid_template_columns.to_css(dest)?; } + Ok(()) } } diff --git a/components/style/values/generics/grid.rs b/components/style/values/generics/grid.rs index 0bed2512dd7..c64a7929107 100644 --- a/components/style/values/generics/grid.rs +++ b/components/style/values/generics/grid.rs @@ -252,6 +252,13 @@ impl Default for TrackSize { } } +impl TrackSize { + /// Returns true if current TrackSize is same as default. + pub fn is_default(&self) -> bool { + *self == TrackSize::default() + } +} + impl ToCss for TrackSize { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { match *self {