LibWeb: Define static position calculation separately for each FC

Static position of a box is defined by formatting context type it
belongs to, so let's define this algorithm separately for each FC
instead of assuming FormattingContext::calculate_static_position_rect()
understands how to handle all of them.

Also with this change calculate_static_position_rect() is no longer
virtual function.
This commit is contained in:
Aliaksandr Kalenik 2024-10-05 22:18:53 +02:00 committed by Alexander Kalenik
commit 1a78edb8c9
Notes: github-actions[bot] 2024-10-06 13:45:57 +00:00
11 changed files with 73 additions and 49 deletions

View file

@ -1329,4 +1329,13 @@ CSSPixels BlockFormattingContext::greatest_child_width(Box const& box) const
return max_width;
}
StaticPositionRect BlockFormattingContext::calculate_static_position_rect(Box const& box) const
{
StaticPositionRect static_position;
auto const& box_state = m_state.get(box);
auto offset_to_static_parent = content_box_rect_in_static_position_ancestor_coordinate_space(box, *box.containing_block());
static_position.rect = { offset_to_static_parent.location().translated(0, box_state.vertical_offset_of_parent_block_container), { 0, 0 } };
return static_position;
}
}

View file

@ -25,6 +25,7 @@ public:
virtual void run(AvailableSpace const&) override;
virtual CSSPixels automatic_content_width() const override;
virtual CSSPixels automatic_content_height() const override;
StaticPositionRect calculate_static_position_rect(Box const&) const;
auto const& left_side_floats() const { return m_left_floats; }
auto const& right_side_floats() const { return m_right_floats; }

View file

@ -24,7 +24,7 @@ public:
Box const& flex_container() const { return context_box(); }
virtual StaticPositionRect calculate_static_position_rect(Box const&) const override;
StaticPositionRect calculate_static_position_rect(Box const&) const;
private:
[[nodiscard]] bool should_treat_main_size_as_auto(Box const&) const;

View file

@ -1174,53 +1174,6 @@ CSSPixelRect FormattingContext::content_box_rect_in_static_position_ancestor_coo
VERIFY_NOT_REACHED();
}
// https://www.w3.org/TR/css-position-3/#staticpos-rect
StaticPositionRect FormattingContext::calculate_static_position_rect(Box const& box) const
{
// NOTE: This is very ad-hoc.
// The purpose of this function is to calculate the approximate position that `box`
// would have had if it were position:static.
CSSPixels x = 0;
CSSPixels y = 0;
VERIFY(box.parent());
if (box.parent()->children_are_inline()) {
// We're an abspos box with inline siblings. This is gonna get messy!
if (auto* sibling = box.previous_sibling()) {
// Hard case: there's a previous sibling. This means there's already inline content
// preceding the hypothetical static position of `box` within its containing block.
// If we had been position:static, that inline content would have been wrapped in
// anonymous block box, so now we get to imagine what the world might have looked like
// in that scenario..
// Basically, we find its last associated line box fragment and place `box` under it.
// FIXME: I'm 100% sure this can be smarter, better and faster.
LineBoxFragment const* last_fragment = nullptr;
auto& cb_state = m_state.get(*sibling->containing_block());
for (auto& line_box : cb_state.line_boxes) {
for (auto& fragment : line_box.fragments()) {
if (&fragment.layout_node() == sibling)
last_fragment = &fragment;
}
}
if (last_fragment) {
x = last_fragment->offset().x() + last_fragment->width();
y = last_fragment->offset().y() + last_fragment->height();
}
} else {
// Easy case: no previous sibling, we're at the top of the containing block.
}
} else {
auto const& box_state = m_state.get(box);
// We're among block siblings, Y can be calculated easily.
y = box_state.vertical_offset_of_parent_block_container;
}
auto offset_to_static_parent = content_box_rect_in_static_position_ancestor_coordinate_space(box, *box.containing_block());
StaticPositionRect static_position_rect;
static_position_rect.rect = { offset_to_static_parent.location().translated(x, y), { 0, 0 } };
return static_position_rect;
}
void FormattingContext::layout_absolutely_positioned_element(Box const& box, AvailableSpace const& available_space)
{
if (box.is_svg_box()) {

View file

@ -107,7 +107,6 @@ public:
[[nodiscard]] CSSPixels calculate_stretch_fit_width(Box const&, AvailableSize const&) const;
[[nodiscard]] CSSPixels calculate_stretch_fit_height(Box const&, AvailableSize const&) const;
virtual StaticPositionRect calculate_static_position_rect(Box const&) const;
bool can_skip_is_anonymous_text_run(Box&);
void compute_inset(NodeWithStyleAndBoxModelMetrics const&);

View file

@ -2508,6 +2508,18 @@ CSSPixels GridFormattingContext::calculate_minimum_contribution(GridItem const&
return calculate_min_content_contribution(item, dimension);
}
StaticPositionRect GridFormattingContext::calculate_static_position_rect(Box const& box) const
{
// Result of this function is only used when containing block is not a grid container.
// If the containing block is a grid container then static position is a grid area rect and
// layout_absolutely_positioned_element() defined for GFC knows how to handle this case.
StaticPositionRect static_position;
auto const& box_state = m_state.get(box);
auto offset_to_static_parent = content_box_rect_in_static_position_ancestor_coordinate_space(box, *box.containing_block());
static_position.rect = { offset_to_static_parent.location().translated(0, 0), { box_state.content_width(), box_state.content_height() } };
return static_position;
}
}
namespace AK {

View file

@ -109,6 +109,7 @@ public:
virtual void run(AvailableSpace const& available_space) override;
virtual CSSPixels automatic_content_width() const override;
virtual CSSPixels automatic_content_height() const override;
StaticPositionRect calculate_static_position_rect(Box const&) const;
Box const& grid_container() const { return context_box(); }

View file

@ -463,4 +463,42 @@ void InlineFormattingContext::set_vertical_float_clearance(CSSPixels vertical_fl
{
m_vertical_float_clearance = vertical_float_clearance;
}
StaticPositionRect InlineFormattingContext::calculate_static_position_rect(Box const& box) const
{
CSSPixels x = 0;
CSSPixels y = 0;
VERIFY(box.parent());
VERIFY(box.parent()->children_are_inline());
// We're an abspos box with inline siblings. This is gonna get messy!
if (auto const* sibling = box.previous_sibling()) {
// Hard case: there's a previous sibling. This means there's already inline content
// preceding the hypothetical static position of `box` within its containing block.
// If we had been position:static, that inline content would have been wrapped in
// anonymous block box, so now we get to imagine what the world might have looked like
// in that scenario..
// Basically, we find its last associated line box fragment and place `box` under it.
// FIXME: I'm 100% sure this can be smarter, better and faster.
LineBoxFragment const* last_fragment = nullptr;
auto const& cb_state = m_state.get(*sibling->containing_block());
for (auto const& line_box : cb_state.line_boxes) {
for (auto const& fragment : line_box.fragments()) {
if (&fragment.layout_node() == sibling)
last_fragment = &fragment;
}
}
if (last_fragment) {
x = last_fragment->offset().x() + last_fragment->width();
y = last_fragment->offset().y() + last_fragment->height();
}
} else {
// Easy case: no previous sibling, we're at the top of the containing block.
}
auto offset_to_static_parent = content_box_rect_in_static_position_ancestor_coordinate_space(box, *box.containing_block());
StaticPositionRect static_position_rect;
static_position_rect.rect = { offset_to_static_parent.location().translated(x, y), { 0, 0 } };
return static_position_rect;
}
}

View file

@ -26,6 +26,7 @@ public:
virtual void run(AvailableSpace const&) override;
virtual CSSPixels automatic_content_height() const override;
virtual CSSPixels automatic_content_width() const override;
StaticPositionRect calculate_static_position_rect(Box const&) const;
void dimension_box_on_line(Box const&, LayoutMode);

View file

@ -1849,6 +1849,15 @@ CSSPixels TableFormattingContext::border_spacing_vertical() const
return computed_values.border_spacing_vertical().to_px(table_box());
}
StaticPositionRect TableFormattingContext::calculate_static_position_rect(Box const& box) const
{
// FIXME: Implement static position calculation for table descendants instead of always returning a rectangle with zero position and size.
StaticPositionRect static_position;
auto offset_to_static_parent = content_box_rect_in_static_position_ancestor_coordinate_space(box, *box.containing_block());
static_position.rect = { offset_to_static_parent.location(), { 0, 0 } };
return static_position;
}
template<>
Vector<TableFormattingContext::Row>& TableFormattingContext::table_rows_or_columns()
{

View file

@ -28,6 +28,7 @@ public:
virtual void run(AvailableSpace const&) override;
virtual CSSPixels automatic_content_width() const override;
virtual CSSPixels automatic_content_height() const override;
StaticPositionRect calculate_static_position_rect(Box const&) const;
Box const& table_box() const { return context_box(); }
TableWrapper const& table_wrapper() const