Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Switch to GridTemplateComponent for subgrid line names
  • Loading branch information
wafflespeanut authored and canova committed Jul 4, 2017
1 parent 8af9122 commit 9d9d99e
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 38 deletions.
5 changes: 5 additions & 0 deletions components/style/gecko/generated/bindings.rs
Expand Up @@ -1041,6 +1041,11 @@ extern "C" {
*mut nsStyleGridTemplate,
track_sizes: u32);
}
extern "C" {
pub fn Gecko_SetGridTemplateLineNamesLength(grid_template:
*mut nsStyleGridTemplate,
track_sizes: u32);
}
extern "C" {
pub fn Gecko_CopyStyleGridTemplateValues(grid_template:
*mut nsStyleGridTemplate,
Expand Down
31 changes: 26 additions & 5 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -1244,7 +1244,7 @@ fn static_assert() {
use std::usize;
use values::CustomIdent;
use values::generics::grid::TrackListType::Auto;
use values::generics::grid::{RepeatCount, TrackSize};
use values::generics::grid::{GridTemplateComponent, RepeatCount, TrackSize};

#[inline]
fn set_line_names(servo_names: &[CustomIdent], gecko_names: &mut nsTArray<nsStringRepr>) {
Expand Down Expand Up @@ -1280,10 +1280,11 @@ fn static_assert() {
${self_grid}.mRepeatAutoIndex = -1;
${self_grid}.set_mIsAutoFill(false);
${self_grid}.set_mIsSubgrid(false);
// FIXME: mIsSubgrid is false only for <none>, but we don't support subgrid name lists at the moment.

let max_lines = nsStyleGridLine_kMaxLine as usize - 1; // for accounting the final <line-names>

match v {
Either::First(track) => {
GridTemplateComponent::TrackList(track) => {
let mut auto_idx = usize::MAX;
let mut auto_track_size = None;
if let Auto(idx) = track.list_type {
Expand Down Expand Up @@ -1314,7 +1315,6 @@ fn static_assert() {
num_values += 1;
}

let max_lines = nsStyleGridLine_kMaxLine as usize - 1; // for accounting the final <line-names>
num_values = cmp::min(num_values, max_lines);
unsafe {
bindings::Gecko_SetStyleGridTemplateArrayLengths(&mut ${self_grid}, num_values as u32);
Expand All @@ -1341,7 +1341,7 @@ fn static_assert() {
let final_names = line_names.next().unwrap();
set_line_names(&final_names, ${self_grid}.mLineNameLists.last_mut().unwrap());
},
Either::Second(_none) => {
GridTemplateComponent::None => {
unsafe {
bindings::Gecko_SetStyleGridTemplateArrayLengths(&mut ${self_grid}, 0);
bindings::Gecko_ResizeTArrayForStrings(
Expand All @@ -1350,6 +1350,27 @@ fn static_assert() {
&mut ${self_grid}.mRepeatAutoLineNameListAfter, 0);
}
},
GridTemplateComponent::Subgrid(list) => {
${self_grid}.set_mIsSubgrid(true);
let num_values = cmp::min(list.names.len(), max_lines + 1);
unsafe {
bindings::Gecko_SetStyleGridTemplateArrayLengths(&mut ${self_grid}, 0);
bindings::Gecko_SetGridTemplateLineNamesLength(&mut ${self_grid}, num_values as u32);
bindings::Gecko_ResizeTArrayForStrings(
&mut ${self_grid}.mRepeatAutoLineNameListBefore, 0);
bindings::Gecko_ResizeTArrayForStrings(
&mut ${self_grid}.mRepeatAutoLineNameListAfter, 0);
}

if let Some(idx) = list.fill_idx {
${self_grid}.set_mIsAutoFill(true);
${self_grid}.mRepeatAutoIndex = idx as i16;
}

for (servo_names, gecko_names) in list.names.iter().zip(${self_grid}.mLineNameLists.iter_mut()) {
set_line_names(servo_names, gecko_names);
}
},
}
}

Expand Down
6 changes: 2 additions & 4 deletions components/style/properties/longhand/position.mako.rs
Expand Up @@ -276,11 +276,9 @@ ${helpers.predefined_type("object-position",
products="gecko",
boxed=True)}

// NOTE: The spec lists only `none | <track-list> | <auto-track-list>`, but gecko seems to support
// `subgrid <line-name-list>?` in addition to this (probably old spec). We should support it soon.
${helpers.predefined_type("grid-template-%ss" % kind,
"TrackListOrNone",
"Either::Second(None_)",
"GridTemplateComponent",
"specified::GenericGridTemplateComponent::None",
products="gecko",
spec="https://drafts.csswg.org/css-grid/#propdef-grid-template-%ss" % kind,
boxed=True,
Expand Down
45 changes: 25 additions & 20 deletions components/style/properties/shorthand/position.mako.rs
Expand Up @@ -245,15 +245,16 @@
use properties::longhands::grid_template_areas::TemplateAreas;
use values::{Either, None_};
use values::generics::grid::{TrackSize, TrackList, TrackListType, concat_serialize_idents};
use values::specified::TrackListOrNone;
use values::specified::{GridTemplateComponent, GenericGridTemplateComponent};
use values::specified::grid::parse_line_names;

/// Parsing for `<grid-template>` shorthand (also used by `grid` shorthand).
pub fn parse_grid_template<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<(TrackListOrNone, TrackListOrNone, Either<TemplateAreas, None_>),
ParseError<'i>> {
-> Result<(GridTemplateComponent,
GridTemplateComponent,
Either<TemplateAreas, None_>), ParseError<'i>> {
if input.try(|i| i.expect_ident_matching("none")).is_ok() {
return Ok((Either::Second(None_), Either::Second(None_), Either::Second(None_)))
return Ok((GenericGridTemplateComponent::None, GenericGridTemplateComponent::None, Either::Second(None_)))
}

let first_line_names = input.try(parse_line_names).unwrap_or(vec![]);
Expand Down Expand Up @@ -296,20 +297,23 @@
};

let template_cols = if input.try(|i| i.expect_delim('/')).is_ok() {
let track_list = TrackList::parse(context, input)?;
if track_list.list_type != TrackListType::Explicit {
return Err(StyleParseError::UnspecifiedError.into())
let value = GridTemplateComponent::parse(context, input)?;
if let GenericGridTemplateComponent::TrackList(ref list) = value {
if list.list_type != TrackListType::Explicit {
return Err(StyleParseError::UnspecifiedError.into())
}
}

Either::First(track_list)
value
} else {
Either::Second(None_)
GenericGridTemplateComponent::None
};

Ok((Either::First(template_rows), template_cols, Either::First(template_areas)))
Ok((GenericGridTemplateComponent::TrackList(template_rows),
template_cols, Either::First(template_areas)))
} else {
let mut template_rows = grid_template_rows::parse(context, input)?;
if let Either::First(ref mut list) = template_rows {
if let GenericGridTemplateComponent::TrackList(ref mut list) = template_rows {
list.line_names[0] = first_line_names; // won't panic
}

Expand All @@ -329,14 +333,14 @@
}

/// Serialization for `<grid-template>` shorthand (also used by `grid` shorthand).
pub fn serialize_grid_template<W>(template_rows: &TrackListOrNone,
template_columns: &TrackListOrNone,
pub fn serialize_grid_template<W>(template_rows: &GridTemplateComponent,
template_columns: &GridTemplateComponent,
template_areas: &Either<TemplateAreas, None_>,
dest: &mut W) -> fmt::Result where W: fmt::Write {
match *template_areas {
Either::Second(_none) => {
if template_rows == &Either::Second(None_) &&
template_columns == &Either::Second(None_) {
if template_rows == &GenericGridTemplateComponent::None &&
template_columns == &GenericGridTemplateComponent::None {
dest.write_str("none")
} else {
template_rows.to_css(dest)?;
Expand All @@ -346,8 +350,8 @@
},
Either::First(ref areas) => {
let track_list = match *template_rows {
Either::First(ref list) => list,
Either::Second(_none) => unreachable!(), // should exist!
GenericGridTemplateComponent::TrackList(ref list) => list,
_ => unreachable!(), // should exist!
};

let mut names_iter = track_list.line_names.iter();
Expand All @@ -371,7 +375,7 @@
concat_serialize_idents(" [", "]", names, " ", dest)?;
}

if let Either::First(ref list) = *template_columns {
if let GenericGridTemplateComponent::TrackList(ref list) = *template_columns {
dest.write_str(" / ")?;
list.to_css(dest)?;
}
Expand Down Expand Up @@ -401,12 +405,13 @@
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::generics::grid::GridTemplateComponent;
use values::specified::{LengthOrPercentage, TrackSize};

pub fn parse_value<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Longhands, ParseError<'i>> {
let mut temp_rows = Either::Second(None_);
let mut temp_cols = Either::Second(None_);
let mut temp_rows = GridTemplateComponent::None;
let mut temp_cols = GridTemplateComponent::None;
let mut temp_areas = Either::Second(None_);
let mut auto_rows = TrackSize::default();
let mut auto_cols = TrackSize::default();
Expand Down
5 changes: 3 additions & 2 deletions components/style/values/computed/mod.rs
Expand Up @@ -19,6 +19,7 @@ use std::fmt;
use style_traits::ToCss;
use super::{CSSFloat, CSSInteger, RGBA};
use super::generics::grid::{TrackBreadth as GenericTrackBreadth, TrackSize as GenericTrackSize};
use super::generics::grid::GridTemplateComponent as GenericGridTemplateComponent;
use super::generics::grid::TrackList as GenericTrackList;
use super::specified;

Expand Down Expand Up @@ -565,8 +566,8 @@ pub type TrackSize = GenericTrackSize<LengthOrPercentage>;
/// (could also be `<auto-track-list>` or `<explicit-track-list>`)
pub type TrackList = GenericTrackList<TrackSize>;

/// `<track-list> | none`
pub type TrackListOrNone = Either<TrackList, None_>;
/// `<grid-template-rows> | <grid-template-columns>`
pub type GridTemplateComponent = GenericGridTemplateComponent<TrackSize>;

impl ClipRectOrAuto {
/// Return an auto (default for clip-rect and image-region) value
Expand Down
14 changes: 14 additions & 0 deletions components/style/values/generics/grid.rs
Expand Up @@ -650,3 +650,17 @@ impl ToCss for LineNameList {

impl ComputedValueAsSpecified for LineNameList {}
no_viewport_percentage!(LineNameList);

/// Variants for `<grid-template-rows> | <grid-template-columns>`
/// Subgrid deferred to Level 2 spec due to lack of implementation.
/// But it's implemented in gecko, so we have to as well.
#[derive(Clone, PartialEq, Debug, ToCss)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum GridTemplateComponent<L> {
/// `none` value.
None,
/// The grid `<track-list>`
TrackList(TrackList<L>),
/// A `subgrid <line-name-list>?`
Subgrid(LineNameList),
}
52 changes: 50 additions & 2 deletions components/style/values/specified/grid.rs
Expand Up @@ -12,8 +12,8 @@ use std::ascii::AsciiExt;
use style_traits::{HasViewportPercentage, ParseError, StyleParseError};
use values::{CSSFloat, CustomIdent, Either};
use values::computed::{self, Context, ToComputedValue};
use values::generics::grid::{RepeatCount, TrackBreadth, TrackKeyword, TrackRepeat};
use values::generics::grid::{TrackSize, TrackList, TrackListType};
use values::generics::grid::{GridTemplateComponent, RepeatCount, TrackBreadth, TrackKeyword, TrackRepeat};
use values::generics::grid::{LineNameList, TrackSize, TrackList, TrackListType};
use values::specified::LengthOrPercentage;

/// Parse a single flexible length.
Expand Down Expand Up @@ -349,3 +349,51 @@ impl ToComputedValue for TrackList<TrackSizeOrRepeat> {
}
}
}

impl Parse for GridTemplateComponent<TrackSizeOrRepeat> { // FIXME: Derive Parse (probably with None_)
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
if input.try(|i| i.expect_ident_matching("none")).is_ok() {
return Ok(GridTemplateComponent::None)
}

if let Ok(t) = input.try(|i| TrackList::parse(context, i)) {
return Ok(GridTemplateComponent::TrackList(t))
}

LineNameList::parse(context, input).map(GridTemplateComponent::Subgrid)
}
}

impl HasViewportPercentage for GridTemplateComponent<TrackSizeOrRepeat> {
#[inline]
fn has_viewport_percentage(&self) -> bool {
match *self {
GridTemplateComponent::TrackList(ref l) => l.has_viewport_percentage(),
_ => false,
}
}
}

impl ToComputedValue for GridTemplateComponent<TrackSizeOrRepeat> {
type ComputedValue = GridTemplateComponent<TrackSize<computed::LengthOrPercentage>>;

#[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
match *self {
GridTemplateComponent::None => GridTemplateComponent::None,
GridTemplateComponent::TrackList(ref l) => GridTemplateComponent::TrackList(l.to_computed_value(context)),
GridTemplateComponent::Subgrid(ref n) => GridTemplateComponent::Subgrid(n.to_computed_value(context)),
}
}

#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
match *computed {
GridTemplateComponent::None => GridTemplateComponent::None,
GridTemplateComponent::TrackList(ref l) =>
GridTemplateComponent::TrackList(ToComputedValue::from_computed_value(l)),
GridTemplateComponent::Subgrid(ref n) =>
GridTemplateComponent::Subgrid(ToComputedValue::from_computed_value(n)),
}
}
}
5 changes: 3 additions & 2 deletions components/style/values/specified/mod.rs
Expand Up @@ -48,6 +48,7 @@ pub use self::position::{Position, PositionComponent};
pub use self::text::{InitialLetter, LetterSpacing, LineHeight, WordSpacing};
pub use self::transform::{TimingFunction, TransformOrigin};
pub use super::generics::grid::GridLine;
pub use super::generics::grid::GridTemplateComponent as GenericGridTemplateComponent;

#[cfg(feature = "gecko")]
pub mod align;
Expand Down Expand Up @@ -685,8 +686,8 @@ pub type TrackSize = GenericTrackSize<LengthOrPercentage>;
/// (could also be `<auto-track-list>` or `<explicit-track-list>`)
pub type TrackList = GenericTrackList<TrackSizeOrRepeat>;

/// `<track-list> | none`
pub type TrackListOrNone = Either<TrackList, None_>;
/// `<grid-template-rows> | <grid-template-columns>`
pub type GridTemplateComponent = GenericGridTemplateComponent<TrackSizeOrRepeat>;

#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
Expand Down
19 changes: 16 additions & 3 deletions tests/unit/style/parsing/position.rs
Expand Up @@ -191,12 +191,18 @@ fn test_grid_template_rows_columns() {
// <track-size> with <track-breadth> as <length-percentage>
assert_roundtrip_with_context!(grid_template_rows::parse, "calc(4em + 5px)");
// <track-size> with <length> followed by <track-repeat> with `<track-size>{3}` (<flex>, auto, minmax)
assert_roundtrip_with_context!(grid_template_rows::parse, "10px repeat(2, 1fr auto minmax(200px, 1fr))");
assert_roundtrip_with_context!(grid_template_rows::parse,
"10px repeat(2, 1fr auto minmax(200px, 1fr))",
"10px 1fr auto minmax(200px, 1fr) 1fr auto minmax(200px, 1fr)");
// <track-repeat> with `<track-size> <line-names>` followed by <track-size>
assert_roundtrip_with_context!(grid_template_rows::parse, "repeat(4, 10px [col-start] 250px [col-end]) 10px");
assert_roundtrip_with_context!(grid_template_rows::parse,
"repeat(2, 10px [col-start] 250px [col-end]) 10px",
"10px [col-start] 250px [col-end] 10px [col-start] 250px [col-end] 10px");
// mixture of <track-size>, <track-repeat> and <line-names>
assert_roundtrip_with_context!(grid_template_rows::parse,
"[a] auto [b] minmax(min-content, 1fr) [b c d] repeat(2, [e] 40px) repeat(5, [f g] auto [h]) [i]");
"[a] auto [b] minmax(min-content, 1fr) [b c d] repeat(2, 40px [e] 30px) [i]",
"[a] auto [b] minmax(min-content, 1fr) [b c d] 40px [e] 30px 40px [e] 30px [i]");
assert!(parse(grid_template_rows::parse, "subgrid").is_ok());

// no span allowed in <line-names>
assert!(parse(grid_template_rows::parse, "[a span] 10px").is_err());
Expand Down Expand Up @@ -232,4 +238,11 @@ fn test_computed_grid_template_rows_colums() {
assert_computed_serialization(grid_template_rows::parse,
"10px repeat(2, 1fr auto minmax(200px, 1fr))",
"10px minmax(auto, 1fr) auto minmax(200px, 1fr) minmax(auto, 1fr) auto minmax(200px, 1fr)");

assert_computed_serialization(grid_template_rows::parse,
"subgrid [a] [] repeat(auto-fill, [])", "subgrid [a] [] repeat(auto-fill, [])");

assert_computed_serialization(grid_template_rows::parse,
"subgrid [a] [b] repeat(2, [c d] [] [e]) [] repeat(auto-fill, [])",
"subgrid [a] [b] [c d] [] [e] [c d] [] [e] [] repeat(auto-fill, [])");
}

0 comments on commit 9d9d99e

Please sign in to comment.