diff --git a/float-pigment-forest/tests/custom/css_aspect_ratio.rs b/float-pigment-forest/tests/custom/css_aspect_ratio.rs index 08e2893..acaf4b7 100644 --- a/float-pigment-forest/tests/custom/css_aspect_ratio.rs +++ b/float-pigment-forest/tests/custom/css_aspect_ratio.rs @@ -297,3 +297,385 @@ pub fn aspect_ratio_2() { assert_eq!(child_a.layout_position().height, 100.); } } + +#[test] +pub fn aspect_ratio_in_block_width_fixed() { + unsafe { + let root = as_ref(Node::new_ptr()); + let container = as_ref(Node::new_ptr()); + container.set_width(DefLength::Points(Len::from_f32(300.))); + container.set_height(DefLength::Auto); + root.append_child(convert_node_ref_to_ptr(container)); + + let child = as_ref(Node::new_ptr()); + child.set_width(DefLength::Points(Len::from_f32(100.))); + child.set_height(DefLength::Auto); + child.set_aspect_ratio(Some(2. / 1.)); + container.append_child(convert_node_ref_to_ptr(child)); + + let child2 = as_ref(Node::new_ptr()); + child2.set_width(DefLength::Points(Len::from_f32(100.))); + child2.set_height(DefLength::Auto); + child2.set_aspect_ratio(Some(0.5 / 1.)); + container.append_child(convert_node_ref_to_ptr(child2)); + + root.layout( + OptionSize::new(OptionNum::some(Len::from_f32(400.)), OptionNum::none()), + Size::new(Len::from_f32(0.), Len::from_f32(0.)), + ); + + assert_eq!(child.layout_position().width, 100.); + assert_eq!(child.layout_position().height, 50.); + assert_eq!(child2.layout_position().width, 100.); + assert_eq!(child2.layout_position().height, 200.); + } +} + +#[test] +pub fn aspect_ratio_in_block_height_fixed() { + unsafe { + let root = as_ref(Node::new_ptr()); + let container = as_ref(Node::new_ptr()); + container.set_width(DefLength::Auto); + container.set_height(DefLength::Points(Len::from_f32(300.))); + root.append_child(convert_node_ref_to_ptr(container)); + + let child = as_ref(Node::new_ptr()); + child.set_width(DefLength::Auto); + child.set_height(DefLength::Points(Len::from_f32(100.))); + child.set_aspect_ratio(Some(2. / 1.)); + container.append_child(convert_node_ref_to_ptr(child)); + + let child2 = as_ref(Node::new_ptr()); + child2.set_width(DefLength::Auto); + child2.set_height(DefLength::Points(Len::from_f32(200.))); + child2.set_aspect_ratio(Some(0.5 / 1.)); + container.append_child(convert_node_ref_to_ptr(child2)); + + root.layout( + OptionSize::new(OptionNum::some(Len::from_f32(400.)), OptionNum::none()), + Size::new(Len::from_f32(0.), Len::from_f32(0.)), + ); + + assert_eq!(child.layout_position().width, 200.); + assert_eq!(child.layout_position().height, 100.); + assert_eq!(child2.layout_position().width, 100.); + assert_eq!(child2.layout_position().height, 200.); + } +} + +// wpt:css/css-sizing/aspect-ratio/block-aspect-ratio-008.html +#[test] +pub fn aspect_ratio_in_parent_block_cross_size_fixed() { + unsafe { + let root = as_ref(Node::new_ptr()); + let container = as_ref(Node::new_ptr()); + container.set_width(DefLength::Points(Len::from_f32(300.))); + container.set_height(DefLength::Auto); + root.append_child(convert_node_ref_to_ptr(container)); + + let child = as_ref(Node::new_ptr()); + child.set_width(DefLength::Auto); + child.set_aspect_ratio(Some(2. / 1.)); + container.append_child(convert_node_ref_to_ptr(child)); + + let container2 = as_ref(Node::new_ptr()); + container2.set_width(DefLength::Auto); + container2.set_height(DefLength::Points(Len::from_f32(300.))); + container2.set_writing_mode(WritingMode::VerticalLr); + root.append_child(convert_node_ref_to_ptr(container2)); + + let child2 = as_ref(Node::new_ptr()); + child2.set_width(DefLength::Auto); + child2.set_aspect_ratio(Some(0.5 / 1.)); + container2.append_child(convert_node_ref_to_ptr(child2)); + + root.layout( + OptionSize::new(OptionNum::some(Len::from_f32(400.)), OptionNum::none()), + Size::new(Len::from_f32(0.), Len::from_f32(0.)), + ); + + println!( + "{}", + root.dump_to_html( + DumpOptions { + recursive: true, + layout: true, + style: DumpStyleMode::Mutation + }, + 0 + ) + ); + + assert_eq!(child.layout_position().width, 300.); + assert_eq!(child.layout_position().height, 150.); + assert_eq!(child2.layout_position().width, 150.); + assert_eq!(child2.layout_position().height, 300.); + } +} + +#[test] +pub fn aspect_ratio_with_min_width_constraint() { + unsafe { + let root = as_ref(Node::new_ptr()); + let container = as_ref(Node::new_ptr()); + container.set_width(DefLength::Points(Len::from_f32(300.))); + container.set_height(DefLength::Auto); + root.append_child(convert_node_ref_to_ptr(container)); + + let child = as_ref(Node::new_ptr()); + child.set_width(DefLength::Auto); + child.set_height(DefLength::Auto); + child.set_min_width(DefLength::Points(Len::from_f32(400.))); + child.set_aspect_ratio(Some(2. / 1.)); + container.append_child(convert_node_ref_to_ptr(child)); + + root.layout( + OptionSize::new(OptionNum::some(Len::from_f32(200.)), OptionNum::none()), + Size::new(Len::from_f32(0.), Len::from_f32(0.)), + ); + + println!( + "{}", + root.dump_to_html( + DumpOptions { + recursive: true, + layout: true, + style: DumpStyleMode::Mutation + }, + 0 + ) + ); + + assert_eq!(child.layout_position().width, 400.); + assert_eq!(child.layout_position().height, 200.); + } +} + +#[test] +pub fn aspect_ratio_with_max_width_constraint() { + unsafe { + let root = as_ref(Node::new_ptr()); + let container = as_ref(Node::new_ptr()); + container.set_width(DefLength::Points(Len::from_f32(300.))); + container.set_height(DefLength::Auto); + root.append_child(convert_node_ref_to_ptr(container)); + + let child = as_ref(Node::new_ptr()); + child.set_width(DefLength::Auto); + child.set_height(DefLength::Auto); + child.set_max_width(DefLength::Points(Len::from_f32(80.))); + child.set_aspect_ratio(Some(2. / 1.)); + container.append_child(convert_node_ref_to_ptr(child)); + + root.layout( + OptionSize::new(OptionNum::some(Len::from_f32(200.)), OptionNum::none()), + Size::new(Len::from_f32(0.), Len::from_f32(0.)), + ); + + println!( + "{}", + root.dump_to_html( + DumpOptions { + recursive: true, + layout: true, + style: DumpStyleMode::Mutation + }, + 0 + ) + ); + + assert_eq!(child.layout_position().width, 80.); + assert_eq!(child.layout_position().height, 40.); + } +} + +#[test] +pub fn aspect_ratio_with_max_width_violating_min_height_constraint() { + unsafe { + let root = as_ref(Node::new_ptr()); + let container = as_ref(Node::new_ptr()); + container.set_width(DefLength::Points(Len::from_f32(300.))); + container.set_height(DefLength::Auto); + root.append_child(convert_node_ref_to_ptr(container)); + + let child = as_ref(Node::new_ptr()); + child.set_width(DefLength::Auto); + child.set_height(DefLength::Auto); + child.set_max_width(DefLength::Points(Len::from_f32(80.))); + child.set_min_height(DefLength::Points(Len::from_f32(100.))); + child.set_aspect_ratio(Some(1. / 1.)); + container.append_child(convert_node_ref_to_ptr(child)); + + root.layout( + OptionSize::new(OptionNum::some(Len::from_f32(200.)), OptionNum::none()), + Size::new(Len::from_f32(0.), Len::from_f32(0.)), + ); + + println!( + "{}", + root.dump_to_html( + DumpOptions { + recursive: true, + layout: true, + style: DumpStyleMode::Mutation + }, + 0 + ) + ); + + assert_eq!(child.layout_position().width, 80.); + assert_eq!(child.layout_position().height, 100.); + } +} + +#[test] +pub fn aspect_ratio_block_size_with_box_sizing() { + unsafe { + let root = as_ref(Node::new_ptr()); + let container = as_ref(Node::new_ptr()); + container.set_width(DefLength::Points(Len::from_f32(300.))); + container.set_height(DefLength::Auto); + root.append_child(convert_node_ref_to_ptr(container)); + + let child = as_ref(Node::new_ptr()); + child.set_width(DefLength::Auto); + child.set_height(DefLength::Auto); + child.set_width(DefLength::Points(Len::from_f32(50.))); + child.set_padding_left(DefLength::Points(Len::from_f32(30.))); + child.set_border_left(DefLength::Points(Len::from_f32(20.))); + child.set_box_sizing(BoxSizing::BorderBox); + child.set_aspect_ratio(Some(2. / 1.)); + container.append_child(convert_node_ref_to_ptr(child)); + + let child2 = as_ref(Node::new_ptr()); + child2.set_width(DefLength::Auto); + child2.set_height(DefLength::Auto); + child2.set_width(DefLength::Points(Len::from_f32(50.))); + child2.set_padding_left(DefLength::Points(Len::from_f32(30.))); + child2.set_border_left(DefLength::Points(Len::from_f32(20.))); + child2.set_box_sizing(BoxSizing::PaddingBox); + child2.set_aspect_ratio(Some(2. / 1.)); + container.append_child(convert_node_ref_to_ptr(child2)); + + let child3 = as_ref(Node::new_ptr()); + child3.set_width(DefLength::Auto); + child3.set_height(DefLength::Auto); + child3.set_width(DefLength::Points(Len::from_f32(50.))); + child3.set_padding_left(DefLength::Points(Len::from_f32(30.))); + child3.set_border_left(DefLength::Points(Len::from_f32(20.))); + child3.set_aspect_ratio(Some(2. / 1.)); + container.append_child(convert_node_ref_to_ptr(child3)); + + root.layout( + OptionSize::new(OptionNum::some(Len::from_f32(200.)), OptionNum::none()), + Size::new(Len::from_f32(0.), Len::from_f32(0.)), + ); + + println!( + "{}", + root.dump_to_html( + DumpOptions { + recursive: true, + layout: true, + style: DumpStyleMode::Mutation + }, + 0 + ) + ); + + assert_eq!(child.layout_position().width, 50.); + assert_eq!(child.layout_position().height, 25.); + assert_eq!(child2.layout_position().width, 80.); + assert_eq!(child2.layout_position().height, 25.); + assert_eq!(child3.layout_position().width, 100.); + assert_eq!(child3.layout_position().height, 25.); + } +} + +#[test] +pub fn aspect_ratio_block_size_with_box_sizing_and_writing_mode() { + unsafe { + let root = as_ref(Node::new_ptr()); + let container = as_ref(Node::new_ptr()); + container.set_width(DefLength::Points(Len::from_f32(300.))); + container.set_height(DefLength::Auto); + container.set_writing_mode(WritingMode::VerticalLr); + root.append_child(convert_node_ref_to_ptr(container)); + + let child = as_ref(Node::new_ptr()); + child.set_width(DefLength::Auto); + child.set_height(DefLength::Auto); + child.set_height(DefLength::Points(Len::from_f32(50.))); + child.set_padding_top(DefLength::Points(Len::from_f32(30.))); + child.set_border_top(DefLength::Points(Len::from_f32(20.))); + child.set_box_sizing(BoxSizing::BorderBox); + child.set_aspect_ratio(Some(2. / 1.)); + container.append_child(convert_node_ref_to_ptr(child)); + + let child2 = as_ref(Node::new_ptr()); + child2.set_width(DefLength::Auto); + child2.set_height(DefLength::Auto); + child2.set_height(DefLength::Points(Len::from_f32(50.))); + child2.set_padding_top(DefLength::Points(Len::from_f32(30.))); + child2.set_border_top(DefLength::Points(Len::from_f32(20.))); + child2.set_box_sizing(BoxSizing::PaddingBox); + child2.set_aspect_ratio(Some(2. / 1.)); + container.append_child(convert_node_ref_to_ptr(child2)); + + let child3 = as_ref(Node::new_ptr()); + child3.set_width(DefLength::Auto); + child3.set_height(DefLength::Auto); + child3.set_height(DefLength::Points(Len::from_f32(50.))); + child3.set_padding_top(DefLength::Points(Len::from_f32(30.))); + child3.set_border_top(DefLength::Points(Len::from_f32(20.))); + child3.set_aspect_ratio(Some(2. / 1.)); + container.append_child(convert_node_ref_to_ptr(child3)); + + root.layout( + OptionSize::new(OptionNum::some(Len::from_f32(200.)), OptionNum::none()), + Size::new(Len::from_f32(0.), Len::from_f32(0.)), + ); + + println!( + "{}", + root.dump_to_html( + DumpOptions { + recursive: true, + layout: true, + style: DumpStyleMode::Mutation + }, + 0 + ) + ); + + assert_eq!(child.layout_position().width, 100.); + assert_eq!(child.layout_position().height, 50.); + assert_eq!(child2.layout_position().width, 100.); + assert_eq!(child2.layout_position().height, 80.); + assert_eq!(child3.layout_position().width, 100.); + assert_eq!(child3.layout_position().height, 100.); + } +} + +#[test] +pub fn apsect_ratio_writing_mode_streched() { + assert_xml!( + r#" +