BlockFormattingContext.cpp 32 KB

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