1use script::layout_dom::ServoLayoutNode;
6
7use crate::context::LayoutContext;
8use crate::dom::{LayoutBox, NodeExt};
9use crate::flexbox::FlexLevelBox;
10use crate::flow::BlockLevelBox;
11use crate::flow::inline::InlineItem;
12use crate::fragment_tree::Fragment;
13
14pub(crate) struct LayoutRoot<'dom> {
37 node: ServoLayoutNode<'dom>,
38}
39
40impl<'dom> TryFrom<ServoLayoutNode<'dom>> for LayoutRoot<'dom> {
41 type Error = ();
42
43 fn try_from(node: ServoLayoutNode<'dom>) -> Result<Self, Self::Error> {
44 if !node.is_absolutely_positioned() {
45 return Err(());
46 }
47
48 let mut is_layout_root = false;
50 node.with_layout_box_base(|base| {
51 let fragments = base.fragments();
52 is_layout_root =
53 fragments.len() == 1 && matches!(fragments.first(), Some(Fragment::LayoutRoot(..)));
54 });
55 if !is_layout_root {
56 return Err(());
57 }
58
59 Ok(Self { node })
60 }
61}
62
63impl LayoutRoot<'_> {
64 pub(crate) fn try_layout(&self, layout_context: &LayoutContext) -> bool {
65 let Some(inner_layout_data) = self.node.inner_layout_data_mut() else {
66 return false;
67 };
68 let layout_box = inner_layout_data.self_box.clone();
69
70 let layout_box = layout_box.borrow();
71 let Some(layout_box) = &*layout_box else {
72 return false;
73 };
74
75 let positioned_box = match layout_box {
76 LayoutBox::BlockLevel(block_level) => {
77 let mut block_level = block_level.borrow_mut();
78 match &mut *block_level {
79 BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(positioned_box) => {
80 positioned_box.clone()
81 },
82 _ => return false,
83 }
84 },
85 LayoutBox::InlineLevel(InlineItem::OutOfFlowAbsolutelyPositionedBox(
86 positioned_box,
87 _,
88 )) => positioned_box.clone(),
89 LayoutBox::FlexLevel(flex_level_box) => {
90 let mut flex_level_box = flex_level_box.borrow_mut();
91 match &mut *flex_level_box {
92 FlexLevelBox::OutOfFlowAbsolutelyPositionedBox(positioned_box) => {
93 positioned_box.clone()
94 },
95 _ => return false,
96 }
97 },
98 _ => return false,
99 };
100
101 let positioned_box = positioned_box.borrow();
102 let formatting_context = &positioned_box.context;
103 let fragments = formatting_context.base.fragments();
104 let Some(Fragment::LayoutRoot(layout_root_fragment)) = fragments.first() else {
105 return false;
106 };
107
108 let layout_inputs = formatting_context.layout_root_layout_inputs.borrow();
109 let Some(layout_inputs) = &*layout_inputs else {
110 return false;
111 };
112
113 layout_inputs
116 .layout(
117 layout_context,
118 formatting_context,
119 &layout_root_fragment.fragment,
120 )
121 .is_ok()
122 }
123
124 pub(crate) fn handle_failed_layout_root_layout(&self) {
125 self.node
126 .clear_fragments_and_dirty_fragment_caches_recursively();
127 }
128}