2020-11-22 13:38:18 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
|
2020-11-22 13:38:18 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-22 13:38:18 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-12-05 20:10:39 +01:00
|
|
|
#include <AK/Vector.h>
|
2020-11-22 13:38:18 +01:00
|
|
|
#include <LibWeb/Forward.h>
|
2021-10-06 21:53:25 +02:00
|
|
|
#include <LibWeb/Layout/BlockContainer.h>
|
2020-11-22 13:38:18 +01:00
|
|
|
#include <LibWeb/Layout/FormattingContext.h>
|
2023-07-29 04:05:45 +00:00
|
|
|
#include <LibWeb/Layout/InlineFormattingContext.h>
|
2020-11-22 13:38:18 +01:00
|
|
|
|
|
|
|
namespace Web::Layout {
|
|
|
|
|
2022-03-22 19:18:05 +01:00
|
|
|
class LineBuilder;
|
|
|
|
|
2021-10-15 19:52:10 +01:00
|
|
|
// https://www.w3.org/TR/css-display/#block-formatting-context
|
2020-11-22 13:38:18 +01:00
|
|
|
class BlockFormattingContext : public FormattingContext {
|
|
|
|
public:
|
2024-09-11 01:03:02 +02:00
|
|
|
explicit BlockFormattingContext(LayoutState&, LayoutMode layout_mode, BlockContainer const&, FormattingContext* parent);
|
2020-11-22 13:38:18 +01:00
|
|
|
~BlockFormattingContext();
|
|
|
|
|
2024-09-11 01:03:02 +02:00
|
|
|
virtual void run(AvailableSpace const&) override;
|
2023-03-19 09:57:31 +01:00
|
|
|
virtual CSSPixels automatic_content_width() const override;
|
2022-11-23 17:46:10 +00:00
|
|
|
virtual CSSPixels automatic_content_height() const override;
|
2020-11-22 13:38:18 +01:00
|
|
|
|
2022-01-22 15:19:18 +01:00
|
|
|
auto const& left_side_floats() const { return m_left_floats; }
|
|
|
|
auto const& right_side_floats() const { return m_right_floats; }
|
2020-12-05 20:10:39 +01:00
|
|
|
|
2023-09-22 15:33:25 +02:00
|
|
|
bool box_should_avoid_floats_because_it_establishes_fc(Box const&);
|
2024-09-11 01:03:02 +02:00
|
|
|
void compute_width(Box const&, AvailableSpace const&);
|
2021-05-30 21:57:13 +02:00
|
|
|
|
2021-10-15 19:52:10 +01:00
|
|
|
// https://www.w3.org/TR/css-display/#block-formatting-context-root
|
2021-10-06 21:53:25 +02:00
|
|
|
BlockContainer const& root() const { return static_cast<BlockContainer const&>(context_box()); }
|
2021-10-06 19:47:10 +02:00
|
|
|
|
2022-02-12 19:54:09 +01:00
|
|
|
virtual void parent_context_did_dimension_child_root_box() override;
|
|
|
|
|
2024-09-27 17:49:02 +02: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 19:54:09 +01:00
|
|
|
|
2023-06-13 13:37:15 +00:00
|
|
|
SpaceUsedAndContainingMarginForFloats space_used_and_containing_margin_for_floats(CSSPixels y) const;
|
2024-01-23 21:10:17 +01: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 14:44:36 +01:00
|
|
|
|
2023-03-19 09:57:31 +01:00
|
|
|
virtual CSSPixels greatest_child_width(Box const&) const override;
|
2022-03-17 12:30:30 +01:00
|
|
|
|
2024-09-11 01:03:02 +02:00
|
|
|
void layout_floating_box(Box const& child, BlockContainer const& containing_block, AvailableSpace const&, CSSPixels y, LineBuilder* = nullptr);
|
2022-03-22 19:18:05 +01:00
|
|
|
|
2024-09-11 01:03:02 +02:00
|
|
|
void layout_block_level_box(Box const&, BlockContainer const&, CSSPixels& bottom_of_lowest_margin_box, AvailableSpace const&);
|
2022-07-09 20:14:13 +02:00
|
|
|
|
2024-11-11 15:12:09 +01:00
|
|
|
void resolve_vertical_box_model_metrics(Box const&, CSSPixels width_of_containing_block);
|
2023-06-07 02:10:55 +00:00
|
|
|
|
2023-07-19 23:44:40 +00:00
|
|
|
enum class DidIntroduceClearance {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
|
|
|
|
2023-07-29 04:05:45 +00:00
|
|
|
[[nodiscard]] DidIntroduceClearance clear_floating_boxes(Node const& child_box, Optional<InlineFormattingContext&> inline_formatting_context);
|
2023-07-19 23:44:40 +00:00
|
|
|
|
|
|
|
void reset_margin_state() { m_margin_state.reset(); }
|
|
|
|
|
2020-11-22 13:38:18 +01:00
|
|
|
private:
|
2022-11-04 17:37:12 +00:00
|
|
|
CSSPixels compute_auto_height_for_block_level_element(Box const&, AvailableSpace const&);
|
2022-12-28 21:42:46 +03:00
|
|
|
|
2022-09-27 15:29:17 +02:00
|
|
|
void compute_width_for_floating_box(Box const&, AvailableSpace const&);
|
2020-11-22 13:38:18 +01:00
|
|
|
|
2023-06-08 16:47:50 +01:00
|
|
|
void compute_width_for_block_level_replaced_element_in_normal_flow(Box const&, AvailableSpace const&);
|
2021-03-08 22:49:34 +01:00
|
|
|
|
2024-09-11 01:03:02 +02:00
|
|
|
void layout_viewport(AvailableSpace const&);
|
2020-11-22 13:38:18 +01:00
|
|
|
|
2024-09-11 01:03:02 +02:00
|
|
|
void layout_block_level_children(BlockContainer const&, AvailableSpace const&);
|
|
|
|
void layout_inline_children(BlockContainer const&, AvailableSpace const&);
|
2020-11-22 13:38:18 +01:00
|
|
|
|
2022-09-27 15:29:17 +02:00
|
|
|
void place_block_level_element_in_normal_flow_horizontally(Box const& child_box, AvailableSpace const&);
|
2022-11-04 17:37:12 +00:00
|
|
|
void place_block_level_element_in_normal_flow_vertically(Box const&, CSSPixels y);
|
2020-12-06 19:48:02 +01:00
|
|
|
|
2024-07-03 20:48:02 +02: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-21 01:43:37 +01:00
|
|
|
|
2023-05-31 10:18:34 +02:00
|
|
|
void measure_scrollable_overflow(Box const&, CSSPixels& bottom_edge, CSSPixels& right_edge) const;
|
|
|
|
|
2022-01-23 01:19:28 +01:00
|
|
|
enum class FloatSide {
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
};
|
|
|
|
|
2022-03-18 14:44:36 +01:00
|
|
|
struct FloatingBox {
|
2023-02-26 16:09:02 -07:00
|
|
|
JS::NonnullGCPtr<Box const> box;
|
2024-01-17 12:19:52 +01:00
|
|
|
|
|
|
|
LayoutState::UsedValues& used_values;
|
|
|
|
|
2022-03-18 14:44:36 +01:00
|
|
|
// Offset from left/right edge to the left content edge of `box`.
|
2022-11-04 17:37:12 +00:00
|
|
|
CSSPixels offset_from_edge { 0 };
|
2022-03-18 14:44:36 +01:00
|
|
|
|
|
|
|
// Top margin edge of `box`.
|
2022-11-04 17:37:12 +00:00
|
|
|
CSSPixels top_margin_edge { 0 };
|
2022-03-18 14:44:36 +01:00
|
|
|
|
|
|
|
// Bottom margin edge of `box`.
|
2022-11-04 17:37:12 +00:00
|
|
|
CSSPixels bottom_margin_edge { 0 };
|
2022-03-18 14:44:36 +01:00
|
|
|
};
|
|
|
|
|
2022-01-22 15:19:18 +01:00
|
|
|
struct FloatSideData {
|
2022-03-18 14:44:36 +01: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 17:37:12 +00:00
|
|
|
CSSPixels current_width { 0 };
|
2022-03-18 14:44:36 +01:00
|
|
|
|
|
|
|
// Highest value of `m_current_width` we've seen.
|
2022-11-04 17:37:12 +00:00
|
|
|
CSSPixels max_width { 0 };
|
2022-03-18 14:44:36 +01: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 17:37:12 +00:00
|
|
|
CSSPixels y_offset { 0 };
|
2022-03-18 14:44:36 +01:00
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
current_boxes.clear();
|
|
|
|
current_width = 0;
|
|
|
|
}
|
2022-01-22 15:19:18 +01:00
|
|
|
};
|
|
|
|
|
2022-12-25 17:13:21 +03:00
|
|
|
struct BlockMarginState {
|
2022-11-04 17:37:12 +00:00
|
|
|
Vector<CSSPixels> current_collapsible_margins;
|
|
|
|
Function<void(CSSPixels)> block_container_y_position_update_callback;
|
2022-12-28 21:42:46 +03:00
|
|
|
bool box_last_in_flow_child_margin_bottom_collapsed { false };
|
2022-12-25 17:13:21 +03:00
|
|
|
|
2022-11-04 17:37:12 +00:00
|
|
|
void add_margin(CSSPixels margin)
|
2022-12-25 17:13:21 +03:00
|
|
|
{
|
|
|
|
current_collapsible_margins.append(margin);
|
|
|
|
}
|
|
|
|
|
2024-05-17 17:14:06 -07:00
|
|
|
void register_block_container_y_position_update_callback(ESCAPING Function<void(CSSPixels)> callback)
|
2022-12-25 14:01:14 +03:00
|
|
|
{
|
|
|
|
block_container_y_position_update_callback = move(callback);
|
|
|
|
}
|
|
|
|
|
2022-11-04 17:37:12 +00:00
|
|
|
CSSPixels current_collapsed_margin() const;
|
2022-12-25 17:13:21 +03:00
|
|
|
|
2022-12-25 14:01:14 +03:00
|
|
|
bool has_block_container_waiting_for_final_y_position() const
|
|
|
|
{
|
|
|
|
return static_cast<bool>(block_container_y_position_update_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_block_waiting_for_final_y_position() const
|
|
|
|
{
|
|
|
|
if (block_container_y_position_update_callback) {
|
2022-11-04 17:37:12 +00:00
|
|
|
CSSPixels collapsed_margin = current_collapsed_margin();
|
2022-12-25 14:01:14 +03:00
|
|
|
block_container_y_position_update_callback(collapsed_margin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-25 17:13:21 +03:00
|
|
|
void reset()
|
|
|
|
{
|
2022-12-25 14:01:14 +03:00
|
|
|
block_container_y_position_update_callback = {};
|
2022-12-25 17:13:21 +03:00
|
|
|
current_collapsible_margins.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-10 17:29:38 +03:00
|
|
|
Optional<CSSPixels> m_y_offset_of_current_block_container;
|
|
|
|
|
2022-12-25 17:13:21 +03:00
|
|
|
BlockMarginState m_margin_state;
|
|
|
|
|
2022-01-22 15:19:18 +01:00
|
|
|
FloatSideData m_left_floats;
|
|
|
|
FloatSideData m_right_floats;
|
2022-02-12 19:54:09 +01:00
|
|
|
|
2022-02-19 16:42:02 +01:00
|
|
|
bool m_was_notified_after_parent_dimensioned_my_root_box { false };
|
2020-11-22 13:38:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|