LibWeb: Let FormattingContext decide the automatic width of its root

Instead of special-casing FlexFormattingContext in the intrinsic sizing
layout helpers, add FormattingContext::automatic_content_width() and let
each context subclass decide what that means.

No behavior change here, just moving this responsibility.
This commit is contained in:
Andreas Kling 2023-03-19 09:57:31 +01:00
parent e945994877
commit 00999a245c
Notes: sideshowbarker 2024-07-16 22:39:49 +09:00
12 changed files with 42 additions and 18 deletions

View file

@ -38,6 +38,11 @@ BlockFormattingContext::~BlockFormattingContext()
}
}
CSSPixels BlockFormattingContext::automatic_content_width() const
{
return greatest_child_width(root());
}
CSSPixels BlockFormattingContext::automatic_content_height() const
{
return compute_auto_height_for_block_formatting_context_root(root());
@ -925,7 +930,7 @@ BlockFormattingContext::SpaceUsedByFloats BlockFormattingContext::space_used_by_
return space_used_by_floats;
}
CSSPixels BlockFormattingContext::greatest_child_width(Box const& box)
CSSPixels BlockFormattingContext::greatest_child_width(Box const& box) const
{
// Similar to FormattingContext::greatest_child_width()
// but this one takes floats into account!

View file

@ -22,6 +22,7 @@ public:
~BlockFormattingContext();
virtual void run(Box const&, LayoutMode, AvailableSpace const&) override;
virtual CSSPixels automatic_content_width() const override;
virtual CSSPixels automatic_content_height() const override;
auto const& left_side_floats() const { return m_left_floats; }
@ -40,7 +41,7 @@ public:
SpaceUsedByFloats space_used_by_floats(CSSPixels y) const;
virtual CSSPixels greatest_child_width(Box const&) override;
virtual CSSPixels greatest_child_width(Box const&) const override;
void layout_floating_box(Box const& child, BlockContainer const& containing_block, LayoutMode, AvailableSpace const&, CSSPixels y, LineBuilder* = nullptr);

View file

@ -75,9 +75,14 @@ FlexFormattingContext::FlexFormattingContext(LayoutState& state, Box const& flex
FlexFormattingContext::~FlexFormattingContext() = default;
CSSPixels FlexFormattingContext::automatic_content_width() const
{
return m_flex_container_state.content_width();
}
CSSPixels FlexFormattingContext::automatic_content_height() const
{
return m_state.get(flex_container()).content_height();
return m_flex_container_state.content_height();
}
void FlexFormattingContext::run(Box const& run_box, LayoutMode, AvailableSpace const& available_content_space)

View file

@ -19,6 +19,7 @@ public:
virtual bool inhibits_floating() const override { return true; }
virtual void run(Box const&, LayoutMode, AvailableSpace const&) override;
virtual CSSPixels automatic_content_width() const override;
virtual CSSPixels automatic_content_height() const override;
Box const& flex_container() const { return context_box(); }

View file

@ -127,7 +127,8 @@ OwnPtr<FormattingContext> FormattingContext::create_independent_formatting_conte
: FormattingContext(Type::Block, state, box)
{
}
virtual CSSPixels automatic_content_height() const override { return 0; };
virtual CSSPixels automatic_content_width() const override { return 0; }
virtual CSSPixels automatic_content_height() const override { return 0; }
virtual void run(Box const&, LayoutMode, AvailableSpace const&) override { }
};
return make<ReplacedFormattingContext>(state, child_box);
@ -170,7 +171,8 @@ OwnPtr<FormattingContext> FormattingContext::create_independent_formatting_conte
: FormattingContext(Type::Block, state, box)
{
}
virtual CSSPixels automatic_content_height() const override { return 0; };
virtual CSSPixels automatic_content_width() const override { return 0; }
virtual CSSPixels automatic_content_height() const override { return 0; }
virtual void run(Box const&, LayoutMode, AvailableSpace const&) override { }
};
return make<DummyFormattingContext>(state, child_box);
@ -208,7 +210,7 @@ OwnPtr<FormattingContext> FormattingContext::layout_inside(Box const& child_box,
return independent_formatting_context;
}
CSSPixels FormattingContext::greatest_child_width(Box const& box)
CSSPixels FormattingContext::greatest_child_width(Box const& box) const
{
CSSPixels max_width = 0;
if (box.children_are_inline()) {
@ -1093,11 +1095,7 @@ CSSPixels FormattingContext::calculate_min_content_width(Layout::Box const& box)
auto available_height = AvailableSize::make_indefinite();
context->run(box, LayoutMode::IntrinsicSizing, AvailableSpace(available_width, available_height));
if (context->type() == FormattingContext::Type::Flex) {
cache.min_content_width = box_state.content_width();
} else {
cache.min_content_width = context->greatest_child_width(box).value();
}
cache.min_content_width = context->automatic_content_width();
if (!isfinite(cache.min_content_width->value())) {
// HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
@ -1131,11 +1129,7 @@ CSSPixels FormattingContext::calculate_max_content_width(Layout::Box const& box)
auto available_height = AvailableSize::make_indefinite();
context->run(box, LayoutMode::IntrinsicSizing, AvailableSpace(available_width, available_height));
if (context->type() == FormattingContext::Type::Flex) {
cache.max_content_width = box_state.content_width();
} else {
cache.max_content_width = context->greatest_child_width(box).value();
}
cache.max_content_width = context->automatic_content_width();
if (!isfinite(cache.max_content_width->value())) {
// HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.

View file

@ -29,7 +29,7 @@ public:
virtual void run(Box const&, LayoutMode, AvailableSpace const&) = 0;
// This function returns the automatic content height of the context's root box.
virtual CSSPixels automatic_content_width() const { return 0; }
virtual CSSPixels automatic_content_width() const = 0;
// This function returns the automatic content height of the context's root box.
virtual CSSPixels automatic_content_height() const = 0;
@ -64,7 +64,7 @@ public:
CSS::Length calculate_inner_width(Layout::Box const&, AvailableSize const&, CSS::Size const& width) const;
CSS::Length calculate_inner_height(Layout::Box const&, AvailableSize const&, CSS::Size const& height) const;
virtual CSSPixels greatest_child_width(Box const&);
virtual CSSPixels greatest_child_width(Box const&) const;
CSSPixels containing_block_width_for(Box const& box) const { return containing_block_width_for(box, m_state); }
CSSPixels containing_block_height_for(Box const& box) const { return containing_block_height_for(box, m_state); }

View file

@ -1901,6 +1901,11 @@ void GridFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const
m_automatic_content_height = total_y;
}
CSSPixels GridFormattingContext::automatic_content_width() const
{
return greatest_child_width(context_box());
}
CSSPixels GridFormattingContext::automatic_content_height() const
{
return m_automatic_content_height;

View file

@ -62,6 +62,7 @@ public:
~GridFormattingContext();
virtual void run(Box const&, LayoutMode, AvailableSpace const& available_space) override;
virtual CSSPixels automatic_content_width() const override;
virtual CSSPixels automatic_content_height() const override;
private:

View file

@ -21,6 +21,11 @@ SVGFormattingContext::SVGFormattingContext(LayoutState& state, Box const& box, F
SVGFormattingContext::~SVGFormattingContext() = default;
CSSPixels SVGFormattingContext::automatic_content_width() const
{
return 0;
}
CSSPixels SVGFormattingContext::automatic_content_height() const
{
return 0;

View file

@ -17,6 +17,7 @@ public:
~SVGFormattingContext();
virtual void run(Box const&, LayoutMode, AvailableSpace const&) override;
virtual CSSPixels automatic_content_width() const override;
virtual CSSPixels automatic_content_height() const override;
};

View file

@ -542,6 +542,11 @@ void TableFormattingContext::run(Box const& box, LayoutMode layout_mode, Availab
m_automatic_content_height = total_content_height;
}
CSSPixels TableFormattingContext::automatic_content_width() const
{
return greatest_child_width(context_box());
}
CSSPixels TableFormattingContext::automatic_content_height() const
{
return m_automatic_content_height;

View file

@ -19,6 +19,7 @@ public:
~TableFormattingContext();
virtual void run(Box const&, LayoutMode, AvailableSpace const&) override;
virtual CSSPixels automatic_content_width() const override;
virtual CSSPixels automatic_content_height() const override;
TableBox const& table_box() const { return static_cast<TableBox const&>(context_box()); }