BlockFormattingContext.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 (box.is_absolutely_positioned() || box.is_floating())
  248. return IterationDecision::Continue;
  249. float child_box_top = child_box.effective_offset().y() - child_box.box_model().margin_box().top;
  250. float child_box_bottom = child_box.effective_offset().y() + child_box.height() + child_box.box_model().margin_box().bottom;
  251. if (!top.has_value() || child_box_top < top.value())
  252. top = child_box_top;
  253. if (!bottom.has_value() || child_box_bottom > bottom.value())
  254. bottom = child_box_bottom;
  255. return IterationDecision::Continue;
  256. });
  257. }
  258. return bottom.value_or(0) - top.value_or(0);
  259. }
  260. void BlockFormattingContext::compute_height(Box& box)
  261. {
  262. auto& computed_values = box.computed_values();
  263. auto& containing_block = *box.containing_block();
  264. // First, resolve the top/bottom parts of the surrounding box model.
  265. box.box_model().margin.top = computed_values.margin().top.resolved_or_zero(box, containing_block.width()).to_px(box);
  266. box.box_model().margin.bottom = computed_values.margin().bottom.resolved_or_zero(box, containing_block.width()).to_px(box);
  267. box.box_model().border.top = computed_values.border_top().width;
  268. box.box_model().border.bottom = computed_values.border_bottom().width;
  269. box.box_model().padding.top = computed_values.padding().top.resolved_or_zero(box, containing_block.width()).to_px(box);
  270. box.box_model().padding.bottom = computed_values.padding().bottom.resolved_or_zero(box, containing_block.width()).to_px(box);
  271. // Then work out what the height is, based on box type and CSS properties.
  272. float height = 0;
  273. if (is<ReplacedBox>(box)) {
  274. height = compute_height_for_replaced_element(downcast<ReplacedBox>(box));
  275. } else {
  276. if (box.computed_values().height().is_undefined_or_auto()) {
  277. height = compute_auto_height_for_block_level_element(box);
  278. } else {
  279. CSS::Length specified_height;
  280. if (computed_values.height().is_percentage() && !containing_block.computed_values().height().is_absolute()) {
  281. specified_height = CSS::Length::make_auto();
  282. } else {
  283. specified_height = computed_values.height().resolved_or_auto(box, containing_block.height());
  284. }
  285. auto specified_max_height = computed_values.max_height().resolved_or_auto(box, containing_block.height());
  286. if (!specified_height.is_auto()) {
  287. float used_height = specified_height.to_px(box);
  288. if (!specified_max_height.is_auto())
  289. used_height = min(used_height, specified_max_height.to_px(box));
  290. height = used_height;
  291. }
  292. }
  293. }
  294. box.set_height(height);
  295. }
  296. void BlockFormattingContext::layout_inline_children(Box& box, LayoutMode layout_mode)
  297. {
  298. InlineFormattingContext context(box, this);
  299. context.run(box, layout_mode);
  300. }
  301. void BlockFormattingContext::layout_block_level_children(Box& box, LayoutMode layout_mode)
  302. {
  303. float content_height = 0;
  304. float content_width = 0;
  305. box.for_each_child_of_type<Box>([&](auto& child_box) {
  306. if (child_box.is_absolutely_positioned())
  307. return IterationDecision::Continue;
  308. if (child_box.is_floating()) {
  309. layout_floating_child(child_box, box);
  310. return IterationDecision::Continue;
  311. }
  312. compute_width(child_box);
  313. layout_inside(child_box, layout_mode);
  314. compute_height(child_box);
  315. if (is<ReplacedBox>(child_box))
  316. place_block_level_replaced_element_in_normal_flow(child_box, box);
  317. else if (is<BlockBox>(child_box))
  318. place_block_level_non_replaced_element_in_normal_flow(child_box, box);
  319. // FIXME: This should be factored differently. It's uncool that we mutate the tree *during* layout!
  320. // Instead, we should generate the marker box during the tree build.
  321. if (is<ListItemBox>(child_box))
  322. downcast<ListItemBox>(child_box).layout_marker();
  323. content_height = max(content_height, child_box.effective_offset().y() + child_box.height() + child_box.box_model().margin_box().bottom);
  324. content_width = max(content_width, downcast<Box>(child_box).width());
  325. return IterationDecision::Continue;
  326. });
  327. if (layout_mode != LayoutMode::Default) {
  328. if (box.computed_values().width().is_undefined() || box.computed_values().width().is_auto())
  329. box.set_width(content_width);
  330. }
  331. }
  332. void BlockFormattingContext::place_block_level_replaced_element_in_normal_flow(Box& child_box, Box& containing_block)
  333. {
  334. VERIFY(!containing_block.is_absolutely_positioned());
  335. auto& replaced_element_box_model = child_box.box_model();
  336. replaced_element_box_model.margin.top = child_box.computed_values().margin().top.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  337. replaced_element_box_model.margin.bottom = child_box.computed_values().margin().bottom.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  338. replaced_element_box_model.border.top = child_box.computed_values().border_top().width;
  339. replaced_element_box_model.border.bottom = child_box.computed_values().border_bottom().width;
  340. replaced_element_box_model.padding.top = child_box.computed_values().padding().top.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  341. replaced_element_box_model.padding.bottom = child_box.computed_values().padding().bottom.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  342. float x = replaced_element_box_model.margin.left
  343. + replaced_element_box_model.border.left
  344. + replaced_element_box_model.padding.left
  345. + replaced_element_box_model.offset.left;
  346. float y = replaced_element_box_model.margin_box().top + containing_block.box_model().offset.top;
  347. child_box.set_offset(x, y);
  348. }
  349. void BlockFormattingContext::place_block_level_non_replaced_element_in_normal_flow(Box& child_box, Box& containing_block)
  350. {
  351. auto& box_model = child_box.box_model();
  352. auto& computed_values = child_box.computed_values();
  353. box_model.margin.top = computed_values.margin().top.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  354. box_model.margin.bottom = computed_values.margin().bottom.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  355. box_model.border.top = computed_values.border_top().width;
  356. box_model.border.bottom = computed_values.border_bottom().width;
  357. box_model.padding.top = computed_values.padding().top.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  358. box_model.padding.bottom = computed_values.padding().bottom.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  359. float x = box_model.margin.left
  360. + box_model.border.left
  361. + box_model.padding.left
  362. + box_model.offset.left;
  363. if (containing_block.computed_values().text_align() == CSS::TextAlign::LibwebCenter) {
  364. x = (containing_block.width() / 2) - child_box.width() / 2;
  365. }
  366. float y = box_model.margin_box().top
  367. + box_model.offset.top;
  368. // NOTE: Empty (0-height) preceding siblings have their margins collapsed with *their* preceding sibling, etc.
  369. float collapsed_bottom_margin_of_preceding_siblings = 0;
  370. auto* relevant_sibling = child_box.previous_sibling_of_type<Layout::BlockBox>();
  371. while (relevant_sibling != nullptr) {
  372. if (!relevant_sibling->is_absolutely_positioned() && !relevant_sibling->is_floating()) {
  373. collapsed_bottom_margin_of_preceding_siblings = max(collapsed_bottom_margin_of_preceding_siblings, relevant_sibling->box_model().margin.bottom);
  374. if (relevant_sibling->border_box_height() > 0)
  375. break;
  376. }
  377. relevant_sibling = relevant_sibling->previous_sibling();
  378. }
  379. if (relevant_sibling) {
  380. y += relevant_sibling->effective_offset().y()
  381. + relevant_sibling->height()
  382. + relevant_sibling->box_model().border_box().bottom;
  383. // Collapse top margin with bottom margin of preceding siblings if needed
  384. float my_margin_top = box_model.margin.top;
  385. if (my_margin_top < 0 || collapsed_bottom_margin_of_preceding_siblings < 0) {
  386. // Negative margins present.
  387. float largest_negative_margin = -min(my_margin_top, collapsed_bottom_margin_of_preceding_siblings);
  388. 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);
  389. float final_margin = largest_positive_margin - largest_negative_margin;
  390. y += final_margin - my_margin_top;
  391. } else if (collapsed_bottom_margin_of_preceding_siblings > my_margin_top) {
  392. // Sibling's margin is larger than mine, adjust so we use sibling's.
  393. y += collapsed_bottom_margin_of_preceding_siblings - my_margin_top;
  394. }
  395. }
  396. if (child_box.computed_values().clear() == CSS::Clear::Left || child_box.computed_values().clear() == CSS::Clear::Both) {
  397. if (!m_left_floating_boxes.is_empty()) {
  398. float clearance_y = 0;
  399. for (auto* floating_box : m_left_floating_boxes) {
  400. clearance_y = max(clearance_y, floating_box->effective_offset().y() + floating_box->box_model().margin_box().bottom);
  401. }
  402. y = max(y, clearance_y);
  403. m_left_floating_boxes.clear();
  404. }
  405. }
  406. if (child_box.computed_values().clear() == CSS::Clear::Right || child_box.computed_values().clear() == CSS::Clear::Both) {
  407. if (!m_right_floating_boxes.is_empty()) {
  408. float clearance_y = 0;
  409. for (auto* floating_box : m_right_floating_boxes) {
  410. clearance_y = max(clearance_y, floating_box->effective_offset().y() + floating_box->box_model().margin_box().bottom);
  411. }
  412. y = max(y, clearance_y);
  413. m_right_floating_boxes.clear();
  414. }
  415. }
  416. child_box.set_offset(x, y);
  417. }
  418. void BlockFormattingContext::layout_initial_containing_block(LayoutMode layout_mode)
  419. {
  420. auto viewport_rect = context_box().frame().viewport_rect();
  421. auto& icb = downcast<Layout::InitialContainingBlockBox>(context_box());
  422. icb.build_stacking_context_tree();
  423. icb.set_width(viewport_rect.width());
  424. layout_block_level_children(context_box(), layout_mode);
  425. VERIFY(!icb.children_are_inline());
  426. // FIXME: The ICB should have the height of the viewport.
  427. // Instead of auto-sizing the ICB, we should spill into overflow.
  428. float lowest_bottom = 0;
  429. icb.for_each_child_of_type<Box>([&](auto& child) {
  430. lowest_bottom = max(lowest_bottom, child.absolute_rect().bottom());
  431. });
  432. // FIXME: This is a hack and should be managed by an overflow mechanism.
  433. icb.set_height(max(static_cast<float>(viewport_rect.height()), lowest_bottom));
  434. }
  435. static Gfx::FloatRect rect_in_coordinate_space(const Box& box, const Box& context_box)
  436. {
  437. Gfx::FloatRect rect { box.effective_offset(), box.size() };
  438. for (auto* ancestor = box.parent(); ancestor; ancestor = ancestor->parent()) {
  439. if (is<Box>(*ancestor)) {
  440. auto offset = downcast<Box>(*ancestor).effective_offset();
  441. rect.move_by(offset);
  442. }
  443. if (ancestor == &context_box)
  444. break;
  445. }
  446. return rect;
  447. }
  448. void BlockFormattingContext::layout_floating_child(Box& box, Box& containing_block)
  449. {
  450. VERIFY(box.is_floating());
  451. compute_width(box);
  452. layout_inside(box, LayoutMode::Default);
  453. compute_height(box);
  454. // First we place the box normally (to get the right y coordinate.)
  455. place_block_level_non_replaced_element_in_normal_flow(box, containing_block);
  456. // Then we float it to the left or right.
  457. float x = box.effective_offset().x();
  458. auto box_in_context_rect = rect_in_coordinate_space(box, context_box());
  459. float y_in_context_box = box_in_context_rect.y();
  460. // Next, float to the left and/or right
  461. if (box.computed_values().float_() == CSS::Float::Left) {
  462. if (!m_left_floating_boxes.is_empty()) {
  463. auto& previous_floating_box = *m_left_floating_boxes.last();
  464. auto previous_rect = rect_in_coordinate_space(previous_floating_box, context_box());
  465. if (previous_rect.contains_vertically(y_in_context_box)) {
  466. // This box touches another already floating box. Stack to the right.
  467. x = previous_floating_box.effective_offset().x() + previous_floating_box.width();
  468. } else {
  469. // This box does not touch another floating box, go all the way to the left.
  470. x = 0;
  471. // Also, forget all previous left-floating boxes while we're here since they're no longer relevant.
  472. m_left_floating_boxes.clear();
  473. }
  474. } else {
  475. // This is the first left-floating box. Go all the way to the left.
  476. x = 0;
  477. }
  478. m_left_floating_boxes.append(&box);
  479. } else if (box.computed_values().float_() == CSS::Float::Right) {
  480. if (!m_right_floating_boxes.is_empty()) {
  481. auto& previous_floating_box = *m_right_floating_boxes.last();
  482. auto previous_rect = rect_in_coordinate_space(previous_floating_box, context_box());
  483. if (previous_rect.contains_vertically(y_in_context_box)) {
  484. // This box touches another already floating box. Stack to the left.
  485. x = previous_floating_box.effective_offset().x() - box.width();
  486. } else {
  487. // This box does not touch another floating box, go all the way to the right.
  488. x = containing_block.width() - box.width();
  489. // Also, forget all previous right-floating boxes while we're here since they're no longer relevant.
  490. m_right_floating_boxes.clear();
  491. }
  492. } else {
  493. // This is the first right-floating box. Go all the way to the right.
  494. x = containing_block.width() - box.width();
  495. }
  496. m_right_floating_boxes.append(&box);
  497. }
  498. box.set_offset(x, box.effective_offset().y());
  499. }
  500. }