BlockFormattingContext.cpp 25 KB

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