1#![allow(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(crate) use construct::AnonymousTableContent;
76pub use construct::TableBuilder;
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;
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 ) {
205 self.style = new_style.clone();
206 self.grid_style = context
207 .stylist
208 .style_for_anonymous::<ServoDangerousStyleElement>(
209 &context.guards,
210 &PseudoElement::ServoTableGrid,
211 new_style,
212 );
213 }
214}
215
216type TableSlotCoordinates = Point2D<usize, UnknownUnit>;
217pub type TableSlotOffset = Vector2D<usize, UnknownUnit>;
218
219#[derive(Debug, MallocSizeOf)]
220pub struct TableSlotCell {
221 pub(crate) context: IndependentFormattingContext,
224
225 colspan: usize,
227
228 rowspan: usize,
231}
232
233impl TableSlotCell {
234 pub fn mock_for_testing(id: usize, colspan: usize, rowspan: usize) -> Self {
235 let base = LayoutBoxBase::new(
236 BaseFragmentInfo::new_for_testing(id),
237 ComputedValues::initial_values_with_font_override(Font::initial_values()).to_arc(),
238 );
239 let contents = IndependentFormattingContextContents::Flow(BlockFormattingContext {
240 contents: BlockContainer::BlockLevelBoxes(Vec::new()),
241 contains_floats: false,
242 });
243 let propagated_data =
244 PropagatedBoxTreeData::default().disallowing_percentage_table_columns();
245 Self {
246 context: IndependentFormattingContext::new(base, contents, propagated_data),
247 colspan,
248 rowspan,
249 }
250 }
251
252 pub fn node_id(&self) -> usize {
254 self.context
255 .base
256 .base_fragment_info
257 .tag
258 .map_or(0, |tag| tag.node.0)
259 }
260}
261
262#[derive(MallocSizeOf)]
267pub enum TableSlot {
268 Cell(ArcRefCell<TableSlotCell>),
270
271 Spanned(Vec<TableSlotOffset>),
279
280 Empty,
284}
285
286impl std::fmt::Debug for TableSlot {
287 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
288 match self {
289 Self::Cell(_) => f.debug_tuple("Cell").finish(),
290 Self::Spanned(spanned) => f.debug_tuple("Spanned").field(spanned).finish(),
291 Self::Empty => write!(f, "Empty"),
292 }
293 }
294}
295
296impl TableSlot {
297 fn new_spanned(offset: TableSlotOffset) -> Self {
298 Self::Spanned(vec![offset])
299 }
300}
301
302#[derive(Debug, MallocSizeOf)]
304pub struct TableTrack {
305 base: LayoutBoxBase,
307
308 group_index: Option<usize>,
311
312 is_anonymous: bool,
315
316 shared_background_style: SharedStyle,
320}
321
322impl TableTrack {
323 fn repair_style(&mut self, new_style: &Arc<ComputedValues>) {
324 self.base.repair_style(new_style);
325 self.shared_background_style = SharedStyle::new(new_style.clone());
326 }
327}
328
329#[derive(Debug, MallocSizeOf, PartialEq)]
330pub enum TableTrackGroupType {
331 HeaderGroup,
332 FooterGroup,
333 RowGroup,
334 ColumnGroup,
335}
336
337#[derive(Debug, MallocSizeOf)]
338pub struct TableTrackGroup {
339 base: LayoutBoxBase,
341
342 group_type: TableTrackGroupType,
344
345 track_range: Range<usize>,
347
348 shared_background_style: SharedStyle,
352}
353
354impl TableTrackGroup {
355 pub(super) fn is_empty(&self) -> bool {
356 self.track_range.is_empty()
357 }
358
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)]
366pub struct TableCaption {
367 pub(crate) context: IndependentFormattingContext,
369}
370
371#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq)]
373pub(crate) struct CollapsedBorder {
374 pub style_color: BorderStyleColor,
375 pub width: Au,
376}
377
378pub(crate) type CollapsedBorderLine = Vec<CollapsedBorder>;
380
381#[derive(Clone, Debug, MallocSizeOf)]
382pub(crate) struct SpecificTableGridInfo {
383 pub collapsed_borders: PhysicalVec<Vec<CollapsedBorderLine>>,
384 pub track_sizes: PhysicalVec<Vec<Au>>,
385}
386
387pub(crate) struct TableLayoutStyle<'a> {
388 table: &'a Table,
389 layout: Option<&'a TableLayout<'a>>,
390}
391
392#[derive(Debug, MallocSizeOf)]
396pub(crate) enum TableLevelBox {
397 Caption(ArcRefCell<TableCaption>),
398 Cell(ArcRefCell<TableSlotCell>),
399 TrackGroup(ArcRefCell<TableTrackGroup>),
400 Track(ArcRefCell<TableTrack>),
401}
402
403impl TableLevelBox {
404 pub(crate) fn with_base<T>(&self, callback: impl FnOnce(&LayoutBoxBase) -> T) -> T {
405 match self {
406 TableLevelBox::Caption(caption) => callback(&caption.borrow().context.base),
407 TableLevelBox::Cell(cell) => callback(&cell.borrow().context.base),
408 TableLevelBox::TrackGroup(track_group) => callback(&track_group.borrow().base),
409 TableLevelBox::Track(track) => callback(&track.borrow().base),
410 }
411 }
412
413 pub(crate) fn with_base_mut<T>(&mut self, callback: impl FnOnce(&mut LayoutBoxBase) -> T) -> T {
414 match self {
415 TableLevelBox::Caption(caption) => callback(&mut caption.borrow_mut().context.base),
416 TableLevelBox::Cell(cell) => callback(&mut cell.borrow_mut().context.base),
417 TableLevelBox::TrackGroup(track_group) => callback(&mut track_group.borrow_mut().base),
418 TableLevelBox::Track(track) => callback(&mut track.borrow_mut().base),
419 }
420 }
421
422 pub(crate) fn repair_style(
423 &self,
424 context: &SharedStyleContext<'_>,
425 node: &ServoLayoutNode,
426 new_style: &Arc<ComputedValues>,
427 ) {
428 match self {
429 TableLevelBox::Caption(caption) => caption
430 .borrow_mut()
431 .context
432 .repair_style(context, node, new_style),
433 TableLevelBox::Cell(cell) => cell
434 .borrow_mut()
435 .context
436 .repair_style(context, node, new_style),
437 TableLevelBox::TrackGroup(track_group) => {
438 track_group.borrow_mut().repair_style(new_style);
439 },
440 TableLevelBox::Track(track) => track.borrow_mut().repair_style(new_style),
441 }
442 }
443
444 pub(crate) fn attached_to_tree(&self, layout_box: WeakLayoutBox) {
445 match self {
446 Self::Caption(caption) => caption.borrow().context.attached_to_tree(layout_box),
447 Self::Cell(cell) => cell.borrow().context.attached_to_tree(layout_box),
448 Self::TrackGroup(_) | Self::Track(_) => {
449 },
452 }
453 }
454
455 pub(crate) fn downgrade(&self) -> WeakTableLevelBox {
456 match self {
457 Self::Caption(caption) => WeakTableLevelBox::Caption(caption.downgrade()),
458 Self::Cell(cell) => WeakTableLevelBox::Cell(cell.downgrade()),
459 Self::TrackGroup(track_group) => WeakTableLevelBox::TrackGroup(track_group.downgrade()),
460 Self::Track(track) => WeakTableLevelBox::Track(track.downgrade()),
461 }
462 }
463}
464
465#[derive(Clone, Debug, MallocSizeOf)]
466pub(crate) enum WeakTableLevelBox {
467 Caption(WeakRefCell<TableCaption>),
468 Cell(WeakRefCell<TableSlotCell>),
469 TrackGroup(WeakRefCell<TableTrackGroup>),
470 Track(WeakRefCell<TableTrack>),
471}
472
473impl WeakTableLevelBox {
474 pub(crate) fn upgrade(&self) -> Option<TableLevelBox> {
475 Some(match self {
476 Self::Caption(caption) => TableLevelBox::Caption(caption.upgrade()?),
477 Self::Cell(cell) => TableLevelBox::Cell(cell.upgrade()?),
478 Self::TrackGroup(track_group) => TableLevelBox::TrackGroup(track_group.upgrade()?),
479 Self::Track(track) => TableLevelBox::Track(track.upgrade()?),
480 })
481 }
482}