Skip to content

Commit

Permalink
Auto merge of #17776 - canaltinova:disabled-mochitests, r=emilio
Browse files Browse the repository at this point in the history
More grid serialization and parsing fixes

These are the bugs I discovered while I was re-enabling the disabled grid mochitests. Now all grid mochitests are passing! Additionally I converted `Vec<CustomIdent>`'s into `Box<[CustomIdent]>`. We are storing so many line names in vectors. These should help a bit.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17776)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Jul 20, 2017
2 parents 513e6b2 + ac1d15f commit 20a3b02
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 46 deletions.
14 changes: 8 additions & 6 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -1371,10 +1371,11 @@ fn static_assert() {
let ident = v.ident.as_ref().map_or(&[] as &[_], |ident| ident.0.as_slice());
self.gecko.${value.gecko}.mLineName.assign(ident);
self.gecko.${value.gecko}.mHasSpan = v.is_span;
self.gecko.${value.gecko}.mInteger = v.line_num.map(|i| {
if let Some(integer) = v.line_num {
// clamping the integer between a range
cmp::max(nsStyleGridLine_kMinLine, cmp::min(i.value(), nsStyleGridLine_kMaxLine))
}).unwrap_or(0);
self.gecko.${value.gecko}.mInteger = cmp::max(nsStyleGridLine_kMinLine,
cmp::min(integer.value(), nsStyleGridLine_kMaxLine));
}
}

pub fn copy_${value.name}_from(&mut self, other: &Self) {
Expand Down Expand Up @@ -1520,7 +1521,7 @@ fn static_assert() {
&mut ${self_grid}.mRepeatAutoLineNameListAfter, 0);
}
},
GridTemplateComponent::Subgrid(mut list) => {
GridTemplateComponent::Subgrid(list) => {
${self_grid}.set_mIsSubgrid(true);
let names_length = match list.fill_idx {
Some(_) => list.names.len() - 1,
Expand All @@ -1537,14 +1538,15 @@ fn static_assert() {
&mut ${self_grid}.mRepeatAutoLineNameListAfter, 0);
}

let mut names = list.names.into_vec();
if let Some(idx) = list.fill_idx {
${self_grid}.set_mIsAutoFill(true);
${self_grid}.mRepeatAutoIndex = idx as i16;
set_line_names(&list.names.swap_remove(idx as usize),
set_line_names(&names.swap_remove(idx as usize),
&mut ${self_grid}.mRepeatAutoLineNameListBefore);
}

for (servo_names, gecko_names) in list.names.iter().zip(${self_grid}.mLineNameLists.iter_mut()) {
for (servo_names, gecko_names) in names.iter().zip(${self_grid}.mLineNameLists.iter_mut()) {
set_line_names(servo_names, gecko_names);
}
},
Expand Down
90 changes: 77 additions & 13 deletions components/style/properties/shorthand/position.mako.rs
Expand Up @@ -252,7 +252,6 @@
-> Result<(GridTemplateComponent,
GridTemplateComponent,
Either<TemplateAreas, None_>), ParseError<'i>> {

// Other shorthand sub properties also parse `none` and `subgrid` keywords and this
// shorthand should know after these keywords there is nothing to parse. Otherwise it
// gets confused and rejects the sub properties that contains `none` or `subgrid`.
Expand All @@ -278,42 +277,43 @@
}
% endfor

let first_line_names = input.try(parse_line_names).unwrap_or(vec![]);
let first_line_names = input.try(parse_line_names).unwrap_or(vec![].into_boxed_slice());
if let Ok(s) = input.try(Parser::expect_string) {
let mut strings = vec![];
let mut values = vec![];
let mut line_names = vec![];
let mut names = first_line_names;
let mut names = first_line_names.into_vec();
let mut string = s.into_owned().into_boxed_str();
loop {
line_names.push(names);
line_names.push(names.into_boxed_slice());
strings.push(string);
let size = input.try(|i| TrackSize::parse(context, i)).unwrap_or_default();
values.push(size);
names = input.try(parse_line_names).unwrap_or(vec![]);
names = input.try(parse_line_names).unwrap_or(vec![].into_boxed_slice()).into_vec();
if let Ok(v) = input.try(parse_line_names) {
names.extend(v);
names.extend(v.into_vec());
}

string = match input.try(Parser::expect_string) {
Ok(s) => s.into_owned().into_boxed_str(),
_ => { // only the named area determines whether we should bail out
line_names.push(names);
line_names.push(names.into_boxed_slice());
break
},
};
}

if line_names.len() == values.len() {
line_names.push(vec![]); // should be one longer than track sizes
// should be one longer than track sizes
line_names.push(vec![].into_boxed_slice());
}

let template_areas = TemplateAreas::from_vec(strings)
.map_err(|()| StyleParseError::UnspecifiedError)?;
let template_rows = TrackList {
list_type: TrackListType::Normal,
values: values,
line_names: line_names,
line_names: line_names.into_boxed_slice(),
auto_repeat: None,
};

Expand Down Expand Up @@ -372,11 +372,41 @@
template_columns.to_css(dest)
},
Either::First(ref areas) => {
// The length of template-area and template-rows values should be equal.
if areas.strings.len() != template_rows.track_list_len() {
return Ok(());
}

let track_list = match *template_rows {
GenericGridTemplateComponent::TrackList(ref list) => list,
_ => unreachable!(), // should exist!
GenericGridTemplateComponent::TrackList(ref list) => {
// We should fail if there is a `repeat` function. `grid` and
// `grid-template` shorthands doesn't accept that. Only longhand accepts.
if list.auto_repeat.is_some() {
return Ok(());
}
list
},
// Others template components shouldn't exist with normal shorthand values.
// But if we need to serialize a group of longhand sub-properties for
// the shorthand, we should be able to return empty string instead of crashing.
_ => return Ok(()),
};

// We need to check some values that longhand accepts but shorthands don't.
match *template_columns {
// We should fail if there is a `repeat` function. `grid` and
// `grid-template` shorthands doesn't accept that. Only longhand accepts that.
GenericGridTemplateComponent::TrackList(ref list) if list.auto_repeat.is_some() => {
return Ok(());
},
// Also the shorthands don't accept subgrids unlike longhand.
// We should fail without an error here.
GenericGridTemplateComponent::Subgrid(_) => {
return Ok(());
},
_ => {},
}

let mut names_iter = track_list.line_names.iter();
for (((i, string), names), size) in areas.strings.iter().enumerate()
.zip(&mut names_iter)
Expand Down Expand Up @@ -428,8 +458,8 @@
use properties::longhands::{grid_auto_columns, grid_auto_rows, grid_auto_flow};
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};
use values::generics::grid::{GridTemplateComponent, TrackListType};
use values::specified::{GenericGridTemplateComponent, LengthOrPercentage, TrackSize};

pub fn parse_value<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Longhands, ParseError<'i>> {
Expand Down Expand Up @@ -507,6 +537,14 @@

impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
// `grid` shorthand resets these properties. If they are not zero, that means they
// are changed by longhands and in that case we should fail serializing `grid`.
if *self.grid_row_gap != LengthOrPercentage::zero() ||
*self.grid_column_gap != LengthOrPercentage::zero() {
return Ok(());
}


if *self.grid_template_areas != Either::Second(None_) ||
(*self.grid_template_rows != GridTemplateComponent::None &&
*self.grid_template_columns != GridTemplateComponent::None) ||
Expand All @@ -517,6 +555,19 @@
}

if self.grid_auto_flow.autoflow == AutoFlow::Column {
// It should fail to serialize if other branch of the if condition's values are set.
if *self.grid_auto_rows != TrackSize::default() ||
*self.grid_template_columns != GridTemplateComponent::None {
return Ok(());
}

// It should fail to serialize if template-rows value is not Explicit.
if let GenericGridTemplateComponent::TrackList(ref list) = *self.grid_template_rows {
if list.list_type != TrackListType::Explicit {
return Ok(());
}
}

self.grid_template_rows.to_css(dest)?;
dest.write_str(" / auto-flow")?;
if self.grid_auto_flow.dense {
Expand All @@ -528,6 +579,19 @@
self.grid_auto_columns.to_css(dest)?;
}
} else {
// It should fail to serialize if other branch of the if condition's values are set.
if *self.grid_auto_columns != TrackSize::default() ||
*self.grid_template_rows != GridTemplateComponent::None {
return Ok(());
}

// It should fail to serialize if template-column value is not Explicit.
if let GenericGridTemplateComponent::TrackList(ref list) = *self.grid_template_columns {
if list.list_type != TrackListType::Explicit {
return Ok(());
}
}

dest.write_str("auto-flow")?;
if self.grid_auto_flow.dense {
dest.write_str(" dense")?;
Expand Down
68 changes: 54 additions & 14 deletions components/style/values/generics/grid.rs
Expand Up @@ -119,9 +119,7 @@ impl Parse for GridLine {
if i.value() <= 0 { // disallow negative integers for grid spans
return Err(StyleParseError::UnspecifiedError.into())
}
} else if grid_line.ident.is_some() { // integer could be omitted
grid_line.line_num = Some(Integer::new(1));
} else {
} else if grid_line.ident.is_none() { // integer could be omitted
return Err(StyleParseError::UnspecifiedError.into())
}
}
Expand Down Expand Up @@ -205,20 +203,18 @@ impl<L: ToComputedValue> ToComputedValue for TrackBreadth<L> {
///
/// https://drafts.csswg.org/css-grid/#typedef-track-size
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, HasViewportPercentage, PartialEq, ToCss)]
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
pub enum TrackSize<L> {
/// A flexible `<track-breadth>`
Breadth(TrackBreadth<L>),
/// A `minmax` function for a range over an inflexible `<track-breadth>`
/// and a flexible `<track-breadth>`
///
/// https://drafts.csswg.org/css-grid/#valdef-grid-template-columns-minmax
#[css(comma, function)]
Minmax(TrackBreadth<L>, TrackBreadth<L>),
/// A `fit-content` function.
///
/// https://drafts.csswg.org/css-grid/#valdef-grid-template-columns-fit-content
#[css(function)]
FitContent(L),
}

Expand Down Expand Up @@ -261,6 +257,34 @@ impl<L: PartialEq> TrackSize<L> {
}
}

impl<L: ToCss> ToCss for TrackSize<L> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
TrackSize::Breadth(ref breadth) => breadth.to_css(dest),
TrackSize::Minmax(ref min, ref max) => {
// According to gecko minmax(auto, <flex>) is equivalent to <flex>,
// and both are serialized as <flex>.
if let TrackBreadth::Keyword(TrackKeyword::Auto) = *min {
if let TrackBreadth::Flex(_) = *max {
return max.to_css(dest);
}
}

dest.write_str("minmax(")?;
min.to_css(dest)?;
dest.write_str(", ")?;
max.to_css(dest)?;
dest.write_str(")")
},
TrackSize::FitContent(ref lop) => {
dest.write_str("fit-content(")?;
lop.to_css(dest)?;
dest.write_str(")")
},
}
}
}

impl<L: ToComputedValue> ToComputedValue for TrackSize<L> {
type ComputedValue = TrackSize<L::ComputedValue>;

Expand Down Expand Up @@ -330,8 +354,13 @@ pub enum RepeatCount {

impl Parse for RepeatCount {
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
if let Ok(i) = input.try(|i| Integer::parse(context, i)) {
// Maximum number of repeat is 10000. The greater numbers should be clamped.
const MAX_LINE: i32 = 10000;
if let Ok(mut i) = input.try(|i| Integer::parse(context, i)) {
if i.value() > 0 {
if i.value() > MAX_LINE {
i = Integer::new(MAX_LINE);
}
Ok(RepeatCount::Number(i))
} else {
Err(StyleParseError::UnspecifiedError.into())
Expand Down Expand Up @@ -362,7 +391,7 @@ pub struct TrackRepeat<L> {
/// If there's no `<line-names>`, then it's represented by an empty vector.
/// For N `<track-size>` values, there will be N+1 `<line-names>`, and so this vector's
/// length is always one value more than that of the `<track-size>`.
pub line_names: Vec<Vec<CustomIdent>>,
pub line_names: Box<[Box<[CustomIdent]>]>,
/// `<track-size>` values.
pub track_sizes: Vec<TrackSize<L>>,
}
Expand Down Expand Up @@ -423,7 +452,8 @@ impl<L: Clone> TrackRepeat<L> {
let mut names_iter = self.line_names.iter();
for (size, names) in self.track_sizes.iter().zip(&mut names_iter) {
prev_names.extend_from_slice(&names);
line_names.push(mem::replace(&mut prev_names, vec![]));
let vec = mem::replace(&mut prev_names, vec![]);
line_names.push(vec.into_boxed_slice());
track_sizes.push(size.clone());
}

Expand All @@ -432,11 +462,11 @@ impl<L: Clone> TrackRepeat<L> {
}
}

line_names.push(prev_names);
line_names.push(prev_names.into_boxed_slice());
TrackRepeat {
count: self.count,
track_sizes: track_sizes,
line_names: line_names,
line_names: line_names.into_boxed_slice(),
}

} else { // if it's auto-fit/auto-fill, then it's left to the layout.
Expand Down Expand Up @@ -517,7 +547,7 @@ pub struct TrackList<T> {
/// If there's no `<line-names>`, then it's represented by an empty vector.
/// For N values, there will be N+1 `<line-names>`, and so this vector's
/// length is always one value more than that of the `<track-size>`.
pub line_names: Vec<Vec<CustomIdent>>,
pub line_names: Box<[Box<[CustomIdent]>]>,
/// `<auto-repeat>` value. There can only be one `<auto-repeat>` in a TrackList.
pub auto_repeat: Option<TrackRepeat<T>>,
}
Expand Down Expand Up @@ -574,7 +604,7 @@ impl<T: ToCss> ToCss for TrackList<T> {
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct LineNameList {
/// The optional `<line-name-list>`
pub names: Vec<Vec<CustomIdent>>,
pub names: Box<[Box<[CustomIdent]>]>,
/// Indicates the line name that requires `auto-fill`
pub fill_idx: Option<u32>,
}
Expand Down Expand Up @@ -626,7 +656,7 @@ impl Parse for LineNameList {
}

Ok(LineNameList {
names: line_names,
names: line_names.into_boxed_slice(),
fill_idx: fill_idx,
})
}
Expand Down Expand Up @@ -677,3 +707,13 @@ pub enum GridTemplateComponent<L> {
/// A `subgrid <line-name-list>?`
Subgrid(LineNameList),
}

impl<L> GridTemplateComponent<L> {
/// Returns length of the <track-list>s <track-size>
pub fn track_list_len(&self) -> usize {
match *self {
GridTemplateComponent::TrackList(ref tracklist) => tracklist.values.len(),
_ => 0,
}
}
}

0 comments on commit 20a3b02

Please sign in to comment.