BlockFormattingContext.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TemporaryChange.h>
  7. #include <LibWeb/CSS/Length.h>
  8. #include <LibWeb/DOM/Node.h>
  9. #include <LibWeb/Dump.h>
  10. #include <LibWeb/HTML/BrowsingContext.h>
  11. #include <LibWeb/Layout/BlockContainer.h>
  12. #include <LibWeb/Layout/BlockFormattingContext.h>
  13. #include <LibWeb/Layout/Box.h>
  14. #include <LibWeb/Layout/InlineFormattingContext.h>
  15. #include <LibWeb/Layout/LineBuilder.h>
  16. #include <LibWeb/Layout/ListItemBox.h>
  17. #include <LibWeb/Layout/ListItemMarkerBox.h>
  18. #include <LibWeb/Layout/ReplacedBox.h>
  19. #include <LibWeb/Layout/TableBox.h>
  20. #include <LibWeb/Layout/TableWrapper.h>
  21. #include <LibWeb/Layout/Viewport.h>
  22. namespace Web::Layout {
  23. BlockFormattingContext::BlockFormattingContext(LayoutState& state, BlockContainer const& root, FormattingContext* parent)
  24. : FormattingContext(Type::Block, state, root, parent)
  25. {
  26. }
  27. BlockFormattingContext::~BlockFormattingContext()
  28. {
  29. if (!m_was_notified_after_parent_dimensioned_my_root_box) {
  30. // HACK: The parent formatting context never notified us after assigning dimensions to our root box.
  31. // Pretend that it did anyway, to make sure absolutely positioned children get laid out.
  32. // FIXME: Get rid of this hack once parent contexts behave properly.
  33. parent_context_did_dimension_child_root_box();
  34. }
  35. }
  36. CSSPixels BlockFormattingContext::automatic_content_width() const
  37. {
  38. return greatest_child_width(root());
  39. }
  40. CSSPixels BlockFormattingContext::automatic_content_height() const
  41. {
  42. return compute_auto_height_for_block_formatting_context_root(root());
  43. }
  44. static bool margins_collapse_through(Box const& box, LayoutState& state)
  45. {
  46. // FIXME: A box's own margins collapse if the 'min-height' property is zero, and it has neither top or bottom borders
  47. // nor top or bottom padding, and it has a 'height' of either 0 or 'auto', and it does not contain a line box, and
  48. // all of its in-flow children's margins (if any) collapse.
  49. // https://www.w3.org/TR/CSS22/box.html#collapsing-margins
  50. return state.get(box).border_box_height() == 0;
  51. }
  52. void BlockFormattingContext::run(Box const&, LayoutMode layout_mode, AvailableSpace const& available_space)
  53. {
  54. if (is<Viewport>(root())) {
  55. layout_viewport(layout_mode, available_space);
  56. return;
  57. }
  58. if (root().children_are_inline())
  59. layout_inline_children(root(), layout_mode, available_space);
  60. else
  61. layout_block_level_children(root(), layout_mode, available_space);
  62. // Assign collapsed margin left after children layout of formatting context to the last child box
  63. if (m_margin_state.current_collapsed_margin() != 0) {
  64. for (auto* child_box = root().last_child_of_type<Box>(); child_box; child_box = child_box->previous_sibling_of_type<Box>()) {
  65. if (child_box->is_absolutely_positioned() || child_box->is_floating())
  66. continue;
  67. if (margins_collapse_through(*child_box, m_state))
  68. continue;
  69. m_state.get_mutable(*child_box).margin_bottom = m_margin_state.current_collapsed_margin().value();
  70. break;
  71. }
  72. }
  73. }
  74. void BlockFormattingContext::parent_context_did_dimension_child_root_box()
  75. {
  76. m_was_notified_after_parent_dimensioned_my_root_box = true;
  77. // Left-side floats: offset_from_edge is from left edge (0) to left content edge of floating_box.
  78. for (auto& floating_box : m_left_floats.all_boxes) {
  79. auto& box_state = m_state.get_mutable(floating_box->box);
  80. box_state.set_content_x(floating_box->offset_from_edge.value());
  81. }
  82. // Right-side floats: offset_from_edge is from right edge (float_containing_block_width) to the left content edge of floating_box.
  83. for (auto& floating_box : m_right_floats.all_boxes) {
  84. auto float_containing_block_width = containing_block_width_for(floating_box->box);
  85. auto& box_state = m_state.get_mutable(floating_box->box);
  86. box_state.set_content_x((float_containing_block_width - floating_box->offset_from_edge).value());
  87. }
  88. // We can also layout absolutely positioned boxes within this BFC.
  89. for (auto& box : m_absolutely_positioned_boxes) {
  90. auto& cb_state = m_state.get(*box->containing_block());
  91. auto available_width = AvailableSize::make_definite(cb_state.content_width() + cb_state.padding_left + cb_state.padding_right);
  92. auto available_height = AvailableSize::make_definite(cb_state.content_height() + cb_state.padding_top + cb_state.padding_bottom);
  93. layout_absolutely_positioned_element(box, AvailableSpace(available_width, available_height));
  94. }
  95. }
  96. void BlockFormattingContext::compute_width(Box const& box, AvailableSpace const& available_space, LayoutMode)
  97. {
  98. if (box.is_absolutely_positioned()) {
  99. compute_width_for_absolutely_positioned_element(box, available_space);
  100. return;
  101. }
  102. if (is<ReplacedBox>(box)) {
  103. // FIXME: This should not be done *by* ReplacedBox
  104. auto& replaced = verify_cast<ReplacedBox>(box);
  105. // FIXME: This const_cast is gross.
  106. const_cast<ReplacedBox&>(replaced).prepare_for_replaced_layout();
  107. compute_width_for_block_level_replaced_element_in_normal_flow(replaced, available_space);
  108. // NOTE: We don't return here.
  109. }
  110. if (box.is_floating()) {
  111. compute_width_for_floating_box(box, available_space);
  112. return;
  113. }
  114. auto const& computed_values = box.computed_values();
  115. auto width_of_containing_block = available_space.width.to_px();
  116. auto width_of_containing_block_as_length_for_resolve = available_space.width.is_definite() ? CSS::Length::make_px(width_of_containing_block) : CSS::Length::make_px(0);
  117. auto zero_value = CSS::Length::make_px(0);
  118. auto margin_left = CSS::Length::make_auto();
  119. auto margin_right = CSS::Length::make_auto();
  120. auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block_as_length_for_resolve).resolved(box);
  121. auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block_as_length_for_resolve).resolved(box);
  122. auto& box_state = m_state.get_mutable(box);
  123. box_state.border_left = computed_values.border_left().width;
  124. box_state.border_right = computed_values.border_right().width;
  125. box_state.padding_left = padding_left.to_px(box);
  126. box_state.padding_right = padding_right.to_px(box);
  127. // NOTE: If we are calculating the min-content or max-content width of this box,
  128. // and the width should be treated as auto, then we can simply return here,
  129. // as the preferred width and min/max constraints are irrelevant for intrinsic sizing.
  130. if (box_state.width_constraint != SizeConstraint::None)
  131. return;
  132. auto try_compute_width = [&](auto const& a_width) {
  133. CSS::Length width = a_width;
  134. margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length_for_resolve).resolved(box);
  135. margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length_for_resolve).resolved(box);
  136. CSSPixels total_px = computed_values.border_left().width + computed_values.border_right().width;
  137. for (auto& value : { margin_left, padding_left, width, padding_right, margin_right }) {
  138. total_px += value.to_px(box);
  139. }
  140. if (!box.is_inline()) {
  141. // 10.3.3 Block-level, non-replaced elements in normal flow
  142. // 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.
  143. if (width.is_auto() && total_px > width_of_containing_block) {
  144. if (margin_left.is_auto())
  145. margin_left = zero_value;
  146. if (margin_right.is_auto())
  147. margin_right = zero_value;
  148. }
  149. // 10.3.3 cont'd.
  150. auto underflow_px = width_of_containing_block - total_px;
  151. if (available_space.width.is_intrinsic_sizing_constraint())
  152. underflow_px = 0;
  153. if (width.is_auto()) {
  154. if (margin_left.is_auto())
  155. margin_left = zero_value;
  156. if (margin_right.is_auto())
  157. margin_right = zero_value;
  158. if (available_space.width.is_definite()) {
  159. if (underflow_px >= 0) {
  160. width = CSS::Length::make_px(underflow_px);
  161. } else {
  162. width = zero_value;
  163. margin_right = CSS::Length::make_px(margin_right.to_px(box) + underflow_px.value());
  164. }
  165. }
  166. } else {
  167. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  168. margin_right = CSS::Length::make_px(margin_right.to_px(box) + underflow_px.value());
  169. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  170. margin_right = CSS::Length::make_px(underflow_px);
  171. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  172. margin_left = CSS::Length::make_px(underflow_px);
  173. } else { // margin_left.is_auto() && margin_right.is_auto()
  174. auto half_of_the_underflow = CSS::Length::make_px(underflow_px / 2);
  175. margin_left = half_of_the_underflow;
  176. margin_right = half_of_the_underflow;
  177. }
  178. }
  179. }
  180. return width;
  181. };
  182. auto input_width = [&] {
  183. if (is<ReplacedBox>(box)) {
  184. // NOTE: Replaced elements had their width calculated independently above.
  185. // We use that width as the input here to ensure that margins get resolved.
  186. return CSS::Length::make_px(box_state.content_width());
  187. }
  188. if (is<TableWrapper>(box))
  189. return CSS::Length::make_px(compute_width_for_table_wrapper(box, available_space));
  190. if (should_treat_width_as_auto(box, available_space))
  191. return CSS::Length::make_auto();
  192. return calculate_inner_width(box, available_space.width, computed_values.width());
  193. }();
  194. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  195. auto used_width = try_compute_width(input_width);
  196. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  197. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  198. if (!computed_values.max_width().is_none()) {
  199. auto max_width = calculate_inner_width(box, available_space.width, computed_values.max_width());
  200. auto used_width_px = used_width.is_auto() ? available_space.width.to_px() : used_width.to_px(box);
  201. if (used_width_px > max_width.to_px(box)) {
  202. used_width = try_compute_width(max_width);
  203. }
  204. }
  205. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  206. // but this time using the value of 'min-width' as the computed value for 'width'.
  207. if (!computed_values.min_width().is_auto()) {
  208. auto min_width = calculate_inner_width(box, available_space.width, computed_values.min_width());
  209. auto used_width_px = used_width.is_auto() ? available_space.width.to_px() : used_width.to_px(box);
  210. if (used_width_px < min_width.to_px(box)) {
  211. used_width = try_compute_width(min_width);
  212. }
  213. }
  214. if (!is<ReplacedBox>(box) && !used_width.is_auto())
  215. box_state.set_content_width(used_width.to_px(box));
  216. box_state.margin_left = margin_left.to_px(box);
  217. box_state.margin_right = margin_right.to_px(box);
  218. resolve_vertical_box_model_metrics(box, m_state);
  219. }
  220. void BlockFormattingContext::compute_width_for_floating_box(Box const& box, AvailableSpace const& available_space)
  221. {
  222. // 10.3.5 Floating, non-replaced elements
  223. auto& computed_values = box.computed_values();
  224. auto zero_value = CSS::Length::make_px(0);
  225. auto width_of_containing_block = available_space.width.to_px();
  226. auto width_of_containing_block_as_length_for_resolve = CSS::Length::make_px(width_of_containing_block);
  227. if (!available_space.width.is_definite())
  228. width_of_containing_block_as_length_for_resolve = CSS::Length::make_px(0);
  229. auto margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length_for_resolve).resolved(box);
  230. auto margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length_for_resolve).resolved(box);
  231. auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block_as_length_for_resolve).resolved(box);
  232. auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block_as_length_for_resolve).resolved(box);
  233. // If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
  234. if (margin_left.is_auto())
  235. margin_left = zero_value;
  236. if (margin_right.is_auto())
  237. margin_right = zero_value;
  238. auto compute_width = [&](auto width) {
  239. // If 'width' is computed as 'auto', the used value is the "shrink-to-fit" width.
  240. if (width.is_auto()) {
  241. // Find the available width: in this case, this is the width of the containing
  242. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  243. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  244. auto available_width = width_of_containing_block
  245. - margin_left.to_px(box) - computed_values.border_left().width - padding_left.to_px(box)
  246. - padding_right.to_px(box) - computed_values.border_right().width - margin_right.to_px(box);
  247. auto result = calculate_shrink_to_fit_widths(box);
  248. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  249. width = CSS::Length::make_px(min(max(result.preferred_minimum_width, available_width), result.preferred_width));
  250. }
  251. return width;
  252. };
  253. auto input_width = [&] {
  254. if (should_treat_width_as_auto(box, available_space))
  255. return CSS::Length::make_auto();
  256. return calculate_inner_width(box, available_space.width, computed_values.width());
  257. }();
  258. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  259. auto width = compute_width(input_width);
  260. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  261. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  262. if (!computed_values.max_width().is_none()) {
  263. auto max_width = calculate_inner_width(box, available_space.width, computed_values.max_width());
  264. if (width.to_px(box) > max_width.to_px(box))
  265. width = compute_width(max_width);
  266. }
  267. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  268. // but this time using the value of 'min-width' as the computed value for 'width'.
  269. if (!computed_values.min_width().is_auto()) {
  270. auto min_width = calculate_inner_width(box, available_space.width, computed_values.min_width());
  271. if (width.to_px(box) < min_width.to_px(box))
  272. width = compute_width(min_width);
  273. }
  274. auto& box_state = m_state.get_mutable(box);
  275. box_state.set_content_width(width.to_px(box));
  276. box_state.margin_left = margin_left.to_px(box);
  277. box_state.margin_right = margin_right.to_px(box);
  278. box_state.border_left = computed_values.border_left().width;
  279. box_state.border_right = computed_values.border_right().width;
  280. box_state.padding_left = padding_left.to_px(box);
  281. box_state.padding_right = padding_right.to_px(box);
  282. resolve_vertical_box_model_metrics(box, m_state);
  283. }
  284. void BlockFormattingContext::compute_width_for_block_level_replaced_element_in_normal_flow(ReplacedBox const& box, AvailableSpace const& available_space)
  285. {
  286. m_state.get_mutable(box).set_content_width(compute_width_for_replaced_element(m_state, box, available_space));
  287. }
  288. CSSPixels BlockFormattingContext::compute_width_for_table_wrapper(Box const& box, AvailableSpace const& available_space)
  289. {
  290. // 17.5.2
  291. // Table wrapper width should be equal to width of table box it contains
  292. auto const& computed_values = box.computed_values();
  293. auto width_of_containing_block = available_space.width.to_px();
  294. auto width_of_containing_block_as_length_for_resolve = available_space.width.is_definite() ? CSS::Length::make_px(width_of_containing_block) : CSS::Length::make_px(0);
  295. auto zero_value = CSS::Length::make_px(0);
  296. auto margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length_for_resolve).resolved(box);
  297. auto margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length_for_resolve).resolved(box);
  298. // If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
  299. if (margin_left.is_auto())
  300. margin_left = zero_value;
  301. if (margin_right.is_auto())
  302. margin_right = zero_value;
  303. // table-wrapper can't have borders or paddings but it might have margin taken from table-root.
  304. auto available_width = width_of_containing_block - margin_left.to_px(box) - margin_right.to_px(box);
  305. LayoutState throwaway_state(&m_state);
  306. auto context = create_independent_formatting_context_if_needed(throwaway_state, box);
  307. VERIFY(context);
  308. context->run(box, LayoutMode::IntrinsicSizing, m_state.get(box).available_inner_space_or_constraints_from(available_space));
  309. auto const* table_box = box.first_child_of_type<TableBox>();
  310. auto table_used_width = throwaway_state.get(*table_box).content_width();
  311. return table_used_width > available_width ? available_width : table_used_width;
  312. }
  313. void BlockFormattingContext::compute_height(Box const& box, AvailableSpace const& available_space)
  314. {
  315. auto const& computed_values = box.computed_values();
  316. auto containing_block_height = CSS::Length::make_px(available_space.height.to_px());
  317. // Then work out what the height is, based on box type and CSS properties.
  318. CSSPixels height = 0;
  319. if (is<ReplacedBox>(box)) {
  320. height = compute_height_for_replaced_element(m_state, verify_cast<ReplacedBox>(box), available_space);
  321. } else {
  322. if (should_treat_height_as_auto(box, available_space)) {
  323. height = compute_auto_height_for_block_level_element(box, m_state.get(box).available_inner_space_or_constraints_from(available_space));
  324. } else {
  325. height = calculate_inner_height(box, available_space.height, computed_values.height()).to_px(box);
  326. }
  327. }
  328. if (!computed_values.max_height().is_none()) {
  329. auto max_height = calculate_inner_height(box, available_space.height, computed_values.max_height());
  330. if (!max_height.is_auto())
  331. height = min(height, max_height.to_px(box));
  332. }
  333. if (!computed_values.min_height().is_auto()) {
  334. height = max(height, calculate_inner_height(box, available_space.height, computed_values.min_height()).to_px(box));
  335. }
  336. m_state.get_mutable(box).set_content_height(height);
  337. }
  338. void BlockFormattingContext::layout_inline_children(BlockContainer const& block_container, LayoutMode layout_mode, AvailableSpace const& available_space)
  339. {
  340. VERIFY(block_container.children_are_inline());
  341. auto& block_container_state = m_state.get_mutable(block_container);
  342. InlineFormattingContext context(m_state, block_container, *this);
  343. context.run(
  344. block_container,
  345. layout_mode,
  346. available_space);
  347. if (!block_container_state.has_definite_width())
  348. block_container_state.set_content_width(context.automatic_content_width());
  349. if (!block_container_state.has_definite_height())
  350. block_container_state.set_content_height(context.automatic_content_height());
  351. }
  352. CSSPixels BlockFormattingContext::compute_auto_height_for_block_level_element(Box const& box, AvailableSpace const& available_space)
  353. {
  354. if (creates_block_formatting_context(box)) {
  355. return compute_auto_height_for_block_formatting_context_root(box);
  356. }
  357. auto const& box_state = m_state.get(box);
  358. auto display = box.display();
  359. if (display.is_flex_inside()) {
  360. // https://drafts.csswg.org/css-flexbox-1/#algo-main-container
  361. // NOTE: The automatic block size of a block-level flex container is its max-content size.
  362. return calculate_max_content_height(box, available_space.width);
  363. }
  364. if (display.is_grid_inside()) {
  365. // https://www.w3.org/TR/css-grid-2/#intrinsic-sizes
  366. // In both inline and block formatting contexts, the grid container’s auto block size is its
  367. // max-content size.
  368. return calculate_max_content_height(box, available_space.width);
  369. }
  370. if (display.is_table_inside()) {
  371. return calculate_max_content_height(box, available_space.height);
  372. }
  373. // https://www.w3.org/TR/CSS22/visudet.html#normal-block
  374. // 10.6.3 Block-level non-replaced elements in normal flow when 'overflow' computes to 'visible'
  375. // The element's height is the distance from its top content edge to the first applicable of the following:
  376. // 1. the bottom edge of the last line box, if the box establishes a inline formatting context with one or more lines
  377. if (box.children_are_inline() && !box_state.line_boxes.is_empty())
  378. return box_state.line_boxes.last().bottom().value();
  379. // 2. the bottom edge of the bottom (possibly collapsed) margin of its last in-flow child, if the child's bottom margin does not collapse with the element's bottom margin
  380. // 3. the bottom border edge of the last in-flow child whose top margin doesn't collapse with the element's bottom margin
  381. if (!box.children_are_inline()) {
  382. for (auto* child_box = box.last_child_of_type<Box>(); child_box; child_box = child_box->previous_sibling_of_type<Box>()) {
  383. if (child_box->is_absolutely_positioned() || child_box->is_floating())
  384. continue;
  385. // FIXME: This is hack. If the last child is a list-item marker box, we ignore it for purposes of height calculation.
  386. // Perhaps markers should not be considered in-flow(?) Perhaps they should always be the first child of the list-item
  387. // box instead of the last child.
  388. if (child_box->is_list_item_marker_box())
  389. continue;
  390. auto const& child_box_state = m_state.get(*child_box);
  391. // Ignore anonymous block containers with no lines. These don't count as in-flow block boxes.
  392. if (!child_box->is_table_wrapper() && child_box->is_anonymous() && child_box->is_block_container() && child_box_state.line_boxes.is_empty())
  393. continue;
  394. auto margin_bottom = m_margin_state.current_collapsed_margin();
  395. if (box_state.padding_bottom == 0 && box_state.border_bottom == 0) {
  396. m_margin_state.box_last_in_flow_child_margin_bottom_collapsed = true;
  397. margin_bottom = 0;
  398. }
  399. return max(0.0f, (child_box_state.offset.y() + child_box_state.content_height() + child_box_state.border_box_bottom() + margin_bottom).value());
  400. }
  401. }
  402. // 4. zero, otherwise
  403. return 0;
  404. }
  405. void BlockFormattingContext::layout_block_level_box(Box const& box, BlockContainer const& block_container, LayoutMode layout_mode, CSSPixels& bottom_of_lowest_margin_box, AvailableSpace const& available_space)
  406. {
  407. auto& box_state = m_state.get_mutable(box);
  408. if (box.is_absolutely_positioned()) {
  409. m_absolutely_positioned_boxes.append(box);
  410. return;
  411. }
  412. // NOTE: ListItemMarkerBoxes are placed by their corresponding ListItemBox.
  413. if (is<ListItemMarkerBox>(box))
  414. return;
  415. auto const y = m_y_offset_of_current_block_container.value();
  416. if (box.is_floating()) {
  417. auto margin_top = !m_margin_state.has_block_container_waiting_for_final_y_position() ? m_margin_state.current_collapsed_margin() : 0;
  418. layout_floating_box(box, block_container, layout_mode, available_space, margin_top + y);
  419. bottom_of_lowest_margin_box = max(bottom_of_lowest_margin_box, box_state.offset.y() + box_state.content_height() + box_state.margin_box_bottom());
  420. return;
  421. }
  422. compute_width(box, available_space, layout_mode);
  423. if (box_state.has_definite_height()) {
  424. compute_height(box, available_space);
  425. }
  426. if (box.computed_values().clear() != CSS::Clear::None) {
  427. m_margin_state.reset();
  428. }
  429. m_margin_state.add_margin(box_state.margin_top);
  430. m_margin_state.update_block_waiting_for_final_y_position();
  431. auto margin_top = m_margin_state.current_collapsed_margin();
  432. if (m_margin_state.has_block_container_waiting_for_final_y_position()) {
  433. // If first child margin top will collapse with margin-top of containing block then margin-top of child is 0
  434. margin_top = 0;
  435. }
  436. place_block_level_element_in_normal_flow_vertically(box, y + margin_top);
  437. place_block_level_element_in_normal_flow_horizontally(box, available_space);
  438. OwnPtr<FormattingContext> independent_formatting_context;
  439. if (!box.is_replaced_box() && box.has_children()) {
  440. independent_formatting_context = create_independent_formatting_context_if_needed(m_state, box);
  441. if (independent_formatting_context) {
  442. // Margins of elements that establish new formatting contexts do not collapse with their in-flow children
  443. m_margin_state.reset();
  444. independent_formatting_context->run(box, layout_mode, box_state.available_inner_space_or_constraints_from(available_space));
  445. } else {
  446. if (box.children_are_inline()) {
  447. layout_inline_children(verify_cast<BlockContainer>(box), layout_mode, box_state.available_inner_space_or_constraints_from(available_space));
  448. } else {
  449. if (box_state.border_top > 0 || box_state.padding_top > 0) {
  450. // margin-top of block container can't collapse with it's children if it has non zero border or padding
  451. m_margin_state.reset();
  452. } else if (!m_margin_state.has_block_container_waiting_for_final_y_position()) {
  453. // margin-top of block container can be updated during children layout hence it's final y position yet to be determined
  454. m_margin_state.register_block_container_y_position_update_callback([&](CSSPixels margin_top) {
  455. place_block_level_element_in_normal_flow_vertically(box, margin_top + y + box_state.border_box_top());
  456. });
  457. }
  458. layout_block_level_children(verify_cast<BlockContainer>(box), layout_mode, box_state.available_inner_space_or_constraints_from(available_space));
  459. }
  460. }
  461. }
  462. compute_height(box, available_space);
  463. if (!margins_collapse_through(box, m_state)) {
  464. if (!m_margin_state.box_last_in_flow_child_margin_bottom_collapsed) {
  465. m_margin_state.reset();
  466. }
  467. m_y_offset_of_current_block_container = box_state.offset.y() + box_state.content_height() + box_state.border_box_bottom();
  468. }
  469. m_margin_state.box_last_in_flow_child_margin_bottom_collapsed = false;
  470. m_margin_state.add_margin(box_state.margin_bottom);
  471. m_margin_state.update_block_waiting_for_final_y_position();
  472. compute_inset(box);
  473. if (is<ListItemBox>(box)) {
  474. layout_list_item_marker(static_cast<ListItemBox const&>(box));
  475. }
  476. bottom_of_lowest_margin_box = max(bottom_of_lowest_margin_box, box_state.offset.y() + box_state.content_height() + box_state.margin_box_bottom());
  477. if (independent_formatting_context)
  478. independent_formatting_context->parent_context_did_dimension_child_root_box();
  479. }
  480. void BlockFormattingContext::layout_block_level_children(BlockContainer const& block_container, LayoutMode layout_mode, AvailableSpace const& available_space)
  481. {
  482. VERIFY(!block_container.children_are_inline());
  483. CSSPixels bottom_of_lowest_margin_box = 0;
  484. TemporaryChange<Optional<CSSPixels>> change { m_y_offset_of_current_block_container, CSSPixels(0) };
  485. block_container.for_each_child_of_type<Box>([&](Box& box) {
  486. layout_block_level_box(box, block_container, layout_mode, bottom_of_lowest_margin_box, available_space);
  487. return IterationDecision::Continue;
  488. });
  489. m_margin_state.block_container_y_position_update_callback = {};
  490. if (layout_mode == LayoutMode::IntrinsicSizing) {
  491. auto& block_container_state = m_state.get_mutable(block_container);
  492. if (!block_container_state.has_definite_width())
  493. block_container_state.set_content_width(greatest_child_width(block_container));
  494. if (!block_container_state.has_definite_height())
  495. block_container_state.set_content_height(bottom_of_lowest_margin_box);
  496. }
  497. }
  498. void BlockFormattingContext::resolve_vertical_box_model_metrics(Box const& box, LayoutState& state)
  499. {
  500. auto& box_state = state.get_mutable(box);
  501. auto const& computed_values = box.computed_values();
  502. auto width_of_containing_block = CSS::Length::make_px(containing_block_width_for(box, state));
  503. box_state.margin_top = computed_values.margin().top().resolved(box, width_of_containing_block).to_px(box);
  504. box_state.margin_bottom = computed_values.margin().bottom().resolved(box, width_of_containing_block).to_px(box);
  505. box_state.border_top = computed_values.border_top().width;
  506. box_state.border_bottom = computed_values.border_bottom().width;
  507. box_state.padding_top = computed_values.padding().top().resolved(box, width_of_containing_block).to_px(box);
  508. box_state.padding_bottom = computed_values.padding().bottom().resolved(box, width_of_containing_block).to_px(box);
  509. }
  510. CSSPixels BlockFormattingContext::BlockMarginState::current_collapsed_margin() const
  511. {
  512. CSSPixels smallest_margin = 0;
  513. CSSPixels largest_margin = 0;
  514. size_t negative_margin_count = 0;
  515. for (auto margin : current_collapsible_margins) {
  516. if (margin < 0)
  517. ++negative_margin_count;
  518. largest_margin = max(largest_margin, margin);
  519. smallest_margin = min(smallest_margin, margin);
  520. }
  521. CSSPixels collapsed_margin = 0;
  522. if (negative_margin_count == current_collapsible_margins.size()) {
  523. // When all margins are negative, the size of the collapsed margin is the smallest (most negative) margin.
  524. collapsed_margin = smallest_margin;
  525. } else if (negative_margin_count > 0) {
  526. // When negative margins are involved, the size of the collapsed margin is the sum of the largest positive margin and the smallest (most negative) negative margin.
  527. collapsed_margin = largest_margin + smallest_margin;
  528. } else {
  529. // Otherwise, collapse all the adjacent margins by using only the largest one.
  530. collapsed_margin = largest_margin;
  531. }
  532. return collapsed_margin;
  533. }
  534. void BlockFormattingContext::place_block_level_element_in_normal_flow_vertically(Box const& child_box, CSSPixels y)
  535. {
  536. auto& box_state = m_state.get_mutable(child_box);
  537. auto const& computed_values = child_box.computed_values();
  538. auto clear_floating_boxes = [&](FloatSideData& float_side) {
  539. if (!float_side.current_boxes.is_empty()) {
  540. // NOTE: Floating boxes are globally relevant within this BFC, *but* their offset coordinates
  541. // are relative to their containing block.
  542. // This means that we have to first convert to a root-space Y coordinate before clearing,
  543. // and then convert back to a local Y coordinate when assigning the cleared offset to
  544. // the `child_box` layout state.
  545. // First, find the lowest margin box edge on this float side and calculate the Y offset just below it.
  546. CSSPixels clearance_y_in_root = 0;
  547. for (auto const& floating_box : float_side.current_boxes) {
  548. auto floating_box_rect_in_root = margin_box_rect_in_ancestor_coordinate_space(floating_box.box, root(), m_state);
  549. clearance_y_in_root = max(clearance_y_in_root, floating_box_rect_in_root.bottom() + 1);
  550. }
  551. // Then, convert the clearance Y to a coordinate relative to the containing block of `child_box`.
  552. CSSPixels clearance_y_in_containing_block = clearance_y_in_root;
  553. for (auto* containing_block = child_box.containing_block(); containing_block && containing_block != &root(); containing_block = containing_block->containing_block())
  554. clearance_y_in_containing_block -= m_state.get(*containing_block).offset.y();
  555. if (clearance_y_in_containing_block > y)
  556. m_y_offset_of_current_block_container = clearance_y_in_containing_block;
  557. y = max(y, clearance_y_in_containing_block);
  558. float_side.clear();
  559. }
  560. };
  561. // Flex-items don't float and also don't clear.
  562. if ((computed_values.clear() == CSS::Clear::Left || computed_values.clear() == CSS::Clear::Both) && !child_box.is_flex_item())
  563. clear_floating_boxes(m_left_floats);
  564. if ((computed_values.clear() == CSS::Clear::Right || computed_values.clear() == CSS::Clear::Both) && !child_box.is_flex_item())
  565. clear_floating_boxes(m_right_floats);
  566. y += box_state.border_box_top();
  567. box_state.set_content_offset(CSSPixelPoint { box_state.offset.x(), y.value() });
  568. }
  569. void BlockFormattingContext::place_block_level_element_in_normal_flow_horizontally(Box const& child_box, AvailableSpace const& available_space)
  570. {
  571. auto& box_state = m_state.get_mutable(child_box);
  572. CSSPixels x = 0;
  573. CSSPixels available_width_within_containing_block = available_space.width.to_px();
  574. if ((!m_left_floats.current_boxes.is_empty() || !m_right_floats.current_boxes.is_empty())
  575. && creates_block_formatting_context(child_box)) {
  576. auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(child_box, root(), m_state);
  577. auto space = space_used_by_floats(box_in_root_rect.y());
  578. available_width_within_containing_block -= space.left + space.right;
  579. x += space.left;
  580. }
  581. if (child_box.containing_block()->computed_values().text_align() == CSS::TextAlign::LibwebCenter) {
  582. x += (available_width_within_containing_block / 2) - box_state.content_width() / 2;
  583. } else {
  584. x += box_state.margin_box_left();
  585. }
  586. box_state.set_content_offset({ x.value(), box_state.offset.y() });
  587. }
  588. static void measure_scrollable_overflow(LayoutState const& state, Box const& box, CSSPixels& bottom_edge, CSSPixels& right_edge)
  589. {
  590. auto const& child_state = state.get(box);
  591. auto child_rect = absolute_content_rect(box, state);
  592. child_rect.inflate(child_state.border_box_top(), child_state.border_box_right(), child_state.border_box_bottom(), child_state.border_box_left());
  593. bottom_edge = max(bottom_edge, child_rect.bottom());
  594. right_edge = max(right_edge, child_rect.right());
  595. if (box.computed_values().overflow_x() == CSS::Overflow::Hidden && box.computed_values().overflow_y() == CSS::Overflow::Hidden)
  596. return;
  597. if (box.children_are_inline()) {
  598. if (!child_state.line_boxes.is_empty()) {
  599. bottom_edge = max(bottom_edge, child_rect.y() + child_state.line_boxes.last().bottom());
  600. for (auto& line_box : child_state.line_boxes) {
  601. if (line_box.fragments().is_empty())
  602. continue;
  603. right_edge = max(right_edge, child_rect.x() + line_box.fragments().last().width());
  604. }
  605. }
  606. } else {
  607. box.for_each_child_of_type<Box>([&](Box const& child) {
  608. measure_scrollable_overflow(state, child, bottom_edge, right_edge);
  609. return IterationDecision::Continue;
  610. });
  611. }
  612. }
  613. void BlockFormattingContext::layout_viewport(LayoutMode layout_mode, AvailableSpace const& available_space)
  614. {
  615. auto viewport_rect = root().browsing_context().viewport_rect();
  616. auto& viewport = verify_cast<Layout::Viewport>(root());
  617. auto& viewport_state = m_state.get_mutable(viewport);
  618. if (root().children_are_inline())
  619. layout_inline_children(root(), layout_mode, available_space);
  620. else
  621. layout_block_level_children(root(), layout_mode, available_space);
  622. CSSPixels bottom_edge = 0;
  623. CSSPixels right_edge = 0;
  624. measure_scrollable_overflow(m_state, viewport, bottom_edge, right_edge);
  625. if (bottom_edge >= viewport_rect.height() || right_edge >= viewport_rect.width()) {
  626. // FIXME: Move overflow data to LayoutState!
  627. auto& overflow_data = viewport_state.ensure_overflow_data();
  628. overflow_data.scrollable_overflow_rect = viewport_rect;
  629. // NOTE: The edges are *within* the rectangle, so we add 1 to get the width and height.
  630. overflow_data.scrollable_overflow_rect.set_size(right_edge + 1, bottom_edge + 1);
  631. }
  632. }
  633. void BlockFormattingContext::layout_floating_box(Box const& box, BlockContainer const&, LayoutMode layout_mode, AvailableSpace const& available_space, CSSPixels y, LineBuilder* line_builder)
  634. {
  635. VERIFY(box.is_floating());
  636. auto& box_state = m_state.get_mutable(box);
  637. CSSPixels width_of_containing_block = available_space.width.to_px();
  638. compute_width(box, available_space, layout_mode);
  639. auto independent_formatting_context = layout_inside(box, layout_mode, box_state.available_inner_space_or_constraints_from(available_space));
  640. compute_height(box, available_space);
  641. // First we place the box normally (to get the right y coordinate.)
  642. // If we have a LineBuilder, we're in the middle of inline layout, otherwise this is block layout.
  643. if (line_builder) {
  644. auto y = line_builder->y_for_float_to_be_inserted_here(box);
  645. box_state.set_content_y(y + box_state.margin_box_top());
  646. } else {
  647. place_block_level_element_in_normal_flow_vertically(box, y + box_state.margin_top);
  648. place_block_level_element_in_normal_flow_horizontally(box, available_space);
  649. }
  650. // Then we float it to the left or right.
  651. auto float_box = [&](FloatSide side, FloatSideData& side_data, FloatSideData& other_side_data) {
  652. CSSPixels offset_from_edge = 0;
  653. auto float_to_edge = [&] {
  654. if (side == FloatSide::Left)
  655. offset_from_edge = box_state.margin_box_left();
  656. else
  657. offset_from_edge = box_state.content_width() + box_state.margin_box_right();
  658. };
  659. auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(box, root(), m_state);
  660. CSSPixels y_in_root = box_in_root_rect.y();
  661. CSSPixels y = box_state.offset.y();
  662. if (side_data.current_boxes.is_empty()) {
  663. // This is the first floating box on this side. Go all the way to the edge.
  664. float_to_edge();
  665. side_data.y_offset = 0;
  666. } else {
  667. // NOTE: If we're in inline layout, the LineBuilder has already provided the right Y offset.
  668. // In block layout, we adjust by the side's current Y offset here.
  669. if (!line_builder)
  670. y_in_root += side_data.y_offset;
  671. bool did_touch_preceding_float = false;
  672. bool did_place_next_to_preceding_float = false;
  673. // Walk all currently tracked floats on the side we're floating towards.
  674. // We're looking for the innermost preceding float that intersects vertically with `box`.
  675. for (auto& preceding_float : side_data.current_boxes.in_reverse()) {
  676. auto const preceding_float_rect = margin_box_rect_in_ancestor_coordinate_space(preceding_float.box, root(), m_state);
  677. if (!preceding_float_rect.contains_vertically(y_in_root))
  678. continue;
  679. // We found a preceding float that intersects vertically with the current float.
  680. // Now we need to find out if there's enough inline-axis space to stack them next to each other.
  681. auto const& preceding_float_state = m_state.get(preceding_float.box);
  682. CSSPixels tentative_offset_from_edge = 0;
  683. bool fits_next_to_preceding_float = false;
  684. if (side == FloatSide::Left) {
  685. tentative_offset_from_edge = preceding_float.offset_from_edge + preceding_float_state.content_width() + preceding_float_state.margin_box_right() + box_state.margin_box_left();
  686. fits_next_to_preceding_float = (tentative_offset_from_edge + box_state.content_width() + box_state.margin_box_right()) <= width_of_containing_block;
  687. } else {
  688. tentative_offset_from_edge = preceding_float.offset_from_edge + preceding_float_state.margin_box_left() + box_state.margin_box_right() + box_state.content_width();
  689. fits_next_to_preceding_float = tentative_offset_from_edge >= 0;
  690. }
  691. did_touch_preceding_float = true;
  692. if (!fits_next_to_preceding_float)
  693. break;
  694. offset_from_edge = tentative_offset_from_edge;
  695. did_place_next_to_preceding_float = true;
  696. break;
  697. }
  698. if (!did_touch_preceding_float || !did_place_next_to_preceding_float) {
  699. // One of two things happened:
  700. // - This box does not touch another floating box.
  701. // - We ran out of horizontal space on this "float line", and need to break.
  702. // Either way, we float this box all the way to the edge.
  703. float_to_edge();
  704. CSSPixels lowest_margin_edge = 0;
  705. for (auto const& box : side_data.current_boxes) {
  706. auto const& box_state = m_state.get(box.box);
  707. lowest_margin_edge = max(lowest_margin_edge, box_state.margin_box_height());
  708. }
  709. side_data.y_offset += lowest_margin_edge;
  710. // Also, forget all previous boxes floated to this side while since they're no longer relevant.
  711. side_data.clear();
  712. }
  713. }
  714. // NOTE: If we're in inline layout, the LineBuilder has already provided the right Y offset.
  715. // In block layout, we adjust by the side's current Y offset here.
  716. // FIXME: It's annoying that we have different behavior for inline vs block here.
  717. // Find a way to unify the behavior so we don't need to branch here.
  718. if (!line_builder)
  719. y += side_data.y_offset;
  720. side_data.all_boxes.append(adopt_own(*new FloatingBox {
  721. .box = box,
  722. .offset_from_edge = offset_from_edge,
  723. .top_margin_edge = y - box_state.margin_box_top(),
  724. .bottom_margin_edge = y + box_state.content_height() + box_state.margin_box_bottom(),
  725. }));
  726. side_data.current_boxes.append(*side_data.all_boxes.last());
  727. if (side == FloatSide::Left) {
  728. side_data.current_width = offset_from_edge + box_state.content_width() + box_state.margin_box_right();
  729. } else {
  730. side_data.current_width = offset_from_edge + box_state.margin_box_left();
  731. }
  732. side_data.max_width = max(side_data.current_width, side_data.max_width);
  733. // NOTE: We don't set the X position here, that happens later, once we know the root block width.
  734. // See parent_context_did_dimension_child_root_box() for that logic.
  735. box_state.set_content_y(y.value());
  736. // If the new box was inserted below the bottom of the opposite side,
  737. // we reset the other side back to its edge.
  738. if (y > other_side_data.y_offset)
  739. other_side_data.clear();
  740. };
  741. // Next, float to the left and/or right
  742. if (box.computed_values().float_() == CSS::Float::Left) {
  743. float_box(FloatSide::Left, m_left_floats, m_right_floats);
  744. } else if (box.computed_values().float_() == CSS::Float::Right) {
  745. float_box(FloatSide::Right, m_right_floats, m_left_floats);
  746. }
  747. m_state.get_mutable(root()).add_floating_descendant(box);
  748. if (line_builder)
  749. line_builder->recalculate_available_space();
  750. if (independent_formatting_context)
  751. independent_formatting_context->parent_context_did_dimension_child_root_box();
  752. }
  753. void BlockFormattingContext::layout_list_item_marker(ListItemBox const& list_item_box)
  754. {
  755. if (!list_item_box.marker())
  756. return;
  757. auto& marker = *list_item_box.marker();
  758. auto& marker_state = m_state.get_mutable(marker);
  759. auto& list_item_state = m_state.get_mutable(list_item_box);
  760. CSSPixels image_width = 0;
  761. CSSPixels image_height = 0;
  762. if (auto const* list_style_image = marker.list_style_image()) {
  763. image_width = list_style_image->natural_width().value_or(0);
  764. image_height = list_style_image->natural_height().value_or(0);
  765. }
  766. CSSPixels default_marker_width = max(4, marker.font().pixel_size_rounded_up() - 4);
  767. if (marker.text().is_empty()) {
  768. marker_state.set_content_width((image_width + default_marker_width).value());
  769. } else {
  770. auto text_width = marker.font().width(marker.text());
  771. marker_state.set_content_width((image_width + text_width).value());
  772. }
  773. marker_state.set_content_height(max(image_height, marker.font().pixel_size_rounded_up() + 1).value());
  774. marker_state.set_content_offset({ -(marker_state.content_width() + default_marker_width),
  775. max(CSSPixels(0.f), (CSSPixels(marker.line_height()) - marker_state.content_height()) / 2.f) });
  776. if (marker_state.content_height() > list_item_state.content_height())
  777. list_item_state.set_content_height(marker_state.content_height());
  778. }
  779. BlockFormattingContext::SpaceUsedByFloats BlockFormattingContext::space_used_by_floats(CSSPixels y) const
  780. {
  781. SpaceUsedByFloats space_used_by_floats;
  782. for (auto const& floating_box_ptr : m_left_floats.all_boxes.in_reverse()) {
  783. auto const& floating_box = *floating_box_ptr;
  784. auto const& floating_box_state = m_state.get(floating_box.box);
  785. // NOTE: The floating box is *not* in the final horizontal position yet, but the size and vertical position is valid.
  786. auto rect = margin_box_rect_in_ancestor_coordinate_space(floating_box.box, root(), m_state);
  787. if (rect.contains_vertically(y.value())) {
  788. space_used_by_floats.left = floating_box.offset_from_edge
  789. + floating_box_state.content_width()
  790. + floating_box_state.margin_box_right();
  791. break;
  792. }
  793. }
  794. for (auto const& floating_box_ptr : m_right_floats.all_boxes.in_reverse()) {
  795. auto const& floating_box = *floating_box_ptr;
  796. auto const& floating_box_state = m_state.get(floating_box.box);
  797. // NOTE: The floating box is *not* in the final horizontal position yet, but the size and vertical position is valid.
  798. auto rect = margin_box_rect_in_ancestor_coordinate_space(floating_box.box, root(), m_state);
  799. if (rect.contains_vertically(y.value())) {
  800. space_used_by_floats.right = floating_box.offset_from_edge
  801. + floating_box_state.margin_box_left();
  802. break;
  803. }
  804. }
  805. return space_used_by_floats;
  806. }
  807. CSSPixels BlockFormattingContext::greatest_child_width(Box const& box) const
  808. {
  809. // Similar to FormattingContext::greatest_child_width()
  810. // but this one takes floats into account!
  811. CSSPixels max_width = m_left_floats.max_width + m_right_floats.max_width;
  812. if (box.children_are_inline()) {
  813. for (auto const& line_box : m_state.get(verify_cast<BlockContainer>(box)).line_boxes) {
  814. CSSPixels width_here = line_box.width();
  815. CSSPixels extra_width_from_left_floats = 0;
  816. for (auto& left_float : m_left_floats.all_boxes) {
  817. if (line_box.baseline() >= left_float->top_margin_edge.value() || line_box.baseline() <= left_float->bottom_margin_edge.value()) {
  818. auto const& left_float_state = m_state.get(left_float->box);
  819. extra_width_from_left_floats = max(extra_width_from_left_floats, left_float->offset_from_edge + left_float_state.content_width() + left_float_state.margin_box_right());
  820. }
  821. }
  822. CSSPixels extra_width_from_right_floats = 0;
  823. for (auto& right_float : m_right_floats.all_boxes) {
  824. if (line_box.baseline() >= right_float->top_margin_edge.value() || line_box.baseline() <= right_float->bottom_margin_edge.value()) {
  825. auto const& right_float_state = m_state.get(right_float->box);
  826. extra_width_from_right_floats = max(extra_width_from_right_floats, right_float->offset_from_edge + right_float_state.margin_box_left());
  827. }
  828. }
  829. width_here += extra_width_from_left_floats + extra_width_from_right_floats;
  830. max_width = max(max_width, width_here);
  831. }
  832. } else {
  833. box.for_each_child_of_type<Box>([&](Box const& child) {
  834. if (!child.is_absolutely_positioned())
  835. max_width = max(max_width, m_state.get(child).margin_box_width());
  836. });
  837. }
  838. return max_width;
  839. }
  840. void BlockFormattingContext::determine_width_of_child(Box const& box, AvailableSpace const& available_space)
  841. {
  842. compute_width(box, available_space);
  843. }
  844. void BlockFormattingContext::determine_height_of_child(Box const& box, AvailableSpace const& available_space)
  845. {
  846. compute_height(box, available_space);
  847. }
  848. }