BlockFormattingContext.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/Length.h>
  7. #include <LibWeb/DOM/Node.h>
  8. #include <LibWeb/HTML/BrowsingContext.h>
  9. #include <LibWeb/Layout/BlockContainer.h>
  10. #include <LibWeb/Layout/BlockFormattingContext.h>
  11. #include <LibWeb/Layout/Box.h>
  12. #include <LibWeb/Layout/InitialContainingBlock.h>
  13. #include <LibWeb/Layout/InlineFormattingContext.h>
  14. #include <LibWeb/Layout/ListItemBox.h>
  15. #include <LibWeb/Layout/ListItemMarkerBox.h>
  16. #include <LibWeb/Layout/ReplacedBox.h>
  17. namespace Web::Layout {
  18. BlockFormattingContext::BlockFormattingContext(FormattingState& state, BlockContainer const& root, FormattingContext* parent)
  19. : FormattingContext(Type::Block, state, root, parent)
  20. {
  21. }
  22. BlockFormattingContext::~BlockFormattingContext()
  23. {
  24. if (!m_was_notified_after_parent_dimensioned_my_root_box) {
  25. // HACK: The parent formatting context never notified us after assigning dimensions to our root box.
  26. // Pretend that it did anyway, to make sure absolutely positioned children get laid out.
  27. // FIXME: Get rid of this hack once parent contexts behave properly.
  28. parent_context_did_dimension_child_root_box();
  29. }
  30. }
  31. bool BlockFormattingContext::is_initial() const
  32. {
  33. return is<InitialContainingBlock>(root());
  34. }
  35. void BlockFormattingContext::run(Box const&, LayoutMode layout_mode)
  36. {
  37. if (is_initial()) {
  38. layout_initial_containing_block(layout_mode);
  39. return;
  40. }
  41. if (root().children_are_inline())
  42. layout_inline_children(root(), layout_mode);
  43. else
  44. layout_block_level_children(root(), layout_mode);
  45. }
  46. void BlockFormattingContext::parent_context_did_dimension_child_root_box()
  47. {
  48. m_was_notified_after_parent_dimensioned_my_root_box = true;
  49. // Left-side floats: offset_from_edge is from left edge (0) to left content edge of floating_box.
  50. for (auto& floating_box : m_left_floats.all_boxes) {
  51. auto& box_state = m_state.get_mutable(floating_box->box);
  52. box_state.offset.set_x(floating_box->offset_from_edge);
  53. }
  54. // Right-side floats: offset_from_edge is from right edge (float_containing_block_width) to the left content edge of floating_box.
  55. for (auto& floating_box : m_right_floats.all_boxes) {
  56. auto float_containing_block_width = m_state.get(*floating_box->box.containing_block()).content_width;
  57. auto& box_state = m_state.get_mutable(floating_box->box);
  58. box_state.offset.set_x(float_containing_block_width - floating_box->offset_from_edge);
  59. }
  60. // We can also layout absolutely positioned boxes within this BFC.
  61. for (auto& box : m_absolutely_positioned_boxes)
  62. layout_absolutely_positioned_element(box);
  63. }
  64. void BlockFormattingContext::compute_width(Box const& box, LayoutMode layout_mode)
  65. {
  66. if (box.is_absolutely_positioned()) {
  67. compute_width_for_absolutely_positioned_element(box);
  68. return;
  69. }
  70. if (is<ReplacedBox>(box)) {
  71. // FIXME: This should not be done *by* ReplacedBox
  72. auto& replaced = verify_cast<ReplacedBox>(box);
  73. // FIXME: This const_cast is gross.
  74. const_cast<ReplacedBox&>(replaced).prepare_for_replaced_layout();
  75. compute_width_for_block_level_replaced_element_in_normal_flow(replaced);
  76. return;
  77. }
  78. if (box.is_floating()) {
  79. compute_width_for_floating_box(box, layout_mode);
  80. return;
  81. }
  82. auto const& computed_values = box.computed_values();
  83. float width_of_containing_block = m_state.get(*box.containing_block()).content_width;
  84. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  85. auto zero_value = CSS::Length::make_px(0);
  86. auto margin_left = CSS::Length::make_auto();
  87. auto margin_right = CSS::Length::make_auto();
  88. const auto padding_left = computed_values.padding().left.resolved(box, width_of_containing_block_as_length).resolved(box);
  89. const auto padding_right = computed_values.padding().right.resolved(box, width_of_containing_block_as_length).resolved(box);
  90. auto try_compute_width = [&](const auto& a_width) {
  91. CSS::Length width = a_width;
  92. margin_left = computed_values.margin().left.resolved(box, width_of_containing_block_as_length).resolved(box);
  93. margin_right = computed_values.margin().right.resolved(box, width_of_containing_block_as_length).resolved(box);
  94. float total_px = computed_values.border_left().width + computed_values.border_right().width;
  95. for (auto& value : { margin_left, padding_left, width, padding_right, margin_right }) {
  96. total_px += value.to_px(box);
  97. }
  98. if (!box.is_inline()) {
  99. // 10.3.3 Block-level, non-replaced elements in normal flow
  100. // If 'width' is not 'auto' and 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' (plus any of 'margin-left' or 'margin-right' that are not 'auto') is larger than the width of the containing block, then any 'auto' values for 'margin-left' or 'margin-right' are, for the following rules, treated as zero.
  101. if (width.is_auto() && total_px > width_of_containing_block) {
  102. if (margin_left.is_auto())
  103. margin_left = zero_value;
  104. if (margin_right.is_auto())
  105. margin_right = zero_value;
  106. }
  107. // 10.3.3 cont'd.
  108. auto underflow_px = width_of_containing_block - total_px;
  109. if (width.is_auto()) {
  110. if (margin_left.is_auto())
  111. margin_left = zero_value;
  112. if (margin_right.is_auto())
  113. margin_right = zero_value;
  114. if (underflow_px >= 0) {
  115. width = CSS::Length(underflow_px, CSS::Length::Type::Px);
  116. } else {
  117. width = zero_value;
  118. margin_right = CSS::Length(margin_right.to_px(box) + underflow_px, CSS::Length::Type::Px);
  119. }
  120. } else {
  121. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  122. margin_right = CSS::Length(margin_right.to_px(box) + underflow_px, CSS::Length::Type::Px);
  123. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  124. margin_right = CSS::Length(underflow_px, CSS::Length::Type::Px);
  125. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  126. margin_left = CSS::Length(underflow_px, CSS::Length::Type::Px);
  127. } else { // margin_left.is_auto() && margin_right.is_auto()
  128. auto half_of_the_underflow = CSS::Length(underflow_px / 2, CSS::Length::Type::Px);
  129. margin_left = half_of_the_underflow;
  130. margin_right = half_of_the_underflow;
  131. }
  132. }
  133. } else if (box.is_inline_block()) {
  134. // 10.3.9 'Inline-block', non-replaced elements in normal flow
  135. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  136. if (margin_left.is_auto())
  137. margin_left = zero_value;
  138. if (margin_right.is_auto())
  139. margin_right = zero_value;
  140. // If 'width' is 'auto', the used value is the shrink-to-fit width as for floating elements.
  141. if (width.is_auto()) {
  142. // Find the available width: in this case, this is the width of the containing
  143. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  144. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  145. float available_width = width_of_containing_block
  146. - margin_left.to_px(box) - computed_values.border_left().width - padding_left.to_px(box)
  147. - padding_right.to_px(box) - computed_values.border_right().width - margin_right.to_px(box);
  148. auto result = calculate_shrink_to_fit_widths(box);
  149. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  150. width = CSS::Length(min(max(result.preferred_minimum_width, available_width), result.preferred_width), CSS::Length::Type::Px);
  151. }
  152. }
  153. return width;
  154. };
  155. auto specified_width = computed_values.width().has_value() ? computed_values.width()->resolved(box, width_of_containing_block_as_length).resolved(box) : CSS::Length::make_auto();
  156. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  157. auto used_width = try_compute_width(specified_width);
  158. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  159. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  160. auto specified_max_width = computed_values.max_width().has_value() ? computed_values.max_width()->resolved(box, width_of_containing_block_as_length).resolved(box) : CSS::Length::make_auto();
  161. if (!specified_max_width.is_auto()) {
  162. if (used_width.to_px(box) > specified_max_width.to_px(box)) {
  163. used_width = try_compute_width(specified_max_width);
  164. }
  165. }
  166. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  167. // but this time using the value of 'min-width' as the computed value for 'width'.
  168. auto specified_min_width = computed_values.min_width().has_value() ? computed_values.min_width()->resolved(box, width_of_containing_block_as_length).resolved(box) : CSS::Length::make_auto();
  169. if (!specified_min_width.is_auto()) {
  170. if (used_width.to_px(box) < specified_min_width.to_px(box)) {
  171. used_width = try_compute_width(specified_min_width);
  172. }
  173. }
  174. auto& box_state = m_state.get_mutable(box);
  175. box_state.content_width = used_width.to_px(box);
  176. box_state.margin_left = margin_left.to_px(box);
  177. box_state.margin_right = margin_right.to_px(box);
  178. box_state.border_left = computed_values.border_left().width;
  179. box_state.border_right = computed_values.border_right().width;
  180. box_state.padding_left = padding_left.to_px(box);
  181. box_state.padding_right = padding_right.to_px(box);
  182. }
  183. void BlockFormattingContext::compute_width_for_floating_box(Box const& box, LayoutMode layout_mode)
  184. {
  185. // 10.3.5 Floating, non-replaced elements
  186. auto& computed_values = box.computed_values();
  187. auto& containing_block = *box.containing_block();
  188. float width_of_containing_block = 0;
  189. switch (layout_mode) {
  190. case LayoutMode::Normal:
  191. width_of_containing_block = m_state.get(containing_block).content_width;
  192. break;
  193. case LayoutMode::MinContent:
  194. width_of_containing_block = 0;
  195. break;
  196. case LayoutMode::MaxContent:
  197. width_of_containing_block = INFINITY;
  198. break;
  199. }
  200. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  201. auto zero_value = CSS::Length::make_px(0);
  202. auto margin_left = computed_values.margin().left.resolved(box, width_of_containing_block_as_length).resolved(box);
  203. auto margin_right = computed_values.margin().right.resolved(box, width_of_containing_block_as_length).resolved(box);
  204. const auto padding_left = computed_values.padding().left.resolved(box, width_of_containing_block_as_length).resolved(box);
  205. const auto padding_right = computed_values.padding().right.resolved(box, width_of_containing_block_as_length).resolved(box);
  206. // If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
  207. if (margin_left.is_auto())
  208. margin_left = zero_value;
  209. if (margin_right.is_auto())
  210. margin_right = zero_value;
  211. auto width = computed_values.width().has_value() ? computed_values.width()->resolved(box, width_of_containing_block_as_length).resolved(box) : CSS::Length::make_auto();
  212. // If 'width' is computed as 'auto', the used value is the "shrink-to-fit" width.
  213. if (width.is_auto()) {
  214. // Find the available width: in this case, this is the width of the containing
  215. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  216. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  217. float available_width = width_of_containing_block
  218. - margin_left.to_px(box) - computed_values.border_left().width - padding_left.to_px(box)
  219. - padding_right.to_px(box) - computed_values.border_right().width - margin_right.to_px(box);
  220. auto result = calculate_shrink_to_fit_widths(box);
  221. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  222. width = CSS::Length(min(max(result.preferred_minimum_width, available_width), result.preferred_width), CSS::Length::Type::Px);
  223. }
  224. auto& box_state = m_state.get_mutable(box);
  225. box_state.content_width = width.to_px(box);
  226. box_state.margin_left = margin_left.to_px(box);
  227. box_state.margin_right = margin_right.to_px(box);
  228. box_state.border_left = computed_values.border_left().width;
  229. box_state.border_right = computed_values.border_right().width;
  230. box_state.padding_left = padding_left.to_px(box);
  231. box_state.padding_right = padding_right.to_px(box);
  232. }
  233. void BlockFormattingContext::compute_width_for_block_level_replaced_element_in_normal_flow(ReplacedBox const& box)
  234. {
  235. m_state.get_mutable(box).content_width = compute_width_for_replaced_element(m_state, box);
  236. }
  237. float BlockFormattingContext::compute_theoretical_height(FormattingState const& state, Box const& box)
  238. {
  239. auto const& computed_values = box.computed_values();
  240. auto const& containing_block = *box.containing_block();
  241. auto const& containing_block_state = state.get(containing_block);
  242. auto containing_block_height = CSS::Length::make_px(containing_block_state.content_height);
  243. auto is_absolute = [](Optional<CSS::LengthPercentage> const& length_percentage) {
  244. return length_percentage.has_value() && length_percentage->is_length() && length_percentage->length().is_absolute();
  245. };
  246. // Then work out what the height is, based on box type and CSS properties.
  247. float height = 0;
  248. if (is<ReplacedBox>(box)) {
  249. height = compute_height_for_replaced_element(state, verify_cast<ReplacedBox>(box));
  250. } else {
  251. if (!box.computed_values().height().has_value()
  252. || (box.computed_values().height()->is_length() && box.computed_values().height()->length().is_auto())
  253. || (computed_values.height().has_value() && computed_values.height()->is_percentage() && !is_absolute(containing_block.computed_values().height()))) {
  254. height = compute_auto_height_for_block_level_element(state, box);
  255. } else {
  256. height = computed_values.height().has_value() ? computed_values.height()->resolved(box, containing_block_height).to_px(box) : 0;
  257. }
  258. }
  259. auto specified_max_height = computed_values.max_height().has_value() ? computed_values.max_height()->resolved(box, containing_block_height).resolved(box) : CSS::Length::make_auto();
  260. if (!specified_max_height.is_auto()
  261. && !(computed_values.max_height().has_value() && computed_values.max_height()->is_percentage() && !is_absolute(containing_block.computed_values().height())))
  262. height = min(height, specified_max_height.to_px(box));
  263. auto specified_min_height = computed_values.min_height().has_value() ? computed_values.min_height()->resolved(box, containing_block_height).resolved(box) : CSS::Length::make_auto();
  264. if (!specified_min_height.is_auto()
  265. && !(computed_values.min_height().has_value() && computed_values.min_height()->is_percentage() && !is_absolute(containing_block.computed_values().height())))
  266. height = max(height, specified_min_height.to_px(box));
  267. return height;
  268. }
  269. void BlockFormattingContext::compute_height(Box const& box, FormattingState& state)
  270. {
  271. auto const& computed_values = box.computed_values();
  272. auto const& containing_block = *box.containing_block();
  273. auto width_of_containing_block_as_length = CSS::Length::make_px(state.get(containing_block).content_width);
  274. // First, resolve the top/bottom parts of the surrounding box model.
  275. auto& box_state = state.get_mutable(box);
  276. // FIXME: While negative values are generally allowed for margins, for now just ignore those for height calculation
  277. box_state.margin_top = max(computed_values.margin().top.resolved(box, width_of_containing_block_as_length).to_px(box), 0);
  278. box_state.margin_bottom = max(computed_values.margin().bottom.resolved(box, width_of_containing_block_as_length).to_px(box), 0);
  279. box_state.border_top = computed_values.border_top().width;
  280. box_state.border_bottom = computed_values.border_bottom().width;
  281. box_state.padding_top = computed_values.padding().top.resolved(box, width_of_containing_block_as_length).to_px(box);
  282. box_state.padding_bottom = computed_values.padding().bottom.resolved(box, width_of_containing_block_as_length).to_px(box);
  283. box_state.content_height = compute_theoretical_height(state, box);
  284. }
  285. void BlockFormattingContext::layout_inline_children(BlockContainer const& block_container, LayoutMode layout_mode)
  286. {
  287. VERIFY(block_container.children_are_inline());
  288. InlineFormattingContext context(m_state, block_container, *this);
  289. context.run(block_container, layout_mode);
  290. }
  291. void BlockFormattingContext::layout_block_level_children(BlockContainer const& block_container, LayoutMode layout_mode)
  292. {
  293. VERIFY(!block_container.children_are_inline());
  294. float content_height = 0;
  295. block_container.for_each_child_of_type<Box>([&](Box& child_box) {
  296. auto& box_state = m_state.get_mutable(child_box);
  297. if (child_box.is_absolutely_positioned()) {
  298. m_absolutely_positioned_boxes.append(child_box);
  299. return IterationDecision::Continue;
  300. }
  301. // NOTE: ListItemMarkerBoxes are placed by their corresponding ListItemBox.
  302. if (is<ListItemMarkerBox>(child_box))
  303. return IterationDecision::Continue;
  304. if (child_box.is_floating()) {
  305. layout_floating_box(child_box, block_container, layout_mode);
  306. content_height = max(content_height, box_state.offset.y() + box_state.content_height + box_state.margin_box_bottom());
  307. return IterationDecision::Continue;
  308. }
  309. compute_width(child_box, layout_mode);
  310. if (is<ReplacedBox>(child_box) || is<BlockContainer>(child_box))
  311. place_block_level_element_in_normal_flow_vertically(child_box, block_container);
  312. if (child_box.has_definite_height()) {
  313. compute_height(child_box, m_state);
  314. }
  315. OwnPtr<FormattingContext> independent_formatting_context;
  316. if (child_box.can_have_children()) {
  317. independent_formatting_context = create_independent_formatting_context_if_needed(m_state, child_box);
  318. if (independent_formatting_context)
  319. independent_formatting_context->run(child_box, layout_mode);
  320. else
  321. layout_block_level_children(verify_cast<BlockContainer>(child_box), layout_mode);
  322. }
  323. compute_height(child_box, m_state);
  324. compute_position(child_box);
  325. if (is<ReplacedBox>(child_box) || is<BlockContainer>(child_box))
  326. place_block_level_element_in_normal_flow_horizontally(child_box, block_container);
  327. if (is<ListItemBox>(child_box)) {
  328. layout_list_item_marker(static_cast<ListItemBox const&>(child_box));
  329. }
  330. content_height = max(content_height, box_state.offset.y() + box_state.content_height + box_state.margin_box_bottom());
  331. if (independent_formatting_context)
  332. independent_formatting_context->parent_context_did_dimension_child_root_box();
  333. return IterationDecision::Continue;
  334. });
  335. if (layout_mode != LayoutMode::Normal) {
  336. auto& width = block_container.computed_values().width();
  337. if (!width.has_value() || (width->is_length() && width->length().is_auto())) {
  338. auto& block_container_state = m_state.get_mutable(block_container);
  339. block_container_state.content_width = greatest_child_width(block_container);
  340. }
  341. }
  342. }
  343. void BlockFormattingContext::compute_vertical_box_model_metrics(Box const& box, BlockContainer const& containing_block)
  344. {
  345. auto& box_state = m_state.get_mutable(box);
  346. auto const& computed_values = box.computed_values();
  347. auto width_of_containing_block = CSS::Length::make_px(m_state.get(containing_block).content_width);
  348. box_state.margin_top = computed_values.margin().top.resolved(box, width_of_containing_block).resolved(containing_block).to_px(box);
  349. box_state.margin_bottom = computed_values.margin().bottom.resolved(box, width_of_containing_block).resolved(containing_block).to_px(box);
  350. box_state.border_top = computed_values.border_top().width;
  351. box_state.border_bottom = computed_values.border_bottom().width;
  352. box_state.padding_top = computed_values.padding().top.resolved(box, width_of_containing_block).resolved(containing_block).to_px(box);
  353. box_state.padding_bottom = computed_values.padding().bottom.resolved(box, width_of_containing_block).resolved(containing_block).to_px(box);
  354. }
  355. void BlockFormattingContext::place_block_level_element_in_normal_flow_vertically(Box const& child_box, BlockContainer const& containing_block)
  356. {
  357. auto& box_state = m_state.get_mutable(child_box);
  358. auto const& computed_values = child_box.computed_values();
  359. compute_vertical_box_model_metrics(child_box, containing_block);
  360. float y = box_state.margin_box_top()
  361. + box_state.offset_top;
  362. // NOTE: Empty (0-height) preceding siblings have their margins collapsed with *their* preceding sibling, etc.
  363. float collapsed_bottom_margin_of_preceding_siblings = 0;
  364. auto* relevant_sibling = child_box.previous_sibling_of_type<Layout::BlockContainer>();
  365. while (relevant_sibling != nullptr) {
  366. if (!relevant_sibling->is_absolutely_positioned() && !relevant_sibling->is_floating()) {
  367. auto const& relevant_sibling_state = m_state.get(*relevant_sibling);
  368. collapsed_bottom_margin_of_preceding_siblings = max(collapsed_bottom_margin_of_preceding_siblings, relevant_sibling_state.margin_bottom);
  369. if (relevant_sibling_state.border_box_height() > 0)
  370. break;
  371. }
  372. relevant_sibling = relevant_sibling->previous_sibling_of_type<Layout::BlockContainer>();
  373. }
  374. if (relevant_sibling) {
  375. auto const& relevant_sibling_state = m_state.get(*relevant_sibling);
  376. y += relevant_sibling_state.offset.y()
  377. + relevant_sibling_state.content_height
  378. + relevant_sibling_state.border_box_bottom();
  379. // Collapse top margin with bottom margin of preceding siblings if needed
  380. float my_margin_top = box_state.margin_top;
  381. if (my_margin_top < 0 || collapsed_bottom_margin_of_preceding_siblings < 0) {
  382. // Negative margins present.
  383. float largest_negative_margin = -min(my_margin_top, collapsed_bottom_margin_of_preceding_siblings);
  384. float largest_positive_margin = (my_margin_top < 0 && collapsed_bottom_margin_of_preceding_siblings < 0) ? 0 : max(my_margin_top, collapsed_bottom_margin_of_preceding_siblings);
  385. float final_margin = largest_positive_margin - largest_negative_margin;
  386. y += final_margin - my_margin_top;
  387. } else if (collapsed_bottom_margin_of_preceding_siblings > my_margin_top) {
  388. // Sibling's margin is larger than mine, adjust so we use sibling's.
  389. y += collapsed_bottom_margin_of_preceding_siblings - my_margin_top;
  390. }
  391. }
  392. auto clear_floating_boxes = [&](FloatSideData& float_side) {
  393. if (!float_side.current_boxes.is_empty()) {
  394. float clearance_y = 0;
  395. for (auto const& floating_box : float_side.current_boxes) {
  396. auto const& floating_box_state = m_state.get(floating_box.box);
  397. clearance_y = max(clearance_y, floating_box_state.offset.y() + floating_box_state.border_box_height());
  398. }
  399. y = max(y, clearance_y);
  400. float_side.clear();
  401. }
  402. };
  403. // Flex-items don't float and also don't clear.
  404. if ((computed_values.clear() == CSS::Clear::Left || computed_values.clear() == CSS::Clear::Both) && !child_box.is_flex_item())
  405. clear_floating_boxes(m_left_floats);
  406. if ((computed_values.clear() == CSS::Clear::Right || computed_values.clear() == CSS::Clear::Both) && !child_box.is_flex_item())
  407. clear_floating_boxes(m_right_floats);
  408. box_state.offset = Gfx::FloatPoint { box_state.offset.x(), y };
  409. }
  410. void BlockFormattingContext::place_block_level_element_in_normal_flow_horizontally(Box const& child_box, BlockContainer const& containing_block)
  411. {
  412. auto& box_state = m_state.get_mutable(child_box);
  413. auto const& containing_block_state = m_state.get(containing_block);
  414. float x = 0;
  415. float available_width_within_containing_block = containing_block_state.content_width;
  416. if ((!m_left_floats.current_boxes.is_empty() || !m_right_floats.current_boxes.is_empty())
  417. && creates_block_formatting_context(child_box)) {
  418. available_width_within_containing_block -= m_left_floats.current_width + m_right_floats.current_width;
  419. x += m_left_floats.current_width;
  420. }
  421. if (containing_block.computed_values().text_align() == CSS::TextAlign::LibwebCenter) {
  422. x += (available_width_within_containing_block / 2) - box_state.content_width / 2;
  423. } else {
  424. x += box_state.margin_box_left() + box_state.offset_left;
  425. }
  426. box_state.offset = Gfx::FloatPoint { x, box_state.offset.y() };
  427. }
  428. void BlockFormattingContext::layout_initial_containing_block(LayoutMode layout_mode)
  429. {
  430. auto viewport_rect = root().browsing_context().viewport_rect();
  431. auto& icb = verify_cast<Layout::InitialContainingBlock>(root());
  432. auto& icb_state = m_state.get_mutable(icb);
  433. VERIFY(!icb.children_are_inline());
  434. layout_block_level_children(root(), layout_mode);
  435. // Compute scrollable overflow.
  436. float bottom_edge = 0;
  437. float right_edge = 0;
  438. icb.for_each_in_subtree_of_type<Box>([&](Box const& child) {
  439. auto const& child_state = m_state.get(child);
  440. auto child_rect = absolute_content_rect(child, m_state);
  441. child_rect.inflate(child_state.border_box_top(), child_state.border_box_right(), child_state.border_box_bottom(), child_state.border_box_left());
  442. bottom_edge = max(bottom_edge, child_rect.bottom());
  443. right_edge = max(right_edge, child_rect.right());
  444. return IterationDecision::Continue;
  445. });
  446. if (bottom_edge >= viewport_rect.height() || right_edge >= viewport_rect.width()) {
  447. // FIXME: Move overflow data to FormattingState!
  448. auto& overflow_data = icb_state.ensure_overflow_data();
  449. overflow_data.scrollable_overflow_rect = viewport_rect.to_type<float>();
  450. // NOTE: The edges are *within* the rectangle, so we add 1 to get the width and height.
  451. overflow_data.scrollable_overflow_rect.set_size(right_edge + 1, bottom_edge + 1);
  452. }
  453. }
  454. void BlockFormattingContext::layout_floating_box(Box const& box, BlockContainer const& containing_block, LayoutMode layout_mode)
  455. {
  456. VERIFY(box.is_floating());
  457. auto& box_state = m_state.get_mutable(box);
  458. float width_of_containing_block = 0;
  459. switch (layout_mode) {
  460. case LayoutMode::Normal:
  461. width_of_containing_block = m_state.get(containing_block).content_width;
  462. break;
  463. case LayoutMode::MinContent:
  464. width_of_containing_block = 0;
  465. break;
  466. case LayoutMode::MaxContent:
  467. width_of_containing_block = INFINITY;
  468. break;
  469. }
  470. compute_width(box, layout_mode);
  471. (void)layout_inside(box, layout_mode);
  472. compute_height(box, m_state);
  473. // First we place the box normally (to get the right y coordinate.)
  474. place_block_level_element_in_normal_flow_vertically(box, containing_block);
  475. place_block_level_element_in_normal_flow_horizontally(box, containing_block);
  476. auto float_box = [&](FloatSide side, FloatSideData& side_data) {
  477. auto first_edge = [&](FormattingState::NodeState const& thing) { return side == FloatSide::Left ? thing.margin_left : thing.margin_right; };
  478. auto second_edge = [&](FormattingState::NodeState const& thing) { return side == FloatSide::Right ? thing.margin_left : thing.margin_right; };
  479. // Then we float it to the left or right.
  480. float offset_from_edge = 0;
  481. auto float_to_edge = [&] {
  482. if (side == FloatSide::Left)
  483. offset_from_edge = box_state.margin_box_left();
  484. else
  485. offset_from_edge = box_state.content_width + box_state.margin_box_right();
  486. };
  487. auto box_in_root_rect = margin_box_rect_in_ancestor_coordinate_space(box, root(), m_state);
  488. float y_in_root = box_in_root_rect.y();
  489. float y = box_state.offset.y();
  490. if (side_data.current_boxes.is_empty()) {
  491. // This is the first floating box on this side. Go all the way to the edge.
  492. float_to_edge();
  493. side_data.y_offset = 0;
  494. } else {
  495. auto& previous_box = side_data.current_boxes.last();
  496. auto const& previous_box_state = m_state.get(previous_box.box);
  497. auto margin_collapsed_with_previous = max(
  498. second_edge(previous_box_state),
  499. first_edge(box_state));
  500. float wanted_offset_from_edge = 0;
  501. bool fits_on_line = false;
  502. if (side == FloatSide::Left) {
  503. auto previous_right_edge = side_data.current_width
  504. - previous_box_state.margin_right
  505. + margin_collapsed_with_previous;
  506. wanted_offset_from_edge = previous_right_edge + box_state.border_left + box_state.padding_left;
  507. fits_on_line = (wanted_offset_from_edge + box_state.content_width + box_state.padding_right + box_state.border_right + box_state.margin_right) <= width_of_containing_block;
  508. } else {
  509. auto previous_left_edge = side_data.current_width
  510. - previous_box_state.margin_left
  511. + margin_collapsed_with_previous;
  512. wanted_offset_from_edge = previous_left_edge + box_state.border_right + box_state.padding_right + box_state.content_width;
  513. fits_on_line = (wanted_offset_from_edge - box_state.padding_left - box_state.border_left - box_state.margin_left) >= 0;
  514. }
  515. if (fits_on_line) {
  516. auto const previous_rect = margin_box_rect_in_ancestor_coordinate_space(previous_box.box, root(), m_state);
  517. if (previous_rect.contains_vertically(y_in_root + side_data.y_offset)) {
  518. // This box touches another already floating box. Stack after others.
  519. offset_from_edge = wanted_offset_from_edge;
  520. } else {
  521. // This box does not touch another floating box, go all the way to the edge.
  522. float_to_edge();
  523. // Also, forget all previous boxes floated to this side while since they're no longer relevant.
  524. side_data.clear();
  525. }
  526. } else {
  527. // We ran out of horizontal space on this "float line", and need to break.
  528. float_to_edge();
  529. float lowest_border_edge = 0;
  530. for (auto const& box : side_data.current_boxes) {
  531. auto const& box_state = m_state.get(box.box);
  532. lowest_border_edge = max(lowest_border_edge, box_state.border_box_height());
  533. }
  534. side_data.y_offset += lowest_border_edge;
  535. // Also, forget all previous boxes floated to this side while since they're no longer relevant.
  536. side_data.clear();
  537. }
  538. }
  539. y += side_data.y_offset;
  540. side_data.all_boxes.append(adopt_own(*new FloatingBox {
  541. .box = box,
  542. .offset_from_edge = offset_from_edge,
  543. .top_margin_edge = y - box_state.margin_box_top(),
  544. .bottom_margin_edge = y + box_state.content_height + box_state.margin_box_bottom(),
  545. }));
  546. side_data.current_boxes.append(*side_data.all_boxes.last());
  547. if (side == FloatSide::Left) {
  548. side_data.current_width = offset_from_edge + box_state.content_width + box_state.margin_box_right();
  549. } else {
  550. side_data.current_width = offset_from_edge + box_state.margin_box_left();
  551. }
  552. side_data.max_width = max(side_data.current_width, side_data.max_width);
  553. // NOTE: We don't set the X position here, that happens later, once we know the root block width.
  554. // See parent_context_did_dimension_child_root_box() for that logic.
  555. box_state.offset.set_y(y);
  556. };
  557. // Next, float to the left and/or right
  558. if (box.computed_values().float_() == CSS::Float::Left) {
  559. float_box(FloatSide::Left, m_left_floats);
  560. } else if (box.computed_values().float_() == CSS::Float::Right) {
  561. float_box(FloatSide::Right, m_right_floats);
  562. }
  563. }
  564. void BlockFormattingContext::layout_list_item_marker(ListItemBox const& list_item_box)
  565. {
  566. if (!list_item_box.marker())
  567. return;
  568. auto& marker = *list_item_box.marker();
  569. auto& marker_state = m_state.get_mutable(marker);
  570. auto& list_item_state = m_state.get_mutable(list_item_box);
  571. int image_width = 0;
  572. int image_height = 0;
  573. if (auto const* list_style_image = marker.list_style_image_bitmap()) {
  574. image_width = list_style_image->rect().width();
  575. image_height = list_style_image->rect().height();
  576. }
  577. if (marker.text().is_empty()) {
  578. marker_state.content_width = image_width + 4;
  579. } else {
  580. auto text_width = marker.font().width(marker.text());
  581. marker_state.content_width = image_width + text_width;
  582. }
  583. marker_state.content_height = max(image_height, marker.line_height());
  584. marker_state.offset = { -(marker_state.content_width + 4), 0 };
  585. if (marker_state.content_height > list_item_state.content_height)
  586. list_item_state.content_height = marker_state.content_height;
  587. }
  588. BlockFormattingContext::SpaceUsedByFloats BlockFormattingContext::space_used_by_floats(float y) const
  589. {
  590. SpaceUsedByFloats space_used_by_floats;
  591. for (auto const& floating_box : m_left_floats.current_boxes.in_reverse()) {
  592. auto const& floating_box_state = m_state.get(floating_box.box);
  593. // NOTE: The floating box is *not* in the final horizontal position yet, but the size and vertical position is valid.
  594. auto rect = margin_box_rect_in_ancestor_coordinate_space(floating_box.box, root(), m_state);
  595. if (rect.contains_vertically(y)) {
  596. space_used_by_floats.left = floating_box.offset_from_edge
  597. + floating_box_state.content_width
  598. + floating_box_state.margin_box_right();
  599. break;
  600. }
  601. }
  602. for (auto const& floating_box : m_right_floats.current_boxes.in_reverse()) {
  603. auto const& floating_box_state = m_state.get(floating_box.box);
  604. // NOTE: The floating box is *not* in the final horizontal position yet, but the size and vertical position is valid.
  605. auto rect = margin_box_rect_in_ancestor_coordinate_space(floating_box.box, root(), m_state);
  606. if (rect.contains_vertically(y)) {
  607. space_used_by_floats.right = floating_box.offset_from_edge
  608. + floating_box_state.margin_box_left();
  609. break;
  610. }
  611. }
  612. return space_used_by_floats;
  613. }
  614. float BlockFormattingContext::greatest_child_width(Box const& box)
  615. {
  616. // Similar to FormattingContext::greatest_child_width()
  617. // but this one takes floats into account!
  618. float max_width = m_left_floats.max_width + m_right_floats.max_width;
  619. if (box.children_are_inline()) {
  620. for (auto const& line_box : m_state.get(verify_cast<BlockContainer>(box)).line_boxes) {
  621. auto width_here = line_box.width();
  622. float extra_width_from_left_floats = 0;
  623. for (auto& left_float : m_left_floats.all_boxes) {
  624. if (line_box.baseline() >= left_float->top_margin_edge || line_box.baseline() <= left_float->bottom_margin_edge) {
  625. auto const& left_float_state = m_state.get(left_float->box);
  626. extra_width_from_left_floats = max(extra_width_from_left_floats, left_float->offset_from_edge + left_float_state.content_width + left_float_state.margin_box_right());
  627. }
  628. }
  629. float extra_width_from_right_floats = 0;
  630. for (auto& right_float : m_right_floats.all_boxes) {
  631. if (line_box.baseline() >= right_float->top_margin_edge || line_box.baseline() <= right_float->bottom_margin_edge) {
  632. auto const& right_float_state = m_state.get(right_float->box);
  633. extra_width_from_right_floats = max(extra_width_from_right_floats, right_float->offset_from_edge + right_float_state.margin_box_left());
  634. }
  635. }
  636. width_here += extra_width_from_left_floats + extra_width_from_right_floats;
  637. max_width = max(max_width, width_here);
  638. }
  639. } else {
  640. box.for_each_child_of_type<Box>([&](auto& child) {
  641. max_width = max(max_width, m_state.get(child).border_box_width());
  642. });
  643. }
  644. return max_width;
  645. }
  646. }