1#![expect(rustdoc::private_intra_doc_links)]
5
6mod construct;
69mod layout;
70
71use std::ops::Range;
72
73use app_units::Au;
74use atomic_refcell::AtomicRef;
75pub use construct::TableBuilder;
76pub(crate) use construct::{AnonymousTableContent, AnonymousTableContentElement};
77use euclid::{Point2D, Size2D, UnknownUnit, Vector2D};
78use malloc_size_of_derive::MallocSizeOf;
79use script::layout_dom::{ServoDangerousStyleElement, ServoLayoutNode};
80use servo_arc::Arc;
81use style::context::SharedStyleContext;
82use style::properties::ComputedValues;
83use style::properties::style_structs::Font;
84use style::selector_parser::PseudoElement;
85
86use super::flow::BlockFormattingContext;
87use crate::cell::{ArcRefCell, WeakRefCell};
88use crate::dom::WeakLayoutBox;
89use crate::flow::BlockContainer;
90use crate::formatting_contexts::{
91 IndependentFormattingContext, IndependentFormattingContextContents,
92};
93use crate::fragment_tree::{BaseFragmentInfo, Fragment};
94use crate::geom::PhysicalVec;
95use crate::layout_box_base::LayoutBoxBase;
96use crate::style_ext::BorderStyleColor;
97use crate::table::layout::TableLayout;
98use crate::{PropagatedBoxTreeData, SharedStyle};
99
100pub type TableSize = Size2D<usize, UnknownUnit>;
101
102#[derive(Debug, MallocSizeOf)]
103pub struct Table {
104 style: Arc<ComputedValues>,
108
109 grid_style: Arc<ComputedValues>,
112
113 grid_base_fragment_info: BaseFragmentInfo,
116
117 pub captions: Vec<ArcRefCell<TableCaption>>,
119
120 pub column_groups: Vec<ArcRefCell<TableTrackGroup>>,
122
123 pub columns: Vec<ArcRefCell<TableTrack>>,
126
127 pub row_groups: Vec<ArcRefCell<TableTrackGroup>>,
129
130 pub rows: Vec<ArcRefCell<TableTrack>>,
132
133 pub slots: Vec<Vec<TableSlot>>,
135
136 pub size: TableSize,
138
139 anonymous: bool,
141
142 percentage_columns_allowed_for_inline_content_sizes: bool,
144}
145
146impl Table {
147 pub(crate) fn new(
148 style: Arc<ComputedValues>,
149 grid_style: Arc<ComputedValues>,
150 base_fragment_info: BaseFragmentInfo,
151 percentage_columns_allowed_for_inline_content_sizes: bool,
152 ) -> Self {
153 Self {
154 style,
155 grid_style,
156 grid_base_fragment_info: base_fragment_info,
157 captions: Vec::new(),
158 column_groups: Vec::new(),
159 columns: Vec::new(),
160 row_groups: Vec::new(),
161 rows: Vec::new(),
162 slots: Vec::new(),
163 size: TableSize::zero(),
164 anonymous: false,
165 percentage_columns_allowed_for_inline_content_sizes,
166 }
167 }
168
169 fn get_slot(&self, coords: TableSlotCoordinates) -> Option<&TableSlot> {
172 self.slots.get(coords.y)?.get(coords.x)
173 }
174
175 fn resolve_first_cell_coords(
176 &self,
177 coords: TableSlotCoordinates,
178 ) -> Option<TableSlotCoordinates> {
179 match self.get_slot(coords) {
180 Some(&TableSlot::Cell(_)) => Some(coords),
181 Some(TableSlot::Spanned(offsets)) => Some(coords - offsets[0]),
182 _ => None,
183 }
184 }
185
186 fn resolve_first_cell(
187 &self,
188 coords: TableSlotCoordinates,
189 ) -> Option<AtomicRef<'_, TableSlotCell>> {
190 let resolved_coords = self.resolve_first_cell_coords(coords)?;
191 let slot = self.get_slot(resolved_coords);
192 match slot {
193 Some(TableSlot::Cell(cell)) => Some(cell.borrow()),
194 _ => unreachable!(
195 "Spanned slot should not point to an empty cell or another spanned slot."
196 ),
197 }
198 }
199
200 pub(crate) fn repair_style(
201 &mut self,
202 context: &SharedStyleContext,
203 new_style: &Arc<ComputedValues>,
204 base: &LayoutBoxBase,
205 ) {
206 self.style = new_style.clone();
207 let new_grid_style = context
208 .stylist
209 .style_for_anonymous::<ServoDangerousStyleElement>(
210 &context.guards,
211 &PseudoElement::ServoTableGrid,
212 new_style,
213 );
214
215 for fragment in base.fragments.borrow().iter() {
220 if let Some(children) = fragment.children() {
221 for child_fragment in &*children {
222 if let Fragment::Box(box_fragment) = child_fragment &&
223 Arc::ptr_eq(&self.grid_style, &box_fragment.style.borrow())
224 {
225 child_fragment.repair_style(&new_grid_style);
226 }
227 }
228 }
229 fragment.repair_style(new_style);
230 }
231 self.grid_style = new_grid_style;
232 }
233
234 pub(crate) fn subtree_size(&self) -> usize {
235 self.slots
236 .iter()
237 .flat_map(|row| row.iter())
238 .map(TableSlot::subtree_size)
239 .sum()
240 }
241}
242
243type TableSlotCoordinates = Point2D<usize, UnknownUnit>;
244pub type TableSlotOffset = Vector2D<usize, UnknownUnit>;
245
246#[derive(Debug, MallocSizeOf)]
247pub struct TableSlotCell {
248 pub(crate) context: IndependentFormattingContext,
251
252 colspan: usize,
254
255 rowspan: usize,
258}
259
260impl TableSlotCell {
261 pub fn mock_for_testing(id: usize, colspan: usize, rowspan: usize) -> Self {
262 let base = LayoutBoxBase::new(
263 BaseFragmentInfo::new_for_testing(id),
264 ComputedValues::initial_values_with_font_override(Font::initial_values()).to_arc(),
265 );
266 let contents = IndependentFormattingContextContents::Flow(BlockFormattingContext {
267 contents: BlockContainer::BlockLevelBoxes(Vec::new()),
268 contains_floats: false,
269 is_table_cell: true,
270 textarea_size: None,
271 });
272 let propagated_data =
273 PropagatedBoxTreeData::default().disallowing_percentage_table_columns();
274 Self {
275 context: IndependentFormattingContext::new(base, contents, propagated_data),
276 colspan,
277 rowspan,
278 }
279 }
280
281 pub fn node_id(&self) -> usize {
283 self.context
284 .base
285 .base_fragment_info
286 .tag
287 .map_or(0, |tag| tag.node.0)
288 }
289}
290
291#[derive(MallocSizeOf)]
296pub enum TableSlot {
297 Cell(ArcRefCell<TableSlotCell>),
299
300 Spanned(Vec<TableSlotOffset>),
308
309 Empty,
313}
314
315impl std::fmt::Debug for TableSlot {
316 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
317 match self {
318 Self::Cell(_) => f.debug_tuple("Cell").finish(),
319 Self::Spanned(spanned) => f.debug_tuple("Spanned").field(spanned).finish(),
320 Self::Empty => write!(f, "Empty"),
321 }
322 }
323}
324
325impl TableSlot {
326 fn new_spanned(offset: TableSlotOffset) -> Self {
327 Self::Spanned(vec![offset])
328 }
329
330 pub(crate) fn subtree_size(&self) -> usize {
331 match self {
332 TableSlot::Cell(cell) => cell.borrow().context.subtree_size(),
333 TableSlot::Spanned(..) | TableSlot::Empty => 0,
334 }
335 }
336}
337
338#[derive(Debug, MallocSizeOf)]
340pub struct TableTrack {
341 base: LayoutBoxBase,
343
344 group_index: Option<usize>,
347
348 is_anonymous: bool,
351
352 shared_background_style: SharedStyle,
356}
357
358impl TableTrack {
359 fn repair_style(&mut self, new_style: &Arc<ComputedValues>) {
360 self.base.repair_style(new_style);
361 self.shared_background_style = SharedStyle::new(new_style.clone());
362 }
363}
364
365#[derive(Debug, MallocSizeOf, PartialEq)]
366pub enum TableTrackGroupType {
367 HeaderGroup,
368 FooterGroup,
369 RowGroup,
370 ColumnGroup,
371}
372
373#[derive(Debug, MallocSizeOf)]
374pub struct TableTrackGroup {
375 base: LayoutBoxBase,
377
378 group_type: TableTrackGroupType,
380
381 track_range: Range<usize>,
383
384 shared_background_style: SharedStyle,
388}
389
390impl TableTrackGroup {
391 pub(super) fn is_empty(&self) -> bool {
392 self.track_range.is_empty()
393 }
394
395 fn repair_style(&mut self, new_style: &Arc<ComputedValues>) {
396 self.base.repair_style(new_style);
397 self.shared_background_style = SharedStyle::new(new_style.clone());
398 }
399}
400
401#[derive(Debug, MallocSizeOf)]
402pub struct TableCaption {
403 pub(crate) context: IndependentFormattingContext,
405}
406
407#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq)]
409pub(crate) struct CollapsedBorder {
410 pub style_color: BorderStyleColor,
411 pub width: Au,
412}
413
414pub(crate) type CollapsedBorderLine = Vec<CollapsedBorder>;
416
417#[derive(Clone, Debug, MallocSizeOf)]
418pub(crate) struct SpecificTableGridInfo {
419 pub collapsed_borders: PhysicalVec<Vec<CollapsedBorderLine>>,
420 pub track_sizes: PhysicalVec<Vec<Au>>,
421}
422
423pub(crate) struct TableLayoutStyle<'a> {
424 table: &'a Table,
425 layout: Option<&'a TableLayout<'a>>,
426}
427
428#[derive(Debug, MallocSizeOf)]
432pub(crate) enum TableLevelBox {
433 Caption(ArcRefCell<TableCaption>),
434 Cell(ArcRefCell<TableSlotCell>),
435 TrackGroup(ArcRefCell<TableTrackGroup>),
436 Track(ArcRefCell<TableTrack>),
437}
438
439impl TableLevelBox {
440 pub(crate) fn with_base<T>(&self, callback: impl FnOnce(&LayoutBoxBase) -> T) -> T {
441 match self {
442 TableLevelBox::Caption(caption) => callback(&caption.borrow().context.base),
443 TableLevelBox::Cell(cell) => callback(&cell.borrow().context.base),
444 TableLevelBox::TrackGroup(track_group) => callback(&track_group.borrow().base),
445 TableLevelBox::Track(track) => callback(&track.borrow().base),
446 }
447 }
448
449 pub(crate) fn with_base_mut<T>(&mut self, callback: impl FnOnce(&mut LayoutBoxBase) -> T) -> T {
450 match self {
451 TableLevelBox::Caption(caption) => callback(&mut caption.borrow_mut().context.base),
452 TableLevelBox::Cell(cell) => callback(&mut cell.borrow_mut().context.base),
453 TableLevelBox::TrackGroup(track_group) => callback(&mut track_group.borrow_mut().base),
454 TableLevelBox::Track(track) => callback(&mut track.borrow_mut().base),
455 }
456 }
457
458 pub(crate) fn repair_style(
459 &self,
460 context: &SharedStyleContext<'_>,
461 node: &ServoLayoutNode,
462 new_style: &Arc<ComputedValues>,
463 ) {
464 match self {
465 TableLevelBox::Caption(caption) => caption
466 .borrow_mut()
467 .context
468 .repair_style(context, node, new_style),
469 TableLevelBox::Cell(cell) => cell
470 .borrow_mut()
471 .context
472 .repair_style(context, node, new_style),
473 TableLevelBox::TrackGroup(track_group) => {
474 track_group.borrow_mut().repair_style(new_style);
475 },
476 TableLevelBox::Track(track) => track.borrow_mut().repair_style(new_style),
477 }
478 }
479
480 pub(crate) fn attached_to_tree(&self, layout_box: WeakLayoutBox) {
481 match self {
482 Self::Caption(caption) => caption.borrow().context.attached_to_tree(layout_box),
483 Self::Cell(cell) => cell.borrow().context.attached_to_tree(layout_box),
484 Self::TrackGroup(_) | Self::Track(_) => {
485 },
488 }
489 }
490
491 pub(crate) fn downgrade(&self) -> WeakTableLevelBox {
492 match self {
493 Self::Caption(caption) => WeakTableLevelBox::Caption(caption.downgrade()),
494 Self::Cell(cell) => WeakTableLevelBox::Cell(cell.downgrade()),
495 Self::TrackGroup(track_group) => WeakTableLevelBox::TrackGroup(track_group.downgrade()),
496 Self::Track(track) => WeakTableLevelBox::Track(track.downgrade()),
497 }
498 }
499}
500
501#[derive(Clone, Debug, MallocSizeOf)]
502pub(crate) enum WeakTableLevelBox {
503 Caption(WeakRefCell<TableCaption>),
504 Cell(WeakRefCell<TableSlotCell>),
505 TrackGroup(WeakRefCell<TableTrackGroup>),
506 Track(WeakRefCell<TableTrack>),
507}
508
509impl WeakTableLevelBox {
510 pub(crate) fn upgrade(&self) -> Option<TableLevelBox> {
511 Some(match self {
512 Self::Caption(caption) => TableLevelBox::Caption(caption.upgrade()?),
513 Self::Cell(cell) => TableLevelBox::Cell(cell.upgrade()?),
514 Self::TrackGroup(track_group) => TableLevelBox::TrackGroup(track_group.upgrade()?),
515 Self::Track(track) => TableLevelBox::Track(track.upgrade()?),
516 })
517 }
518}