use super::*;
pub(in crate::layout::table) fn table_row_span_height(
row_heights: &[f32],
row_occupancy: &[bool],
row: usize,
rowspan: usize,
table_metrics: TableMetrics,
) -> f32 {
let end = (row + rowspan.max(1)).min(row_heights.len());
if row >= end {
return 0.0;
}
table_grid_height(
&row_heights[row..end],
&row_occupancy[row..end.min(row_occupancy.len())],
table_metrics,
)
}
pub(in crate::layout::table) fn table_row_block_start(
row_heights: &[f32],
row_occupancy: &[bool],
row: usize,
table_metrics: TableMetrics,
) -> f32 {
let target_occupies_grid = row_occupancy
.get(row)
.copied()
.unwrap_or_else(|| row_heights.get(row).copied().unwrap_or(0.0) > 0.0);
let mut offset = 0.0;
let mut previous_occupies_grid = false;
for (index, height) in row_heights.iter().take(row).enumerate() {
let occupies_grid = row_occupancy.get(index).copied().unwrap_or(*height > 0.0);
if !occupies_grid {
continue;
}
if previous_occupies_grid {
offset += table_metrics.spacing.vertical.length_points();
}
offset += height.max(0.0);
previous_occupies_grid = true;
}
if target_occupies_grid && previous_occupies_grid {
offset += table_metrics.spacing.vertical.length_points();
}
offset
}
pub(in crate::layout::table) fn table_grid_height(
row_heights: &[f32],
row_occupancy: &[bool],
table_metrics: TableMetrics,
) -> f32 {
row_heights
.iter()
.enumerate()
.filter_map(|(index, height)| {
row_occupancy
.get(index)
.cloned()
.unwrap_or(*height > 0.0)
.then_some(*height)
})
.sum::<f32>()
+ table_metrics.spacing.vertical.length_points()
* table_internal_vertical_gap_count(row_occupancy) as f32
}
pub(in crate::layout::table) fn table_vertical_edge_spacing(
row_occupancy: &[bool],
table_metrics: TableMetrics,
) -> f32 {
if table_metrics.border_collapse == css::BorderCollapse::Separate
&& row_occupancy.iter().any(|occupied| *occupied)
{
table_metrics.spacing.vertical.length_points()
} else {
0.0
}
}
pub(in crate::layout::table) fn table_content_height(
row_heights: &[f32],
row_occupancy: &[bool],
table_metrics: TableMetrics,
) -> f32 {
table_grid_height(row_heights, row_occupancy, table_metrics.clone())
+ table_vertical_edge_spacing(row_occupancy, table_metrics) * 2.0
}
pub(in crate::layout::table) fn repeated_table_rows_height(
row_indices: &[usize],
row_heights: &[f32],
row_occupancy: &[bool],
table_metrics: TableMetrics,
) -> f32 {
let occupied_heights = row_indices
.iter()
.filter(|row_index| {
row_occupancy
.get(**row_index)
.cloned()
.unwrap_or_else(|| row_heights.get(**row_index).cloned().unwrap_or(0.0) > 0.0)
})
.map(|row_index| row_heights.get(*row_index).cloned().unwrap_or(0.0))
.collect::<Vec<_>>();
table_grid_height(
&occupied_heights,
&vec![true; occupied_heights.len()],
table_metrics,
)
}
pub(in crate::layout::table) fn table_row_top(
grid_top: f32,
row_heights: &[f32],
row_occupancy: &[bool],
table_metrics: TableMetrics,
row: usize,
) -> f32 {
let row = row.min(row_heights.len());
let offset = row_heights[..row]
.iter()
.enumerate()
.filter_map(|(index, height)| {
row_occupancy
.get(index)
.cloned()
.unwrap_or(*height > 0.0)
.then_some(*height)
})
.sum::<f32>()
+ table_metrics.spacing.vertical.length_points()
* table_internal_vertical_gap_count_before(row_occupancy, row) as f32;
grid_top - offset
}
pub(in crate::layout::table) fn inline_table_first_occupying_row_range(
table_top: f32,
top_caption_height: f32,
table_border_widths: css::Edges,
table_padding: css::Edges,
row_heights: &[f32],
row_occupancy: &[bool],
table_metrics: TableMetrics,
) -> Option<(f32, f32)> {
let (row_index, row_height) = row_heights
.iter()
.cloned()
.enumerate()
.find(|(index, _)| row_occupancy.get(*index).cloned().unwrap_or(false))?;
let grid_top = table_top
- top_caption_height
- table_border_widths.top
- table_padding.top
- table_vertical_edge_spacing(row_occupancy, table_metrics.clone());
let row_top = table_row_top(
grid_top,
row_heights,
row_occupancy,
table_metrics,
row_index,
);
Some((row_top, row_top - row_height))
}
fn table_internal_vertical_gap_count(row_occupancy: &[bool]) -> usize {
row_occupancy
.iter()
.filter(|occupied| **occupied)
.count()
.saturating_sub(1)
}
pub(in crate::layout::table) fn table_internal_vertical_gap_count_before(
row_occupancy: &[bool],
row: usize,
) -> usize {
let row = row.min(row_occupancy.len());
let occupied_before = row_occupancy[..row]
.iter()
.filter(|occupied| **occupied)
.count();
if occupied_before == 0 {
return 0;
}
if row_occupancy[row..].iter().any(|occupied| *occupied) {
occupied_before
} else {
occupied_before.saturating_sub(1)
}
}
pub(in crate::layout::table) fn table_row_is_collapsed(style: &ComputedStyle) -> bool {
style.visibility == Visibility::Collapse
}
pub(in crate::layout::table) fn table_horizontal_borders(
style: &ComputedStyle,
) -> NonContentLength {
let borders = table_cell_used_border_edges(style);
non_content_pt(borders.left + borders.right)
}
pub(in crate::layout::table) fn table_vertical_borders(style: &ComputedStyle) -> NonContentLength {
let borders = table_cell_used_border_edges(style);
non_content_pt(borders.top + borders.bottom)
}
pub(in crate::layout::table) fn table_cell_used_border_edges(style: &ComputedStyle) -> css::Edges {
let mut borders = used_border_widths(style);
if style.border_collapse == css::BorderCollapse::Collapse {
borders.top *= 0.5;
borders.right *= 0.5;
borders.bottom *= 0.5;
borders.left *= 0.5;
}
borders
}
#[cfg(test)]
mod ordering_tests {
use std::collections::HashMap;
use super::*;
use crate::dom::{Node, NodeKind};
fn test_signature(name: &str) -> ElementSignature {
ElementSignature::new(name, HashMap::new())
}
fn table_row<'a>(
row_signature: ElementSignature,
row_group: &'a Element,
row_group_signature: ElementSignature,
) -> TableRow<'a> {
TableRow {
element: None,
signature: row_signature,
ancestors: Vec::new(),
row_groups: vec![TableRowGroup {
element: row_group,
signature: row_group_signature,
style: None,
}],
style: None,
cells: Vec::new(),
running_cells: Vec::new(),
}
}
#[test]
fn source_selected_header_and_footer_survive_visual_row_ordering() {
let group_nodes = [
Node::element("tbody"),
Node::element("thead"),
Node::element("thead"),
Node::element("tbody"),
Node::element("thead"),
Node::element("tfoot"),
Node::element("tbody"),
Node::element("tfoot"),
Node::element("tbody"),
Node::element("tfoot"),
];
let group_elements = group_nodes
.iter()
.map(|node| match &node.kind {
NodeKind::Element(element) => element,
NodeKind::Text(_) => unreachable!("table fixture contains only elements"),
})
.collect::<Vec<_>>();
let group_signatures = (0..group_nodes.len())
.map(|index| test_signature(&format!("group-{index}")))
.collect::<Vec<_>>();
let source_row_signatures = [
"body-1", "head-1", "head-2", "body-2", "head-3", "foot-1", "body-3", "foot-2",
"body-4", "foot-3",
]
.map(test_signature);
let rows = source_row_signatures
.iter()
.cloned()
.enumerate()
.map(|(index, row_signature)| {
table_row(
row_signature,
group_elements[index],
group_signatures[index].clone(),
)
})
.collect();
let ordering = order_table_rows(rows);
let expected_visual_order =
[1, 0, 2, 3, 4, 6, 7, 8, 9, 5].map(|index| source_row_signatures[index].clone());
assert_eq!(
ordering
.rows
.iter()
.map(|row| row.signature.clone())
.collect::<Vec<_>>(),
expected_visual_order,
);
assert_eq!(ordering.repeating_header_rows, vec![0]);
assert_eq!(ordering.repeating_footer_rows, vec![9]);
}
}