2022-02-20 15:51:24 +01:00
|
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
|
* Copyright (c) 2022-2024, Andreas Kling <andreas@ladybird.org>
|
2024-01-26 15:56:25 +00:00
|
|
|
|
* Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
|
2024-10-13 21:29:47 +02:00
|
|
|
|
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
2022-02-20 15:51:24 +01:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2023-05-31 11:47:26 +02:00
|
|
|
|
#include <AK/Debug.h>
|
2024-03-17 19:11:50 +01:00
|
|
|
|
#include <LibWeb/DOM/ShadowRoot.h>
|
2022-09-27 15:29:17 +02:00
|
|
|
|
#include <LibWeb/Layout/AvailableSpace.h>
|
2024-01-13 13:11:31 +01:00
|
|
|
|
#include <LibWeb/Layout/InlineNode.h>
|
2022-07-16 23:30:32 +02:00
|
|
|
|
#include <LibWeb/Layout/LayoutState.h>
|
2023-07-12 18:55:23 +02:00
|
|
|
|
#include <LibWeb/Layout/Viewport.h>
|
2023-11-04 21:31:35 +00:00
|
|
|
|
#include <LibWeb/Painting/SVGPathPaintable.h>
|
2024-01-26 15:56:25 +00:00
|
|
|
|
#include <LibWeb/Painting/SVGSVGPaintable.h>
|
2024-01-25 19:21:56 +01:00
|
|
|
|
#include <LibWeb/Painting/TextPaintable.h>
|
2022-02-20 15:51:24 +01:00
|
|
|
|
|
|
|
|
|
namespace Web::Layout {
|
|
|
|
|
|
2023-05-23 07:47:37 +02:00
|
|
|
|
LayoutState::~LayoutState()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-04 12:24:48 +02:00
|
|
|
|
LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyle const& node)
|
2022-02-28 12:32:23 +01:00
|
|
|
|
{
|
2024-04-05 13:47:48 -07:00
|
|
|
|
if (auto* used_values = used_values_per_layout_node.get(node).value_or(nullptr))
|
2023-05-23 07:47:37 +02:00
|
|
|
|
return *used_values;
|
2022-07-11 17:12:58 +02:00
|
|
|
|
|
2023-09-04 12:24:48 +02:00
|
|
|
|
auto const* containing_block_used_values = node.is_viewport() ? nullptr : &get(*node.containing_block());
|
2022-07-17 18:46:38 +02:00
|
|
|
|
|
2023-05-23 07:47:37 +02:00
|
|
|
|
auto new_used_values = adopt_own(*new UsedValues);
|
|
|
|
|
auto* new_used_values_ptr = new_used_values.ptr();
|
2025-08-27 12:37:26 +02:00
|
|
|
|
new_used_values->set_node(node, containing_block_used_values);
|
2024-04-05 13:47:48 -07:00
|
|
|
|
used_values_per_layout_node.set(node, move(new_used_values));
|
2023-05-23 07:47:37 +02:00
|
|
|
|
return *new_used_values_ptr;
|
2022-02-28 12:32:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-04 12:24:48 +02:00
|
|
|
|
LayoutState::UsedValues const& LayoutState::get(NodeWithStyle const& node) const
|
2022-02-28 12:32:23 +01:00
|
|
|
|
{
|
2024-04-05 13:47:48 -07:00
|
|
|
|
if (auto const* used_values = used_values_per_layout_node.get(node).value_or(nullptr))
|
2023-05-23 07:47:37 +02:00
|
|
|
|
return *used_values;
|
2022-07-11 17:12:58 +02:00
|
|
|
|
|
2023-09-04 12:24:48 +02:00
|
|
|
|
auto const* containing_block_used_values = node.is_viewport() ? nullptr : &get(*node.containing_block());
|
2022-07-17 18:46:38 +02:00
|
|
|
|
|
2023-05-23 07:47:37 +02:00
|
|
|
|
auto new_used_values = adopt_own(*new UsedValues);
|
|
|
|
|
auto* new_used_values_ptr = new_used_values.ptr();
|
2025-08-27 12:37:26 +02:00
|
|
|
|
new_used_values->set_node(node, containing_block_used_values);
|
2024-04-05 13:47:48 -07:00
|
|
|
|
const_cast<LayoutState*>(this)->used_values_per_layout_node.set(node, move(new_used_values));
|
2023-05-23 07:47:37 +02:00
|
|
|
|
return *new_used_values_ptr;
|
2022-02-28 12:32:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-10 11:43:46 +02:00
|
|
|
|
// https://drafts.csswg.org/css-overflow-3/#scrollable-overflow-region
|
2023-07-12 18:55:23 +02:00
|
|
|
|
static CSSPixelRect measure_scrollable_overflow(Box const& box)
|
2023-05-29 08:53:39 +02:00
|
|
|
|
{
|
2023-07-12 18:55:23 +02:00
|
|
|
|
if (!box.paintable_box())
|
|
|
|
|
return {};
|
|
|
|
|
|
2025-08-29 10:30:55 +02:00
|
|
|
|
auto const& paintable_box = *box.paintable_box();
|
2023-07-12 18:55:23 +02:00
|
|
|
|
|
|
|
|
|
if (paintable_box.scrollable_overflow_rect().has_value())
|
|
|
|
|
return paintable_box.scrollable_overflow_rect().value();
|
|
|
|
|
|
|
|
|
|
// The scrollable overflow area is the union of:
|
|
|
|
|
|
|
|
|
|
// - The scroll container’s own padding box.
|
2025-07-10 11:43:46 +02:00
|
|
|
|
auto const paintable_absolute_padding_box = paintable_box.absolute_padding_box_rect();
|
|
|
|
|
auto scrollable_overflow_rect = paintable_absolute_padding_box;
|
2023-07-12 18:55:23 +02:00
|
|
|
|
|
|
|
|
|
// - All line boxes directly contained by the scroll container.
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (auto const* paintable_with_lines = as_if<Painting::PaintableWithLines>(box.first_paintable())) {
|
|
|
|
|
for (auto const& fragment : paintable_with_lines->fragments())
|
2025-01-15 13:51:39 +01:00
|
|
|
|
scrollable_overflow_rect.unite(fragment.absolute_rect());
|
2023-07-12 18:55:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 14:29:09 +01:00
|
|
|
|
auto content_overflow_rect = scrollable_overflow_rect;
|
|
|
|
|
|
2025-07-10 11:43:46 +02:00
|
|
|
|
// - The border boxes of all boxes for which it is the containing block and whose border boxes are positioned not
|
|
|
|
|
// wholly in the negative scrollable overflow region,
|
2023-07-12 18:55:23 +02:00
|
|
|
|
// FIXME: accounting for transforms by projecting each box onto the plane of the element that establishes its 3D rendering context. [CSS3-TRANSFORMS]
|
2025-07-05 17:05:55 +02:00
|
|
|
|
box.for_each_in_subtree_of_type<Box>([&box, &scrollable_overflow_rect, &content_overflow_rect](Box const& child) {
|
|
|
|
|
if (!child.paintable_box())
|
|
|
|
|
return TraversalDecision::Continue;
|
2023-07-12 18:55:23 +02:00
|
|
|
|
|
2025-07-05 17:05:55 +02:00
|
|
|
|
if (child.containing_block() != &box)
|
2024-10-15 10:35:11 -04:00
|
|
|
|
return TraversalDecision::Continue;
|
2025-07-05 17:05:55 +02:00
|
|
|
|
|
|
|
|
|
// https://drafts.csswg.org/css-position/#fixed-positioning-containing-block
|
|
|
|
|
// [..] As a result, parts of fixed-positioned boxes that extend outside the layout viewport/page area
|
|
|
|
|
// cannot be scrolled to and will not print.
|
|
|
|
|
// FIXME: Properly establish the fixed positioning containing block for `position: fixed`
|
|
|
|
|
if (child.is_fixed_position())
|
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
|
|
|
|
|
auto child_border_box = child.paintable_box()->absolute_border_box_rect();
|
|
|
|
|
|
|
|
|
|
// Border boxes with zero area do not affect the scrollable overflow area.
|
|
|
|
|
if (child_border_box.is_empty())
|
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
|
|
|
|
|
// NOTE: Here we check that the child is not wholly in the negative scrollable overflow region.
|
|
|
|
|
if (child_border_box.bottom() < 0 || child_border_box.right() < 0)
|
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
|
|
|
|
|
scrollable_overflow_rect.unite(child_border_box);
|
|
|
|
|
content_overflow_rect.unite(child_border_box);
|
|
|
|
|
|
2025-07-10 11:43:46 +02:00
|
|
|
|
// - The scrollable overflow areas of all of the above boxes (including zero-area boxes and accounting for
|
|
|
|
|
// transforms as described above), provided they themselves have overflow: visible (i.e. do not themselves
|
|
|
|
|
// trap the overflow) and that scrollable overflow is not already clipped (e.g. by the clip property or the
|
|
|
|
|
// contain property).
|
2025-07-05 17:05:55 +02:00
|
|
|
|
if (child.computed_values().overflow_x() == CSS::Overflow::Visible || child.computed_values().overflow_y() == CSS::Overflow::Visible) {
|
|
|
|
|
auto child_scrollable_overflow = measure_scrollable_overflow(child);
|
|
|
|
|
if (child.computed_values().overflow_x() == CSS::Overflow::Visible)
|
|
|
|
|
scrollable_overflow_rect.unite_horizontally(child_scrollable_overflow);
|
|
|
|
|
if (child.computed_values().overflow_y() == CSS::Overflow::Visible)
|
|
|
|
|
scrollable_overflow_rect.unite_vertically(child_scrollable_overflow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
});
|
2023-07-12 18:55:23 +02:00
|
|
|
|
|
|
|
|
|
// FIXME: - The margin areas of grid item and flex item boxes for which the box establishes a containing block.
|
|
|
|
|
|
2025-07-10 11:43:46 +02:00
|
|
|
|
// - Additional padding added to the scrollable overflow rectangle as necessary to enable scroll positions that
|
|
|
|
|
// satisfy the requirements of both place-content: start and place-content: end alignment.
|
|
|
|
|
auto has_scrollable_overflow = !paintable_absolute_padding_box.contains(scrollable_overflow_rect);
|
2024-08-01 14:29:09 +01:00
|
|
|
|
if (has_scrollable_overflow) {
|
2025-02-17 13:54:36 +01:00
|
|
|
|
scrollable_overflow_rect.set_height(max(scrollable_overflow_rect.height(), content_overflow_rect.height() + paintable_box.box_model().padding.bottom));
|
2024-08-01 14:29:09 +01:00
|
|
|
|
}
|
2023-07-12 18:55:23 +02:00
|
|
|
|
|
2025-07-10 11:43:46 +02:00
|
|
|
|
// Additionally, due to Web-compatibility constraints (caused by authors exploiting legacy bugs to surreptitiously
|
|
|
|
|
// hide content from visual readers but not search engines and/or speech output), UAs must clip any content in the
|
|
|
|
|
// unreachable scrollable overflow region.
|
|
|
|
|
//
|
|
|
|
|
// https://drafts.csswg.org/css-overflow-3/#unreachable-scrollable-overflow-region
|
|
|
|
|
// Unless otherwise adjusted (e.g. by content alignment [css-align-3]), the area beyond the scroll origin in either
|
|
|
|
|
// axis is considered the unreachable scrollable overflow region: content rendered here is not accessible to the
|
|
|
|
|
// reader, see § 2.2 Scrollable Overflow.
|
|
|
|
|
// FIXME: The scroll origin and overflow directions are determined by ( block-start, inline-start ) or ( main-start,
|
|
|
|
|
// cross-start) for flex containers. Currently we assume the top-left of the absolute padding box.
|
|
|
|
|
if (scrollable_overflow_rect.x() < paintable_absolute_padding_box.x() || scrollable_overflow_rect.y() < paintable_absolute_padding_box.y()) {
|
|
|
|
|
scrollable_overflow_rect.set_size({
|
|
|
|
|
max(scrollable_overflow_rect.width() + min(scrollable_overflow_rect.x() - paintable_absolute_padding_box.x(), 0), 0),
|
|
|
|
|
max(scrollable_overflow_rect.height() + min(scrollable_overflow_rect.y() - paintable_absolute_padding_box.y(), 0), 0),
|
|
|
|
|
});
|
|
|
|
|
scrollable_overflow_rect.set_location({
|
|
|
|
|
max(scrollable_overflow_rect.x(), paintable_absolute_padding_box.x()),
|
|
|
|
|
max(scrollable_overflow_rect.y(), paintable_absolute_padding_box.y()),
|
|
|
|
|
});
|
|
|
|
|
has_scrollable_overflow = !paintable_absolute_padding_box.contains(scrollable_overflow_rect);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-29 10:30:55 +02:00
|
|
|
|
const_cast<Painting::PaintableBox&>(paintable_box).set_overflow_data({
|
2023-07-12 18:55:23 +02:00
|
|
|
|
.scrollable_overflow_rect = scrollable_overflow_rect,
|
2024-08-01 14:29:09 +01:00
|
|
|
|
.has_scrollable_overflow = has_scrollable_overflow,
|
2023-07-12 18:55:23 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return scrollable_overflow_rect;
|
2023-05-29 08:53:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 14:03:30 +01:00
|
|
|
|
void LayoutState::resolve_relative_positions()
|
2023-08-15 15:11:10 +02:00
|
|
|
|
{
|
2024-01-18 14:03:30 +01:00
|
|
|
|
// This function resolves relative position offsets of fragments that belong to inline paintables.
|
2023-08-15 15:11:10 +02:00
|
|
|
|
// It runs *after* the paint tree has been constructed, so it modifies paintable node & fragment offsets directly.
|
|
|
|
|
for (auto& it : used_values_per_layout_node) {
|
|
|
|
|
auto& used_values = *it.value;
|
2023-09-04 12:24:48 +02:00
|
|
|
|
auto& node = const_cast<NodeWithStyle&>(used_values.node());
|
2023-08-15 15:11:10 +02:00
|
|
|
|
|
2024-10-13 21:29:47 +02:00
|
|
|
|
for (auto& paintable : node.paintables()) {
|
|
|
|
|
if (!(is<Painting::PaintableWithLines>(paintable) && is<Layout::InlineNode>(paintable.layout_node())))
|
2024-01-12 21:25:05 +01:00
|
|
|
|
continue;
|
2024-10-13 21:29:47 +02:00
|
|
|
|
|
|
|
|
|
auto const& inline_paintable = static_cast<Painting::PaintableWithLines&>(paintable);
|
|
|
|
|
for (auto& fragment : inline_paintable.fragments()) {
|
|
|
|
|
auto const& fragment_node = fragment.layout_node();
|
|
|
|
|
if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*fragment_node.parent()))
|
|
|
|
|
continue;
|
|
|
|
|
// Collect effective relative position offset from inline-flow parent chain.
|
|
|
|
|
CSSPixelPoint offset;
|
|
|
|
|
for (auto* ancestor = fragment_node.parent(); ancestor; ancestor = ancestor->parent()) {
|
|
|
|
|
if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*ancestor))
|
|
|
|
|
break;
|
|
|
|
|
if (!ancestor->display().is_inline_outside() || !ancestor->display().is_flow_inside())
|
|
|
|
|
break;
|
|
|
|
|
if (ancestor->computed_values().position() == CSS::Positioning::Relative) {
|
2025-02-17 13:54:36 +01:00
|
|
|
|
VERIFY(ancestor->first_paintable());
|
|
|
|
|
auto const& ancestor_node = as<Painting::PaintableBox>(*ancestor->first_paintable());
|
2024-10-13 21:29:47 +02:00
|
|
|
|
auto const& inset = ancestor_node.box_model().inset;
|
|
|
|
|
offset.translate_by(inset.left, inset.top);
|
|
|
|
|
}
|
2023-08-15 15:11:10 +02:00
|
|
|
|
}
|
2024-10-13 21:29:47 +02:00
|
|
|
|
const_cast<Painting::PaintableFragment&>(fragment).set_offset(fragment.offset().translated(offset));
|
2023-08-15 15:11:10 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 12:30:27 +02:00
|
|
|
|
static void build_paint_tree(Node& node, Painting::Paintable* parent_paintable = nullptr)
|
|
|
|
|
{
|
2024-10-13 21:29:47 +02:00
|
|
|
|
for (auto& paintable : node.paintables()) {
|
|
|
|
|
if (parent_paintable && !paintable.forms_unconnected_subtree()) {
|
|
|
|
|
VERIFY(!paintable.parent());
|
|
|
|
|
parent_paintable->append_child(paintable);
|
2023-09-10 14:03:13 +01:00
|
|
|
|
}
|
2024-10-13 21:29:47 +02:00
|
|
|
|
paintable.set_dom_node(node.dom_node());
|
2023-09-10 14:03:13 +01:00
|
|
|
|
if (node.dom_node())
|
|
|
|
|
node.dom_node()->set_paintable(paintable);
|
2023-08-18 12:30:27 +02:00
|
|
|
|
}
|
|
|
|
|
for (auto* child = node.first_child(); child; child = child->next_sibling()) {
|
2024-10-13 21:29:47 +02:00
|
|
|
|
build_paint_tree(*child, node.first_paintable());
|
2023-08-18 12:30:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LayoutState::commit(Box& root)
|
2022-02-20 15:51:24 +01:00
|
|
|
|
{
|
2025-08-29 10:32:52 +02:00
|
|
|
|
// Go through the layout tree and detach all paintables. The layout tree should only point to the new paintable tree
|
|
|
|
|
// which we're about to build.
|
|
|
|
|
root.for_each_in_inclusive_subtree([](Node& node) {
|
2024-10-14 16:07:56 +02:00
|
|
|
|
node.clear_paintables();
|
2024-05-04 14:47:04 +01:00
|
|
|
|
return TraversalDecision::Continue;
|
2023-08-29 16:27:16 +02:00
|
|
|
|
});
|
2024-10-13 21:29:47 +02:00
|
|
|
|
|
2025-07-30 16:57:03 +02:00
|
|
|
|
// After this point, we should have a clean slate to build the new paint tree.
|
|
|
|
|
|
2024-10-13 21:29:47 +02:00
|
|
|
|
HashTable<Layout::InlineNode*> inline_nodes;
|
|
|
|
|
|
2024-03-17 19:11:50 +01:00
|
|
|
|
root.document().for_each_shadow_including_inclusive_descendant([&](DOM::Node& node) {
|
2024-10-14 16:07:56 +02:00
|
|
|
|
node.clear_paintable();
|
2024-10-13 21:29:47 +02:00
|
|
|
|
if (node.layout_node() && is<InlineNode>(node.layout_node())) {
|
2025-01-15 16:37:30 +01:00
|
|
|
|
// Inline nodes might have a continuation chain; add all inline nodes that are part of it.
|
|
|
|
|
for (GC::Ptr inline_node = static_cast<NodeWithStyleAndBoxModelMetrics*>(node.layout_node());
|
|
|
|
|
inline_node; inline_node = inline_node->continuation_of_node()) {
|
|
|
|
|
if (is<InlineNode>(*inline_node))
|
|
|
|
|
inline_nodes.set(static_cast<InlineNode*>(inline_node.ptr()));
|
|
|
|
|
}
|
2024-10-13 21:29:47 +02:00
|
|
|
|
}
|
2024-05-04 14:47:04 +01:00
|
|
|
|
return TraversalDecision::Continue;
|
2024-03-17 19:11:50 +01:00
|
|
|
|
});
|
2023-08-29 16:27:16 +02:00
|
|
|
|
|
2022-03-10 16:46:44 +01:00
|
|
|
|
HashTable<Layout::TextNode*> text_nodes;
|
2024-10-13 21:29:47 +02:00
|
|
|
|
HashTable<Painting::PaintableWithLines*> inline_node_paintables;
|
|
|
|
|
|
2025-08-29 11:00:35 +02:00
|
|
|
|
auto transfer_box_model_metrics = [](Painting::BoxModelMetrics& box_model, UsedValues const& used_values) {
|
2025-02-17 13:54:36 +01:00
|
|
|
|
box_model.inset = { used_values.inset_top, used_values.inset_right, used_values.inset_bottom, used_values.inset_left };
|
|
|
|
|
box_model.padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left };
|
|
|
|
|
box_model.border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left };
|
|
|
|
|
box_model.margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left };
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-13 21:29:47 +02:00
|
|
|
|
auto try_to_relocate_fragment_in_inline_node = [&](auto& fragment, size_t line_index) -> bool {
|
|
|
|
|
for (auto const* parent = fragment.layout_node().parent(); parent; parent = parent->parent()) {
|
2025-08-24 17:34:46 +02:00
|
|
|
|
if (parent->is_atomic_inline())
|
|
|
|
|
break;
|
2024-10-13 21:29:47 +02:00
|
|
|
|
if (is<InlineNode>(*parent)) {
|
|
|
|
|
auto& inline_node = const_cast<InlineNode&>(static_cast<InlineNode const&>(*parent));
|
|
|
|
|
auto line_paintable = inline_node.create_paintable_for_line_with_index(line_index);
|
|
|
|
|
line_paintable->add_fragment(fragment);
|
2025-02-17 13:54:36 +01:00
|
|
|
|
if (auto const* used_values = used_values_per_layout_node.get(inline_node).value_or(nullptr))
|
|
|
|
|
transfer_box_model_metrics(line_paintable->box_model(), *used_values);
|
2024-10-13 21:29:47 +02:00
|
|
|
|
if (!inline_node_paintables.contains(line_paintable.ptr())) {
|
|
|
|
|
inline_node_paintables.set(line_paintable.ptr());
|
|
|
|
|
inline_node.add_paintable(line_paintable);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2023-07-12 15:41:30 +02:00
|
|
|
|
|
2023-05-23 07:47:37 +02:00
|
|
|
|
for (auto& it : used_values_per_layout_node) {
|
|
|
|
|
auto& used_values = *it.value;
|
2025-08-29 11:00:35 +02:00
|
|
|
|
auto& node = used_values.node();
|
2023-09-04 12:24:48 +02:00
|
|
|
|
|
2023-08-18 12:30:27 +02:00
|
|
|
|
auto paintable = node.create_paintable();
|
2024-10-14 16:07:56 +02:00
|
|
|
|
node.add_paintable(paintable);
|
2022-03-10 15:50:57 +01:00
|
|
|
|
|
2022-03-09 23:53:41 +01:00
|
|
|
|
// For boxes, transfer all the state needed for painting.
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (auto* paintable_box = as_if<Painting::PaintableBox>(paintable.ptr())) {
|
|
|
|
|
transfer_box_model_metrics(paintable_box->box_model(), used_values);
|
2025-02-17 13:54:36 +01:00
|
|
|
|
|
2025-08-29 11:00:35 +02:00
|
|
|
|
paintable_box->set_offset(used_values.offset);
|
|
|
|
|
paintable_box->set_content_size(used_values.content_width(), used_values.content_height());
|
|
|
|
|
if (used_values.override_borders_data().has_value())
|
|
|
|
|
paintable_box->set_override_borders_data(used_values.override_borders_data().value());
|
|
|
|
|
if (used_values.table_cell_coordinates().has_value())
|
|
|
|
|
paintable_box->set_table_cell_coordinates(used_values.table_cell_coordinates().value());
|
2022-03-10 11:12:06 +01:00
|
|
|
|
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (auto* paintable_with_lines = as_if<Painting::PaintableWithLines>(*paintable_box)) {
|
2024-10-13 21:29:47 +02:00
|
|
|
|
for (size_t line_index = 0; line_index < used_values.line_boxes.size(); ++line_index) {
|
|
|
|
|
auto& line_box = used_values.line_boxes[line_index];
|
2025-09-12 10:06:27 +02:00
|
|
|
|
for (auto const& fragment : line_box.fragments()) {
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (auto const* text_node = as_if<TextNode>(fragment.layout_node()))
|
|
|
|
|
text_nodes.set(const_cast<TextNode*>(text_node));
|
2024-10-13 21:29:47 +02:00
|
|
|
|
auto did_relocate_fragment = try_to_relocate_fragment_in_inline_node(fragment, line_index);
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (!did_relocate_fragment)
|
|
|
|
|
paintable_with_lines->add_fragment(fragment);
|
2024-10-13 21:29:47 +02:00
|
|
|
|
}
|
2024-01-12 21:25:05 +01:00
|
|
|
|
}
|
2023-07-12 15:41:30 +02:00
|
|
|
|
}
|
2023-10-29 17:08:35 +00:00
|
|
|
|
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (auto* svg_graphics_paintable = as_if<Painting::SVGGraphicsPaintable>(paintable.ptr());
|
|
|
|
|
svg_graphics_paintable && used_values.computed_svg_transforms().has_value()) {
|
|
|
|
|
svg_graphics_paintable->set_computed_transforms(*used_values.computed_svg_transforms());
|
2023-10-29 19:11:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (auto* svg_path_paintable = as_if<Painting::SVGPathPaintable>(paintable.ptr());
|
|
|
|
|
svg_path_paintable && used_values.computed_svg_path().has_value()) {
|
|
|
|
|
svg_path_paintable->set_computed_path(move(*used_values.computed_svg_path()));
|
2023-10-29 17:08:35 +00:00
|
|
|
|
}
|
2024-09-09 12:15:49 +02:00
|
|
|
|
|
|
|
|
|
if (node.display().is_grid_inside()) {
|
2025-08-29 11:00:35 +02:00
|
|
|
|
paintable_box->set_used_values_for_grid_template_columns(used_values.grid_template_columns());
|
|
|
|
|
paintable_box->set_used_values_for_grid_template_rows(used_values.grid_template_rows());
|
2024-09-09 12:15:49 +02:00
|
|
|
|
}
|
2023-07-12 15:41:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-13 21:29:47 +02:00
|
|
|
|
// Create paintables for inline nodes without fragments to make possible querying their geometry.
|
|
|
|
|
for (auto& inline_node : inline_nodes) {
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (inline_node->first_paintable())
|
2024-10-13 21:29:47 +02:00
|
|
|
|
continue;
|
2025-08-29 11:00:35 +02:00
|
|
|
|
|
2024-11-22 10:54:00 +01:00
|
|
|
|
auto line_paintable = inline_node->create_paintable_for_line_with_index(0);
|
|
|
|
|
inline_node->add_paintable(line_paintable);
|
|
|
|
|
inline_node_paintables.set(line_paintable.ptr());
|
2025-02-17 13:54:36 +01:00
|
|
|
|
if (auto const* used_values = used_values_per_layout_node.get(*inline_node).value_or(nullptr))
|
|
|
|
|
transfer_box_model_metrics(line_paintable->box_model(), *used_values);
|
2024-10-13 21:29:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 14:03:30 +01:00
|
|
|
|
// Resolve relative positions for regular boxes (not line box fragments):
|
|
|
|
|
// NOTE: This needs to occur before fragments are transferred into the corresponding inline paintables, because
|
|
|
|
|
// after this transfer, the containing_line_box_fragment will no longer be valid.
|
|
|
|
|
for (auto& it : used_values_per_layout_node) {
|
|
|
|
|
auto& used_values = *it.value;
|
|
|
|
|
auto& node = const_cast<NodeWithStyle&>(used_values.node());
|
|
|
|
|
|
|
|
|
|
if (!node.is_box())
|
|
|
|
|
continue;
|
|
|
|
|
|
2025-02-17 13:54:36 +01:00
|
|
|
|
auto& paintable = as<Painting::PaintableBox>(*node.first_paintable());
|
2024-01-18 14:03:30 +01:00
|
|
|
|
CSSPixelPoint offset;
|
|
|
|
|
|
|
|
|
|
if (used_values.containing_line_box_fragment.has_value()) {
|
|
|
|
|
// Atomic inline case:
|
|
|
|
|
// We know that `node` is an atomic inline because `containing_line_box_fragments` refers to the
|
|
|
|
|
// line box fragment in the parent block container that contains it.
|
|
|
|
|
auto const& containing_line_box_fragment = used_values.containing_line_box_fragment.value();
|
|
|
|
|
auto const& containing_block = *node.containing_block();
|
|
|
|
|
auto const& containing_block_used_values = get(containing_block);
|
|
|
|
|
auto const& fragment = containing_block_used_values.line_boxes[containing_line_box_fragment.line_box_index].fragments()[containing_line_box_fragment.fragment_index];
|
|
|
|
|
|
|
|
|
|
// The fragment has the final offset for the atomic inline, so we just need to copy it from there.
|
|
|
|
|
offset = fragment.offset();
|
|
|
|
|
} else {
|
|
|
|
|
// Not an atomic inline, much simpler case.
|
|
|
|
|
offset = used_values.offset;
|
|
|
|
|
}
|
2025-08-29 11:00:35 +02:00
|
|
|
|
|
2024-01-18 14:03:30 +01:00
|
|
|
|
// Apply relative position inset if appropriate.
|
|
|
|
|
if (node.computed_values().position() == CSS::Positioning::Relative && is<NodeWithStyleAndBoxModelMetrics>(node)) {
|
2025-02-17 13:54:36 +01:00
|
|
|
|
auto const& inset = paintable.box_model().inset;
|
2024-01-18 14:03:30 +01:00
|
|
|
|
offset.translate_by(inset.left, inset.top);
|
|
|
|
|
}
|
|
|
|
|
paintable.set_offset(offset);
|
|
|
|
|
}
|
2023-08-15 15:11:10 +02:00
|
|
|
|
|
2025-08-29 11:00:35 +02:00
|
|
|
|
for (auto* text_node : text_nodes)
|
2024-10-14 16:07:56 +02:00
|
|
|
|
text_node->add_paintable(text_node->create_paintable());
|
2023-08-18 12:30:27 +02:00
|
|
|
|
|
|
|
|
|
build_paint_tree(root);
|
|
|
|
|
|
2024-01-18 14:03:30 +01:00
|
|
|
|
resolve_relative_positions();
|
|
|
|
|
|
2024-10-13 21:29:47 +02:00
|
|
|
|
// Measure size of paintables created for inline nodes.
|
2025-08-27 12:37:59 +02:00
|
|
|
|
for (auto* paintable_with_lines : inline_node_paintables) {
|
|
|
|
|
if (!is<InlineNode>(paintable_with_lines->layout_node()))
|
2024-10-13 21:29:47 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
2024-11-22 10:54:00 +01:00
|
|
|
|
Optional<CSSPixelPoint> offset;
|
|
|
|
|
CSSPixelSize size;
|
|
|
|
|
auto line_index = paintable_with_lines->line_index();
|
2025-08-27 12:37:59 +02:00
|
|
|
|
paintable_with_lines->for_each_in_inclusive_subtree_of_type<Painting::PaintableWithLines>([&](auto& paintable) {
|
|
|
|
|
if (paintable.line_index() != line_index)
|
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
|
2025-08-28 21:35:21 +02:00
|
|
|
|
auto used_values = used_values_per_layout_node.get(paintable.layout_node_with_style_and_box_metrics());
|
|
|
|
|
if (&paintable != paintable_with_lines && used_values.has_value())
|
|
|
|
|
size.set_width(size.width() + used_values.value()->margin_box_left() + used_values.value()->margin_box_right());
|
2025-08-27 12:37:59 +02:00
|
|
|
|
|
|
|
|
|
auto const& fragments = paintable.fragments();
|
|
|
|
|
if (!fragments.is_empty()) {
|
2025-08-28 21:35:21 +02:00
|
|
|
|
if (!offset.has_value() || (fragments.first().offset().x() < offset->x()))
|
2025-08-27 12:37:59 +02:00
|
|
|
|
offset = fragments.first().offset();
|
2025-08-28 21:35:21 +02:00
|
|
|
|
if (&paintable == paintable_with_lines->first_child() && used_values.has_value())
|
|
|
|
|
offset->translate_by(-used_values.value()->margin_box_left(), 0);
|
2024-11-22 10:54:00 +01:00
|
|
|
|
}
|
2025-08-27 12:37:59 +02:00
|
|
|
|
for (auto const& fragment : fragments)
|
|
|
|
|
size.set_width(size.width() + fragment.width());
|
2025-08-29 11:00:35 +02:00
|
|
|
|
|
2024-11-22 10:54:00 +01:00
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (offset.has_value()) {
|
2025-08-27 12:37:59 +02:00
|
|
|
|
if (!paintable_with_lines->fragments().is_empty())
|
2024-11-22 10:54:00 +01:00
|
|
|
|
offset.value().set_y(paintable_with_lines->fragments().first().offset().y());
|
2025-08-27 12:37:59 +02:00
|
|
|
|
|
2024-11-22 10:54:00 +01:00
|
|
|
|
// FIXME: If this paintable does not have any fragment we do no know the y offset. It should be where text should
|
|
|
|
|
// start if there had been any for this node. Pick y offset of the leftmost fragment in the inclusive subtree in the meantime.
|
|
|
|
|
paintable_with_lines->set_offset(offset.value());
|
2024-10-13 21:29:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 10:54:00 +01:00
|
|
|
|
if (!paintable_with_lines->fragments().is_empty()) {
|
2025-08-29 11:00:35 +02:00
|
|
|
|
for (auto const& fragment : paintable_with_lines->fragments())
|
2024-11-22 10:54:00 +01:00
|
|
|
|
size.set_height(max(size.height(), fragment.height()));
|
|
|
|
|
} else {
|
|
|
|
|
size.set_height(paintable_with_lines->layout_node().computed_values().line_height());
|
2024-10-13 21:29:47 +02:00
|
|
|
|
}
|
2024-11-22 10:54:00 +01:00
|
|
|
|
|
2025-08-27 12:37:59 +02:00
|
|
|
|
paintable_with_lines->set_content_size(size);
|
2024-10-13 21:29:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 08:53:39 +02:00
|
|
|
|
// Measure overflow in scroll containers.
|
|
|
|
|
for (auto& it : used_values_per_layout_node) {
|
|
|
|
|
auto& used_values = *it.value;
|
2025-08-27 12:37:59 +02:00
|
|
|
|
auto const* box = as_if<Box>(used_values.node());
|
|
|
|
|
if (!box)
|
2023-05-29 08:53:39 +02:00
|
|
|
|
continue;
|
2025-08-27 12:37:59 +02:00
|
|
|
|
measure_scrollable_overflow(*box);
|
2024-02-22 02:09:16 +01:00
|
|
|
|
|
|
|
|
|
// The scroll offset can become invalid if the scrollable overflow rectangle has changed after layout.
|
|
|
|
|
// For example, if the scroll container has been scrolled to the very end and is then resized to become larger
|
|
|
|
|
// (scrollable overflow rect become smaller), the scroll offset would be out of bounds.
|
2025-08-27 12:37:59 +02:00
|
|
|
|
auto& paintable_box = const_cast<Painting::PaintableBox&>(*box->paintable_box());
|
2024-02-22 02:09:16 +01:00
|
|
|
|
if (!paintable_box.scroll_offset().is_zero())
|
2025-10-07 14:09:30 +01:00
|
|
|
|
(void)paintable_box.set_scroll_offset(paintable_box.scroll_offset());
|
2024-09-02 20:46:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto& it : used_values_per_layout_node) {
|
|
|
|
|
auto& used_values = *it.value;
|
2025-02-27 16:56:43 +01:00
|
|
|
|
auto& node = used_values.node();
|
|
|
|
|
for (auto& paintable : node.paintables()) {
|
2025-08-29 11:00:35 +02:00
|
|
|
|
auto* paintable_box = as_if<Painting::PaintableBox>(paintable);
|
2025-02-27 16:56:43 +01:00
|
|
|
|
if (!paintable_box)
|
|
|
|
|
continue;
|
2024-08-24 19:20:31 +02:00
|
|
|
|
|
2025-02-27 16:56:43 +01:00
|
|
|
|
if (node.is_sticky_position()) {
|
|
|
|
|
// https://drafts.csswg.org/css-position/#insets
|
2025-08-29 11:00:35 +02:00
|
|
|
|
// For sticky positioned boxes, the inset is instead relative to the relevant scrollport’s size.
|
|
|
|
|
// Negative values are allowed.
|
2024-09-02 20:46:41 +02:00
|
|
|
|
|
2025-02-27 16:56:43 +01:00
|
|
|
|
auto sticky_insets = make<Painting::PaintableBox::StickyInsets>();
|
|
|
|
|
auto const& inset = node.computed_values().inset();
|
2024-09-02 20:46:41 +02:00
|
|
|
|
|
2025-02-27 16:56:43 +01:00
|
|
|
|
auto const* nearest_scrollable_ancestor = paintable_box->nearest_scrollable_ancestor();
|
|
|
|
|
CSSPixelSize scrollport_size;
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (nearest_scrollable_ancestor)
|
2025-02-27 16:56:43 +01:00
|
|
|
|
scrollport_size = nearest_scrollable_ancestor->absolute_rect().size();
|
2024-09-02 20:46:41 +02:00
|
|
|
|
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (!inset.top().is_auto())
|
2025-09-01 12:51:52 +01:00
|
|
|
|
sticky_insets->top = inset.top().to_px_or_zero(node, scrollport_size.height());
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (!inset.right().is_auto())
|
2025-09-01 12:51:52 +01:00
|
|
|
|
sticky_insets->right = inset.right().to_px_or_zero(node, scrollport_size.width());
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (!inset.bottom().is_auto())
|
2025-09-01 12:51:52 +01:00
|
|
|
|
sticky_insets->bottom = inset.bottom().to_px_or_zero(node, scrollport_size.height());
|
2025-08-29 11:00:35 +02:00
|
|
|
|
if (!inset.left().is_auto())
|
2025-09-01 12:51:52 +01:00
|
|
|
|
sticky_insets->left = inset.left().to_px_or_zero(node, scrollport_size.width());
|
2025-02-27 16:56:43 +01:00
|
|
|
|
paintable_box->set_sticky_insets(move(sticky_insets));
|
2024-08-24 19:20:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-29 08:53:39 +02:00
|
|
|
|
}
|
2022-02-20 15:51:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-27 12:37:26 +02:00
|
|
|
|
void LayoutState::UsedValues::set_node(NodeWithStyle const& node, UsedValues const* containing_block_used_values)
|
2022-07-17 18:46:38 +02:00
|
|
|
|
{
|
|
|
|
|
m_node = &node;
|
2024-01-17 11:23:33 +01:00
|
|
|
|
m_containing_block_used_values = containing_block_used_values;
|
2022-07-17 18:46:38 +02:00
|
|
|
|
|
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();
|
|
|
|
|
|
2023-05-02 06:51:32 +02:00
|
|
|
|
auto adjust_for_box_sizing = [&](CSSPixels unadjusted_pixels, CSS::Size const& computed_size, bool width) -> CSSPixels {
|
|
|
|
|
// box-sizing: content-box and/or automatic size don't require any adjustment.
|
|
|
|
|
if (computed_values.box_sizing() == CSS::BoxSizing::ContentBox || computed_size.is_auto())
|
|
|
|
|
return unadjusted_pixels;
|
|
|
|
|
|
|
|
|
|
// box-sizing: border-box requires us to subtract the relevant border and padding from the size.
|
|
|
|
|
CSSPixels border_and_padding;
|
|
|
|
|
|
|
|
|
|
if (width) {
|
2023-07-30 15:54:57 +01:00
|
|
|
|
border_and_padding = computed_values.border_left().width
|
2025-09-01 12:51:52 +01:00
|
|
|
|
+ computed_values.padding().left().to_px_or_zero(*m_node, containing_block_used_values->content_width())
|
2023-07-30 15:54:57 +01:00
|
|
|
|
+ computed_values.border_right().width
|
2025-09-01 12:51:52 +01:00
|
|
|
|
+ computed_values.padding().right().to_px_or_zero(*m_node, containing_block_used_values->content_width());
|
2023-05-02 06:51:32 +02:00
|
|
|
|
} else {
|
2023-07-30 15:54:57 +01:00
|
|
|
|
border_and_padding = computed_values.border_top().width
|
2025-09-01 12:51:52 +01:00
|
|
|
|
+ computed_values.padding().top().to_px_or_zero(*m_node, containing_block_used_values->content_width())
|
2023-07-30 15:54:57 +01:00
|
|
|
|
+ computed_values.border_bottom().width
|
2025-09-01 12:51:52 +01:00
|
|
|
|
+ computed_values.padding().bottom().to_px_or_zero(*m_node, containing_block_used_values->content_width());
|
2023-05-02 06:51:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return unadjusted_pixels - border_and_padding;
|
|
|
|
|
};
|
|
|
|
|
|
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()
|
2023-06-22 18:02:04 +03:00
|
|
|
|
&& !node.is_absolutely_positioned()
|
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();
|
2025-02-05 14:13:33 +01:00
|
|
|
|
resolved_definite_size = clamp_to_max_dimension_value(
|
|
|
|
|
available_width
|
2022-09-27 15:29:17 +02:00
|
|
|
|
- margin_left
|
|
|
|
|
- margin_right
|
|
|
|
|
- padding_left
|
|
|
|
|
- padding_right
|
|
|
|
|
- border_left
|
2025-02-05 14:13:33 +01:00
|
|
|
|
- border_right);
|
2022-07-19 13:46:27 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-07-17 18:46:38 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-30 15:46:05 +01:00
|
|
|
|
if (size.is_calculated()) {
|
LibWeb/CSS: Wrap calc()-resolution data in a struct
Initially I added this to the existing CalculationContext, but in
reality, we have some data at parse-time and different data at
resolve-time, so it made more sense to keep those separate.
Instead of needing a variety of methods for resolving a Foo, depending
on whether we have a Layout::Node available, or a percentage basis, or
a length resolution context... put those in a
CalculationResolutionContext, and just pass that one thing to these
methods. This also removes the need for separate resolve_*_percentage()
methods, because we can just pass the percentage basis in to the regular
resolve_foo() method.
This also corrects the issue that *any* calculation may need to resolve
lengths, but we previously only passed a length resolution context to
specific types in some situations. Now, they can all have one available,
though it's up to the caller to provide it.
2025-01-22 16:05:32 +00:00
|
|
|
|
CSS::CalculationResolutionContext context {
|
|
|
|
|
.length_resolution_context = CSS::Length::ResolutionContext::for_layout_node(node),
|
|
|
|
|
};
|
2023-03-30 15:46:05 +01:00
|
|
|
|
if (size.calculated().contains_percentage()) {
|
2022-10-13 11:16:54 +02:00
|
|
|
|
if (!containing_block_has_definite_size)
|
|
|
|
|
return false;
|
2023-08-29 18:57:09 +02:00
|
|
|
|
auto containing_block_size_as_length = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
|
LibWeb/CSS: Wrap calc()-resolution data in a struct
Initially I added this to the existing CalculationContext, but in
reality, we have some data at parse-time and different data at
resolve-time, so it made more sense to keep those separate.
Instead of needing a variety of methods for resolving a Foo, depending
on whether we have a Layout::Node available, or a percentage basis, or
a length resolution context... put those in a
CalculationResolutionContext, and just pass that one thing to these
methods. This also removes the need for separate resolve_*_percentage()
methods, because we can just pass the percentage basis in to the regular
resolve_foo() method.
This also corrects the issue that *any* calculation may need to resolve
lengths, but we previously only passed a length resolution context to
specific types in some situations. Now, they can all have one available,
though it's up to the caller to provide it.
2025-01-22 16:05:32 +00:00
|
|
|
|
context.percentage_basis = CSS::Length::make_px(containing_block_size_as_length);
|
2022-09-27 15:29:17 +02:00
|
|
|
|
}
|
2025-09-24 14:33:18 +01:00
|
|
|
|
resolved_definite_size = clamp_to_max_dimension_value(adjust_for_box_sizing(size.calculated().resolve_length(context)->to_px(node), size, width));
|
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()) {
|
2023-03-30 15:46:05 +01:00
|
|
|
|
VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
|
2025-02-05 14:13:33 +01:00
|
|
|
|
resolved_definite_size = clamp_to_max_dimension_value(adjust_for_box_sizing(size.length().to_px(node), size, width));
|
2022-09-27 15:29:17 +02:00
|
|
|
|
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();
|
2025-02-05 14:13:33 +01:00
|
|
|
|
resolved_definite_size = clamp_to_max_dimension_value(adjust_for_box_sizing(containing_block_size.scaled(size.percentage().as_fraction()), size, width));
|
2022-07-19 13:46:27 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-07-17 18:46:38 +02:00
|
|
|
|
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)
|
2025-02-05 14:13:33 +01:00
|
|
|
|
m_content_width = clamp_to_max_dimension_value(max(min_width, m_content_width));
|
2022-10-13 14:49:08 +02:00
|
|
|
|
if (has_definite_max_width)
|
2025-02-05 14:13:33 +01:00
|
|
|
|
m_content_width = clamp_to_max_dimension_value(min(max_width, m_content_width));
|
2022-10-13 14:49:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_has_definite_height) {
|
|
|
|
|
if (has_definite_min_height)
|
2025-02-05 14:13:33 +01:00
|
|
|
|
m_content_height = clamp_to_max_dimension_value(max(min_height, m_content_height));
|
2022-10-13 14:49:08 +02:00
|
|
|
|
if (has_definite_max_height)
|
2025-02-05 14:13:33 +01:00
|
|
|
|
m_content_height = clamp_to_max_dimension_value(min(max_height, m_content_height));
|
2022-10-13 14:49:08 +02:00
|
|
|
|
}
|
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) {
|
2023-03-26 22:25:53 +11:00
|
|
|
|
// Negative widths are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
|
2023-05-31 11:47:26 +02:00
|
|
|
|
dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative width for {}: {}", m_node->debug_description(), width);
|
2023-03-25 17:33:22 +01:00
|
|
|
|
width = 0;
|
|
|
|
|
}
|
2025-02-05 14:13:33 +01:00
|
|
|
|
m_content_width = clamp_to_max_dimension_value(width);
|
2024-02-21 15:27:37 +01:00
|
|
|
|
// FIXME: We should not do this! Definiteness of widths should be determined early,
|
|
|
|
|
// and not changed later (except for some special cases in flex layout..)
|
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.
|
2023-05-31 11:47:26 +02:00
|
|
|
|
dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative height for {}: {}", m_node->debug_description(), height);
|
2023-03-25 17:33:22 +01:00
|
|
|
|
height = 0;
|
|
|
|
|
}
|
2025-02-05 14:13:33 +01:00
|
|
|
|
m_content_height = clamp_to_max_dimension_value(height);
|
2022-07-17 17:59:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 15:29:17 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-27 14:12:05 +02:00
|
|
|
|
void LayoutState::UsedValues::set_indefinite_content_width()
|
|
|
|
|
{
|
|
|
|
|
m_has_definite_width = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LayoutState::UsedValues::set_indefinite_content_height()
|
|
|
|
|
{
|
|
|
|
|
m_has_definite_height = false;
|
|
|
|
|
}
|
2025-05-13 07:06:33 -04:00
|
|
|
|
|
2022-02-20 15:51:24 +01:00
|
|
|
|
}
|