2020-11-22 09:38:18 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
|
2020-11-22 09:38:18 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-22 09:38:18 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2020-12-05 16:10:39 -03:00
|
|
|
#include <AK/Vector.h>
|
2020-11-22 09:38:18 -03:00
|
|
|
#include <LibWeb/Forward.h>
|
2021-10-06 16:53:25 -03:00
|
|
|
#include <LibWeb/Layout/BlockContainer.h>
|
2020-11-22 09:38:18 -03:00
|
|
|
#include <LibWeb/Layout/FormattingContext.h>
|
2023-07-29 01:05:45 -03:00
|
|
|
#include <LibWeb/Layout/InlineFormattingContext.h>
|
2020-11-22 09:38:18 -03:00
|
|
|
|
|
|
|
|
namespace Web::Layout {
|
|
|
|
|
|
2022-03-22 15:18:05 -03:00
|
|
|
class LineBuilder;
|
|
|
|
|
|
2021-10-15 15:52:10 -03:00
|
|
|
// https://www.w3.org/TR/css-display/#block-formatting-context
|
2020-11-22 09:38:18 -03:00
|
|
|
class BlockFormattingContext : public FormattingContext {
|
|
|
|
|
public:
|
2024-09-10 20:03:02 -03:00
|
|
|
explicit BlockFormattingContext(LayoutState&, LayoutMode layout_mode, BlockContainer const&, FormattingContext* parent);
|
2020-11-22 09:38:18 -03:00
|
|
|
~BlockFormattingContext();
|
|
|
|
|
|
2024-09-10 20:03:02 -03:00
|
|
|
virtual void run(AvailableSpace const&) override;
|
2023-03-19 05:57:31 -03:00
|
|
|
virtual CSSPixels automatic_content_width() const override;
|
2022-11-23 14:46:10 -03:00
|
|
|
virtual CSSPixels automatic_content_height() const override;
|
2020-11-22 09:38:18 -03:00
|
|
|
|
2022-01-22 11:19:18 -03:00
|
|
|
auto const& left_side_floats() const { return m_left_floats; }
|
|
|
|
|
auto const& right_side_floats() const { return m_right_floats; }
|
2020-12-05 16:10:39 -03:00
|
|
|
|
2023-09-22 10:33:25 -03:00
|
|
|
bool box_should_avoid_floats_because_it_establishes_fc(Box const&);
|
2024-09-10 20:03:02 -03:00
|
|
|
void compute_width(Box const&, AvailableSpace const&);
|
2025-07-28 16:24:30 -03:00
|
|
|
void avoid_float_intrusions(Box const&, AvailableSpace const&);
|
2021-05-30 16:57:13 -03:00
|
|
|
|
2021-10-15 15:52:10 -03:00
|
|
|
// https://www.w3.org/TR/css-display/#block-formatting-context-root
|
2021-10-06 16:53:25 -03:00
|
|
|
BlockContainer const& root() const { return static_cast<BlockContainer const&>(context_box()); }
|
2021-10-06 14:47:10 -03:00
|
|
|
|
2022-02-12 15:54:09 -03:00
|
|
|
virtual void parent_context_did_dimension_child_root_box() override;
|
|
|
|
|
|
2024-09-27 12:49:02 -03:00
|
|
|
void resolve_used_height_if_not_treated_as_auto(Box const&, AvailableSpace const&);
|
|
|
|
|
void resolve_used_height_if_treated_as_auto(Box const&, AvailableSpace const&, FormattingContext const* box_formatting_context = nullptr);
|
2022-02-12 15:54:09 -03:00
|
|
|
|
2025-03-26 08:55:39 -03:00
|
|
|
template<typename Callback>
|
|
|
|
|
void for_each_floating_box(Callback callback)
|
|
|
|
|
{
|
|
|
|
|
for (auto const& floating_box : m_left_floats.all_boxes) {
|
|
|
|
|
if (callback(*floating_box) == IterationDecision::Break)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (auto const& floating_box : m_right_floats.all_boxes) {
|
|
|
|
|
if (callback(*floating_box) == IterationDecision::Break)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-13 10:37:15 -03:00
|
|
|
SpaceUsedAndContainingMarginForFloats space_used_and_containing_margin_for_floats(CSSPixels y) const;
|
2024-01-23 17:10:17 -03:00
|
|
|
[[nodiscard]] SpaceUsedByFloats intrusion_by_floats_into_box(Box const&, CSSPixels y_in_box) const;
|
|
|
|
|
[[nodiscard]] SpaceUsedByFloats intrusion_by_floats_into_box(LayoutState::UsedValues const&, CSSPixels y_in_box) const;
|
2022-03-18 10:44:36 -03:00
|
|
|
|
2023-03-19 05:57:31 -03:00
|
|
|
virtual CSSPixels greatest_child_width(Box const&) const override;
|
2022-03-17 08:30:30 -03:00
|
|
|
|
2024-09-10 20:03:02 -03:00
|
|
|
void layout_floating_box(Box const& child, BlockContainer const& containing_block, AvailableSpace const&, CSSPixels y, LineBuilder* = nullptr);
|
2022-03-22 15:18:05 -03:00
|
|
|
|
2024-09-10 20:03:02 -03:00
|
|
|
void layout_block_level_box(Box const&, BlockContainer const&, CSSPixels& bottom_of_lowest_margin_box, AvailableSpace const&);
|
2022-07-09 15:14:13 -03:00
|
|
|
|
2024-11-11 11:12:09 -03:00
|
|
|
void resolve_vertical_box_model_metrics(Box const&, CSSPixels width_of_containing_block);
|
2023-06-06 23:10:55 -03:00
|
|
|
|
2023-07-19 20:44:40 -03:00
|
|
|
enum class DidIntroduceClearance {
|
|
|
|
|
Yes,
|
|
|
|
|
No,
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-29 01:05:45 -03:00
|
|
|
[[nodiscard]] DidIntroduceClearance clear_floating_boxes(Node const& child_box, Optional<InlineFormattingContext&> inline_formatting_context);
|
2023-07-19 20:44:40 -03:00
|
|
|
|
|
|
|
|
void reset_margin_state() { m_margin_state.reset(); }
|
|
|
|
|
|
2025-05-18 14:03:57 -03:00
|
|
|
struct FloatingBox {
|
|
|
|
|
GC::Ref<Box const> box;
|
|
|
|
|
|
|
|
|
|
LayoutState::UsedValues& used_values;
|
|
|
|
|
|
|
|
|
|
// Offset from left/right edge to the left content edge of `box`.
|
|
|
|
|
CSSPixels offset_from_edge { 0 };
|
|
|
|
|
|
|
|
|
|
// Top margin edge of `box`.
|
|
|
|
|
CSSPixels top_margin_edge { 0 };
|
|
|
|
|
|
|
|
|
|
// Bottom margin edge of `box`.
|
|
|
|
|
CSSPixels bottom_margin_edge { 0 };
|
|
|
|
|
|
|
|
|
|
CSSPixelRect margin_box_rect_in_root_coordinate_space;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Optional<FloatingBox&> last_inserted_float() { return m_last_inserted_float; }
|
|
|
|
|
|
2020-11-22 09:38:18 -03:00
|
|
|
private:
|
2022-11-04 14:37:12 -03:00
|
|
|
CSSPixels compute_auto_height_for_block_level_element(Box const&, AvailableSpace const&);
|
2022-12-28 15:42:46 -03:00
|
|
|
|
2022-09-27 10:29:17 -03:00
|
|
|
void compute_width_for_floating_box(Box const&, AvailableSpace const&);
|
2020-11-22 09:38:18 -03:00
|
|
|
|
2023-06-08 12:47:50 -03:00
|
|
|
void compute_width_for_block_level_replaced_element_in_normal_flow(Box const&, AvailableSpace const&);
|
2021-03-08 18:49:34 -03:00
|
|
|
|
2024-09-10 20:03:02 -03:00
|
|
|
void layout_viewport(AvailableSpace const&);
|
2020-11-22 09:38:18 -03:00
|
|
|
|
2024-09-10 20:03:02 -03:00
|
|
|
void layout_block_level_children(BlockContainer const&, AvailableSpace const&);
|
|
|
|
|
void layout_inline_children(BlockContainer const&, AvailableSpace const&);
|
2020-11-22 09:38:18 -03:00
|
|
|
|
2022-09-27 10:29:17 -03:00
|
|
|
void place_block_level_element_in_normal_flow_horizontally(Box const& child_box, AvailableSpace const&);
|
2022-11-04 14:37:12 -03:00
|
|
|
void place_block_level_element_in_normal_flow_vertically(Box const&, CSSPixels y);
|
2020-12-06 15:48:02 -03:00
|
|
|
|
2024-07-03 15:48:02 -03:00
|
|
|
void ensure_sizes_correct_for_left_offset_calculation(ListItemBox const&);
|
|
|
|
|
void layout_list_item_marker(ListItemBox const&, CSSPixels const& left_space_before_list_item_elements_formatted);
|
2022-02-20 21:43:37 -03:00
|
|
|
|
2023-05-31 05:18:34 -03:00
|
|
|
void measure_scrollable_overflow(Box const&, CSSPixels& bottom_edge, CSSPixels& right_edge) const;
|
|
|
|
|
|
2022-01-22 21:19:28 -03:00
|
|
|
enum class FloatSide {
|
|
|
|
|
Left,
|
|
|
|
|
Right,
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-22 11:19:18 -03:00
|
|
|
struct FloatSideData {
|
2022-03-18 10:44:36 -03:00
|
|
|
// Floating boxes currently accumulating on this side.
|
|
|
|
|
Vector<FloatingBox&> current_boxes;
|
|
|
|
|
|
|
|
|
|
// Combined width of boxes currently accumulating on this side.
|
|
|
|
|
// This is the innermost margin of the innermost floating box.
|
2022-11-04 14:37:12 -03:00
|
|
|
CSSPixels current_width { 0 };
|
2022-03-18 10:44:36 -03:00
|
|
|
|
|
|
|
|
// Highest value of `m_current_width` we've seen.
|
2022-11-04 14:37:12 -03:00
|
|
|
CSSPixels max_width { 0 };
|
2022-03-18 10:44:36 -03:00
|
|
|
|
|
|
|
|
// All floating boxes encountered thus far within this BFC.
|
|
|
|
|
Vector<NonnullOwnPtr<FloatingBox>> all_boxes;
|
|
|
|
|
|
|
|
|
|
// Current Y offset from BFC root top.
|
2022-11-04 14:37:12 -03:00
|
|
|
CSSPixels y_offset { 0 };
|
2022-03-18 10:44:36 -03:00
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
|
{
|
|
|
|
|
current_boxes.clear();
|
|
|
|
|
current_width = 0;
|
|
|
|
|
}
|
2022-01-22 11:19:18 -03:00
|
|
|
};
|
|
|
|
|
|
2025-09-07 09:50:56 -03:00
|
|
|
class BlockMarginState {
|
|
|
|
|
public:
|
2022-11-04 14:37:12 -03:00
|
|
|
void add_margin(CSSPixels margin)
|
2022-12-25 11:13:21 -03:00
|
|
|
{
|
2024-10-20 17:03:00 -03:00
|
|
|
if (margin < 0) {
|
2025-09-07 09:50:56 -03:00
|
|
|
m_current_negative_collapsible_margin = min(margin, m_current_negative_collapsible_margin);
|
2024-10-20 17:03:00 -03:00
|
|
|
} else {
|
2025-09-07 09:50:56 -03:00
|
|
|
m_current_positive_collapsible_margin = max(margin, m_current_positive_collapsible_margin);
|
2024-10-20 17:03:00 -03:00
|
|
|
}
|
2022-12-25 11:13:21 -03:00
|
|
|
}
|
|
|
|
|
|
2024-05-17 21:14:06 -03:00
|
|
|
void register_block_container_y_position_update_callback(ESCAPING Function<void(CSSPixels)> callback)
|
2022-12-25 08:01:14 -03:00
|
|
|
{
|
2025-09-07 09:50:56 -03:00
|
|
|
m_block_container_y_position_update_callback = move(callback);
|
2022-12-25 08:01:14 -03:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 09:50:56 -03:00
|
|
|
CSSPixels current_collapsed_margin() const
|
|
|
|
|
{
|
|
|
|
|
return m_current_positive_collapsible_margin + m_current_negative_collapsible_margin;
|
|
|
|
|
}
|
2022-12-25 11:13:21 -03:00
|
|
|
|
2022-12-25 08:01:14 -03:00
|
|
|
bool has_block_container_waiting_for_final_y_position() const
|
|
|
|
|
{
|
2025-09-07 09:50:56 -03:00
|
|
|
return static_cast<bool>(m_block_container_y_position_update_callback);
|
2022-12-25 08:01:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void update_block_waiting_for_final_y_position() const
|
|
|
|
|
{
|
2025-09-07 09:50:56 -03:00
|
|
|
if (m_block_container_y_position_update_callback) {
|
2022-11-04 14:37:12 -03:00
|
|
|
CSSPixels collapsed_margin = current_collapsed_margin();
|
2025-09-07 09:50:56 -03:00
|
|
|
m_block_container_y_position_update_callback(collapsed_margin);
|
2022-12-25 08:01:14 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-25 11:13:21 -03:00
|
|
|
void reset()
|
|
|
|
|
{
|
2025-09-07 09:50:56 -03:00
|
|
|
m_block_container_y_position_update_callback = {};
|
|
|
|
|
m_current_negative_collapsible_margin = 0;
|
|
|
|
|
m_current_positive_collapsible_margin = 0;
|
2022-12-25 11:13:21 -03:00
|
|
|
}
|
2025-09-07 09:50:56 -03:00
|
|
|
|
|
|
|
|
bool box_last_in_flow_child_margin_bottom_collapsed() const { return m_box_last_in_flow_child_margin_bottom_collapsed; }
|
|
|
|
|
void set_box_last_in_flow_child_margin_bottom_collapsed(bool v) { m_box_last_in_flow_child_margin_bottom_collapsed = v; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
CSSPixels m_current_positive_collapsible_margin;
|
|
|
|
|
CSSPixels m_current_negative_collapsible_margin;
|
|
|
|
|
Function<void(CSSPixels)> m_block_container_y_position_update_callback;
|
|
|
|
|
bool m_box_last_in_flow_child_margin_bottom_collapsed { false };
|
2022-12-25 11:13:21 -03:00
|
|
|
};
|
|
|
|
|
|
2023-02-10 11:29:38 -03:00
|
|
|
Optional<CSSPixels> m_y_offset_of_current_block_container;
|
|
|
|
|
|
2022-12-25 11:13:21 -03:00
|
|
|
BlockMarginState m_margin_state;
|
|
|
|
|
|
2022-01-22 11:19:18 -03:00
|
|
|
FloatSideData m_left_floats;
|
|
|
|
|
FloatSideData m_right_floats;
|
2025-05-18 14:03:57 -03:00
|
|
|
Optional<FloatingBox&> m_last_inserted_float;
|
2022-02-12 15:54:09 -03:00
|
|
|
|
2022-02-19 12:42:02 -03:00
|
|
|
bool m_was_notified_after_parent_dimensioned_my_root_box { false };
|
2020-11-22 09:38:18 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|