/src/serenity/Userland/Libraries/LibWeb/Layout/LayoutState.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022-2024, Andreas Kling <kling@serenityos.org> |
3 | | * Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #include <AK/Debug.h> |
9 | | #include <LibWeb/DOM/ShadowRoot.h> |
10 | | #include <LibWeb/Layout/AvailableSpace.h> |
11 | | #include <LibWeb/Layout/BlockContainer.h> |
12 | | #include <LibWeb/Layout/InlineNode.h> |
13 | | #include <LibWeb/Layout/LayoutState.h> |
14 | | #include <LibWeb/Layout/Viewport.h> |
15 | | #include <LibWeb/Painting/InlinePaintable.h> |
16 | | #include <LibWeb/Painting/SVGPathPaintable.h> |
17 | | #include <LibWeb/Painting/SVGSVGPaintable.h> |
18 | | #include <LibWeb/Painting/TextPaintable.h> |
19 | | |
20 | | namespace Web::Layout { |
21 | | |
22 | | LayoutState::LayoutState(LayoutState const* parent) |
23 | 0 | : m_parent(parent) |
24 | 0 | , m_root(find_root()) |
25 | 0 | { |
26 | 0 | } |
27 | | |
28 | | LayoutState::~LayoutState() |
29 | 0 | { |
30 | 0 | } |
31 | | |
32 | | LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyle const& node) |
33 | 0 | { |
34 | 0 | if (auto* used_values = used_values_per_layout_node.get(node).value_or(nullptr)) |
35 | 0 | return *used_values; |
36 | | |
37 | 0 | for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) { |
38 | 0 | if (auto* ancestor_used_values = ancestor->used_values_per_layout_node.get(node).value_or(nullptr)) { |
39 | 0 | auto cow_used_values = adopt_own(*new UsedValues(*ancestor_used_values)); |
40 | 0 | auto* cow_used_values_ptr = cow_used_values.ptr(); |
41 | 0 | used_values_per_layout_node.set(node, move(cow_used_values)); |
42 | 0 | return *cow_used_values_ptr; |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | 0 | auto const* containing_block_used_values = node.is_viewport() ? nullptr : &get(*node.containing_block()); |
47 | |
|
48 | 0 | auto new_used_values = adopt_own(*new UsedValues); |
49 | 0 | auto* new_used_values_ptr = new_used_values.ptr(); |
50 | 0 | new_used_values->set_node(const_cast<NodeWithStyle&>(node), containing_block_used_values); |
51 | 0 | used_values_per_layout_node.set(node, move(new_used_values)); |
52 | 0 | return *new_used_values_ptr; |
53 | 0 | } |
54 | | |
55 | | LayoutState::UsedValues const& LayoutState::get(NodeWithStyle const& node) const |
56 | 0 | { |
57 | 0 | if (auto const* used_values = used_values_per_layout_node.get(node).value_or(nullptr)) |
58 | 0 | return *used_values; |
59 | | |
60 | 0 | for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) { |
61 | 0 | if (auto const* ancestor_used_values = ancestor->used_values_per_layout_node.get(node).value_or(nullptr)) |
62 | 0 | return *ancestor_used_values; |
63 | 0 | } |
64 | | |
65 | 0 | auto const* containing_block_used_values = node.is_viewport() ? nullptr : &get(*node.containing_block()); |
66 | |
|
67 | 0 | auto new_used_values = adopt_own(*new UsedValues); |
68 | 0 | auto* new_used_values_ptr = new_used_values.ptr(); |
69 | 0 | new_used_values->set_node(const_cast<NodeWithStyle&>(node), containing_block_used_values); |
70 | 0 | const_cast<LayoutState*>(this)->used_values_per_layout_node.set(node, move(new_used_values)); |
71 | 0 | return *new_used_values_ptr; |
72 | 0 | } |
73 | | |
74 | | // https://www.w3.org/TR/css-overflow-3/#scrollable-overflow |
75 | | static CSSPixelRect measure_scrollable_overflow(Box const& box) |
76 | 0 | { |
77 | 0 | if (!box.paintable_box()) |
78 | 0 | return {}; |
79 | | |
80 | 0 | auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box()); |
81 | |
|
82 | 0 | if (paintable_box.scrollable_overflow_rect().has_value()) |
83 | 0 | return paintable_box.scrollable_overflow_rect().value(); |
84 | | |
85 | | // The scrollable overflow area is the union of: |
86 | | |
87 | | // - The scroll container’s own padding box. |
88 | 0 | auto scrollable_overflow_rect = paintable_box.absolute_padding_box_rect(); |
89 | | |
90 | | // - All line boxes directly contained by the scroll container. |
91 | 0 | if (is<Painting::PaintableWithLines>(box.paintable())) { |
92 | 0 | for (auto const& fragment : static_cast<Painting::PaintableWithLines const&>(*box.paintable()).fragments()) { |
93 | 0 | scrollable_overflow_rect = scrollable_overflow_rect.united(fragment.absolute_rect()); |
94 | 0 | } |
95 | 0 | } |
96 | |
|
97 | 0 | auto content_overflow_rect = scrollable_overflow_rect; |
98 | | |
99 | | // - The border boxes of all boxes for which it is the containing block |
100 | | // and whose border boxes are positioned not wholly in the negative scrollable overflow region, |
101 | | // FIXME: accounting for transforms by projecting each box onto the plane of the element that establishes its 3D rendering context. [CSS3-TRANSFORMS] |
102 | 0 | if (!box.children_are_inline()) { |
103 | 0 | box.for_each_child_of_type<Box>([&box, &scrollable_overflow_rect, &content_overflow_rect](Box const& child) { |
104 | 0 | if (!child.paintable_box()) |
105 | 0 | return IterationDecision::Continue; |
106 | | |
107 | 0 | auto child_border_box = child.paintable_box()->absolute_border_box_rect(); |
108 | | |
109 | | // NOTE: Here we check that the child is not wholly in the negative scrollable overflow region. |
110 | 0 | if (child_border_box.bottom() < 0 || child_border_box.right() < 0) |
111 | 0 | return IterationDecision::Continue; |
112 | | |
113 | 0 | scrollable_overflow_rect = scrollable_overflow_rect.united(child_border_box); |
114 | 0 | content_overflow_rect = content_overflow_rect.united(child_border_box); |
115 | | |
116 | | // - The scrollable overflow areas of all of the above boxes |
117 | | // (including zero-area boxes and accounting for transforms as described above), |
118 | | // provided they themselves have overflow: visible (i.e. do not themselves trap the overflow) |
119 | | // and that scrollable overflow is not already clipped (e.g. by the clip property or the contain property). |
120 | 0 | if (is<Viewport>(box) || child.computed_values().overflow_x() == CSS::Overflow::Visible || child.computed_values().overflow_y() == CSS::Overflow::Visible) { |
121 | 0 | auto child_scrollable_overflow = measure_scrollable_overflow(child); |
122 | 0 | if (is<Viewport>(box) || child.computed_values().overflow_x() == CSS::Overflow::Visible) |
123 | 0 | scrollable_overflow_rect.unite_horizontally(child_scrollable_overflow); |
124 | 0 | if (is<Viewport>(box) || child.computed_values().overflow_y() == CSS::Overflow::Visible) |
125 | 0 | scrollable_overflow_rect.unite_vertically(child_scrollable_overflow); |
126 | 0 | } |
127 | |
|
128 | 0 | return IterationDecision::Continue; |
129 | 0 | }); |
130 | 0 | } else { |
131 | 0 | box.for_each_child([&scrollable_overflow_rect, &content_overflow_rect](Node const& child) { |
132 | 0 | if (child.paintable() && child.paintable()->is_inline_paintable()) { |
133 | 0 | for (auto const& fragment : static_cast<Painting::InlinePaintable const&>(*child.paintable()).fragments()) { |
134 | 0 | scrollable_overflow_rect = scrollable_overflow_rect.united(fragment.absolute_rect()); |
135 | 0 | content_overflow_rect = content_overflow_rect.united(fragment.absolute_rect()); |
136 | 0 | } |
137 | 0 | } |
138 | |
|
139 | 0 | return IterationDecision::Continue; |
140 | 0 | }); |
141 | 0 | } |
142 | | |
143 | | // FIXME: - The margin areas of grid item and flex item boxes for which the box establishes a containing block. |
144 | | |
145 | | // - Additional padding added to the end-side of the scrollable overflow rectangle as necessary |
146 | | // to enable a scroll position that satisfies the requirements of place-content: end alignment. |
147 | 0 | auto has_scrollable_overflow = !paintable_box.absolute_padding_box_rect().contains(scrollable_overflow_rect); |
148 | 0 | if (has_scrollable_overflow) { |
149 | 0 | scrollable_overflow_rect.set_height(max(scrollable_overflow_rect.height(), content_overflow_rect.height() + box.box_model().padding.bottom)); |
150 | 0 | } |
151 | |
|
152 | 0 | paintable_box.set_overflow_data(Painting::PaintableBox::OverflowData { |
153 | 0 | .scrollable_overflow_rect = scrollable_overflow_rect, |
154 | 0 | .has_scrollable_overflow = has_scrollable_overflow, |
155 | 0 | }); |
156 | |
|
157 | 0 | return scrollable_overflow_rect; |
158 | 0 | } |
159 | | |
160 | | void LayoutState::resolve_relative_positions() |
161 | 0 | { |
162 | | // This function resolves relative position offsets of fragments that belong to inline paintables. |
163 | | // It runs *after* the paint tree has been constructed, so it modifies paintable node & fragment offsets directly. |
164 | 0 | for (auto& it : used_values_per_layout_node) { |
165 | 0 | auto& used_values = *it.value; |
166 | 0 | auto& node = const_cast<NodeWithStyle&>(used_values.node()); |
167 | |
|
168 | 0 | auto* paintable = node.paintable(); |
169 | 0 | if (!paintable) |
170 | 0 | continue; |
171 | 0 | if (!is<Painting::InlinePaintable>(*paintable)) |
172 | 0 | continue; |
173 | | |
174 | 0 | auto const& inline_paintable = static_cast<Painting::InlinePaintable&>(*paintable); |
175 | 0 | for (auto& fragment : inline_paintable.fragments()) { |
176 | 0 | auto const& fragment_node = fragment.layout_node(); |
177 | 0 | if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*fragment_node.parent())) |
178 | 0 | continue; |
179 | | // Collect effective relative position offset from inline-flow parent chain. |
180 | 0 | CSSPixelPoint offset; |
181 | 0 | for (auto* ancestor = fragment_node.parent(); ancestor; ancestor = ancestor->parent()) { |
182 | 0 | if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*ancestor)) |
183 | 0 | break; |
184 | 0 | if (!ancestor->display().is_inline_outside() || !ancestor->display().is_flow_inside()) |
185 | 0 | break; |
186 | 0 | if (ancestor->computed_values().position() == CSS::Positioning::Relative) { |
187 | 0 | auto const& ancestor_node = static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*ancestor); |
188 | 0 | auto const& inset = ancestor_node.box_model().inset; |
189 | 0 | offset.translate_by(inset.left, inset.top); |
190 | 0 | } |
191 | 0 | } |
192 | 0 | const_cast<Painting::PaintableFragment&>(fragment).set_offset(fragment.offset().translated(offset)); |
193 | 0 | } |
194 | 0 | } |
195 | 0 | } |
196 | | |
197 | | static void build_paint_tree(Node& node, Painting::Paintable* parent_paintable = nullptr) |
198 | 0 | { |
199 | 0 | Painting::Paintable* paintable = nullptr; |
200 | 0 | if (node.paintable()) { |
201 | 0 | paintable = const_cast<Painting::Paintable*>(node.paintable()); |
202 | 0 | if (parent_paintable && !paintable->forms_unconnected_subtree()) { |
203 | 0 | VERIFY(!paintable->parent()); |
204 | 0 | parent_paintable->append_child(*paintable); |
205 | 0 | } |
206 | 0 | paintable->set_dom_node(node.dom_node()); |
207 | 0 | if (node.dom_node()) |
208 | 0 | node.dom_node()->set_paintable(paintable); |
209 | 0 | } |
210 | 0 | for (auto* child = node.first_child(); child; child = child->next_sibling()) { |
211 | 0 | build_paint_tree(*child, paintable); |
212 | 0 | } |
213 | 0 | } |
214 | | |
215 | | void LayoutState::commit(Box& root) |
216 | 0 | { |
217 | | // Only the top-level LayoutState should ever be committed. |
218 | 0 | VERIFY(!m_parent); |
219 | | |
220 | | // NOTE: In case this is a relayout of an existing tree, we start by detaching the old paint tree |
221 | | // from the layout tree. This is done to ensure that we don't end up with any old-tree pointers |
222 | | // when text paintables shift around in the tree. |
223 | 0 | root.for_each_in_inclusive_subtree([&](Layout::Node& node) { |
224 | 0 | node.set_paintable(nullptr); |
225 | 0 | return TraversalDecision::Continue; |
226 | 0 | }); |
227 | 0 | root.document().for_each_shadow_including_inclusive_descendant([&](DOM::Node& node) { |
228 | 0 | node.set_paintable(nullptr); |
229 | 0 | return TraversalDecision::Continue; |
230 | 0 | }); |
231 | |
|
232 | 0 | HashTable<Layout::TextNode*> text_nodes; |
233 | |
|
234 | 0 | Vector<Painting::PaintableWithLines&> paintables_with_lines; |
235 | |
|
236 | 0 | for (auto& it : used_values_per_layout_node) { |
237 | 0 | auto& used_values = *it.value; |
238 | 0 | auto& node = const_cast<NodeWithStyle&>(used_values.node()); |
239 | |
|
240 | 0 | if (is<NodeWithStyleAndBoxModelMetrics>(node)) { |
241 | | // Transfer box model metrics. |
242 | 0 | auto& box_model = static_cast<NodeWithStyleAndBoxModelMetrics&>(node).box_model(); |
243 | 0 | box_model.inset = { used_values.inset_top, used_values.inset_right, used_values.inset_bottom, used_values.inset_left }; |
244 | 0 | box_model.padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left }; |
245 | 0 | box_model.border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left }; |
246 | 0 | box_model.margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left }; |
247 | 0 | } |
248 | |
|
249 | 0 | auto paintable = node.create_paintable(); |
250 | |
|
251 | 0 | node.set_paintable(paintable); |
252 | | |
253 | | // For boxes, transfer all the state needed for painting. |
254 | 0 | if (paintable && is<Painting::PaintableBox>(*paintable)) { |
255 | 0 | auto& paintable_box = static_cast<Painting::PaintableBox&>(*paintable); |
256 | 0 | paintable_box.set_offset(used_values.offset); |
257 | 0 | paintable_box.set_content_size(used_values.content_width(), used_values.content_height()); |
258 | 0 | if (used_values.override_borders_data().has_value()) { |
259 | 0 | paintable_box.set_override_borders_data(used_values.override_borders_data().value()); |
260 | 0 | } |
261 | 0 | if (used_values.table_cell_coordinates().has_value()) { |
262 | 0 | paintable_box.set_table_cell_coordinates(used_values.table_cell_coordinates().value()); |
263 | 0 | } |
264 | |
|
265 | 0 | if (is<Painting::PaintableWithLines>(paintable_box)) { |
266 | 0 | auto& paintable_with_lines = static_cast<Painting::PaintableWithLines&>(paintable_box); |
267 | 0 | for (auto& line_box : used_values.line_boxes) { |
268 | 0 | for (auto& fragment : line_box.fragments()) |
269 | 0 | paintable_with_lines.add_fragment(fragment); |
270 | 0 | } |
271 | 0 | paintables_with_lines.append(paintable_with_lines); |
272 | 0 | } |
273 | |
|
274 | 0 | if (used_values.computed_svg_transforms().has_value() && is<Painting::SVGGraphicsPaintable>(paintable_box)) { |
275 | 0 | auto& svg_graphics_paintable = static_cast<Painting::SVGGraphicsPaintable&>(paintable_box); |
276 | 0 | svg_graphics_paintable.set_computed_transforms(*used_values.computed_svg_transforms()); |
277 | 0 | } |
278 | |
|
279 | 0 | if (used_values.computed_svg_path().has_value() && is<Painting::SVGPathPaintable>(paintable_box)) { |
280 | 0 | auto& svg_geometry_paintable = static_cast<Painting::SVGPathPaintable&>(paintable_box); |
281 | 0 | svg_geometry_paintable.set_computed_path(move(*used_values.computed_svg_path())); |
282 | 0 | } |
283 | 0 | } |
284 | 0 | } |
285 | | |
286 | | // Resolve relative positions for regular boxes (not line box fragments): |
287 | | // NOTE: This needs to occur before fragments are transferred into the corresponding inline paintables, because |
288 | | // after this transfer, the containing_line_box_fragment will no longer be valid. |
289 | 0 | for (auto& it : used_values_per_layout_node) { |
290 | 0 | auto& used_values = *it.value; |
291 | 0 | auto& node = const_cast<NodeWithStyle&>(used_values.node()); |
292 | |
|
293 | 0 | if (!node.is_box()) |
294 | 0 | continue; |
295 | | |
296 | 0 | auto& paintable = static_cast<Painting::PaintableBox&>(*node.paintable()); |
297 | 0 | CSSPixelPoint offset; |
298 | |
|
299 | 0 | if (used_values.containing_line_box_fragment.has_value()) { |
300 | | // Atomic inline case: |
301 | | // We know that `node` is an atomic inline because `containing_line_box_fragments` refers to the |
302 | | // line box fragment in the parent block container that contains it. |
303 | 0 | auto const& containing_line_box_fragment = used_values.containing_line_box_fragment.value(); |
304 | 0 | auto const& containing_block = *node.containing_block(); |
305 | 0 | auto const& containing_block_used_values = get(containing_block); |
306 | 0 | auto const& fragment = containing_block_used_values.line_boxes[containing_line_box_fragment.line_box_index].fragments()[containing_line_box_fragment.fragment_index]; |
307 | | |
308 | | // The fragment has the final offset for the atomic inline, so we just need to copy it from there. |
309 | 0 | offset = fragment.offset(); |
310 | 0 | } else { |
311 | | // Not an atomic inline, much simpler case. |
312 | 0 | offset = used_values.offset; |
313 | 0 | } |
314 | | // Apply relative position inset if appropriate. |
315 | 0 | if (node.computed_values().position() == CSS::Positioning::Relative && is<NodeWithStyleAndBoxModelMetrics>(node)) { |
316 | 0 | auto const& inset = static_cast<NodeWithStyleAndBoxModelMetrics const&>(node).box_model().inset; |
317 | 0 | offset.translate_by(inset.left, inset.top); |
318 | 0 | } |
319 | 0 | paintable.set_offset(offset); |
320 | 0 | } |
321 | | |
322 | | // Make a pass over all the line boxes to: |
323 | | // - Collect all text nodes, so we can create paintables for them later. |
324 | | // - Relocate fragments into matching inline paintables |
325 | 0 | for (auto& paintable_with_lines : paintables_with_lines) { |
326 | 0 | Vector<Painting::PaintableFragment> fragments_with_inline_paintables_removed; |
327 | 0 | for (auto& fragment : paintable_with_lines.fragments()) { |
328 | 0 | if (fragment.layout_node().is_text_node()) |
329 | 0 | text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node()))); |
330 | |
|
331 | 0 | auto find_closest_inline_paintable = [&](auto& fragment) -> Painting::InlinePaintable const* { |
332 | 0 | for (auto const* parent = fragment.layout_node().parent(); parent; parent = parent->parent()) { |
333 | 0 | if (is<InlineNode>(*parent)) |
334 | 0 | return static_cast<Painting::InlinePaintable const*>(parent->paintable()); |
335 | 0 | } |
336 | 0 | return nullptr; |
337 | 0 | }; |
338 | |
|
339 | 0 | if (auto const* inline_paintable = find_closest_inline_paintable(fragment)) { |
340 | 0 | const_cast<Painting::InlinePaintable*>(inline_paintable)->fragments().append(fragment); |
341 | 0 | } else { |
342 | 0 | fragments_with_inline_paintables_removed.append(fragment); |
343 | 0 | } |
344 | 0 | } |
345 | 0 | paintable_with_lines.set_fragments(move(fragments_with_inline_paintables_removed)); |
346 | 0 | } |
347 | |
|
348 | 0 | for (auto* text_node : text_nodes) { |
349 | 0 | text_node->set_paintable(text_node->create_paintable()); |
350 | 0 | auto* paintable = text_node->paintable(); |
351 | 0 | auto const& font = text_node->first_available_font(); |
352 | 0 | auto const glyph_height = CSSPixels::nearest_value_for(font.pixel_size()); |
353 | 0 | auto const css_line_thickness = [&] { |
354 | 0 | auto computed_thickness = text_node->computed_values().text_decoration_thickness().resolved(*text_node, CSS::Length(1, CSS::Length::Type::Em).to_px(*text_node)); |
355 | 0 | if (computed_thickness.is_auto()) |
356 | 0 | return max(glyph_height.scaled(0.1), 1); |
357 | 0 | return computed_thickness.to_px(*text_node); |
358 | 0 | }(); |
359 | 0 | auto& text_paintable = static_cast<Painting::TextPaintable&>(*paintable); |
360 | 0 | text_paintable.set_text_decoration_thickness(css_line_thickness); |
361 | 0 | } |
362 | |
|
363 | 0 | build_paint_tree(root); |
364 | |
|
365 | 0 | resolve_relative_positions(); |
366 | | |
367 | | // Measure overflow in scroll containers. |
368 | 0 | for (auto& it : used_values_per_layout_node) { |
369 | 0 | auto& used_values = *it.value; |
370 | 0 | if (!used_values.node().is_box()) |
371 | 0 | continue; |
372 | 0 | auto const& box = static_cast<Layout::Box const&>(used_values.node()); |
373 | 0 | measure_scrollable_overflow(box); |
374 | | |
375 | | // The scroll offset can become invalid if the scrollable overflow rectangle has changed after layout. |
376 | | // For example, if the scroll container has been scrolled to the very end and is then resized to become larger |
377 | | // (scrollable overflow rect become smaller), the scroll offset would be out of bounds. |
378 | 0 | auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box()); |
379 | 0 | if (!paintable_box.scroll_offset().is_zero()) |
380 | 0 | paintable_box.set_scroll_offset(paintable_box.scroll_offset()); |
381 | 0 | } |
382 | 0 | } |
383 | | |
384 | | void LayoutState::UsedValues::set_node(NodeWithStyle& node, UsedValues const* containing_block_used_values) |
385 | 0 | { |
386 | 0 | m_node = &node; |
387 | 0 | m_containing_block_used_values = containing_block_used_values; |
388 | | |
389 | | // NOTE: In the code below, we decide if `node` has definite width and/or height. |
390 | | // This attempts to cover all the *general* cases where CSS considers sizes to be definite. |
391 | | // If `node` has definite values for min/max-width or min/max-height and a definite |
392 | | // preferred size in the same axis, we clamp the preferred size here as well. |
393 | | // |
394 | | // There are additional cases where CSS considers values to be definite. We model all of |
395 | | // those by having our engine consider sizes to be definite *once they are assigned to |
396 | | // the UsedValues by calling set_content_width() or set_content_height(). |
397 | |
|
398 | 0 | auto const& computed_values = node.computed_values(); |
399 | |
|
400 | 0 | auto adjust_for_box_sizing = [&](CSSPixels unadjusted_pixels, CSS::Size const& computed_size, bool width) -> CSSPixels { |
401 | | // box-sizing: content-box and/or automatic size don't require any adjustment. |
402 | 0 | if (computed_values.box_sizing() == CSS::BoxSizing::ContentBox || computed_size.is_auto()) |
403 | 0 | return unadjusted_pixels; |
404 | | |
405 | | // box-sizing: border-box requires us to subtract the relevant border and padding from the size. |
406 | 0 | CSSPixels border_and_padding; |
407 | |
|
408 | 0 | if (width) { |
409 | 0 | border_and_padding = computed_values.border_left().width |
410 | 0 | + computed_values.padding().left().to_px(*m_node, containing_block_used_values->content_width()) |
411 | 0 | + computed_values.border_right().width |
412 | 0 | + computed_values.padding().right().to_px(*m_node, containing_block_used_values->content_width()); |
413 | 0 | } else { |
414 | 0 | border_and_padding = computed_values.border_top().width |
415 | 0 | + computed_values.padding().top().to_px(*m_node, containing_block_used_values->content_width()) |
416 | 0 | + computed_values.border_bottom().width |
417 | 0 | + computed_values.padding().bottom().to_px(*m_node, containing_block_used_values->content_width()); |
418 | 0 | } |
419 | |
|
420 | 0 | return unadjusted_pixels - border_and_padding; |
421 | 0 | }; |
422 | |
|
423 | 0 | auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) { |
424 | | // A size that can be determined without performing layout; that is, |
425 | | // a <length>, |
426 | | // a measure of text (without consideration of line-wrapping), |
427 | | // a size of the initial containing block, |
428 | | // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes. |
429 | |
|
430 | 0 | 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; |
431 | |
|
432 | 0 | if (size.is_auto()) { |
433 | | // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width. |
434 | 0 | if (width |
435 | 0 | && !node.is_floating() |
436 | 0 | && !node.is_absolutely_positioned() |
437 | 0 | && node.display().is_block_outside() |
438 | 0 | && node.parent() |
439 | 0 | && !node.parent()->is_floating() |
440 | 0 | && (node.parent()->display().is_flow_root_inside() |
441 | 0 | || node.parent()->display().is_flow_inside())) { |
442 | 0 | if (containing_block_has_definite_size) { |
443 | 0 | CSSPixels available_width = containing_block_used_values->content_width(); |
444 | 0 | resolved_definite_size = available_width |
445 | 0 | - margin_left |
446 | 0 | - margin_right |
447 | 0 | - padding_left |
448 | 0 | - padding_right |
449 | 0 | - border_left |
450 | 0 | - border_right; |
451 | 0 | return true; |
452 | 0 | } |
453 | 0 | return false; |
454 | 0 | } |
455 | 0 | return false; |
456 | 0 | } |
457 | | |
458 | 0 | if (size.is_calculated()) { |
459 | 0 | if (size.calculated().contains_percentage()) { |
460 | 0 | if (!containing_block_has_definite_size) |
461 | 0 | return false; |
462 | 0 | auto containing_block_size_as_length = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height(); |
463 | 0 | resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length_percentage(node, containing_block_size_as_length).value_or(CSS::Length::make_auto()).to_px(node), size, width); |
464 | 0 | return true; |
465 | 0 | } |
466 | 0 | resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length(node)->to_px(node), size, width); |
467 | 0 | return true; |
468 | 0 | } |
469 | | |
470 | 0 | if (size.is_length()) { |
471 | 0 | VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above. |
472 | 0 | resolved_definite_size = adjust_for_box_sizing(size.length().to_px(node), size, width); |
473 | 0 | return true; |
474 | 0 | } |
475 | 0 | if (size.is_percentage()) { |
476 | 0 | if (containing_block_has_definite_size) { |
477 | 0 | auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height(); |
478 | 0 | resolved_definite_size = adjust_for_box_sizing(containing_block_size.scaled(size.percentage().as_fraction()), size, width); |
479 | 0 | return true; |
480 | 0 | } |
481 | 0 | return false; |
482 | 0 | } |
483 | 0 | return false; |
484 | 0 | }; |
485 | |
|
486 | 0 | CSSPixels min_width = 0; |
487 | 0 | bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true); |
488 | 0 | CSSPixels max_width = 0; |
489 | 0 | bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true); |
490 | |
|
491 | 0 | CSSPixels min_height = 0; |
492 | 0 | bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false); |
493 | 0 | CSSPixels max_height = 0; |
494 | 0 | bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false); |
495 | |
|
496 | 0 | m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true); |
497 | 0 | m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false); |
498 | | |
499 | | // For boxes with a preferred aspect ratio and one definite size, we can infer the other size |
500 | | // and consider it definite since this did not require performing layout. |
501 | 0 | if (is<Box>(node)) { |
502 | 0 | auto const& box = static_cast<Box const&>(node); |
503 | 0 | if (auto aspect_ratio = box.preferred_aspect_ratio(); aspect_ratio.has_value()) { |
504 | 0 | if (m_has_definite_width && m_has_definite_height) { |
505 | | // Both width and height are definite. |
506 | 0 | } else if (m_has_definite_width) { |
507 | 0 | m_content_height = m_content_width / *aspect_ratio; |
508 | 0 | m_has_definite_height = true; |
509 | 0 | } else if (m_has_definite_height) { |
510 | 0 | m_content_width = m_content_height * *aspect_ratio; |
511 | 0 | m_has_definite_width = true; |
512 | 0 | } |
513 | 0 | } |
514 | 0 | } |
515 | |
|
516 | 0 | if (m_has_definite_width) { |
517 | 0 | if (has_definite_min_width) |
518 | 0 | m_content_width = max(min_width, m_content_width); |
519 | 0 | if (has_definite_max_width) |
520 | 0 | m_content_width = min(max_width, m_content_width); |
521 | 0 | } |
522 | |
|
523 | 0 | if (m_has_definite_height) { |
524 | 0 | if (has_definite_min_height) |
525 | 0 | m_content_height = max(min_height, m_content_height); |
526 | 0 | if (has_definite_max_height) |
527 | 0 | m_content_height = min(max_height, m_content_height); |
528 | 0 | } |
529 | 0 | } |
530 | | |
531 | | void LayoutState::UsedValues::set_content_width(CSSPixels width) |
532 | 0 | { |
533 | 0 | VERIFY(!width.might_be_saturated()); |
534 | 0 | if (width < 0) { |
535 | | // Negative widths are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage. |
536 | 0 | dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative width for {}: {}", m_node->debug_description(), width); |
537 | 0 | width = 0; |
538 | 0 | } |
539 | 0 | m_content_width = width; |
540 | | // FIXME: We should not do this! Definiteness of widths should be determined early, |
541 | | // and not changed later (except for some special cases in flex layout..) |
542 | 0 | m_has_definite_width = true; |
543 | 0 | } |
544 | | |
545 | | void LayoutState::UsedValues::set_content_height(CSSPixels height) |
546 | 0 | { |
547 | 0 | VERIFY(!height.might_be_saturated()); |
548 | 0 | if (height < 0) { |
549 | | // Negative heights are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage. |
550 | 0 | dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative height for {}: {}", m_node->debug_description(), height); |
551 | 0 | height = 0; |
552 | 0 | } |
553 | 0 | m_content_height = height; |
554 | 0 | } |
555 | | |
556 | | void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width) |
557 | 0 | { |
558 | 0 | m_content_width = width; |
559 | 0 | } |
560 | | |
561 | | void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height) |
562 | 0 | { |
563 | 0 | m_content_height = height; |
564 | 0 | } |
565 | | |
566 | | AvailableSize LayoutState::UsedValues::available_width_inside() const |
567 | 0 | { |
568 | 0 | if (width_constraint == SizeConstraint::MinContent) |
569 | 0 | return AvailableSize::make_min_content(); |
570 | 0 | if (width_constraint == SizeConstraint::MaxContent) |
571 | 0 | return AvailableSize::make_max_content(); |
572 | 0 | if (has_definite_width()) |
573 | 0 | return AvailableSize::make_definite(m_content_width); |
574 | 0 | return AvailableSize::make_indefinite(); |
575 | 0 | } |
576 | | |
577 | | AvailableSize LayoutState::UsedValues::available_height_inside() const |
578 | 0 | { |
579 | 0 | if (height_constraint == SizeConstraint::MinContent) |
580 | 0 | return AvailableSize::make_min_content(); |
581 | 0 | if (height_constraint == SizeConstraint::MaxContent) |
582 | 0 | return AvailableSize::make_max_content(); |
583 | 0 | if (has_definite_height()) |
584 | 0 | return AvailableSize::make_definite(m_content_height); |
585 | 0 | return AvailableSize::make_indefinite(); |
586 | 0 | } |
587 | | |
588 | | AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const |
589 | 0 | { |
590 | 0 | auto inner_width = available_width_inside(); |
591 | 0 | auto inner_height = available_height_inside(); |
592 | |
|
593 | 0 | if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint()) |
594 | 0 | inner_width = outer_space.width; |
595 | 0 | if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint()) |
596 | 0 | inner_height = outer_space.height; |
597 | 0 | return AvailableSpace(inner_width, inner_height); |
598 | 0 | } |
599 | | |
600 | | void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset) |
601 | 0 | { |
602 | 0 | set_content_x(new_offset.x()); |
603 | 0 | set_content_y(new_offset.y()); |
604 | 0 | } |
605 | | |
606 | | void LayoutState::UsedValues::set_content_x(CSSPixels x) |
607 | 0 | { |
608 | 0 | offset.set_x(x); |
609 | 0 | } |
610 | | |
611 | | void LayoutState::UsedValues::set_content_y(CSSPixels y) |
612 | 0 | { |
613 | 0 | offset.set_y(y); |
614 | 0 | } |
615 | | |
616 | | void LayoutState::UsedValues::set_indefinite_content_width() |
617 | 0 | { |
618 | 0 | m_has_definite_width = false; |
619 | 0 | } |
620 | | |
621 | | void LayoutState::UsedValues::set_indefinite_content_height() |
622 | 0 | { |
623 | 0 | m_has_definite_height = false; |
624 | 0 | } |
625 | | |
626 | | } |