mirror of
https://github.com/servo/servo.git
synced 2025-08-09 15:35:34 +01:00
style: Replace existing GridAutoFlow struct with a bitflags!
backed one.
Differential Revision: https://phabricator.services.mozilla.com/D62787
This commit is contained in:
parent
0e3c122890
commit
cf20c627b5
3 changed files with 75 additions and 112 deletions
|
@ -7,6 +7,7 @@
|
|||
//!
|
||||
//! [position]: https://drafts.csswg.org/css-backgrounds-3/#position
|
||||
|
||||
use crate::gecko_bindings::structs;
|
||||
use crate::parser::{Parse, ParserContext};
|
||||
use crate::selector_map::PrecomputedHashMap;
|
||||
use crate::str::HTML_SPACE_CHARACTERS;
|
||||
|
@ -350,66 +351,24 @@ impl Side for VerticalPositionKeyword {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone,
|
||||
Copy,
|
||||
Debug,
|
||||
Eq,
|
||||
MallocSizeOf,
|
||||
PartialEq,
|
||||
SpecifiedValueInfo,
|
||||
ToComputedValue,
|
||||
ToCss,
|
||||
ToResolvedValue,
|
||||
ToShmem,
|
||||
)]
|
||||
/// Auto-placement algorithm Option
|
||||
pub enum AutoFlow {
|
||||
/// The auto-placement algorithm places items by filling each row in turn,
|
||||
/// adding new rows as necessary.
|
||||
Row,
|
||||
/// 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,
|
||||
}
|
||||
bitflags! {
|
||||
/// Controls how the auto-placement algorithm works
|
||||
/// specifying exactly how auto-placed items get flowed into the grid
|
||||
#[derive(
|
||||
MallocSizeOf,
|
||||
SpecifiedValueInfo,
|
||||
ToComputedValue,
|
||||
ToResolvedValue,
|
||||
ToShmem
|
||||
)]
|
||||
#[value_info(other_values = "row,column,dense")]
|
||||
pub struct GridAutoFlow: u8 {
|
||||
/// 'row' - mutually exclusive with 'column'
|
||||
const ROW = structs::NS_STYLE_GRID_AUTO_FLOW_ROW as u8;
|
||||
/// 'column' - mutually exclusive with 'row'
|
||||
const COLUMN = structs::NS_STYLE_GRID_AUTO_FLOW_COLUMN as u8;
|
||||
/// 'dense'
|
||||
const DENSE = structs::NS_STYLE_GRID_AUTO_FLOW_DENSE as u8;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -419,26 +378,26 @@ impl Parse for GridAutoFlow {
|
|||
_context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
) -> Result<GridAutoFlow, ParseError<'i>> {
|
||||
let mut value = None;
|
||||
let mut dense = false;
|
||||
let mut track = None;
|
||||
let mut dense = GridAutoFlow::empty();
|
||||
|
||||
while !input.is_exhausted() {
|
||||
let location = input.current_source_location();
|
||||
let ident = input.expect_ident()?;
|
||||
let success = match_ignore_ascii_case! { &ident,
|
||||
"row" if value.is_none() => {
|
||||
value = Some(AutoFlow::Row);
|
||||
"row" if track.is_none() => {
|
||||
track = Some(GridAutoFlow::ROW);
|
||||
true
|
||||
},
|
||||
"column" if value.is_none() => {
|
||||
value = Some(AutoFlow::Column);
|
||||
"column" if track.is_none() => {
|
||||
track = Some(GridAutoFlow::COLUMN);
|
||||
true
|
||||
},
|
||||
"dense" if !dense => {
|
||||
dense = true;
|
||||
"dense" if dense.is_empty() => {
|
||||
dense = GridAutoFlow::DENSE;
|
||||
true
|
||||
},
|
||||
_ => false
|
||||
_ => false,
|
||||
};
|
||||
if !success {
|
||||
return Err(location
|
||||
|
@ -446,47 +405,51 @@ impl Parse for GridAutoFlow {
|
|||
}
|
||||
}
|
||||
|
||||
if value.is_some() || dense {
|
||||
Ok(GridAutoFlow {
|
||||
autoflow: value.unwrap_or(AutoFlow::Row),
|
||||
dense: dense,
|
||||
})
|
||||
if track.is_some() || !dense.is_empty() {
|
||||
Ok(track.unwrap_or(GridAutoFlow::ROW) | dense)
|
||||
} else {
|
||||
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")]
|
||||
impl From<u8> for GridAutoFlow {
|
||||
fn from(bits: u8) -> GridAutoFlow {
|
||||
use crate::gecko_bindings::structs;
|
||||
|
||||
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,
|
||||
}
|
||||
GridAutoFlow::from_bits(bits).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "gecko")]
|
||||
impl From<GridAutoFlow> for u8 {
|
||||
fn from(v: GridAutoFlow) -> u8 {
|
||||
use crate::gecko_bindings::structs;
|
||||
|
||||
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
|
||||
v.bits
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue