#![allow(clippy::field_reassign_with_default)]
use strata_layout::{ContainingBlock, LayoutNode, Rect};
use strata_primitives::geom::EdgeInsets;
use strata_style::types::{ComputedStyle, Dimension, Display, Position};
#[test]
fn test_containing_block_resolution_across_static_ancestors() {
let mut root_style = ComputedStyle::default();
root_style.display = Display::Block;
root_style.position = Position::Relative;
root_style.width = Dimension::Px(800.0);
root_style.height = Dimension::Px(600.0);
let mut static_style = ComputedStyle::default();
static_style.display = Display::Block;
static_style.position = Position::Static;
static_style.margin = EdgeInsets::all(40.0);
static_style.width = Dimension::Px(400.0);
static_style.height = Dimension::Px(300.0);
let mut abs_style = ComputedStyle::default();
abs_style.display = Display::Block;
abs_style.position = Position::Absolute;
abs_style.insets.top = Dimension::Px(15.0);
abs_style.insets.right = Dimension::Px(25.0);
abs_style.width = Dimension::Px(120.0);
abs_style.height = Dimension::Px(60.0);
let abs_node = LayoutNode::new(abs_style);
let static_node = LayoutNode::with_children(static_style, vec![abs_node]);
let mut root = LayoutNode::with_children(root_style, vec![static_node]);
root.compute_layout(800.0, 600.0);
let abs_geom = &root.children[0].children[0].geometry.border_box;
assert_eq!(abs_geom.x, 655.0);
assert_eq!(abs_geom.y, 15.0);
assert_eq!(abs_geom.width, 120.0);
assert_eq!(abs_geom.height, 60.0);
}
#[test]
fn test_double_anchored_horizontal_and_vertical_stretch() {
let mut root_style = ComputedStyle::default();
root_style.display = Display::Block;
root_style.position = Position::Relative;
root_style.width = Dimension::Px(600.0);
root_style.height = Dimension::Px(500.0);
let mut child_style = ComputedStyle::default();
child_style.display = Display::Block;
child_style.position = Position::Absolute;
child_style.insets.top = Dimension::Px(20.0);
child_style.insets.bottom = Dimension::Px(30.0);
child_style.insets.left = Dimension::Px(40.0);
child_style.insets.right = Dimension::Px(50.0);
child_style.width = Dimension::Auto;
child_style.height = Dimension::Auto;
let mut root = LayoutNode::with_children(root_style, vec![LayoutNode::new(child_style)]);
root.compute_layout(600.0, 500.0);
let child_geom = &root.children[0].geometry.border_box;
assert_eq!(child_geom.x, 40.0);
assert_eq!(child_geom.y, 20.0);
assert_eq!(child_geom.width, 510.0);
assert_eq!(child_geom.height, 450.0);
}
#[test]
fn test_absolute_auto_margin_centering() {
let mut root_style = ComputedStyle::default();
root_style.display = Display::Block;
root_style.position = Position::Relative;
root_style.width = Dimension::Px(800.0);
root_style.height = Dimension::Px(600.0);
let mut modal_style = ComputedStyle::default();
modal_style.display = Display::Block;
modal_style.position = Position::Absolute;
modal_style.insets.top = Dimension::Px(0.0);
modal_style.insets.bottom = Dimension::Px(0.0);
modal_style.insets.left = Dimension::Px(0.0);
modal_style.insets.right = Dimension::Px(0.0);
modal_style.margin_auto_top = true;
modal_style.margin_auto_bottom = true;
modal_style.margin_auto_left = true;
modal_style.margin_auto_right = true;
modal_style.width = Dimension::Px(300.0);
modal_style.height = Dimension::Px(200.0);
let mut root = LayoutNode::with_children(root_style, vec![LayoutNode::new(modal_style)]);
root.compute_layout(800.0, 600.0);
let modal_geom = &root.children[0].geometry.border_box;
assert_eq!(modal_geom.x, 250.0);
assert_eq!(modal_geom.y, 200.0);
assert_eq!(modal_geom.width, 300.0);
assert_eq!(modal_geom.height, 200.0);
}
#[test]
fn test_sticky_positioning_clamping_logic() {
let mut parent = LayoutNode::new(ComputedStyle {
display: Display::Block,
position: Position::Relative,
width: Dimension::Px(400.0),
height: Dimension::Px(800.0),
..Default::default()
});
let header = LayoutNode::new(ComputedStyle {
display: Display::Block,
width: Dimension::Px(400.0),
height: Dimension::Px(100.0),
..Default::default()
});
parent.add_child(header);
let mut sticky_child = LayoutNode::new(ComputedStyle {
display: Display::Block,
position: Position::Sticky,
width: Dimension::Px(400.0),
height: Dimension::Px(50.0),
..Default::default()
});
sticky_child.style.insets.top = Dimension::Px(10.0);
parent.add_child(sticky_child);
parent.compute_layout(400.0, 800.0);
assert_eq!(parent.children[1].geometry.border_box.y, 100.0);
let cb_scrolled = ContainingBlock::with_scroll(
parent.geometry.padding_box,
Rect::new(0.0, 0.0, 400.0, 600.0),
0.0,
150.0,
);
strata_layout::solve_positioning_children(&mut parent, cb_scrolled);
assert_eq!(parent.children[1].geometry.border_box.y, 160.0);
let cb_overscrolled = ContainingBlock::with_scroll(
parent.geometry.padding_box,
Rect::new(0.0, 0.0, 400.0, 600.0),
0.0,
900.0,
);
strata_layout::solve_positioning_children(&mut parent, cb_overscrolled);
assert_eq!(parent.children[1].geometry.border_box.y, 750.0);
}
#[test]
fn test_percentage_insets_resolution() {
let mut root_style = ComputedStyle::default();
root_style.display = Display::Block;
root_style.position = Position::Relative;
root_style.width = Dimension::Px(1000.0);
root_style.height = Dimension::Px(800.0);
let mut abs_style = ComputedStyle::default();
abs_style.display = Display::Block;
abs_style.position = Position::Absolute;
abs_style.insets.left = Dimension::Percent(20.0);
abs_style.insets.top = Dimension::Percent(25.0);
abs_style.width = Dimension::Percent(50.0);
abs_style.height = Dimension::Percent(40.0);
let mut root = LayoutNode::with_children(root_style, vec![LayoutNode::new(abs_style)]);
root.compute_layout(1000.0, 800.0);
let child_geom = &root.children[0].geometry.border_box;
assert_eq!(child_geom.x, 200.0);
assert_eq!(child_geom.y, 200.0);
assert_eq!(child_geom.width, 500.0);
assert_eq!(child_geom.height, 320.0);
}
#[test]
fn test_sticky_element_clamped_to_static_parent_container() {
let mut grandparent = LayoutNode::new(ComputedStyle {
display: Display::Block,
position: Position::Relative,
width: Dimension::Px(600.0),
height: Dimension::Px(3000.0),
..Default::default()
});
let mut parent = LayoutNode::new(ComputedStyle {
display: Display::Block,
position: Position::Static,
width: Dimension::Px(600.0),
height: Dimension::Px(400.0),
..Default::default()
});
let mut sticky_child = LayoutNode::new(ComputedStyle {
display: Display::Block,
position: Position::Sticky,
width: Dimension::Px(600.0),
height: Dimension::Px(50.0),
..Default::default()
});
sticky_child.style.insets.top = Dimension::Px(0.0);
parent.add_child(sticky_child);
grandparent.add_child(parent);
grandparent.compute_layout(600.0, 3000.0);
let cb_scrolled = ContainingBlock::with_scroll(
grandparent.geometry.padding_box,
Rect::new(0.0, 0.0, 600.0, 800.0),
0.0,
600.0,
);
strata_layout::solve_positioning_children(&mut grandparent, cb_scrolled);
let sticky_geom = &grandparent.children[0].children[0].geometry.border_box;
assert_eq!(
sticky_geom.y, 350.0,
"Sticky child must be clamped to static parent container bottom"
);
}
#[test]
fn test_absolute_auto_width_and_height_with_right_inset_in_block_and_flex_parents() {
use strata_style::types::FlexDirection;
for is_flex in [false, true] {
let mut parent_style = ComputedStyle {
display: if is_flex {
Display::Flex
} else {
Display::Block
},
flex_direction: FlexDirection::Row,
position: Position::Relative,
width: Dimension::Px(360.0),
height: Dimension::Px(80.0),
..Default::default()
};
if is_flex {
parent_style.justify_content = strata_style::types::JustifyContent::SpaceBetween;
}
let mut dropdown_wrap_style = ComputedStyle {
display: Display::Block,
position: Position::Absolute,
width: Dimension::Auto,
height: Dimension::Auto,
..Default::default()
};
dropdown_wrap_style.insets.right = Dimension::Px(8.0);
dropdown_wrap_style.insets.top = Dimension::Px(36.0);
let menu_panel = LayoutNode::new(ComputedStyle {
display: Display::Block,
width: Dimension::Px(192.0),
height: Dimension::Px(112.0),
..Default::default()
});
let dropdown_wrap = LayoutNode::with_children(dropdown_wrap_style, vec![menu_panel]);
let mut parent = LayoutNode::with_children(parent_style, vec![dropdown_wrap]);
parent.compute_layout(360.0, 80.0);
let wrap_box = parent.children[0].geometry.border_box;
let panel_box = parent.children[0].children[0].geometry.border_box;
assert_eq!(
wrap_box.width, 192.0,
"is_flex={is_flex}: auto width must shrink-to-fit w-48 child"
);
assert_eq!(
wrap_box.height, 112.0,
"is_flex={is_flex}: auto height must wrap laid-out child"
);
assert_eq!(
wrap_box.x, 160.0,
"is_flex={is_flex}: right inset must subtract intrinsic width"
);
assert_eq!(
wrap_box.y, 36.0,
"is_flex={is_flex}: top inset must anchor to 36px"
);
assert_eq!(
panel_box.x, 160.0,
"is_flex={is_flex}: child subtree must translate to item_x"
);
assert_eq!(
panel_box.y, 36.0,
"is_flex={is_flex}: child subtree must translate to item_y"
);
assert!(
wrap_box.contains_point(220.0, 90.0),
"border_box hit-test must cover dropdown"
);
}
}
#[test]
fn test_absolute_auto_height_with_bottom_and_right_insets() {
let mut root_style = ComputedStyle {
display: Display::Flex,
flex_direction: strata_style::types::FlexDirection::Column,
position: Position::Relative,
width: Dimension::Px(400.0),
height: Dimension::Px(300.0),
..Default::default()
};
root_style.align_items = strata_style::types::AlignItems::Center;
let mut popover_style = ComputedStyle {
display: Display::Flex,
flex_direction: strata_style::types::FlexDirection::Column,
position: Position::Absolute,
width: Dimension::Auto,
height: Dimension::Auto,
..Default::default()
};
popover_style.insets.right = Dimension::Px(12.0);
popover_style.insets.bottom = Dimension::Px(16.0);
let item_a = LayoutNode::new(ComputedStyle {
display: Display::Block,
width: Dimension::Px(180.0),
height: Dimension::Px(36.0),
..Default::default()
});
let item_b = LayoutNode::new(ComputedStyle {
display: Display::Block,
width: Dimension::Px(180.0),
height: Dimension::Px(44.0),
..Default::default()
});
let popover = LayoutNode::with_children(popover_style, vec![item_a, item_b]);
let mut root = LayoutNode::with_children(root_style, vec![popover]);
root.compute_layout(400.0, 300.0);
let pop_box = root.children[0].geometry.border_box;
assert_eq!(pop_box.width, 180.0);
assert_eq!(pop_box.height, 80.0);
assert_eq!(pop_box.x, 208.0);
assert_eq!(pop_box.y, 204.0);
assert_eq!(root.children[0].children[0].geometry.border_box.y, 204.0);
assert_eq!(root.children[0].children[1].geometry.border_box.y, 240.0);
}
#[test]
fn test_nested_relative_insets_do_not_compound_across_positioned_ancestors() {
let row0 = LayoutNode::new(ComputedStyle {
display: Display::Block,
width: Dimension::Px(400.0),
height: Dimension::Px(22.0),
..Default::default()
});
let row1 = LayoutNode::new(ComputedStyle {
display: Display::Block,
width: Dimension::Px(400.0),
height: Dimension::Px(22.0),
..Default::default()
});
let mut slice_style = ComputedStyle {
display: Display::Flex,
flex_direction: strata_style::types::FlexDirection::Column,
position: Position::Relative,
width: Dimension::Px(400.0),
height: Dimension::Auto,
..Default::default()
};
slice_style.insets.top = Dimension::Px(-50.0);
slice_style.insets.left = Dimension::Px(12.0);
let slice = LayoutNode::with_children(slice_style, vec![row0, row1]);
let viewport = LayoutNode::with_children(
ComputedStyle {
display: Display::Block,
position: Position::Relative,
width: Dimension::Px(400.0),
height: Dimension::Px(240.0),
..Default::default()
},
vec![slice],
);
let body = LayoutNode::with_children(
ComputedStyle {
display: Display::Flex,
flex_direction: strata_style::types::FlexDirection::Row,
position: Position::Relative,
width: Dimension::Px(400.0),
height: Dimension::Px(240.0),
..Default::default()
},
vec![viewport],
);
let mut root = LayoutNode::with_children(
ComputedStyle {
display: Display::Flex,
flex_direction: strata_style::types::FlexDirection::Column,
position: Position::Relative,
width: Dimension::Px(400.0),
height: Dimension::Px(240.0),
..Default::default()
},
vec![body],
);
root.compute_layout(400.0, 240.0);
let slice_box = root.children[0].children[0].children[0].geometry.border_box;
let row0_box = root.children[0].children[0].children[0].children[0]
.geometry
.border_box;
let row1_box = root.children[0].children[0].children[0].children[1]
.geometry
.border_box;
assert_eq!(slice_box.x, 12.0, "relative left must apply exactly once");
assert_eq!(slice_box.y, -50.0, "relative top must apply exactly once");
assert_eq!(row0_box.y, -50.0);
assert_eq!(row1_box.y, -28.0);
root.compute_layout(400.0, 240.0);
let slice_box_2 = root.children[0].children[0].children[0].geometry.border_box;
assert_eq!(slice_box_2.x, 12.0);
assert_eq!(slice_box_2.y, -50.0);
}