diff --git a/moli-layout/src/builder.rs b/moli-layout/src/builder.rs index 68f919ed58c..35560cb0ef4 100644 --- a/moli-layout/src/builder.rs +++ b/moli-layout/src/builder.rs @@ -1225,18 +1225,7 @@ where let layout_box = world .box_by_id(id) .ok_or(LayoutError::InvalidBoxReference { index: id.index() })?; - Ok(match layout_box.style.display() { - LayoutDisplay::Table | LayoutDisplay::InlineTable => Some(TableBoxRole::Root), - LayoutDisplay::TableCaption => Some(TableBoxRole::Caption), - LayoutDisplay::TableRowGroup - | LayoutDisplay::TableHeaderGroup - | LayoutDisplay::TableFooterGroup => Some(TableBoxRole::RowGroup), - LayoutDisplay::TableColumnGroup => Some(TableBoxRole::ColumnGroup), - LayoutDisplay::TableColumn => Some(TableBoxRole::Column), - LayoutDisplay::TableRow => Some(TableBoxRole::Row), - LayoutDisplay::TableCell => Some(TableBoxRole::Cell), - _ => None, - }) + Ok(TableBoxRole::from_display(layout_box.style.display())) } fn is_inline_in_flow(&self, world: &LayoutWorld, id: LayoutBoxId) -> bool { @@ -1453,41 +1442,7 @@ fn principal_kind( } } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum TableBoxRole { - Root, - Caption, - RowGroup, - ColumnGroup, - Column, - Row, - Cell, -} - -impl TableBoxRole { - const fn requires_parent(self) -> bool { - !matches!(self, Self::Root) - } - - const fn is_direct_table_child(self) -> bool { - matches!( - self, - Self::Caption | Self::RowGroup | Self::ColumnGroup | Self::Column - ) - } - - const fn debug_name(self) -> &'static str { - match self { - Self::Root => "table-root", - Self::Caption => "table-caption", - Self::RowGroup => "table-row-group", - Self::ColumnGroup => "table-column-group", - Self::Column => "table-column", - Self::Row => "table-row", - Self::Cell => "table-cell", - } - } -} +use crate::table_role::TableRole as TableBoxRole; fn push_diagnostic( diagnostics: &mut Vec, diff --git a/moli-layout/src/lib.rs b/moli-layout/src/lib.rs index 74566980a5a..3d872b7aee7 100644 --- a/moli-layout/src/lib.rs +++ b/moli-layout/src/lib.rs @@ -37,6 +37,7 @@ mod stacking; mod style; mod stylo_to_parley; mod table; +mod table_role; mod taffy_tree; mod text; mod text_normalization; diff --git a/moli-layout/src/table_role.rs b/moli-layout/src/table_role.rs new file mode 100644 index 00000000000..9f087a18cf3 --- /dev/null +++ b/moli-layout/src/table_role.rs @@ -0,0 +1,63 @@ +//! Shared table roles used by construction and invariant validation. + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum TableRole { + Root, + Caption, + RowGroup, + ColumnGroup, + Column, + Row, + Cell, +} + +impl TableRole { + pub(crate) const fn requires_parent(self) -> bool { + !matches!(self, Self::Root) + } + + pub(crate) const fn is_direct_table_child(self) -> bool { + matches!( + self, + Self::Caption | Self::RowGroup | Self::ColumnGroup | Self::Column + ) + } + + pub(crate) const fn debug_name(self) -> &'static str { + match self { + Self::Root => "table-root", + Self::Caption => "table-caption", + Self::RowGroup => "table-row-group", + Self::ColumnGroup => "table-column-group", + Self::Column => "table-column", + Self::Row => "table-row", + Self::Cell => "table-cell", + } + } + pub(crate) const fn from_display(display: crate::LayoutDisplay) -> Option { + use crate::LayoutDisplay as Display; + match display { + Display::Table | Display::InlineTable => Some(Self::Root), + Display::TableCaption => Some(Self::Caption), + Display::TableRowGroup | Display::TableHeaderGroup | Display::TableFooterGroup => { + Some(Self::RowGroup) + } + Display::TableColumnGroup => Some(Self::ColumnGroup), + Display::TableColumn => Some(Self::Column), + Display::TableRow => Some(Self::Row), + Display::TableCell => Some(Self::Cell), + Display::None + | Display::Contents + | Display::Block + | Display::FlowRoot + | Display::Inline + | Display::InlineBlock + | Display::Flex + | Display::InlineFlex + | Display::Grid + | Display::InlineGrid + | Display::BlockListItem + | Display::InlineListItem => None, + } + } +} diff --git a/moli-layout/src/world.rs b/moli-layout/src/world.rs index 9f23e4e4c91..28fb21b7581 100644 --- a/moli-layout/src/world.rs +++ b/moli-layout/src/world.rs @@ -1152,7 +1152,7 @@ where Some(TableInvariantRole::Root) => layout_box.children.iter().all(|child| { self.box_by_id(*child) .and_then(|child| table_role(child.style.display())) - .is_some_and(TableInvariantRole::is_direct_root_child) + .is_some_and(TableInvariantRole::is_direct_table_child) }), Some(TableInvariantRole::RowGroup) => layout_box.children.iter().all(|child| { self.box_by_id(*child).is_some_and(|child| { @@ -1246,63 +1246,10 @@ where } } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum TableInvariantRole { - Root, - Caption, - RowGroup, - ColumnGroup, - Column, - Row, - Cell, -} - -impl TableInvariantRole { - const fn is_direct_root_child(self) -> bool { - matches!( - self, - Self::Caption | Self::RowGroup | Self::ColumnGroup | Self::Column - ) - } - - const fn debug_name(self) -> &'static str { - match self { - Self::Root => "table-root", - Self::Caption => "table-caption", - Self::RowGroup => "table-row-group", - Self::ColumnGroup => "table-column-group", - Self::Column => "table-column", - Self::Row => "table-row", - Self::Cell => "table-cell", - } - } -} +use crate::table_role::TableRole as TableInvariantRole; const fn table_role(display: crate::LayoutDisplay) -> Option { - use crate::LayoutDisplay as Display; - match display { - Display::Table | Display::InlineTable => Some(TableInvariantRole::Root), - Display::TableCaption => Some(TableInvariantRole::Caption), - Display::TableRowGroup | Display::TableHeaderGroup | Display::TableFooterGroup => { - Some(TableInvariantRole::RowGroup) - } - Display::TableColumnGroup => Some(TableInvariantRole::ColumnGroup), - Display::TableColumn => Some(TableInvariantRole::Column), - Display::TableRow => Some(TableInvariantRole::Row), - Display::TableCell => Some(TableInvariantRole::Cell), - Display::None - | Display::Contents - | Display::Block - | Display::FlowRoot - | Display::Inline - | Display::InlineBlock - | Display::Flex - | Display::InlineFlex - | Display::Grid - | Display::InlineGrid - | Display::BlockListItem - | Display::InlineListItem => None, - } + TableInvariantRole::from_display(display) } fn default_capability_diagnostics(