BlockFormattingContext.cpp 38 KB

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