refactor(layout): share table roles and display classification

Table construction and invariant validation maintain duplicate role enums, display mappings and diagnostic names. Use one internal role type and classification while keeping construction-only parent requirements, world validation and invalid-box errors at their existing sites.
This commit is contained in:
ldm0
2026-11-09 22:11:24 +08:00
committed by Donough Liu
parent ef345c8e67
commit 13a216cf25
4 changed files with 69 additions and 103 deletions
+2 -47
View File
@@ -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<S::NodeId>, 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<LayoutCapabilityDiagnostic>,
+1
View File
@@ -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;
+63
View File
@@ -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<Self> {
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,
}
}
}
+3 -56
View File
@@ -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<TableInvariantRole> {
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(