style: Replace existing GridAutoFlow struct with a bitflags! backed one.

Differential Revision: https://phabricator.services.mozilla.com/D62787
This commit is contained in:
Martin McNickle 2020-02-14 15:48:01 +00:00 committed by Emilio Cobos Álvarez
parent 0e3c122890
commit cf20c627b5
3 changed files with 75 additions and 112 deletions

View file

@ -375,7 +375,7 @@ ${helpers.predefined_type(
${helpers.predefined_type( ${helpers.predefined_type(
"grid-auto-flow", "grid-auto-flow",
"GridAutoFlow", "GridAutoFlow",
"computed::GridAutoFlow::row()", "computed::GridAutoFlow::ROW",
engines="gecko", engines="gecko",
animation_value_type="discrete", animation_value_type="discrete",
spec="https://drafts.csswg.org/css-grid/#propdef-grid-auto-flow", spec="https://drafts.csswg.org/css-grid/#propdef-grid-auto-flow",

View file

@ -549,7 +549,7 @@
use crate::properties::longhands::{grid_auto_columns, grid_auto_rows, grid_auto_flow}; use crate::properties::longhands::{grid_auto_columns, grid_auto_rows, grid_auto_flow};
use crate::values::generics::grid::GridTemplateComponent; use crate::values::generics::grid::GridTemplateComponent;
use crate::values::specified::{GenericGridTemplateComponent, ImplicitGridTracks}; use crate::values::specified::{GenericGridTemplateComponent, ImplicitGridTracks};
use crate::values::specified::position::{AutoFlow, GridAutoFlow, GridTemplateAreas}; use crate::values::specified::position::{GridAutoFlow, GridTemplateAreas};
pub fn parse_value<'i, 't>( pub fn parse_value<'i, 't>(
context: &ParserContext, context: &ParserContext,
@ -566,28 +566,28 @@
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
is_row: bool, is_row: bool,
) -> Result<GridAutoFlow, ParseError<'i>> { ) -> Result<GridAutoFlow, ParseError<'i>> {
let mut auto_flow = None; let mut track = None;
let mut dense = false; let mut dense = GridAutoFlow::empty();
for _ in 0..2 { for _ in 0..2 {
if input.try(|i| i.expect_ident_matching("auto-flow")).is_ok() { if input.try(|i| i.expect_ident_matching("auto-flow")).is_ok() {
auto_flow = if is_row { track = if is_row {
Some(AutoFlow::Row) Some(GridAutoFlow::ROW)
} else { } else {
Some(AutoFlow::Column) Some(GridAutoFlow::COLUMN)
}; };
} else if input.try(|i| i.expect_ident_matching("dense")).is_ok() { } else if input.try(|i| i.expect_ident_matching("dense")).is_ok() {
dense = true; dense = GridAutoFlow::DENSE
} else { } else {
break break
} }
} }
auto_flow.map(|flow| { if track.is_some() {
GridAutoFlow { Ok(track.unwrap() | dense)
autoflow: flow, } else {
dense: dense, Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
} }
}).ok_or(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
} }
if let Ok((rows, cols, areas)) = input.try(|i| super::grid_template::parse_grid_template(context, i)) { if let Ok((rows, cols, areas)) = input.try(|i| super::grid_template::parse_grid_template(context, i)) {
@ -637,7 +637,7 @@
self.grid_template_areas, dest); self.grid_template_areas, dest);
} }
if self.grid_auto_flow.autoflow == AutoFlow::Column { if self.grid_auto_flow.contains(GridAutoFlow::COLUMN) {
// It should fail to serialize if other branch of the if condition's values are set. // It should fail to serialize if other branch of the if condition's values are set.
if !self.grid_auto_rows.is_initial() || if !self.grid_auto_rows.is_initial() ||
!self.grid_template_columns.is_initial() { !self.grid_template_columns.is_initial() {
@ -653,7 +653,7 @@
self.grid_template_rows.to_css(dest)?; self.grid_template_rows.to_css(dest)?;
dest.write_str(" / auto-flow")?; dest.write_str(" / auto-flow")?;
if self.grid_auto_flow.dense { if self.grid_auto_flow.contains(GridAutoFlow::DENSE) {
dest.write_str(" dense")?; dest.write_str(" dense")?;
} }
@ -676,7 +676,7 @@
} }
dest.write_str("auto-flow")?; dest.write_str("auto-flow")?;
if self.grid_auto_flow.dense { if self.grid_auto_flow.contains(GridAutoFlow::DENSE) {
dest.write_str(" dense")?; dest.write_str(" dense")?;
} }

View file

@ -7,6 +7,7 @@
//! //!
//! [position]: https://drafts.csswg.org/css-backgrounds-3/#position //! [position]: https://drafts.csswg.org/css-backgrounds-3/#position
use crate::gecko_bindings::structs;
use crate::parser::{Parse, ParserContext}; use crate::parser::{Parse, ParserContext};
use crate::selector_map::PrecomputedHashMap; use crate::selector_map::PrecomputedHashMap;
use crate::str::HTML_SPACE_CHARACTERS; use crate::str::HTML_SPACE_CHARACTERS;
@ -350,66 +351,24 @@ impl Side for VerticalPositionKeyword {
} }
} }
#[derive( bitflags! {
Clone, /// Controls how the auto-placement algorithm works
Copy, /// specifying exactly how auto-placed items get flowed into the grid
Debug, #[derive(
Eq, MallocSizeOf,
MallocSizeOf, SpecifiedValueInfo,
PartialEq, ToComputedValue,
SpecifiedValueInfo, ToResolvedValue,
ToComputedValue, ToShmem
ToCss, )]
ToResolvedValue, #[value_info(other_values = "row,column,dense")]
ToShmem, pub struct GridAutoFlow: u8 {
)] /// 'row' - mutually exclusive with 'column'
/// Auto-placement algorithm Option const ROW = structs::NS_STYLE_GRID_AUTO_FLOW_ROW as u8;
pub enum AutoFlow { /// 'column' - mutually exclusive with 'row'
/// The auto-placement algorithm places items by filling each row in turn, const COLUMN = structs::NS_STYLE_GRID_AUTO_FLOW_COLUMN as u8;
/// adding new rows as necessary. /// 'dense'
Row, const DENSE = structs::NS_STYLE_GRID_AUTO_FLOW_DENSE as u8;
/// The auto-placement algorithm places items by filling each column in turn,
/// adding new columns as necessary.
Column,
}
/// If `dense` is specified, `row` is implied.
fn is_row_dense(autoflow: &AutoFlow, dense: &bool) -> bool {
*autoflow == AutoFlow::Row && *dense
}
#[derive(
Clone,
Copy,
Debug,
Eq,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
/// Controls how the auto-placement algorithm works
/// specifying exactly how auto-placed items get flowed into the grid
pub struct GridAutoFlow {
/// Specifiy how auto-placement algorithm fills each `row` or `column` in turn
#[css(contextual_skip_if = "is_row_dense")]
pub autoflow: AutoFlow,
/// Specify use `dense` packing algorithm or not
#[css(represents_keyword)]
pub dense: bool,
}
impl GridAutoFlow {
#[inline]
/// Get default `grid-auto-flow` as `row`
pub fn row() -> GridAutoFlow {
GridAutoFlow {
autoflow: AutoFlow::Row,
dense: false,
}
} }
} }
@ -419,26 +378,26 @@ impl Parse for GridAutoFlow {
_context: &ParserContext, _context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
) -> Result<GridAutoFlow, ParseError<'i>> { ) -> Result<GridAutoFlow, ParseError<'i>> {
let mut value = None; let mut track = None;
let mut dense = false; let mut dense = GridAutoFlow::empty();
while !input.is_exhausted() { while !input.is_exhausted() {
let location = input.current_source_location(); let location = input.current_source_location();
let ident = input.expect_ident()?; let ident = input.expect_ident()?;
let success = match_ignore_ascii_case! { &ident, let success = match_ignore_ascii_case! { &ident,
"row" if value.is_none() => { "row" if track.is_none() => {
value = Some(AutoFlow::Row); track = Some(GridAutoFlow::ROW);
true true
}, },
"column" if value.is_none() => { "column" if track.is_none() => {
value = Some(AutoFlow::Column); track = Some(GridAutoFlow::COLUMN);
true true
}, },
"dense" if !dense => { "dense" if dense.is_empty() => {
dense = true; dense = GridAutoFlow::DENSE;
true true
}, },
_ => false _ => false,
}; };
if !success { if !success {
return Err(location return Err(location
@ -446,47 +405,51 @@ impl Parse for GridAutoFlow {
} }
} }
if value.is_some() || dense { if track.is_some() || !dense.is_empty() {
Ok(GridAutoFlow { Ok(track.unwrap_or(GridAutoFlow::ROW) | dense)
autoflow: value.unwrap_or(AutoFlow::Row),
dense: dense,
})
} else { } else {
Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)) Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
} }
} }
} }
impl ToCss for GridAutoFlow {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
if *self == GridAutoFlow::ROW {
return dest.write_str("row");
}
if *self == GridAutoFlow::COLUMN {
return dest.write_str("column");
}
if *self == GridAutoFlow::ROW | GridAutoFlow::DENSE {
return dest.write_str("dense");
}
if *self == GridAutoFlow::COLUMN | GridAutoFlow::DENSE {
return dest.write_str("column dense");
}
debug_assert!(false, "Unknown or invalid grid-autoflow value");
Ok(())
}
}
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
impl From<u8> for GridAutoFlow { impl From<u8> for GridAutoFlow {
fn from(bits: u8) -> GridAutoFlow { fn from(bits: u8) -> GridAutoFlow {
use crate::gecko_bindings::structs; GridAutoFlow::from_bits(bits).unwrap()
GridAutoFlow {
autoflow: 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,
}
} }
} }
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
impl From<GridAutoFlow> for u8 { impl From<GridAutoFlow> for u8 {
fn from(v: GridAutoFlow) -> u8 { fn from(v: GridAutoFlow) -> u8 {
use crate::gecko_bindings::structs; v.bits
let mut result: u8 = match v.autoflow {
AutoFlow::Row => structs::NS_STYLE_GRID_AUTO_FLOW_ROW as u8,
AutoFlow::Column => structs::NS_STYLE_GRID_AUTO_FLOW_COLUMN as u8,
};
if v.dense {
result |= structs::NS_STYLE_GRID_AUTO_FLOW_DENSE as u8;
}
result
} }
} }