BlockFormattingContext.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Vector.h>
  8. #include <LibWeb/Forward.h>
  9. #include <LibWeb/Layout/BlockContainer.h>
  10. #include <LibWeb/Layout/FormattingContext.h>
  11. #include <LibWeb/Layout/InlineFormattingContext.h>
  12. namespace Web::Layout {
  13. class LineBuilder;
  14. // https://www.w3.org/TR/css-display/#block-formatting-context
  15. class BlockFormattingContext : public FormattingContext {
  16. public:
  17. explicit BlockFormattingContext(LayoutState&, BlockContainer const&, FormattingContext* parent);
  18. ~BlockFormattingContext();
  19. virtual void run(Box const&, LayoutMode, AvailableSpace const&) override;
  20. virtual CSSPixels automatic_content_width() const override;
  21. virtual CSSPixels automatic_content_height() const override;
  22. auto const& left_side_floats() const { return m_left_floats; }
  23. auto const& right_side_floats() const { return m_right_floats; }
  24. bool box_should_avoid_floats_because_it_establishes_fc(Box const&);
  25. void compute_width(Box const&, AvailableSpace const&, LayoutMode = LayoutMode::Normal);
  26. // https://www.w3.org/TR/css-display/#block-formatting-context-root
  27. BlockContainer const& root() const { return static_cast<BlockContainer const&>(context_box()); }
  28. virtual void parent_context_did_dimension_child_root_box() override;
  29. void compute_height(Box const&, AvailableSpace const&);
  30. void add_absolutely_positioned_box(Box const& box) { m_absolutely_positioned_boxes.append(box); }
  31. SpaceUsedAndContainingMarginForFloats space_used_and_containing_margin_for_floats(CSSPixels y) const;
  32. [[nodiscard]] SpaceUsedByFloats intrusion_by_floats_into_box(Box const&, CSSPixels y_in_box) const;
  33. [[nodiscard]] SpaceUsedByFloats intrusion_by_floats_into_box(LayoutState::UsedValues const&, CSSPixels y_in_box) const;
  34. virtual CSSPixels greatest_child_width(Box const&) const override;
  35. void layout_floating_box(Box const& child, BlockContainer const& containing_block, LayoutMode, AvailableSpace const&, CSSPixels y, LineBuilder* = nullptr);
  36. void layout_block_level_box(Box const&, BlockContainer const&, LayoutMode, CSSPixels& bottom_of_lowest_margin_box, AvailableSpace const&);
  37. void resolve_vertical_box_model_metrics(Box const&);
  38. enum class DidIntroduceClearance {
  39. Yes,
  40. No,
  41. };
  42. [[nodiscard]] DidIntroduceClearance clear_floating_boxes(Node const& child_box, Optional<InlineFormattingContext&> inline_formatting_context);
  43. void reset_margin_state() { m_margin_state.reset(); }
  44. private:
  45. CSSPixels compute_auto_height_for_block_level_element(Box const&, AvailableSpace const&);
  46. void compute_width_for_floating_box(Box const&, AvailableSpace const&);
  47. void compute_width_for_block_level_replaced_element_in_normal_flow(Box const&, AvailableSpace const&);
  48. CSSPixels compute_table_box_width_inside_table_wrapper(Box const&, AvailableSpace const&);
  49. void layout_viewport(LayoutMode, AvailableSpace const&);
  50. void layout_block_level_children(BlockContainer const&, LayoutMode, AvailableSpace const&);
  51. void layout_inline_children(BlockContainer const&, LayoutMode, AvailableSpace const&);
  52. void place_block_level_element_in_normal_flow_horizontally(Box const& child_box, AvailableSpace const&);
  53. void place_block_level_element_in_normal_flow_vertically(Box const&, CSSPixels y);
  54. void layout_list_item_marker(ListItemBox const&);
  55. void measure_scrollable_overflow(Box const&, CSSPixels& bottom_edge, CSSPixels& right_edge) const;
  56. enum class FloatSide {
  57. Left,
  58. Right,
  59. };
  60. struct FloatingBox {
  61. JS::NonnullGCPtr<Box const> box;
  62. LayoutState::UsedValues& used_values;
  63. // Offset from left/right edge to the left content edge of `box`.
  64. CSSPixels offset_from_edge { 0 };
  65. // Top margin edge of `box`.
  66. CSSPixels top_margin_edge { 0 };
  67. // Bottom margin edge of `box`.
  68. CSSPixels bottom_margin_edge { 0 };
  69. };
  70. struct FloatSideData {
  71. // Floating boxes currently accumulating on this side.
  72. Vector<FloatingBox&> current_boxes;
  73. // Combined width of boxes currently accumulating on this side.
  74. // This is the innermost margin of the innermost floating box.
  75. CSSPixels current_width { 0 };
  76. // Highest value of `m_current_width` we've seen.
  77. CSSPixels max_width { 0 };
  78. // All floating boxes encountered thus far within this BFC.
  79. Vector<NonnullOwnPtr<FloatingBox>> all_boxes;
  80. // Current Y offset from BFC root top.
  81. CSSPixels y_offset { 0 };
  82. void clear()
  83. {
  84. current_boxes.clear();
  85. current_width = 0;
  86. }
  87. };
  88. struct BlockMarginState {
  89. Vector<CSSPixels> current_collapsible_margins;
  90. Function<void(CSSPixels)> block_container_y_position_update_callback;
  91. bool box_last_in_flow_child_margin_bottom_collapsed { false };
  92. void add_margin(CSSPixels margin)
  93. {
  94. current_collapsible_margins.append(margin);
  95. }
  96. void register_block_container_y_position_update_callback(Function<void(CSSPixels)> callback)
  97. {
  98. block_container_y_position_update_callback = move(callback);
  99. }
  100. CSSPixels current_collapsed_margin() const;
  101. bool has_block_container_waiting_for_final_y_position() const
  102. {
  103. return static_cast<bool>(block_container_y_position_update_callback);
  104. }
  105. void update_block_waiting_for_final_y_position() const
  106. {
  107. if (block_container_y_position_update_callback) {
  108. CSSPixels collapsed_margin = current_collapsed_margin();
  109. block_container_y_position_update_callback(collapsed_margin);
  110. }
  111. }
  112. void reset()
  113. {
  114. block_container_y_position_update_callback = {};
  115. current_collapsible_margins.clear();
  116. }
  117. };
  118. Optional<CSSPixels> m_y_offset_of_current_block_container;
  119. BlockMarginState m_margin_state;
  120. FloatSideData m_left_floats;
  121. FloatSideData m_right_floats;
  122. Vector<JS::NonnullGCPtr<Box const>> m_absolutely_positioned_boxes;
  123. bool m_was_notified_after_parent_dimensioned_my_root_box { false };
  124. };
  125. }