| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  | #include <LibWeb/Layout/AvailableSpace.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | #include <LibWeb/Layout/BlockContainer.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:30:32 +02:00
										 |  |  | #include <LibWeb/Layout/LayoutState.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-10 16:46:44 +01:00
										 |  |  | #include <LibWeb/Layout/TextNode.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Layout { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  | LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyleAndBoxModelMetrics const& box) | 
					
						
							| 
									
										
										
										
											2022-02-28 12:32:23 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  |     auto serial_id = box.serial_id(); | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     if (used_values_per_layout_node[serial_id]) | 
					
						
							|  |  |  |         return *used_values_per_layout_node[serial_id]; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) { | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |         if (ancestor->used_values_per_layout_node[serial_id]) { | 
					
						
							|  |  |  |             auto cow_used_values = adopt_own(*new UsedValues(*ancestor->used_values_per_layout_node[serial_id])); | 
					
						
							|  |  |  |             auto* cow_used_values_ptr = cow_used_values.ptr(); | 
					
						
							|  |  |  |             used_values_per_layout_node[serial_id] = move(cow_used_values); | 
					
						
							|  |  |  |             return *cow_used_values_ptr; | 
					
						
							| 
									
										
										
										
											2022-03-12 16:33:11 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-12 16:33:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-25 11:04:29 +01:00
										 |  |  |     auto const* containing_block_used_values = box.is_viewport() ? nullptr : &get(*box.containing_block()); | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     used_values_per_layout_node[serial_id] = adopt_own(*new UsedValues); | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |     used_values_per_layout_node[serial_id]->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values); | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     return *used_values_per_layout_node[serial_id]; | 
					
						
							| 
									
										
										
										
											2022-02-28 12:32:23 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  | LayoutState::UsedValues const& LayoutState::get(NodeWithStyleAndBoxModelMetrics const& box) const | 
					
						
							| 
									
										
										
										
											2022-02-28 12:32:23 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  |     auto serial_id = box.serial_id(); | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     if (used_values_per_layout_node[serial_id]) | 
					
						
							|  |  |  |         return *used_values_per_layout_node[serial_id]; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (auto* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) { | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |         if (ancestor->used_values_per_layout_node[serial_id]) | 
					
						
							|  |  |  |             return *ancestor->used_values_per_layout_node[serial_id]; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-25 11:04:29 +01:00
										 |  |  |     auto const* containing_block_used_values = box.is_viewport() ? nullptr : &get(*box.containing_block()); | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     const_cast<LayoutState*>(this)->used_values_per_layout_node[serial_id] = adopt_own(*new UsedValues); | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |     const_cast<LayoutState*>(this)->used_values_per_layout_node[serial_id]->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values); | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     return *used_values_per_layout_node[serial_id]; | 
					
						
							| 
									
										
										
										
											2022-02-28 12:32:23 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:30:32 +02:00
										 |  |  | void LayoutState::commit() | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-16 23:30:32 +02:00
										 |  |  |     // Only the top-level LayoutState should ever be committed.
 | 
					
						
							| 
									
										
										
										
											2022-03-12 16:33:11 +01:00
										 |  |  |     VERIFY(!m_parent); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 16:46:44 +01:00
										 |  |  |     HashTable<Layout::TextNode*> text_nodes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     for (auto& used_values_ptr : used_values_per_layout_node) { | 
					
						
							|  |  |  |         if (!used_values_ptr) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |         auto& used_values = *used_values_ptr; | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |         auto& node = const_cast<NodeWithStyleAndBoxModelMetrics&>(used_values.node()); | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Transfer box model metrics.
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |         node.box_model().inset = { used_values.inset_top.value(), used_values.inset_right.value(), used_values.inset_bottom.value(), used_values.inset_left.value() }; | 
					
						
							|  |  |  |         node.box_model().padding = { used_values.padding_top.value(), used_values.padding_right.value(), used_values.padding_bottom.value(), used_values.padding_left.value() }; | 
					
						
							|  |  |  |         node.box_model().border = { used_values.border_top.value(), used_values.border_right.value(), used_values.border_bottom.value(), used_values.border_left.value() }; | 
					
						
							|  |  |  |         node.box_model().margin = { used_values.margin_top.value(), used_values.margin_right.value(), used_values.margin_bottom.value(), used_values.margin_left.value() }; | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 15:50:57 +01:00
										 |  |  |         node.set_paintable(node.create_paintable()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-09 23:53:41 +01:00
										 |  |  |         // For boxes, transfer all the state needed for painting.
 | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  |         if (is<Layout::Box>(node)) { | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |             auto& box = static_cast<Layout::Box const&>(node); | 
					
						
							| 
									
										
										
										
											2022-03-10 15:50:57 +01:00
										 |  |  |             auto& paint_box = const_cast<Painting::PaintableBox&>(*box.paint_box()); | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |             paint_box.set_offset(used_values.offset); | 
					
						
							| 
									
										
										
										
											2022-07-17 17:59:02 +02:00
										 |  |  |             paint_box.set_content_size(used_values.content_width(), used_values.content_height()); | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |             paint_box.set_overflow_data(move(used_values.overflow_data)); | 
					
						
							|  |  |  |             paint_box.set_containing_line_box_fragment(used_values.containing_line_box_fragment); | 
					
						
							| 
									
										
										
										
											2022-03-10 11:12:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 16:46:44 +01:00
										 |  |  |             if (is<Layout::BlockContainer>(box)) { | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |                 for (auto& line_box : used_values.line_boxes) { | 
					
						
							| 
									
										
										
										
											2022-03-10 16:46:44 +01:00
										 |  |  |                     for (auto& fragment : line_box.fragments()) { | 
					
						
							|  |  |  |                         if (fragment.layout_node().is_text_node()) | 
					
						
							|  |  |  |                             text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node()))); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |                 static_cast<Painting::PaintableWithLines&>(paint_box).set_line_boxes(move(used_values.line_boxes)); | 
					
						
							| 
									
										
										
										
											2022-03-10 16:46:44 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-10 16:46:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (auto* text_node : text_nodes) | 
					
						
							|  |  |  |         text_node->set_paintable(text_node->create_paintable()); | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | CSSPixels box_baseline(LayoutState const& state, Box const& box) | 
					
						
							| 
									
										
										
										
											2022-12-04 19:51:29 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     auto const& box_state = state.get(box); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 14:50:52 +01:00
										 |  |  |     // https://www.w3.org/TR/CSS2/visudet.html#propdef-vertical-align
 | 
					
						
							| 
									
										
										
										
											2022-12-04 19:51:29 +03:00
										 |  |  |     auto const& vertical_align = box.computed_values().vertical_align(); | 
					
						
							|  |  |  |     if (vertical_align.has<CSS::VerticalAlign>()) { | 
					
						
							|  |  |  |         switch (vertical_align.get<CSS::VerticalAlign>()) { | 
					
						
							|  |  |  |         case CSS::VerticalAlign::Top: | 
					
						
							| 
									
										
										
										
											2023-01-01 14:50:52 +01:00
										 |  |  |             // Top: Align the top of the aligned subtree with the top of the line box.
 | 
					
						
							| 
									
										
										
										
											2022-12-04 19:51:29 +03:00
										 |  |  |             return box_state.border_box_top(); | 
					
						
							|  |  |  |         case CSS::VerticalAlign::Bottom: | 
					
						
							| 
									
										
										
										
											2023-01-01 14:50:52 +01:00
										 |  |  |             // Bottom: Align the bottom of the aligned subtree with the bottom of the line box.
 | 
					
						
							| 
									
										
										
										
											2023-03-14 10:28:01 +01:00
										 |  |  |             return box_state.content_height() + box_state.margin_box_top(); | 
					
						
							| 
									
										
										
										
											2023-01-01 14:50:52 +01:00
										 |  |  |         case CSS::VerticalAlign::TextTop: | 
					
						
							|  |  |  |             // TextTop: Align the top of the box with the top of the parent's content area (see 10.6.1).
 | 
					
						
							|  |  |  |             return box.computed_values().font_size(); | 
					
						
							|  |  |  |         case CSS::VerticalAlign::TextBottom: | 
					
						
							|  |  |  |             // TextTop: Align the bottom of the box with the bottom of the parent's content area (see 10.6.1).
 | 
					
						
							|  |  |  |             return box_state.content_height() - (box.containing_block()->font().pixel_metrics().descent * 2); | 
					
						
							| 
									
										
										
										
											2022-12-04 19:51:29 +03:00
										 |  |  |         default: | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!box_state.line_boxes.is_empty()) | 
					
						
							| 
									
										
										
										
											2023-03-14 10:28:01 +01:00
										 |  |  |         return box_state.margin_box_top() + box_state.offset.y() + box_state.line_boxes.last().baseline(); | 
					
						
							| 
									
										
										
										
											2022-12-04 19:51:29 +03:00
										 |  |  |     if (box.has_children() && !box.children_are_inline()) { | 
					
						
							|  |  |  |         auto const* child_box = box.last_child_of_type<Box>(); | 
					
						
							|  |  |  |         VERIFY(child_box); | 
					
						
							|  |  |  |         return box_baseline(state, *child_box); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-14 10:28:01 +01:00
										 |  |  |     return box_state.margin_box_height(); | 
					
						
							| 
									
										
										
										
											2022-12-04 19:51:29 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | CSSPixelRect margin_box_rect(Box const& box, LayoutState const& state) | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto const& box_state = state.get(box); | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |     auto rect = CSSPixelRect { box_state.offset, { box_state.content_width(), box_state.content_height() } }; | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  |     rect.set_x(rect.x() - box_state.margin_box_left()); | 
					
						
							|  |  |  |     rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right()); | 
					
						
							|  |  |  |     rect.set_y(rect.y() - box_state.margin_box_top()); | 
					
						
							|  |  |  |     rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom()); | 
					
						
							|  |  |  |     return rect; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | CSSPixelRect border_box_rect(Box const& box, LayoutState const& state) | 
					
						
							| 
									
										
										
										
											2022-09-07 13:29:58 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto const& box_state = state.get(box); | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |     auto rect = CSSPixelRect { box_state.offset, { box_state.content_width(), box_state.content_height() } }; | 
					
						
							| 
									
										
										
										
											2022-09-07 13:29:58 +02:00
										 |  |  |     rect.set_x(rect.x() - box_state.border_box_left()); | 
					
						
							|  |  |  |     rect.set_width(rect.width() + box_state.border_box_left() + box_state.border_box_right()); | 
					
						
							|  |  |  |     rect.set_y(rect.y() - box_state.border_box_top()); | 
					
						
							|  |  |  |     rect.set_height(rect.height() + box_state.border_box_top() + box_state.border_box_bottom()); | 
					
						
							|  |  |  |     return rect; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | CSSPixelRect border_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state) | 
					
						
							| 
									
										
										
										
											2022-09-07 13:29:58 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto rect = border_box_rect(box, state); | 
					
						
							| 
									
										
										
										
											2022-09-22 14:17:11 +02:00
										 |  |  |     if (&box == &ancestor_box) | 
					
						
							|  |  |  |         return rect; | 
					
						
							|  |  |  |     for (auto const* current = box.containing_block(); current; current = current->containing_block()) { | 
					
						
							| 
									
										
										
										
											2022-09-07 13:29:58 +02:00
										 |  |  |         if (current == &ancestor_box) | 
					
						
							| 
									
										
										
										
											2022-09-22 14:17:11 +02:00
										 |  |  |             return rect; | 
					
						
							|  |  |  |         auto const& current_state = state.get(static_cast<Box const&>(*current)); | 
					
						
							|  |  |  |         rect.translate_by(current_state.offset); | 
					
						
							| 
									
										
										
										
											2022-09-07 13:29:58 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-22 14:17:11 +02:00
										 |  |  |     // If we get here, ancestor_box was not a containing block ancestor of `box`!
 | 
					
						
							|  |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2022-09-07 13:29:58 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | CSSPixelRect content_box_rect(Box const& box, LayoutState const& state) | 
					
						
							| 
									
										
										
										
											2022-09-08 13:21:57 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto const& box_state = state.get(box); | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |     return CSSPixelRect { box_state.offset, { box_state.content_width(), box_state.content_height() } }; | 
					
						
							| 
									
										
										
										
											2022-09-08 13:21:57 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | CSSPixelRect content_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state) | 
					
						
							| 
									
										
										
										
											2022-09-08 13:21:57 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto rect = content_box_rect(box, state); | 
					
						
							| 
									
										
										
										
											2022-09-22 14:17:11 +02:00
										 |  |  |     if (&box == &ancestor_box) | 
					
						
							|  |  |  |         return rect; | 
					
						
							|  |  |  |     for (auto const* current = box.containing_block(); current; current = current->containing_block()) { | 
					
						
							| 
									
										
										
										
											2022-09-08 13:21:57 +02:00
										 |  |  |         if (current == &ancestor_box) | 
					
						
							| 
									
										
										
										
											2022-09-22 14:17:11 +02:00
										 |  |  |             return rect; | 
					
						
							|  |  |  |         auto const& current_state = state.get(static_cast<Box const&>(*current)); | 
					
						
							|  |  |  |         rect.translate_by(current_state.offset); | 
					
						
							| 
									
										
										
										
											2022-09-08 13:21:57 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-22 14:17:11 +02:00
										 |  |  |     // If we get here, ancestor_box was not a containing block ancestor of `box`!
 | 
					
						
							|  |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2022-09-08 13:21:57 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | CSSPixelRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state) | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto rect = margin_box_rect(box, state); | 
					
						
							| 
									
										
										
										
											2022-09-22 14:17:11 +02:00
										 |  |  |     if (&box == &ancestor_box) | 
					
						
							|  |  |  |         return rect; | 
					
						
							|  |  |  |     for (auto const* current = box.containing_block(); current; current = current->containing_block()) { | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  |         if (current == &ancestor_box) | 
					
						
							| 
									
										
										
										
											2022-09-22 14:17:11 +02:00
										 |  |  |             return rect; | 
					
						
							|  |  |  |         auto const& current_state = state.get(static_cast<Box const&>(*current)); | 
					
						
							|  |  |  |         rect.translate_by(current_state.offset); | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-22 14:17:11 +02:00
										 |  |  |     // If we get here, ancestor_box was not a containing block ancestor of `box`!
 | 
					
						
							|  |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | CSSPixelRect absolute_content_rect(Box const& box, LayoutState const& state) | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto const& box_state = state.get(box); | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |     CSSPixelRect rect { box_state.offset, { box_state.content_width(), box_state.content_height() } }; | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  |     for (auto* block = box.containing_block(); block; block = block->containing_block()) | 
					
						
							|  |  |  |         rect.translate_by(state.get(*block).offset); | 
					
						
							|  |  |  |     return rect; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  | void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_node = &node; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-13 14:49:08 +02:00
										 |  |  |     // NOTE: In the code below, we decide if `node` has definite width and/or height.
 | 
					
						
							|  |  |  |     //       This attempts to cover all the *general* cases where CSS considers sizes to be definite.
 | 
					
						
							|  |  |  |     //       If `node` has definite values for min/max-width or min/max-height and a definite
 | 
					
						
							|  |  |  |     //       preferred size in the same axis, we clamp the preferred size here as well.
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     //       There are additional cases where CSS considers values to be definite. We model all of
 | 
					
						
							|  |  |  |     //       those by having our engine consider sizes to be definite *once they are assigned to
 | 
					
						
							|  |  |  |     //       the UsedValues by calling set_content_width() or set_content_height().
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |     auto const& computed_values = node.computed_values(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |     auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) { | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |         // A size that can be determined without performing layout; that is,
 | 
					
						
							|  |  |  |         // a <length>,
 | 
					
						
							|  |  |  |         // a measure of text (without consideration of line-wrapping),
 | 
					
						
							|  |  |  |         // a size of the initial containing block,
 | 
					
						
							|  |  |  |         // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto containing_block_has_definite_size = containing_block_used_values ? (width ? containing_block_used_values->has_definite_width() : containing_block_used_values->has_definite_height()) : false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (size.is_auto()) { | 
					
						
							|  |  |  |             // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |             if (width | 
					
						
							| 
									
										
										
										
											2022-10-07 13:07:40 +02:00
										 |  |  |                 && !node.is_floating() | 
					
						
							| 
									
										
										
										
											2022-10-06 16:02:53 +02:00
										 |  |  |                 && node.display().is_block_outside() | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |                 && node.parent() | 
					
						
							|  |  |  |                 && !node.parent()->is_floating() | 
					
						
							| 
									
										
										
										
											2022-10-06 16:02:53 +02:00
										 |  |  |                 && (node.parent()->display().is_flow_root_inside() | 
					
						
							|  |  |  |                     || node.parent()->display().is_flow_inside())) { | 
					
						
							| 
									
										
										
										
											2022-07-19 13:46:27 +02:00
										 |  |  |                 if (containing_block_has_definite_size) { | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |                     CSSPixels available_width = containing_block_used_values->content_width(); | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |                     resolved_definite_size = available_width | 
					
						
							|  |  |  |                         - margin_left | 
					
						
							|  |  |  |                         - margin_right | 
					
						
							|  |  |  |                         - padding_left | 
					
						
							|  |  |  |                         - padding_right | 
					
						
							|  |  |  |                         - border_left | 
					
						
							|  |  |  |                         - border_right; | 
					
						
							| 
									
										
										
										
											2022-07-19 13:46:27 +02:00
										 |  |  |                     return true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |         if (size.is_length() && size.length().is_calculated()) { | 
					
						
							| 
									
										
										
										
											2022-10-13 11:16:54 +02:00
										 |  |  |             if (size.length().calculated_style_value()->contains_percentage()) { | 
					
						
							|  |  |  |                 if (!containing_block_has_definite_size) | 
					
						
							|  |  |  |                     return false; | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |                 auto& calc_value = *size.length().calculated_style_value(); | 
					
						
							| 
									
										
										
										
											2022-10-13 11:16:54 +02:00
										 |  |  |                 auto containing_block_size_as_length = width | 
					
						
							|  |  |  |                     ? CSS::Length::make_px(containing_block_used_values->content_width()) | 
					
						
							|  |  |  |                     : CSS::Length::make_px(containing_block_used_values->content_height()); | 
					
						
							|  |  |  |                 resolved_definite_size = calc_value.resolve_length_percentage(node, containing_block_size_as_length).value_or(CSS::Length::make_auto()).to_px(node); | 
					
						
							|  |  |  |                 return true; | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-07-19 13:46:27 +02:00
										 |  |  |             resolved_definite_size = size.length().to_px(node); | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2022-07-19 13:46:27 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (size.is_length()) { | 
					
						
							|  |  |  |             VERIFY(!size.is_auto());                // This should have been covered by the Size::is_auto() branch above.
 | 
					
						
							|  |  |  |             VERIFY(!size.length().is_calculated()); // Covered above.
 | 
					
						
							|  |  |  |             resolved_definite_size = size.length().to_px(node); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-07-19 13:46:27 +02:00
										 |  |  |         if (size.is_percentage()) { | 
					
						
							|  |  |  |             if (containing_block_has_definite_size) { | 
					
						
							|  |  |  |                 auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height(); | 
					
						
							|  |  |  |                 resolved_definite_size = containing_block_size * size.percentage().as_fraction(); | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |         // FIXME: Determine if calc() value is definite.
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |     CSSPixels min_width = 0; | 
					
						
							| 
									
										
										
										
											2022-10-13 14:49:08 +02:00
										 |  |  |     bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true); | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |     CSSPixels max_width = 0; | 
					
						
							| 
									
										
										
										
											2022-10-13 14:49:08 +02:00
										 |  |  |     bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |     CSSPixels min_height = 0; | 
					
						
							| 
									
										
										
										
											2022-10-13 14:49:08 +02:00
										 |  |  |     bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false); | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |     CSSPixels max_height = 0; | 
					
						
							| 
									
										
										
										
											2022-10-13 14:49:08 +02:00
										 |  |  |     bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-19 13:46:27 +02:00
										 |  |  |     m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true); | 
					
						
							|  |  |  |     m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false); | 
					
						
							| 
									
										
										
										
											2022-10-13 14:49:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_has_definite_width) { | 
					
						
							|  |  |  |         if (has_definite_min_width) | 
					
						
							|  |  |  |             m_content_width = max(min_width, m_content_width); | 
					
						
							|  |  |  |         if (has_definite_max_width) | 
					
						
							|  |  |  |             m_content_width = min(max_width, m_content_width); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_has_definite_height) { | 
					
						
							|  |  |  |         if (has_definite_min_height) | 
					
						
							|  |  |  |             m_content_height = max(min_height, m_content_height); | 
					
						
							|  |  |  |         if (has_definite_max_height) | 
					
						
							|  |  |  |             m_content_height = min(max_height, m_content_height); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | void LayoutState::UsedValues::set_content_width(CSSPixels width) | 
					
						
							| 
									
										
										
										
											2022-07-17 17:59:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-25 17:33:22 +01:00
										 |  |  |     if (width < 0) { | 
					
						
							|  |  |  |         // Negative heights are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
 | 
					
						
							|  |  |  |         dbgln("FIXME: Layout calculated a negative width for {}: {}", m_node->debug_description(), width); | 
					
						
							|  |  |  |         width = 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-17 17:59:02 +02:00
										 |  |  |     m_content_width = width; | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     m_has_definite_width = true; | 
					
						
							| 
									
										
										
										
											2022-07-17 17:59:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | void LayoutState::UsedValues::set_content_height(CSSPixels height) | 
					
						
							| 
									
										
										
										
											2022-07-17 17:59:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-25 17:33:22 +01:00
										 |  |  |     if (height < 0) { | 
					
						
							|  |  |  |         // Negative heights are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
 | 
					
						
							|  |  |  |         dbgln("FIXME: Layout calculated a negative height for {}: {}", m_node->debug_description(), height); | 
					
						
							|  |  |  |         height = 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-17 17:59:02 +02:00
										 |  |  |     m_content_height = height; | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     m_has_definite_height = true; | 
					
						
							| 
									
										
										
										
											2022-07-17 17:59:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width) | 
					
						
							| 
									
										
										
										
											2022-10-15 09:26:30 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     m_content_width = width; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height) | 
					
						
							| 
									
										
										
										
											2022-10-15 09:26:30 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     m_content_height = height; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | CSSPixels LayoutState::resolved_definite_width(Box const& box) const | 
					
						
							| 
									
										
										
										
											2022-09-14 11:43:57 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     return get(box).content_width(); | 
					
						
							| 
									
										
										
										
											2022-09-14 11:43:57 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | CSSPixels LayoutState::resolved_definite_height(Box const& box) const | 
					
						
							| 
									
										
										
										
											2022-09-14 11:43:57 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     return get(box).content_height(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AvailableSize LayoutState::UsedValues::available_width_inside() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (width_constraint == SizeConstraint::MinContent) | 
					
						
							|  |  |  |         return AvailableSize::make_min_content(); | 
					
						
							|  |  |  |     if (width_constraint == SizeConstraint::MaxContent) | 
					
						
							|  |  |  |         return AvailableSize::make_max_content(); | 
					
						
							|  |  |  |     if (has_definite_width()) | 
					
						
							|  |  |  |         return AvailableSize::make_definite(m_content_width); | 
					
						
							|  |  |  |     return AvailableSize::make_indefinite(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AvailableSize LayoutState::UsedValues::available_height_inside() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (height_constraint == SizeConstraint::MinContent) | 
					
						
							|  |  |  |         return AvailableSize::make_min_content(); | 
					
						
							|  |  |  |     if (height_constraint == SizeConstraint::MaxContent) | 
					
						
							|  |  |  |         return AvailableSize::make_max_content(); | 
					
						
							|  |  |  |     if (has_definite_height()) | 
					
						
							|  |  |  |         return AvailableSize::make_definite(m_content_height); | 
					
						
							|  |  |  |     return AvailableSize::make_indefinite(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto inner_width = available_width_inside(); | 
					
						
							|  |  |  |     auto inner_height = available_height_inside(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint()) | 
					
						
							|  |  |  |         inner_width = outer_space.width; | 
					
						
							|  |  |  |     if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint()) | 
					
						
							|  |  |  |         inner_height = outer_space.height; | 
					
						
							|  |  |  |     return AvailableSpace(inner_width, inner_height); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset) | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  |     set_content_x(new_offset.x()); | 
					
						
							|  |  |  |     set_content_y(new_offset.y()); | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | void LayoutState::UsedValues::set_content_x(CSSPixels x) | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     offset.set_x(x); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 20:32:50 +00:00
										 |  |  | void LayoutState::UsedValues::set_content_y(CSSPixels y) | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     offset.set_y(y); | 
					
						
							| 
									
										
										
										
											2022-09-14 11:43:57 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | } |