use super::model::GridAxisTopology;
use super::*;
pub(super) struct PositionedGridStaticContext<'a> {
pub(super) container_style: &'a ComputedStyle,
pub(super) stylesheets: &'a Stylesheets<'a>,
pub(super) inner_x: f32,
pub(super) inner_width: PhysicalContentWidth,
pub(super) content_top: f32,
pub(super) definite_content_height: Option<PhysicalContentHeight>,
pub(super) content_height: PhysicalContentHeight,
pub(super) columns: &'a GridAxisTopology,
pub(super) rows: &'a GridAxisTopology,
pub(super) column_line_offsets: &'a [f32],
pub(super) row_line_offsets: &'a [f32],
pub(super) positioning_containing_block: Option<ContainingBlock>,
}
#[derive(Debug, Clone, Copy)]
enum GridAbsposStaticPositionSource {
ContentEdges,
PlacementArea { padding_box: ContainingBlock },
}
#[derive(Debug, Clone)]
pub(in crate::layout) struct GridPositioningScope {
container_style: ComputedStyle,
inner_x: f32,
inner_width: PhysicalContentWidth,
content_top: f32,
content_height: PhysicalContentHeight,
columns: GridAxisTopology,
rows: GridAxisTopology,
containing_block: ContainingBlock,
}
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct GridDescendantContainingBlock {
pub(in crate::layout) containing_block: ContainingBlock,
}
fn grid_abspos_static_position_area(
source: GridAbsposStaticPositionSource,
placement_area: PageTopRect,
child_style: &ComputedStyle,
container_style: &ComputedStyle,
) -> PageTopRect {
let GridAbsposStaticPositionSource::PlacementArea { padding_box } = source else {
return placement_area;
};
let mut left = placement_area.x();
let mut right = left + placement_area.width();
let mut top = placement_area.top_y();
let mut bottom = placement_area.bottom_y();
let padding = padding_box.rect;
let axes = WritingModeAxes::new(
container_style.writing_mode,
container_style.used_direction(),
);
let mut use_padding_edge = |placement: &css::GridPlacement, side: LogicalSide| {
if !matches!(placement, css::GridPlacement::Auto) {
return;
}
match axes.physical_side(side) {
PhysicalSide::Left => left = padding.x(),
PhysicalSide::Right => right = padding.x() + padding.width(),
PhysicalSide::Top => top = padding.top_y(),
PhysicalSide::Bottom => bottom = padding.bottom_y(),
}
};
use_padding_edge(&child_style.grid_column_start, LogicalSide::InlineStart);
use_padding_edge(&child_style.grid_column_end, LogicalSide::InlineEnd);
use_padding_edge(&child_style.grid_row_start, LogicalSide::BlockStart);
use_padding_edge(&child_style.grid_row_end, LogicalSide::BlockEnd);
PageTopRect::new(
left.min(right),
top.max(bottom),
(right - left).abs(),
(top - bottom).abs(),
)
}
fn grid_positioned_child_static_rect(
source: GridAbsposStaticPositionSource,
area: PageTopRect,
child_style: &ComputedStyle,
container_style: &ComputedStyle,
) -> PositionedChildStaticRect {
let left = area.x();
let right = left + area.width();
let static_rect = match source {
GridAbsposStaticPositionSource::ContentEdges => {
PositionedChildStaticRect::new(left, right, area.top_y())
}
GridAbsposStaticPositionSource::PlacementArea { .. } => {
PositionedChildStaticRect::with_containing_block(
left,
right,
area.top_y(),
ContainingBlock::from_page_top_rect(area),
)
}
};
static_rect.with_static_alignment(grid_abspos_static_alignment(
area,
child_style,
container_style,
))
}
impl GridPositioningScope {
pub(super) fn new(
container_style: &ComputedStyle,
geometry: GridPositioningGeometry<'_>,
containing_block: ContainingBlock,
) -> Self {
Self {
container_style: container_style.clone(),
inner_x: geometry.inner_x,
inner_width: geometry.inner_width,
content_top: geometry.content_top,
content_height: geometry.content_height,
columns: geometry.columns.clone(),
rows: geometry.rows.clone(),
containing_block,
}
}
pub(in crate::layout) fn descendant_containing_block(
&self,
style: &ComputedStyle,
containing_block: ContainingBlock,
) -> Option<GridDescendantContainingBlock> {
(containing_block == self.containing_block).then(|| {
let content_area = PageTopRect::new(
self.inner_x,
self.content_top,
self.inner_width.points(),
self.content_height.points(),
);
let placement_area = grid_explicit_placement_area(
&self.container_style,
style,
content_area,
&self.columns,
&self.rows,
)
.unwrap_or(content_area);
let area = grid_abspos_static_position_area(
GridAbsposStaticPositionSource::PlacementArea {
padding_box: self.containing_block,
},
placement_area,
style,
&self.container_style,
);
let placement_selects_grid_area = !matches!(
(
&style.grid_column_start,
&style.grid_column_end,
&style.grid_row_start,
&style.grid_row_end,
),
(
css::GridPlacement::Auto,
css::GridPlacement::Auto,
css::GridPlacement::Auto,
css::GridPlacement::Auto,
)
);
GridDescendantContainingBlock {
containing_block: if placement_selects_grid_area {
ContainingBlock::from_page_top_rect(area)
} else {
self.containing_block
},
}
})
}
}
fn grid_static_auto_end_line_offset(
start: Option<f32>,
end: &css::GridPlacement,
line_offsets: &[f32],
explicit_tracks: &css::GridTrackList,
content_alignment: css::ContentAlignment,
container_size: f32,
) -> Option<f32> {
if !matches!(end, css::GridPlacement::Auto) {
return None;
}
let start = start?;
grid_static_following_line_offset(start, line_offsets).or_else(|| {
(matches!(explicit_tracks, css::GridTrackList::None)
&& line_offsets.len() <= 1
&& start.abs() <= 0.01
&& matches!(
content_alignment.keyword,
ContentAlignmentKeyword::Normal | ContentAlignmentKeyword::Stretch
))
.then_some(container_size)
})
}
fn grid_static_following_line_offset(start: f32, line_offsets: &[f32]) -> Option<f32> {
line_offsets
.windows(2)
.find(|pair| (pair[0] - start).abs() <= 0.01)
.map(|pair| pair[1])
}
fn grid_explicit_placement_area(
container_style: &ComputedStyle,
child_style: &ComputedStyle,
content_area: PageTopRect,
columns: &GridAxisTopology,
rows: &GridAxisTopology,
) -> Option<PageTopRect> {
let axes = WritingModeAxes::new(
container_style.writing_mode,
container_style.used_direction(),
);
let (inline_start, inline_end) = grid_static_logical_axis_range(
LogicalAxis::Inline,
container_style,
child_style,
&axes,
content_area,
columns,
rows,
)?;
let (block_start, block_end) = grid_static_logical_axis_range(
LogicalAxis::Block,
container_style,
child_style,
&axes,
content_area,
columns,
rows,
)?;
let mut left = content_area.x();
let mut right = left + content_area.width();
let mut top = content_area.top_y();
let mut bottom = content_area.bottom_y();
grid_project_logical_range(
&mut left,
&mut right,
&mut top,
&mut bottom,
content_area,
axes.physical_start_side(LogicalAxis::Inline),
inline_start,
inline_end,
);
grid_project_logical_range(
&mut left,
&mut right,
&mut top,
&mut bottom,
content_area,
axes.physical_start_side(LogicalAxis::Block),
block_start,
block_end,
);
Some(PageTopRect::new(
left.min(right),
top.max(bottom),
(right - left).abs(),
(top - bottom).abs(),
))
}
fn grid_static_logical_axis_range(
axis: LogicalAxis,
container_style: &ComputedStyle,
child_style: &ComputedStyle,
axes: &WritingModeAxes,
content_area: PageTopRect,
columns: &GridAxisTopology,
rows: &GridAxisTopology,
) -> Option<(f32, f32)> {
let (tracks, auto_tracks, start, end, gap, content_alignment) = match axis {
LogicalAxis::Inline => (
&container_style.grid_template_columns,
&container_style.grid_auto_columns,
&child_style.grid_column_start,
&child_style.grid_column_end,
container_style.column_gap.clone(),
container_style.justify_content,
),
LogicalAxis::Block => (
&container_style.grid_template_rows,
&container_style.grid_auto_rows,
&child_style.grid_row_start,
&child_style.grid_row_end,
container_style.row_gap.clone(),
container_style.align_content,
),
};
let physical_topology = match axes.physical_axis(axis) {
PhysicalAxis::Horizontal => columns,
PhysicalAxis::Vertical => rows,
};
let logical_topology = if axes.is_reversed(axis) {
physical_topology.reversed()
} else {
physical_topology.clone()
};
let line_offsets = logical_topology.line_offsets();
let container_size = match axes.physical_axis(axis) {
PhysicalAxis::Horizontal => content_area.width(),
PhysicalAxis::Vertical => content_area.height(),
};
if matches!(start, css::GridPlacement::Auto) && matches!(end, css::GridPlacement::Auto) {
return Some((0.0, container_size));
}
let resolved_start = grid_static_line_position(
tracks,
auto_tracks,
start,
&line_offsets,
gap.clone(),
content_alignment,
container_size,
);
let resolved_end = grid_static_line_position(
tracks,
auto_tracks,
end,
&line_offsets,
gap,
content_alignment,
container_size,
);
let start = match (resolved_start, end) {
(Some(start), _) => start.offset,
(None, css::GridPlacement::Auto) => resolved_end?.offset,
(None, _) => return None,
};
let end = match (resolved_end, start) {
(Some(end), _) => u16::try_from(end.line_index)
.ok()
.and_then(|line| {
logical_topology.aligned_area_bounds(
content_alignment,
container_size,
line.checked_sub(1)?,
line,
)
})
.map_or(end.offset, |(_, edge)| edge),
(None, _) if matches!(end, css::GridPlacement::Auto) => start,
(None, _) => return None,
};
(end >= start).then_some((start, end))
}
#[allow(clippy::too_many_arguments)]
fn grid_project_logical_range(
left: &mut f32,
right: &mut f32,
top: &mut f32,
bottom: &mut f32,
content_area: PageTopRect,
start_side: PhysicalSide,
start: f32,
end: f32,
) {
match start_side {
PhysicalSide::Left => {
*left = content_area.x() + start;
*right = content_area.x() + end;
}
PhysicalSide::Right => {
*left = content_area.x() + content_area.width() - end;
*right = content_area.x() + content_area.width() - start;
}
PhysicalSide::Top => {
*top = content_area.top_y() - start;
*bottom = content_area.top_y() - end;
}
PhysicalSide::Bottom => {
*top = content_area.bottom_y() + end;
*bottom = content_area.bottom_y() + start;
}
}
}
pub(super) struct GridPositioningGeometry<'a> {
pub(super) inner_x: f32,
pub(super) inner_width: PhysicalContentWidth,
pub(super) content_top: f32,
pub(super) content_height: PhysicalContentHeight,
pub(super) columns: &'a GridAxisTopology,
pub(super) rows: &'a GridAxisTopology,
}
impl<'a> LayoutBuilder<'a> {
pub(super) fn layout_positioned_grid_child<'grid>(
&mut self,
child: &GridChild<'grid>,
in_flow_children: &[GridChild<'grid>],
context: PositionedGridStaticContext<'_>,
) {
let source = context.positioning_containing_block.map_or(
GridAbsposStaticPositionSource::ContentEdges,
|padding_box| GridAbsposStaticPositionSource::PlacementArea { padding_box },
);
let row_container_size = context
.definite_content_height
.unwrap_or(context.content_height)
.points();
let content_area = PageTopRect::new(
context.inner_x,
context.content_top,
context.inner_width.points(),
row_container_size,
);
if matches!(source, GridAbsposStaticPositionSource::ContentEdges) {
let static_rect = grid_positioned_child_static_rect(
source,
content_area,
&child.style,
context.container_style,
);
self.layout_positioned_formatting_context_child(
child,
context.stylesheets,
static_rect,
);
return;
}
let hypothetical_child = positioned_grid_static_probe_child(child);
let mut hypothetical_children = Vec::with_capacity(in_flow_children.len() + 1);
hypothetical_children.extend_from_slice(in_flow_children);
hypothetical_children.push(hypothetical_child);
let mut hypothetical = self
.compute_grid_layout(
context.container_style,
&hypothetical_children,
context.stylesheets,
context.inner_width,
context.definite_content_height,
GridLayoutPurpose::FinalLayout,
)
.and_then(|layout| layout.items.into_iter().nth(in_flow_children.len()))
.unwrap_or(GridItemLayout::new(
GridRect::new(
GridPoint::new(0.0, 0.0),
GridSize::new(context.inner_width.points(), child.style.line_height),
),
None,
));
let column_start = grid_static_line_offset(
&context.container_style.grid_template_columns,
&context.container_style.grid_auto_columns,
&child.style.grid_column_start,
context.column_line_offsets,
context.container_style.column_gap.clone(),
context.container_style.justify_content,
context.inner_width.points(),
);
let column_end = grid_static_line_offset(
&context.container_style.grid_template_columns,
&context.container_style.grid_auto_columns,
&child.style.grid_column_end,
context.column_line_offsets,
context.container_style.column_gap.clone(),
context.container_style.justify_content,
context.inner_width.points(),
);
let column_end = match (column_start, column_end) {
(Some(start), Some(end)) if (start - end).abs() <= 0.01 => {
grid_static_following_line_offset(start, context.column_line_offsets).or(Some(end))
}
(start, end) => end.or_else(|| {
grid_static_auto_end_line_offset(
start,
&child.style.grid_column_end,
context.column_line_offsets,
&context.container_style.grid_template_columns,
context.container_style.justify_content,
context.inner_width.points(),
)
}),
};
let column_end = column_end.or_else(|| {
hypothetical.area.and_then(|area| {
content_aligned_grid_line_offset(
context.container_style.justify_content,
context.inner_width.points(),
context.column_line_offsets,
usize::from(area.column_end).saturating_sub(1),
)
})
});
if let Some(x) = column_start {
hypothetical.set_axis_geometry(GridAxis::Column, x, hypothetical.width());
}
if let (Some(start), Some(end)) = (column_start, column_end)
&& end >= start
{
hypothetical.set_axis_geometry(GridAxis::Column, hypothetical.x(), end - start);
}
if matches!(child.style.grid_column_start, css::GridPlacement::Auto)
&& matches!(child.style.grid_column_end, css::GridPlacement::Auto)
{
hypothetical.set_axis_geometry(GridAxis::Column, 0.0, context.inner_width.points());
}
if WritingModeAxes::new(
context.container_style.writing_mode,
context.container_style.direction,
)
.physical_axis(LogicalAxis::Inline)
== PhysicalAxis::Horizontal
&& WritingModeAxes::new(
context.container_style.writing_mode,
context.container_style.direction,
)
.physical_side(LogicalSide::InlineStart)
== PhysicalSide::Right
&& column_start.is_some()
&& column_end.is_some()
{
let logical_start = column_start.unwrap_or(0.0);
let logical_end = column_end
.unwrap_or(context.inner_width.points())
.max(logical_start);
hypothetical.set_axis_geometry(
GridAxis::Column,
context.inner_width.points() - logical_end,
logical_end - logical_start,
);
}
let row_start = grid_static_line_offset(
&context.container_style.grid_template_rows,
&context.container_style.grid_auto_rows,
&child.style.grid_row_start,
context.row_line_offsets,
context.container_style.row_gap.clone(),
context.container_style.align_content,
row_container_size,
);
let row_end = grid_static_line_offset(
&context.container_style.grid_template_rows,
&context.container_style.grid_auto_rows,
&child.style.grid_row_end,
context.row_line_offsets,
context.container_style.row_gap.clone(),
context.container_style.align_content,
row_container_size,
);
let row_end = match (row_start, row_end) {
(Some(start), Some(end)) if (start - end).abs() <= 0.01 => {
grid_static_following_line_offset(start, context.row_line_offsets).or(Some(end))
}
(start, end) => end.or_else(|| {
grid_static_auto_end_line_offset(
start,
&child.style.grid_row_end,
context.row_line_offsets,
&context.container_style.grid_template_rows,
context.container_style.align_content,
row_container_size,
)
}),
};
let row_end = row_end.or_else(|| {
hypothetical.area.and_then(|area| {
content_aligned_grid_line_offset(
context.container_style.align_content,
row_container_size,
context.row_line_offsets,
usize::from(area.row_end).saturating_sub(1),
)
})
});
if let Some(y) = row_start {
hypothetical.set_axis_geometry(GridAxis::Row, y, hypothetical.height());
}
if let (Some(start), Some(end)) = (row_start, row_end)
&& end >= start
{
hypothetical.set_axis_geometry(GridAxis::Row, hypothetical.y(), end - start);
}
if matches!(child.style.grid_row_start, css::GridPlacement::Auto)
&& matches!(child.style.grid_row_end, css::GridPlacement::Auto)
{
hypothetical.set_axis_geometry(GridAxis::Row, 0.0, row_container_size);
}
if WritingModeAxes::new(
context.container_style.writing_mode,
context.container_style.direction,
)
.swaps_physical_axes()
{
let x_start = grid_static_line_offset(
&context.container_style.grid_template_rows,
&context.container_style.grid_auto_rows,
&child.style.grid_row_start,
context.column_line_offsets,
context.container_style.row_gap.clone(),
context.container_style.align_content,
context.inner_width.points(),
)
.unwrap_or(0.0);
let x_end = grid_static_line_offset(
&context.container_style.grid_template_rows,
&context.container_style.grid_auto_rows,
&child.style.grid_row_end,
context.column_line_offsets,
context.container_style.row_gap.clone(),
context.container_style.align_content,
context.inner_width.points(),
)
.unwrap_or(context.inner_width.points())
.max(x_start);
let x = if matches!(
context.container_style.writing_mode,
WritingMode::VerticalRl | WritingMode::SidewaysRl
) {
context.inner_width.points() - x_end
} else {
x_start
};
hypothetical.set_axis_geometry(GridAxis::Column, x, x_end - x_start);
let y_start = grid_static_line_offset(
&context.container_style.grid_template_columns,
&context.container_style.grid_auto_columns,
&child.style.grid_column_start,
context.row_line_offsets,
context.container_style.column_gap.clone(),
context.container_style.justify_content,
row_container_size,
)
.unwrap_or(0.0);
let y_end = grid_static_line_offset(
&context.container_style.grid_template_columns,
&context.container_style.grid_auto_columns,
&child.style.grid_column_end,
context.row_line_offsets,
context.container_style.column_gap.clone(),
context.container_style.justify_content,
row_container_size,
)
.unwrap_or(row_container_size)
.max(y_start);
let y = if context.container_style.direction == Direction::Rtl {
row_container_size - y_end
} else {
y_start
};
hypothetical.set_axis_geometry(GridAxis::Row, y, y_end - y_start);
}
let probed_placement_area = PageTopRect::new(
context.inner_x + hypothetical.x(),
context.content_top - hypothetical.y(),
hypothetical.width(),
hypothetical.height(),
);
let placement_area = grid_explicit_placement_area(
context.container_style,
&child.style,
PageTopRect::new(
context.inner_x,
context.content_top,
context.inner_width.points(),
row_container_size,
),
context.columns,
context.rows,
)
.unwrap_or(probed_placement_area);
let static_area = grid_abspos_static_position_area(
source,
placement_area,
&child.style,
context.container_style,
);
let static_rect = grid_positioned_child_static_rect(
source,
static_area,
&child.style,
context.container_style,
)
.with_static_physical_edges(
grid_explicit_static_left_edge(&child.style, context.container_style, placement_area),
grid_explicit_static_right_edge(&child.style, context.container_style, placement_area),
grid_explicit_static_top_edge(&child.style, context.container_style, placement_area),
)
.with_static_alignment(grid_abspos_static_alignment(
placement_area,
&child.style,
context.container_style,
));
self.layout_positioned_formatting_context_child(child, context.stylesheets, static_rect);
}
}
fn grid_explicit_static_left_edge(
child_style: &ComputedStyle,
container_style: &ComputedStyle,
placement_area: PageTopRect,
) -> Option<f32> {
let axes = WritingModeAxes::new(
container_style.writing_mode,
container_style.used_direction(),
);
match axes.physical_side(LogicalSide::InlineStart) {
PhysicalSide::Left
if !matches!(child_style.grid_column_start, css::GridPlacement::Auto)
&& matches!(child_style.grid_column_end, css::GridPlacement::Auto) =>
{
Some(placement_area.x())
}
_ => match axes.physical_side(LogicalSide::InlineEnd) {
PhysicalSide::Left
if !matches!(child_style.grid_column_end, css::GridPlacement::Auto)
&& matches!(child_style.grid_column_start, css::GridPlacement::Auto) =>
{
Some(placement_area.x() + placement_area.width())
}
_ => None,
},
}
}
fn grid_explicit_static_right_edge(
child_style: &ComputedStyle,
container_style: &ComputedStyle,
placement_area: PageTopRect,
) -> Option<f32> {
let axes = WritingModeAxes::new(
container_style.writing_mode,
container_style.used_direction(),
);
match axes.physical_side(LogicalSide::InlineStart) {
PhysicalSide::Right
if !matches!(child_style.grid_column_start, css::GridPlacement::Auto)
&& matches!(child_style.grid_column_end, css::GridPlacement::Auto) =>
{
Some(placement_area.x())
}
_ => match axes.physical_side(LogicalSide::InlineEnd) {
PhysicalSide::Right
if !matches!(child_style.grid_column_end, css::GridPlacement::Auto)
&& matches!(child_style.grid_column_start, css::GridPlacement::Auto) =>
{
Some(placement_area.x() + placement_area.width())
}
_ => None,
},
}
}
fn grid_explicit_static_top_edge(
child_style: &ComputedStyle,
container_style: &ComputedStyle,
placement_area: PageTopRect,
) -> Option<f32> {
let axes = WritingModeAxes::new(
container_style.writing_mode,
container_style.used_direction(),
);
match axes.physical_side(LogicalSide::BlockStart) {
PhysicalSide::Top
if !matches!(child_style.grid_row_start, css::GridPlacement::Auto)
&& matches!(child_style.grid_row_end, css::GridPlacement::Auto) =>
{
Some(placement_area.top_y())
}
_ => match axes.physical_side(LogicalSide::BlockEnd) {
PhysicalSide::Top
if !matches!(child_style.grid_row_end, css::GridPlacement::Auto)
&& matches!(child_style.grid_row_start, css::GridPlacement::Auto) =>
{
Some(placement_area.top_y() - placement_area.height())
}
_ => None,
},
}
}
fn effective_grid_abspos_alignment(
self_alignment: css::SelfAlignment,
container_alignment: css::SelfAlignment,
) -> css::SelfAlignment {
if self_alignment.keyword == SelfAlignmentKeyword::Auto {
container_alignment
} else {
self_alignment
}
}
fn grid_abspos_static_alignment(
area: PageTopRect,
child_style: &ComputedStyle,
container_style: &ComputedStyle,
) -> AbsposStaticAlignment {
AbsposStaticAlignment::new(
area,
container_style.writing_mode,
container_style.direction,
child_style.writing_mode,
child_style.used_direction(),
effective_grid_abspos_alignment(child_style.justify_self, container_style.justify_items),
effective_grid_abspos_alignment(child_style.align_self, container_style.align_items),
)
}
fn grid_static_line_offset(
tracks: &css::GridTrackList,
auto_tracks: &css::GridAutoTrackList,
placement: &css::GridPlacement,
line_offsets: &[f32],
gap: css::ComputedGap,
content_alignment: css::ContentAlignment,
container_size: f32,
) -> Option<f32> {
grid_static_line_position(
tracks,
auto_tracks,
placement,
line_offsets,
gap,
content_alignment,
container_size,
)
.map(|line| line.offset)
}
fn grid_static_line_position(
tracks: &css::GridTrackList,
auto_tracks: &css::GridAutoTrackList,
placement: &css::GridPlacement,
line_offsets: &[f32],
gap: css::ComputedGap,
content_alignment: css::ContentAlignment,
container_size: f32,
) -> Option<ResolvedGridStaticLine> {
let after_explicit_implicit_line = grid_static_placement_is_after_explicit_line(
tracks,
auto_tracks,
placement,
gap.clone(),
container_size,
);
if after_explicit_implicit_line {
let from_layout = grid_layout_line_static_offset_with_inferred_gaps(
tracks,
auto_tracks,
placement,
line_offsets,
gap.clone(),
GridLayoutLineStaticOffsetOptions {
content_alignment,
container_size,
include_inferred_gaps: false,
},
);
return from_layout.or_else(|| {
grid_line_static_position(
tracks,
auto_tracks,
placement,
gap,
content_alignment,
container_size,
)
});
}
grid_line_static_position(
tracks,
auto_tracks,
placement,
gap.clone(),
content_alignment,
container_size,
)
.or_else(|| {
grid_layout_line_static_position(
tracks,
auto_tracks,
placement,
line_offsets,
gap,
content_alignment,
container_size,
)
})
}
fn grid_static_placement_is_after_explicit_line(
tracks: &css::GridTrackList,
auto_tracks: &css::GridAutoTrackList,
placement: &css::GridPlacement,
gap: css::ComputedGap,
container_size: f32,
) -> bool {
let css::GridTrackList::Tracks {
components,
trailing_names,
} = tracks
else {
return false;
};
let line_names = if grid_track_list_has_auto_repeat(tracks) {
let Some(SimpleAutoRepeatCount::Count(auto_repeat_count)) =
simple_fixed_auto_repeat_count(components, gap, content_box_pt(container_size))
else {
return false;
};
let Some(line_names) = auto_repeat_explicit_line_names(tracks, auto_repeat_count.get())
else {
return false;
};
line_names
} else if let Some(line_names) = explicit_grid_line_names(components, trailing_names) {
line_names
} else {
return false;
};
grid_line_static_offset_index(placement, &line_names, auto_tracks, container_size)
.is_some_and(|line_index| line_index > line_names.len() as i32)
}
#[cfg(test)]
fn grid_layout_line_static_offset(
tracks: &css::GridTrackList,
auto_tracks: &css::GridAutoTrackList,
placement: &css::GridPlacement,
line_offsets: &[f32],
gap: css::ComputedGap,
content_alignment: css::ContentAlignment,
container_size: f32,
) -> Option<f32> {
grid_layout_line_static_position(
tracks,
auto_tracks,
placement,
line_offsets,
gap,
content_alignment,
container_size,
)
.map(|line| line.offset)
}
fn grid_layout_line_static_position(
tracks: &css::GridTrackList,
auto_tracks: &css::GridAutoTrackList,
placement: &css::GridPlacement,
line_offsets: &[f32],
gap: css::ComputedGap,
content_alignment: css::ContentAlignment,
container_size: f32,
) -> Option<ResolvedGridStaticLine> {
grid_layout_line_static_offset_with_inferred_gaps(
tracks,
auto_tracks,
placement,
line_offsets,
gap,
GridLayoutLineStaticOffsetOptions {
content_alignment,
container_size,
include_inferred_gaps: false,
},
)
}
#[derive(Clone, Copy)]
struct GridLayoutLineStaticOffsetOptions {
content_alignment: css::ContentAlignment,
container_size: f32,
include_inferred_gaps: bool,
}
fn grid_layout_line_static_offset_with_inferred_gaps(
tracks: &css::GridTrackList,
auto_tracks: &css::GridAutoTrackList,
placement: &css::GridPlacement,
line_offsets: &[f32],
gap: css::ComputedGap,
options: GridLayoutLineStaticOffsetOptions,
) -> Option<ResolvedGridStaticLine> {
let css::GridPlacement::Line(_) = placement else {
return None;
};
if grid_track_list_has_auto_repeat(tracks) {
let SimpleAutoRepeatCount::Count(auto_repeat_count) = simple_fixed_auto_repeat_count(
grid_track_list_components(tracks)?,
gap.clone(),
content_box_pt(options.container_size),
)?
else {
return None;
};
let auto_repeat_count = auto_repeat_count.get();
let line_names = auto_repeat_explicit_line_names(tracks, auto_repeat_count)?;
let auto_fill_line_offsets;
let line_offsets = if grid_track_list_has_auto_fit(tracks) {
line_offsets
} else {
let track_sizes = auto_repeat_explicit_track_sizes(
tracks,
auto_repeat_count,
options.container_size,
)?;
let gap =
definite_grid_gap_size(gap.clone(), layout_pt(options.container_size)).points();
auto_fill_line_offsets = grid_line_offsets_from_track_sizes(&track_sizes, gap);
&auto_fill_line_offsets
};
let line_index = grid_line_static_offset_index(
placement,
&line_names,
auto_tracks,
options.container_size,
)?;
let line_offsets = auto_repeat_static_line_offsets(
line_offsets,
&line_names,
auto_tracks,
line_index,
gap,
options.container_size,
)?;
let offset = content_aligned_grid_line_offset(
options.content_alignment,
options.container_size,
&line_offsets.offsets,
line_offsets.offset_index(line_index)?,
)?;
return Some(ResolvedGridStaticLine { line_index, offset });
};
let (components, trailing_names) = match tracks {
css::GridTrackList::Tracks {
components,
trailing_names,
} => (components.as_slice(), trailing_names.as_slice()),
css::GridTrackList::None | css::GridTrackList::Subgrid { .. } => (&[][..], &[][..]),
};
let line_names = explicit_grid_line_names(components, trailing_names)?;
let line_index = match placement {
css::GridPlacement::Line(line) if line.name().is_none() => {
let index = line.index()?;
if index > 0 {
index
} else {
explicit_grid_line_index(index, i32::try_from(line_names.len()).ok()?)?
}
}
_ => grid_line_static_offset_index(
placement,
&line_names,
auto_tracks,
options.container_size,
)?,
};
let offset_index = usize::try_from(line_index - 1).ok()?;
line_offsets.get(offset_index)?;
let inferred_offsets;
let line_offsets = if options.include_inferred_gaps {
let gap = definite_grid_gap_size(gap, layout_pt(options.container_size)).points();
inferred_offsets = line_offsets
.iter()
.enumerate()
.map(|(index, offset)| *offset + gap * index as f32)
.collect::<Vec<_>>();
&inferred_offsets
} else {
line_offsets
};
let offset = content_aligned_grid_line_offset(
options.content_alignment,
options.container_size,
line_offsets,
offset_index,
)?;
Some(ResolvedGridStaticLine { line_index, offset })
}
fn grid_track_list_components(
tracks: &css::GridTrackList,
) -> Option<&[css::GridTrackListComponent]> {
let css::GridTrackList::Tracks { components, .. } = tracks else {
return None;
};
Some(components)
}
fn grid_track_list_has_auto_repeat(tracks: &css::GridTrackList) -> bool {
let css::GridTrackList::Tracks { components, .. } = tracks else {
return false;
};
components.iter().any(grid_track_component_has_auto_repeat)
}
pub(super) fn grid_track_list_has_auto_fit(tracks: &css::GridTrackList) -> bool {
let css::GridTrackList::Tracks { components, .. } = tracks else {
return false;
};
components.iter().any(grid_track_component_has_auto_fit)
}
fn grid_track_component_has_auto_repeat(component: &css::GridTrackListComponent) -> bool {
let css::GridTrackListComponent::Repeat(_, repeat) = component else {
return false;
};
matches!(
repeat.count,
css::GridRepeatCount::AutoFill | css::GridRepeatCount::AutoFit
)
}
fn grid_track_component_has_auto_fit(component: &css::GridTrackListComponent) -> bool {
let css::GridTrackListComponent::Repeat(_, repeat) = component else {
return false;
};
matches!(repeat.count, css::GridRepeatCount::AutoFit)
}
fn auto_repeat_explicit_line_names(
tracks: &css::GridTrackList,
auto_repeat_count: u16,
) -> Option<Vec<Vec<String>>> {
let css::GridTrackList::Tracks {
components,
trailing_names,
} = tracks
else {
return None;
};
let mut lines = Vec::new();
let mut current_line_names = Vec::new();
collect_auto_repeat_line_names(
components,
auto_repeat_count,
&mut current_line_names,
&mut lines,
)?;
current_line_names.extend(trailing_names.iter().cloned());
lines.push(current_line_names);
Some(lines)
}
fn auto_repeat_explicit_track_sizes(
tracks: &css::GridTrackList,
auto_repeat_count: u16,
container_size: f32,
) -> Option<Vec<f32>> {
let components = grid_track_list_components(tracks)?;
let mut sizes = Vec::new();
collect_auto_repeat_track_sizes(components, auto_repeat_count, container_size, &mut sizes)?;
Some(sizes)
}
fn collect_auto_repeat_track_sizes(
components: &[css::GridTrackListComponent],
auto_repeat_count: u16,
container_size: f32,
sizes: &mut Vec<f32>,
) -> Option<()> {
for component in components {
match component {
css::GridTrackListComponent::Track(_, size) => {
sizes.push(
definite_auto_repeat_track_size(size, content_box_pt(container_size))?.points(),
);
}
css::GridTrackListComponent::Repeat(_, repeat) => {
let count = match repeat.count {
css::GridRepeatCount::Number(count) => count,
css::GridRepeatCount::AutoFill | css::GridRepeatCount::AutoFit => {
auto_repeat_count
}
};
for _ in 0..count {
collect_auto_repeat_track_sizes(
&repeat.tracks,
auto_repeat_count,
container_size,
sizes,
)?;
}
}
}
}
Some(())
}
struct AutoRepeatStaticLineOffsets {
first_line_index: i32,
offsets: Vec<f32>,
}
impl AutoRepeatStaticLineOffsets {
fn offset_index(&self, line_index: i32) -> Option<usize> {
usize::try_from(line_index.checked_sub(self.first_line_index)?).ok()
}
}
fn auto_repeat_static_line_offsets(
line_offsets: &[f32],
line_names: &[Vec<String>],
auto_tracks: &css::GridAutoTrackList,
line_index: i32,
gap: css::ComputedGap,
container_size: f32,
) -> Option<AutoRepeatStaticLineOffsets> {
let explicit_line_count = i32::try_from(line_names.len()).ok()?;
let before_track_count = if line_index < 1 {
usize::try_from(1_i32.checked_sub(line_index)?).ok()?
} else {
0
};
let after_track_count = if line_index > explicit_line_count {
usize::try_from(line_index.checked_sub(explicit_line_count)?).ok()?
} else {
0
};
let gap = definite_grid_gap_size(gap, layout_pt(container_size)).points();
if before_track_count == 0 && after_track_count == 0 {
return Some(AutoRepeatStaticLineOffsets {
first_line_index: 1,
offsets: line_offsets.to_vec(),
});
}
let mut offsets = line_offsets.to_vec();
let mut first_line_index = 1;
if before_track_count > 0 {
let mut expanded = Vec::with_capacity(before_track_count + offsets.len());
let mut offset = 0.0;
expanded.push(offset);
for auto_index in (0..before_track_count).rev() {
offset +=
cycled_definite_auto_track_size_before(auto_tracks, auto_index, container_size)?;
offset += gap;
expanded.push(offset);
}
let explicit_start_offset = offset;
expanded.extend(
offsets
.iter()
.skip(1)
.map(|line_offset| explicit_start_offset + *line_offset),
);
offsets = expanded;
first_line_index = 1_i32.checked_sub(i32::try_from(before_track_count).ok()?)?;
}
if after_track_count > 0 {
let mut offset = offsets.last().cloned()?;
for auto_index in 0..after_track_count {
offset += gap;
offset +=
cycled_definite_auto_track_size_after(auto_tracks, auto_index, container_size)?;
offsets.push(offset);
}
}
let result = AutoRepeatStaticLineOffsets {
first_line_index,
offsets,
};
result
.offset_index(line_index)
.and_then(|index| result.offsets.get(index))
.cloned()?;
Some(result)
}
fn collect_auto_repeat_line_names(
components: &[css::GridTrackListComponent],
auto_repeat_count: u16,
current_line_names: &mut Vec<String>,
lines: &mut Vec<Vec<String>>,
) -> Option<()> {
for component in components {
match component {
css::GridTrackListComponent::Track(names, _) => {
current_line_names.extend(names.iter().cloned());
lines.push(std::mem::take(current_line_names));
}
css::GridTrackListComponent::Repeat(names, repeat) => {
current_line_names.extend(names.iter().cloned());
let count = match repeat.count {
css::GridRepeatCount::Number(count) => count,
css::GridRepeatCount::AutoFill | css::GridRepeatCount::AutoFit => {
auto_repeat_count
}
};
for _ in 0..count {
collect_auto_repeat_line_names(
&repeat.tracks,
auto_repeat_count,
current_line_names,
lines,
)?;
current_line_names.extend(repeat.trailing_names.iter().cloned());
}
}
}
}
Some(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn content_edge_static_source_preserves_the_grid_content_rectangle() {
let style = ComputedStyle::initial();
let content = PageTopRect::new(12.0, 80.0, 40.0, 30.0);
let padding = ContainingBlock::from_page_top_rect(PageTopRect::new(4.0, 92.0, 64.0, 54.0));
assert_eq!(
grid_abspos_static_position_area(
GridAbsposStaticPositionSource::ContentEdges,
content,
&style,
&style,
),
content,
"a Grid parent that is not the actual containing block ignores grid placement",
);
assert_ne!(content, padding.rect);
}
#[test]
fn placement_area_auto_edges_follow_padding_box_in_vertical_rtl_grid() {
let mut style = ComputedStyle::initial();
style.writing_mode = WritingMode::VerticalRl;
style.direction = Direction::Rtl;
style.grid_column_end = css::GridPlacement::Line(css::GridLinePlacement::Number(
std::num::NonZeroI32::new(2).unwrap(),
));
style.grid_row_end = css::GridPlacement::Line(css::GridLinePlacement::Number(
std::num::NonZeroI32::new(2).unwrap(),
));
let placement = PageTopRect::new(20.0, 90.0, 30.0, 40.0);
let padding_box =
ContainingBlock::from_page_top_rect(PageTopRect::new(10.0, 100.0, 60.0, 80.0));
assert_eq!(
grid_abspos_static_position_area(
GridAbsposStaticPositionSource::PlacementArea { padding_box },
placement,
&style,
&style,
),
PageTopRect::new(20.0, 90.0, 50.0, 70.0),
);
}
#[test]
fn numeric_line_static_offset_includes_track_gaps() {
let tracks = css::GridTrackList::Tracks {
components: vec![
css::GridTrackListComponent::Track(
Vec::new(),
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
},
),
css::GridTrackListComponent::Track(
Vec::new(),
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
},
),
],
trailing_names: Vec::new(),
};
let placement = css::GridPlacement::Line(css::GridLinePlacement::Number(
std::num::NonZeroI32::new(2).unwrap(),
));
assert_eq!(
grid_line_static_offset(
&tracks,
&css::GridAutoTrackList::initial(),
&placement,
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::Start),
45.0,
),
Some(25.0)
);
}
#[test]
fn named_line_static_offset_resolves_explicit_line_occurrences() {
let fixed_track = css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
};
let tracks = css::GridTrackList::Tracks {
components: vec![
css::GridTrackListComponent::Track(vec!["main".to_string()], fixed_track.clone()),
css::GridTrackListComponent::Track(vec!["main".to_string()], fixed_track),
],
trailing_names: vec!["main".to_string()],
};
let placement = css::GridPlacement::Line(css::GridLinePlacement::Named {
name: "main".to_string(),
occurrence: std::num::NonZeroI32::new(2),
});
assert_eq!(
grid_line_static_offset(
&tracks,
&css::GridAutoTrackList::initial(),
&placement,
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::Start),
45.0,
),
Some(25.0)
);
}
#[test]
fn negative_numeric_line_static_offset_counts_from_explicit_grid_end() {
let fixed_track = css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
};
let tracks = css::GridTrackList::Tracks {
components: vec![
css::GridTrackListComponent::Track(Vec::new(), fixed_track.clone()),
css::GridTrackListComponent::Track(Vec::new(), fixed_track),
],
trailing_names: Vec::new(),
};
let placement = css::GridPlacement::Line(css::GridLinePlacement::Number(
std::num::NonZeroI32::new(-1).unwrap(),
));
assert_eq!(
grid_line_static_offset(
&tracks,
&css::GridAutoTrackList::initial(),
&placement,
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::Start),
45.0,
),
Some(45.0)
);
}
#[test]
fn negative_named_line_static_offset_counts_from_explicit_grid_end() {
let fixed_track = css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
};
let tracks = css::GridTrackList::Tracks {
components: vec![
css::GridTrackListComponent::Track(vec!["main".to_string()], fixed_track.clone()),
css::GridTrackListComponent::Track(vec!["main".to_string()], fixed_track),
],
trailing_names: vec!["main".to_string()],
};
let placement = css::GridPlacement::Line(css::GridLinePlacement::Named {
name: "main".to_string(),
occurrence: std::num::NonZeroI32::new(-1),
});
assert_eq!(
grid_line_static_offset(
&tracks,
&css::GridAutoTrackList::initial(),
&placement,
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::Start),
45.0,
),
Some(45.0)
);
}
#[test]
fn numeric_static_offset_uses_final_collapsed_track_lines() {
let placement = css::GridPlacement::Line(css::GridLinePlacement::Number(
std::num::NonZeroI32::new(3).unwrap(),
));
let tracks = css::GridTrackList::Tracks {
components: vec![css::GridTrackListComponent::Repeat(
Vec::new(),
css::GridRepeat {
count: css::GridRepeatCount::AutoFit,
tracks: vec![css::GridTrackListComponent::Track(
Vec::new(),
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
},
)],
trailing_names: Vec::new(),
},
)],
trailing_names: Vec::new(),
};
assert_eq!(
grid_layout_line_static_offset(
&tracks,
&css::GridAutoTrackList::initial(),
&placement,
&[0.0, 25.0, 45.0, 45.0],
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::Start),
70.0,
),
Some(45.0)
);
assert_eq!(
grid_layout_line_static_offset(
&tracks,
&css::GridAutoTrackList::initial(),
&placement,
&[0.0, 25.0, 45.0, 45.0],
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::End),
70.0,
),
Some(70.0)
);
assert_eq!(
grid_layout_line_static_offset(
&tracks,
&css::GridAutoTrackList::initial(),
&placement,
&[0.0, 25.0, 45.0, 45.0],
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::SpaceBetween),
70.0,
),
Some(70.0)
);
let space_around = grid_layout_line_static_offset(
&tracks,
&css::GridAutoTrackList::initial(),
&placement,
&[0.0, 25.0, 45.0, 45.0],
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0)),
css::ContentAlignment::new(css::ContentAlignmentKeyword::SpaceAround),
70.0,
)
.unwrap();
assert!(
(space_around - 63.75).abs() < 0.001,
"space-around should add half edge interval plus one between interval: {space_around}"
);
let space_evenly = grid_layout_line_static_offset(
&tracks,
&css::GridAutoTrackList::initial(),
&placement,
&[0.0, 25.0, 45.0, 45.0],
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0)),
css::ContentAlignment::new(css::ContentAlignmentKeyword::SpaceEvenly),
70.0,
)
.unwrap();
assert!(
(space_evenly - (45.0 + 25.0 / 3.0 * 2.0)).abs() < 0.001,
"space-evenly should add one edge interval and one between interval: {space_evenly}"
);
}
#[test]
fn named_static_offset_expands_final_auto_fit_line_names() {
let tracks = css::GridTrackList::Tracks {
components: vec![css::GridTrackListComponent::Repeat(
Vec::new(),
css::GridRepeat {
count: css::GridRepeatCount::AutoFit,
tracks: vec![css::GridTrackListComponent::Track(
vec!["slot".to_string()],
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
},
)],
trailing_names: vec!["end".to_string()],
},
)],
trailing_names: Vec::new(),
};
let placement = css::GridPlacement::Line(css::GridLinePlacement::Named {
name: "slot".to_string(),
occurrence: std::num::NonZeroI32::new(3),
});
assert_eq!(
grid_layout_line_static_offset(
&tracks,
&css::GridAutoTrackList::initial(),
&placement,
&[0.0, 25.0, 45.0, 45.0],
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::Start),
70.0,
),
Some(45.0)
);
}
#[test]
fn named_static_offset_expands_final_auto_fill_line_names() {
let fixed_track = css::GridTrackListComponent::Track(
vec!["slot".to_string()],
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(10.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(10.0),
),
},
);
let tracks = css::GridTrackList::Tracks {
components: vec![css::GridTrackListComponent::Repeat(
Vec::new(),
css::GridRepeat {
count: css::GridRepeatCount::AutoFill,
tracks: vec![
fixed_track,
css::GridTrackListComponent::Track(
vec!["middle".to_string()],
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(10.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(10.0),
),
},
),
],
trailing_names: vec!["end".to_string()],
},
)],
trailing_names: Vec::new(),
};
let placement = css::GridPlacement::Line(css::GridLinePlacement::Named {
name: "middle".to_string(),
occurrence: std::num::NonZeroI32::new(2),
});
assert_eq!(
grid_layout_line_static_offset(
&tracks,
&css::GridAutoTrackList::initial(),
&placement,
&[0.0, 15.0, 30.0, 45.0, 60.0],
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::Start),
60.0,
),
Some(45.0)
);
}
#[test]
fn named_static_offset_resolves_auto_fill_after_explicit_implicit_line() {
let tracks = css::GridTrackList::Tracks {
components: vec![css::GridTrackListComponent::Repeat(
Vec::new(),
css::GridRepeat {
count: css::GridRepeatCount::AutoFill,
tracks: vec![css::GridTrackListComponent::Track(
vec!["slot".to_string()],
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
},
)],
trailing_names: vec!["end".to_string()],
},
)],
trailing_names: Vec::new(),
};
let auto_tracks = css::GridAutoTrackList::from_tracks(vec![css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(30.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(30.0),
),
}])
.expect("test grid auto-track list is non-empty");
let placement = css::GridPlacement::Line(css::GridLinePlacement::Named {
name: "slot".to_string(),
occurrence: std::num::NonZeroI32::new(4),
});
assert_eq!(
grid_layout_line_static_offset(
&tracks,
&auto_tracks,
&placement,
&[0.0, 25.0, 50.0, 70.0],
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::Start),
70.0,
),
Some(105.0)
);
}
#[test]
fn named_static_offset_resolves_auto_fill_before_explicit_implicit_line() {
let tracks = css::GridTrackList::Tracks {
components: vec![css::GridTrackListComponent::Repeat(
Vec::new(),
css::GridRepeat {
count: css::GridRepeatCount::AutoFill,
tracks: vec![css::GridTrackListComponent::Track(
vec!["slot".to_string()],
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
},
)],
trailing_names: vec!["end".to_string()],
},
)],
trailing_names: Vec::new(),
};
let auto_tracks = css::GridAutoTrackList::from_tracks(vec![
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(30.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(30.0),
),
},
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(40.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(40.0),
),
},
])
.expect("test grid auto-track list is non-empty");
let placement = css::GridPlacement::Line(css::GridLinePlacement::Named {
name: "slot".to_string(),
occurrence: std::num::NonZeroI32::new(-4),
});
assert_eq!(
grid_layout_line_static_offset(
&tracks,
&auto_tracks,
&placement,
&[0.0, 25.0, 50.0, 70.0],
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::Start),
70.0,
),
Some(0.0)
);
}
#[test]
fn named_static_offset_resolves_auto_fit_after_explicit_implicit_line() {
let tracks = css::GridTrackList::Tracks {
components: vec![css::GridTrackListComponent::Repeat(
Vec::new(),
css::GridRepeat {
count: css::GridRepeatCount::AutoFit,
tracks: vec![css::GridTrackListComponent::Track(
vec!["slot".to_string()],
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
},
)],
trailing_names: vec!["end".to_string()],
},
)],
trailing_names: Vec::new(),
};
let auto_tracks = css::GridAutoTrackList::from_tracks(vec![css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(30.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(30.0),
),
}])
.expect("test grid auto-track list is non-empty");
let placement = css::GridPlacement::Line(css::GridLinePlacement::Named {
name: "slot".to_string(),
occurrence: std::num::NonZeroI32::new(4),
});
assert_eq!(
grid_layout_line_static_offset(
&tracks,
&auto_tracks,
&placement,
&[0.0, 25.0, 45.0, 45.0],
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::Start),
70.0,
),
Some(80.0)
);
}
#[test]
fn named_static_offset_resolves_auto_fit_before_explicit_implicit_line() {
let tracks = css::GridTrackList::Tracks {
components: vec![css::GridTrackListComponent::Repeat(
Vec::new(),
css::GridRepeat {
count: css::GridRepeatCount::AutoFit,
tracks: vec![css::GridTrackListComponent::Track(
vec!["slot".to_string()],
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(20.0),
),
},
)],
trailing_names: vec!["end".to_string()],
},
)],
trailing_names: Vec::new(),
};
let auto_tracks = css::GridAutoTrackList::from_tracks(vec![
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(30.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(30.0),
),
},
css::GridTrackSize {
min: css::GridMinTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(40.0),
),
max: css::GridMaxTrackBreadth::LengthPercentage(
css::ComputedLengthPercentage::from_points(40.0),
),
},
])
.expect("test grid auto-track list is non-empty");
let placement = css::GridPlacement::Line(css::GridLinePlacement::Named {
name: "slot".to_string(),
occurrence: std::num::NonZeroI32::new(-4),
});
assert_eq!(
grid_layout_line_static_offset(
&tracks,
&auto_tracks,
&placement,
&[0.0, 25.0, 45.0, 45.0],
css::ComputedGap::LengthPercentage(css::ComputedLengthPercentage::from_points(5.0),),
css::ContentAlignment::new(css::ContentAlignmentKeyword::Start),
70.0,
),
Some(0.0)
);
}
}