BlockFormattingContext.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * Copyright (c) 2020, 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/Layout/BlockBox.h>
  9. #include <LibWeb/Layout/BlockFormattingContext.h>
  10. #include <LibWeb/Layout/Box.h>
  11. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  12. #include <LibWeb/Layout/InlineFormattingContext.h>
  13. #include <LibWeb/Layout/ListItemBox.h>
  14. #include <LibWeb/Layout/ReplacedBox.h>
  15. #include <LibWeb/Page/BrowsingContext.h>
  16. namespace Web::Layout {
  17. BlockFormattingContext::BlockFormattingContext(Box& context_box, FormattingContext* parent)
  18. : FormattingContext(context_box, parent)
  19. {
  20. }
  21. BlockFormattingContext::~BlockFormattingContext()
  22. {
  23. }
  24. bool BlockFormattingContext::is_initial() const
  25. {
  26. return is<InitialContainingBlockBox>(context_box());
  27. }
  28. void BlockFormattingContext::run(Box& box, LayoutMode layout_mode)
  29. {
  30. if (is_initial()) {
  31. layout_initial_containing_block(layout_mode);
  32. return;
  33. }
  34. // FIXME: BFC currently computes the width+height of the target box.
  35. // This is necessary to be able to place absolutely positioned descendants.
  36. // The same work is also done by the parent BFC for each of its blocks..
  37. if (layout_mode == LayoutMode::Default)
  38. compute_width(box);
  39. if (box.children_are_inline()) {
  40. layout_inline_children(box, layout_mode);
  41. } else {
  42. layout_block_level_children(box, layout_mode);
  43. }
  44. if (layout_mode == LayoutMode::Default) {
  45. compute_height(box);
  46. box.for_each_child_of_type<Box>([&](auto& child_box) {
  47. if (child_box.is_absolutely_positioned()) {
  48. layout_absolutely_positioned_element(child_box);
  49. }
  50. return IterationDecision::Continue;
  51. });
  52. }
  53. }
  54. void BlockFormattingContext::compute_width(Box& box)
  55. {
  56. if (box.is_absolutely_positioned()) {
  57. compute_width_for_absolutely_positioned_element(box);
  58. return;
  59. }
  60. if (is<ReplacedBox>(box)) {
  61. // FIXME: This should not be done *by* ReplacedBox
  62. auto& replaced = verify_cast<ReplacedBox>(box);
  63. replaced.prepare_for_replaced_layout();
  64. compute_width_for_block_level_replaced_element_in_normal_flow(replaced);
  65. return;
  66. }
  67. if (box.is_floating()) {
  68. compute_width_for_floating_box(box);
  69. return;
  70. }
  71. auto& computed_values = box.computed_values();
  72. float width_of_containing_block = box.width_of_logical_containing_block();
  73. auto zero_value = CSS::Length::make_px(0);
  74. auto margin_left = CSS::Length::make_auto();
  75. auto margin_right = CSS::Length::make_auto();
  76. const auto padding_left = computed_values.padding().left.resolved_or_zero(box, width_of_containing_block);
  77. const auto padding_right = computed_values.padding().right.resolved_or_zero(box, width_of_containing_block);
  78. auto try_compute_width = [&](const auto& a_width) {
  79. CSS::Length width = a_width;
  80. margin_left = computed_values.margin().left.resolved_or_zero(box, width_of_containing_block);
  81. margin_right = computed_values.margin().right.resolved_or_zero(box, width_of_containing_block);
  82. float total_px = computed_values.border_left().width + computed_values.border_right().width;
  83. for (auto& value : { margin_left, padding_left, width, padding_right, margin_right }) {
  84. total_px += value.to_px(box);
  85. }
  86. if (!box.is_inline()) {
  87. // 10.3.3 Block-level, non-replaced elements in normal flow
  88. // 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.
  89. if (width.is_auto() && total_px > width_of_containing_block) {
  90. if (margin_left.is_auto())
  91. margin_left = zero_value;
  92. if (margin_right.is_auto())
  93. margin_right = zero_value;
  94. }
  95. // 10.3.3 cont'd.
  96. auto underflow_px = width_of_containing_block - total_px;
  97. if (width.is_auto()) {
  98. if (margin_left.is_auto())
  99. margin_left = zero_value;
  100. if (margin_right.is_auto())
  101. margin_right = zero_value;
  102. if (underflow_px >= 0) {
  103. width = CSS::Length(underflow_px, CSS::Length::Type::Px);
  104. } else {
  105. width = zero_value;
  106. margin_right = CSS::Length(margin_right.to_px(box) + underflow_px, CSS::Length::Type::Px);
  107. }
  108. } else {
  109. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  110. margin_right = CSS::Length(margin_right.to_px(box) + underflow_px, CSS::Length::Type::Px);
  111. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  112. margin_right = CSS::Length(underflow_px, CSS::Length::Type::Px);
  113. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  114. margin_left = CSS::Length(underflow_px, CSS::Length::Type::Px);
  115. } else { // margin_left.is_auto() && margin_right.is_auto()
  116. auto half_of_the_underflow = CSS::Length(underflow_px / 2, CSS::Length::Type::Px);
  117. margin_left = half_of_the_underflow;
  118. margin_right = half_of_the_underflow;
  119. }
  120. }
  121. } else if (box.is_inline_block()) {
  122. // 10.3.9 'Inline-block', non-replaced elements in normal flow
  123. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  124. if (margin_left.is_auto())
  125. margin_left = zero_value;
  126. if (margin_right.is_auto())
  127. margin_right = zero_value;
  128. // If 'width' is 'auto', the used value is the shrink-to-fit width as for floating elements.
  129. if (width.is_auto()) {
  130. // Find the available width: in this case, this is the width of the containing
  131. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  132. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  133. float available_width = width_of_containing_block
  134. - margin_left.to_px(box) - computed_values.border_left().width - padding_left.to_px(box)
  135. - padding_right.to_px(box) - computed_values.border_right().width - margin_right.to_px(box);
  136. auto result = calculate_shrink_to_fit_widths(box);
  137. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  138. width = CSS::Length(min(max(result.preferred_minimum_width, available_width), result.preferred_width), CSS::Length::Type::Px);
  139. }
  140. }
  141. return width;
  142. };
  143. auto specified_width = computed_values.width().resolved_or_auto(box, width_of_containing_block);
  144. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  145. auto used_width = try_compute_width(specified_width);
  146. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  147. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  148. auto specified_max_width = computed_values.max_width().resolved_or_auto(box, width_of_containing_block);
  149. if (!specified_max_width.is_auto()) {
  150. if (used_width.to_px(box) > specified_max_width.to_px(box)) {
  151. used_width = try_compute_width(specified_max_width);
  152. }
  153. }
  154. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  155. // but this time using the value of 'min-width' as the computed value for 'width'.
  156. auto specified_min_width = computed_values.min_width().resolved_or_auto(box, width_of_containing_block);
  157. if (!specified_min_width.is_auto()) {
  158. if (used_width.to_px(box) < specified_min_width.to_px(box)) {
  159. used_width = try_compute_width(specified_min_width);
  160. }
  161. }
  162. box.set_width(used_width.to_px(box));
  163. box.box_model().margin.left = margin_left.to_px(box);
  164. box.box_model().margin.right = margin_right.to_px(box);
  165. box.box_model().border.left = computed_values.border_left().width;
  166. box.box_model().border.right = computed_values.border_right().width;
  167. box.box_model().padding.left = padding_left.to_px(box);
  168. box.box_model().padding.right = padding_right.to_px(box);
  169. }
  170. void BlockFormattingContext::compute_width_for_floating_box(Box& box)
  171. {
  172. // 10.3.5 Floating, non-replaced elements
  173. auto& computed_values = box.computed_values();
  174. float width_of_containing_block = box.width_of_logical_containing_block();
  175. auto zero_value = CSS::Length::make_px(0);
  176. auto margin_left = computed_values.margin().left.resolved_or_zero(box, width_of_containing_block);
  177. auto margin_right = computed_values.margin().right.resolved_or_zero(box, width_of_containing_block);
  178. const auto padding_left = computed_values.padding().left.resolved_or_zero(box, width_of_containing_block);
  179. const auto padding_right = computed_values.padding().right.resolved_or_zero(box, width_of_containing_block);
  180. // If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
  181. if (margin_left.is_auto())
  182. margin_left = zero_value;
  183. if (margin_right.is_auto())
  184. margin_right = zero_value;
  185. auto width = computed_values.width().resolved_or_auto(box, width_of_containing_block);
  186. // If 'width' is computed as 'auto', the used value is the "shrink-to-fit" width.
  187. if (width.is_auto()) {
  188. // Find the available width: in this case, this is the width of the containing
  189. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  190. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  191. float available_width = width_of_containing_block
  192. - margin_left.to_px(box) - computed_values.border_left().width - padding_left.to_px(box)
  193. - padding_right.to_px(box) - computed_values.border_right().width - margin_right.to_px(box);
  194. auto result = calculate_shrink_to_fit_widths(box);
  195. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  196. width = CSS::Length(min(max(result.preferred_minimum_width, available_width), result.preferred_width), CSS::Length::Type::Px);
  197. }
  198. float final_width = width.resolved_or_zero(box, width_of_containing_block).to_px(box);
  199. box.set_width(final_width);
  200. box.box_model().margin.left = margin_left.to_px(box);
  201. box.box_model().margin.right = margin_right.to_px(box);
  202. box.box_model().border.left = computed_values.border_left().width;
  203. box.box_model().border.right = computed_values.border_right().width;
  204. box.box_model().padding.left = padding_left.to_px(box);
  205. box.box_model().padding.right = padding_right.to_px(box);
  206. }
  207. void BlockFormattingContext::compute_width_for_block_level_replaced_element_in_normal_flow(ReplacedBox& box)
  208. {
  209. box.set_width(compute_width_for_replaced_element(box));
  210. }
  211. static float compute_auto_height_for_block_level_element(const Box& box)
  212. {
  213. Optional<float> top;
  214. Optional<float> bottom;
  215. if (box.children_are_inline()) {
  216. // If it only has inline-level children, the height is the distance between
  217. // the top of the topmost line box and the bottom of the bottommost line box.
  218. if (!box.line_boxes().is_empty()) {
  219. for (auto& fragment : box.line_boxes().first().fragments()) {
  220. if (!top.has_value() || fragment.offset().y() < top.value())
  221. top = fragment.offset().y();
  222. }
  223. for (auto& fragment : box.line_boxes().last().fragments()) {
  224. if (!bottom.has_value() || (fragment.offset().y() + fragment.height()) > bottom.value())
  225. bottom = fragment.offset().y() + fragment.height();
  226. }
  227. }
  228. } else {
  229. // If it has block-level children, the height is the distance between
  230. // the top margin-edge of the topmost block-level child box
  231. // and the bottom margin-edge of the bottommost block-level child box.
  232. box.for_each_child_of_type<Box>([&](Layout::Box& child_box) {
  233. if (child_box.is_absolutely_positioned())
  234. return IterationDecision::Continue;
  235. if ((box.computed_values().overflow_y() == CSS::Overflow::Visible) && child_box.is_floating())
  236. return IterationDecision::Continue;
  237. float child_box_top = child_box.effective_offset().y() - child_box.box_model().margin_box().top;
  238. float child_box_bottom = child_box.effective_offset().y() + child_box.height() + child_box.box_model().margin_box().bottom;
  239. if (!top.has_value() || child_box_top < top.value())
  240. top = child_box_top;
  241. if (!bottom.has_value() || child_box_bottom > bottom.value())
  242. bottom = child_box_bottom;
  243. return IterationDecision::Continue;
  244. });
  245. }
  246. return bottom.value_or(0) - top.value_or(0);
  247. }
  248. float BlockFormattingContext::compute_theoretical_height(const Box& box)
  249. {
  250. auto& computed_values = box.computed_values();
  251. auto& containing_block = *box.containing_block();
  252. // Then work out what the height is, based on box type and CSS properties.
  253. float height = 0;
  254. if (is<ReplacedBox>(box)) {
  255. height = compute_height_for_replaced_element(verify_cast<ReplacedBox>(box));
  256. } else {
  257. if (box.computed_values().height().is_undefined_or_auto()
  258. || (computed_values.height().is_percentage() && !containing_block.computed_values().height().is_absolute())) {
  259. height = compute_auto_height_for_block_level_element(box);
  260. } else {
  261. height = computed_values.height().resolved_or_auto(box, containing_block.height()).to_px(box);
  262. }
  263. }
  264. auto specified_max_height = computed_values.max_height().resolved_or_auto(box, containing_block.height());
  265. if (!specified_max_height.is_auto()
  266. && !(computed_values.max_height().is_percentage() && !containing_block.computed_values().height().is_absolute()))
  267. height = min(height, specified_max_height.to_px(box));
  268. auto specified_min_height = computed_values.min_height().resolved_or_auto(box, containing_block.height());
  269. if (!specified_min_height.is_auto()
  270. && !(computed_values.min_height().is_percentage() && !containing_block.computed_values().height().is_absolute()))
  271. height = max(height, specified_min_height.to_px(box));
  272. return height;
  273. }
  274. void BlockFormattingContext::compute_height(Box& box)
  275. {
  276. auto& computed_values = box.computed_values();
  277. auto& containing_block = *box.containing_block();
  278. // First, resolve the top/bottom parts of the surrounding box model.
  279. box.box_model().margin.top = computed_values.margin().top.resolved_or_zero(box, containing_block.width()).to_px(box);
  280. box.box_model().margin.bottom = computed_values.margin().bottom.resolved_or_zero(box, containing_block.width()).to_px(box);
  281. box.box_model().border.top = computed_values.border_top().width;
  282. box.box_model().border.bottom = computed_values.border_bottom().width;
  283. box.box_model().padding.top = computed_values.padding().top.resolved_or_zero(box, containing_block.width()).to_px(box);
  284. box.box_model().padding.bottom = computed_values.padding().bottom.resolved_or_zero(box, containing_block.width()).to_px(box);
  285. auto height = compute_theoretical_height(box);
  286. box.set_height(height);
  287. }
  288. void BlockFormattingContext::compute_position(Box& box)
  289. {
  290. // 9.4.3 Relative positioning
  291. // Once a box has been laid out according to the normal flow or floated, it may be shifted relative to this position.
  292. auto& box_model = box.box_model();
  293. auto& computed_values = box.computed_values();
  294. float width_of_containing_block = box.width_of_logical_containing_block();
  295. auto specified_left = computed_values.offset().left.resolved_or_zero(box, width_of_containing_block);
  296. auto specified_right = computed_values.offset().right.resolved_or_zero(box, width_of_containing_block);
  297. if (specified_left.is_auto() && specified_right.is_auto()) {
  298. // If both 'left' and 'right' are 'auto' (their initial values), the used values are '0' (i.e., the boxes stay in their original position).
  299. box_model.offset.left = 0;
  300. box_model.offset.right = 0;
  301. } else if (specified_left.is_auto()) {
  302. // If 'left' is 'auto', its used value is minus the value of 'right' (i.e., the boxes move to the left by the value of 'right').
  303. box_model.offset.right = specified_right.to_px(box);
  304. box_model.offset.left = 0 - box_model.offset.right;
  305. } else if (specified_right.is_auto()) {
  306. // If 'right' is specified as 'auto', its used value is minus the value of 'left'.
  307. box_model.offset.left = specified_left.to_px(box);
  308. box_model.offset.right = 0 - box_model.offset.left;
  309. } else {
  310. // If neither 'left' nor 'right' is 'auto', the position is over-constrained, and one of them has to be ignored.
  311. // If the 'direction' property of the containing block is 'ltr', the value of 'left' wins and 'right' becomes -'left'.
  312. // If 'direction' of the containing block is 'rtl', 'right' wins and 'left' is ignored.
  313. // FIXME: Check direction (assuming 'ltr' for now).
  314. box_model.offset.left = specified_left.to_px(box);
  315. box_model.offset.right = 0 - box_model.offset.left;
  316. }
  317. }
  318. void BlockFormattingContext::layout_inline_children(Box& box, LayoutMode layout_mode)
  319. {
  320. InlineFormattingContext context(box, this);
  321. context.run(box, layout_mode);
  322. }
  323. void BlockFormattingContext::layout_block_level_children(Box& box, LayoutMode layout_mode)
  324. {
  325. float content_height = 0;
  326. float content_width = 0;
  327. box.for_each_child_of_type<Box>([&](auto& child_box) {
  328. if (child_box.is_absolutely_positioned())
  329. return IterationDecision::Continue;
  330. if (child_box.is_floating()) {
  331. layout_floating_child(child_box, box);
  332. return IterationDecision::Continue;
  333. }
  334. compute_width(child_box);
  335. layout_inside(child_box, layout_mode);
  336. compute_height(child_box);
  337. if (child_box.computed_values().position() == CSS::Position::Relative)
  338. compute_position(child_box);
  339. if (is<ReplacedBox>(child_box))
  340. place_block_level_replaced_element_in_normal_flow(child_box, box);
  341. else if (is<BlockBox>(child_box))
  342. place_block_level_non_replaced_element_in_normal_flow(child_box, box);
  343. // FIXME: This should be factored differently. It's uncool that we mutate the tree *during* layout!
  344. // Instead, we should generate the marker box during the tree build.
  345. if (is<ListItemBox>(child_box))
  346. verify_cast<ListItemBox>(child_box).layout_marker();
  347. content_height = max(content_height, child_box.effective_offset().y() + child_box.height() + child_box.box_model().margin_box().bottom);
  348. content_width = max(content_width, verify_cast<Box>(child_box).width());
  349. return IterationDecision::Continue;
  350. });
  351. if (layout_mode != LayoutMode::Default) {
  352. if (box.computed_values().width().is_undefined() || box.computed_values().width().is_auto())
  353. box.set_width(content_width);
  354. }
  355. }
  356. void BlockFormattingContext::place_block_level_replaced_element_in_normal_flow(Box& child_box, Box& containing_block)
  357. {
  358. VERIFY(!containing_block.is_absolutely_positioned());
  359. auto& replaced_element_box_model = child_box.box_model();
  360. replaced_element_box_model.margin.top = child_box.computed_values().margin().top.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  361. replaced_element_box_model.margin.bottom = child_box.computed_values().margin().bottom.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  362. replaced_element_box_model.border.top = child_box.computed_values().border_top().width;
  363. replaced_element_box_model.border.bottom = child_box.computed_values().border_bottom().width;
  364. replaced_element_box_model.padding.top = child_box.computed_values().padding().top.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  365. replaced_element_box_model.padding.bottom = child_box.computed_values().padding().bottom.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  366. float x = replaced_element_box_model.margin.left
  367. + replaced_element_box_model.border.left
  368. + replaced_element_box_model.padding.left
  369. + replaced_element_box_model.offset.left;
  370. float y = replaced_element_box_model.margin_box().top + containing_block.box_model().offset.top;
  371. child_box.set_offset(x, y);
  372. }
  373. void BlockFormattingContext::place_block_level_non_replaced_element_in_normal_flow(Box& child_box, Box& containing_block)
  374. {
  375. auto& box_model = child_box.box_model();
  376. auto& computed_values = child_box.computed_values();
  377. box_model.margin.top = computed_values.margin().top.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  378. box_model.margin.bottom = computed_values.margin().bottom.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  379. box_model.border.top = computed_values.border_top().width;
  380. box_model.border.bottom = computed_values.border_bottom().width;
  381. box_model.padding.top = computed_values.padding().top.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  382. box_model.padding.bottom = computed_values.padding().bottom.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  383. float x = box_model.margin.left
  384. + box_model.border.left
  385. + box_model.padding.left
  386. + box_model.offset.left;
  387. if (containing_block.computed_values().text_align() == CSS::TextAlign::LibwebCenter) {
  388. x = (containing_block.width() / 2) - child_box.width() / 2;
  389. }
  390. float y = box_model.margin_box().top
  391. + box_model.offset.top;
  392. // NOTE: Empty (0-height) preceding siblings have their margins collapsed with *their* preceding sibling, etc.
  393. float collapsed_bottom_margin_of_preceding_siblings = 0;
  394. auto* relevant_sibling = child_box.previous_sibling_of_type<Layout::BlockBox>();
  395. while (relevant_sibling != nullptr) {
  396. if (!relevant_sibling->is_absolutely_positioned() && !relevant_sibling->is_floating()) {
  397. collapsed_bottom_margin_of_preceding_siblings = max(collapsed_bottom_margin_of_preceding_siblings, relevant_sibling->box_model().margin.bottom);
  398. if (relevant_sibling->border_box_height() > 0)
  399. break;
  400. }
  401. relevant_sibling = relevant_sibling->previous_sibling();
  402. }
  403. if (relevant_sibling) {
  404. y += relevant_sibling->effective_offset().y()
  405. + relevant_sibling->height()
  406. + relevant_sibling->box_model().border_box().bottom;
  407. // Collapse top margin with bottom margin of preceding siblings if needed
  408. float my_margin_top = box_model.margin.top;
  409. if (my_margin_top < 0 || collapsed_bottom_margin_of_preceding_siblings < 0) {
  410. // Negative margins present.
  411. float largest_negative_margin = -min(my_margin_top, collapsed_bottom_margin_of_preceding_siblings);
  412. 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);
  413. float final_margin = largest_positive_margin - largest_negative_margin;
  414. y += final_margin - my_margin_top;
  415. } else if (collapsed_bottom_margin_of_preceding_siblings > my_margin_top) {
  416. // Sibling's margin is larger than mine, adjust so we use sibling's.
  417. y += collapsed_bottom_margin_of_preceding_siblings - my_margin_top;
  418. }
  419. }
  420. auto clear_floating_boxes = [&](auto& floating_boxes) {
  421. if (!floating_boxes.is_empty()) {
  422. float clearance_y = 0;
  423. for (auto* floating_box : floating_boxes) {
  424. clearance_y = max(clearance_y, floating_box->effective_offset().y() + floating_box->border_box_height());
  425. }
  426. y = max(y, clearance_y);
  427. floating_boxes.clear();
  428. }
  429. };
  430. // Flex-items don't float and also don't clear.
  431. if ((computed_values.clear() == CSS::Clear::Left || computed_values.clear() == CSS::Clear::Both) && !child_box.is_flex_item())
  432. clear_floating_boxes(m_left_floating_boxes);
  433. if ((computed_values.clear() == CSS::Clear::Right || computed_values.clear() == CSS::Clear::Both) && !child_box.is_flex_item())
  434. clear_floating_boxes(m_right_floating_boxes);
  435. child_box.set_offset(x, y);
  436. }
  437. void BlockFormattingContext::layout_initial_containing_block(LayoutMode layout_mode)
  438. {
  439. auto viewport_rect = context_box().browsing_context().viewport_rect();
  440. auto& icb = verify_cast<Layout::InitialContainingBlockBox>(context_box());
  441. icb.build_stacking_context_tree();
  442. icb.set_width(viewport_rect.width());
  443. layout_block_level_children(context_box(), layout_mode);
  444. VERIFY(!icb.children_are_inline());
  445. // FIXME: The ICB should have the height of the viewport.
  446. // Instead of auto-sizing the ICB, we should spill into overflow.
  447. float lowest_bottom = 0;
  448. icb.for_each_child_of_type<Box>([&](auto& child) {
  449. lowest_bottom = max(lowest_bottom, child.absolute_rect().bottom());
  450. });
  451. // FIXME: This is a hack and should be managed by an overflow mechanism.
  452. icb.set_height(max(static_cast<float>(viewport_rect.height()), lowest_bottom));
  453. }
  454. static Gfx::FloatRect rect_in_coordinate_space(const Box& box, const Box& context_box)
  455. {
  456. Gfx::FloatRect rect = box.margin_box_as_relative_rect();
  457. for (auto* ancestor = box.parent(); ancestor; ancestor = ancestor->parent()) {
  458. if (is<Box>(*ancestor)) {
  459. auto offset = verify_cast<Box>(*ancestor).effective_offset();
  460. rect.translate_by(offset);
  461. }
  462. if (ancestor == &context_box)
  463. break;
  464. }
  465. return rect;
  466. }
  467. void BlockFormattingContext::layout_floating_child(Box& box, Box& containing_block)
  468. {
  469. VERIFY(box.is_floating());
  470. compute_width(box);
  471. layout_inside(box, LayoutMode::Default);
  472. compute_height(box);
  473. // First we place the box normally (to get the right y coordinate.)
  474. place_block_level_non_replaced_element_in_normal_flow(box, containing_block);
  475. // Then we float it to the left or right.
  476. float x = box.effective_offset().x();
  477. auto box_in_context_rect = rect_in_coordinate_space(box, context_box());
  478. float y_in_context_box = box_in_context_rect.y();
  479. // Next, float to the left and/or right
  480. if (box.computed_values().float_() == CSS::Float::Left) {
  481. if (!m_left_floating_boxes.is_empty()) {
  482. auto& previous_floating_box = *m_left_floating_boxes.last();
  483. auto previous_rect = rect_in_coordinate_space(previous_floating_box, context_box());
  484. if (previous_rect.contains_vertically(y_in_context_box)) {
  485. // This box touches another already floating box. Stack to the right.
  486. x = previous_floating_box.margin_box_as_relative_rect().x() + previous_floating_box.margin_box_as_relative_rect().width() + box.box_model().margin_box().left;
  487. } else {
  488. // This box does not touch another floating box, go all the way to the left.
  489. x = box.box_model().margin_box().left;
  490. // Also, forget all previous left-floating boxes while we're here since they're no longer relevant.
  491. m_left_floating_boxes.clear();
  492. }
  493. } else {
  494. // This is the first left-floating box. Go all the way to the left.
  495. x = box.box_model().margin_box().left;
  496. }
  497. m_left_floating_boxes.append(&box);
  498. } else if (box.computed_values().float_() == CSS::Float::Right) {
  499. if (!m_right_floating_boxes.is_empty()) {
  500. auto& previous_floating_box = *m_right_floating_boxes.last();
  501. auto previous_rect = rect_in_coordinate_space(previous_floating_box, context_box());
  502. if (previous_rect.contains_vertically(y_in_context_box)) {
  503. // This box touches another already floating box. Stack to the left.
  504. x = previous_floating_box.margin_box_as_relative_rect().x() - box.box_model().margin_box().right - box.width();
  505. } else {
  506. // This box does not touch another floating box, go all the way to the right.
  507. x = containing_block.width() - box.box_model().margin_box().right - box.width();
  508. // Also, forget all previous right-floating boxes while we're here since they're no longer relevant.
  509. m_right_floating_boxes.clear();
  510. }
  511. } else {
  512. // This is the first right-floating box. Go all the way to the right.
  513. x = containing_block.width() - box.box_model().margin_box().right - box.width();
  514. }
  515. m_right_floating_boxes.append(&box);
  516. }
  517. box.set_offset(x, box.effective_offset().y());
  518. }
  519. }