BlockFormattingContext.cpp 33 KB

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