BlockFormattingContext.cpp 30 KB

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