Skip to content
Permalink
Browse files

Add shorthand parsing and serialization for grid

  • Loading branch information
wafflespeanut committed May 24, 2017
1 parent 7b68d8d commit 101e09e560667b8a7d3a457e7f1da66294a920fb
Showing with 103 additions and 0 deletions.
  1. +103 −0 components/style/properties/shorthand/position.mako.rs
@@ -394,6 +394,109 @@
}
</%helpers:shorthand>

<%helpers:shorthand name="grid"
sub_properties="grid-template-rows grid-template-columns grid-template-areas
grid-auto-rows grid-auto-columns grid-row-gap grid-column-gap
grid-auto-flow"
spec="https://drafts.csswg.org/css-grid/#propdef-grid"
disable_when_testing="True"
products="gecko">
use properties::longhands::{grid_auto_columns, grid_auto_rows, grid_auto_flow};
use properties::longhands::{grid_template_columns, grid_template_rows};
use properties::longhands::grid_auto_flow::computed_value::{AutoFlow, T as SpecifiedAutoFlow};
use values::{Either, None_};
use values::specified::{LengthOrPercentage, TrackSize};

pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let mut temp_rows = Either::Second(None_);
let mut temp_cols = Either::Second(None_);
let mut temp_areas = Either::Second(None_);
let mut auto_rows = TrackSize::default();
let mut auto_cols = TrackSize::default();
let mut flow = grid_auto_flow::get_initial_value();

fn parse_auto_flow(input: &mut Parser, is_row: bool) -> Result<SpecifiedAutoFlow, ()> {
let mut auto_flow = None;
let mut dense = false;
for _ in 0..2 {
if input.try(|i| i.expect_ident_matching("auto-flow")).is_ok() {
auto_flow = if is_row {
Some(AutoFlow::Row)
} else {
Some(AutoFlow::Column)
};
} else if input.try(|i| i.expect_ident_matching("dense")).is_ok() {
dense = true;
} else {
break
}
}

auto_flow.map(|flow| {
SpecifiedAutoFlow {
autoflow: flow,
dense: dense,
}
}).ok_or(())
}

if let Ok((rows, cols, areas)) = input.try(|i| super::grid_template::parse_grid_template(context, i)) {
temp_rows = rows;
temp_cols = cols;
temp_areas = areas;
} else if let Ok(rows) = input.try(|i| grid_template_rows::parse(context, i)) {
temp_rows = rows;
input.expect_delim('/')?;
flow = parse_auto_flow(input, false)?;
auto_cols = grid_auto_columns::parse(context, input).unwrap_or_default();
} else {
flow = parse_auto_flow(input, true)?;
auto_rows = input.try(|i| grid_auto_rows::parse(context, i)).unwrap_or_default();
input.expect_delim('/')?;
temp_cols = grid_template_columns::parse(context, input)?;
}

Ok(expanded! {
grid_template_rows: temp_rows,
grid_template_columns: temp_cols,
grid_template_areas: temp_areas,
grid_auto_rows: auto_rows,
grid_auto_columns: auto_cols,
grid_auto_flow: flow,
// This shorthand also resets grid gap
grid_row_gap: LengthOrPercentage::zero(),
grid_column_gap: LengthOrPercentage::zero(),
})
}

impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&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 {
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)
} else {
dest.write_str("auto-flow ")?;
if self.grid_auto_flow.dense {
dest.write_str("dense ")?;
}

self.grid_auto_rows.to_css(dest)?;
dest.write_str(" / ")?;
self.grid_template_columns.to_css(dest)
}
}
}
</%helpers:shorthand>

<%helpers:shorthand name="place-content" sub_properties="align-content justify-content"
spec="https://drafts.csswg.org/css-align/#propdef-place-content"
products="gecko" disable_when_testing="True">

0 comments on commit 101e09e

Please sign in to comment.
You can’t perform that action at this time.