BlockFormattingContext.cpp 30 KB

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