BlockFormattingContext.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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/SVGSVGBox.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. if (root().children_are_inline())
  39. return m_state.get(root()).content_width();
  40. return greatest_child_width(root());
  41. }
  42. CSSPixels BlockFormattingContext::automatic_content_height() const
  43. {
  44. return compute_auto_height_for_block_formatting_context_root(root());
  45. }
  46. static bool margins_collapse_through(Box const& box, LayoutState& state)
  47. {
  48. // FIXME: A box's own margins collapse if the 'min-height' property is zero, and it has neither top or bottom borders
  49. // nor top or bottom padding, and it has a 'height' of either 0 or 'auto', and it does not contain a line box, and
  50. // all of its in-flow children's margins (if any) collapse.
  51. // https://www.w3.org/TR/CSS22/box.html#collapsing-margins
  52. // FIXME: For the purpose of margin collapsing (CSS 2 §8.3.1 Collapsing margins), if the block axis is the
  53. // ratio-dependent axis, it is not considered to have a computed block-size of auto.
  54. // https://www.w3.org/TR/css-sizing-4/#aspect-ratio-margin-collapse
  55. if (box.computed_values().clear() != CSS::Clear::None)
  56. return false;
  57. return state.get(box).border_box_height() == 0;
  58. }
  59. void BlockFormattingContext::run(Box const&, LayoutMode layout_mode, AvailableSpace const& available_space)
  60. {
  61. if (is<Viewport>(root())) {
  62. layout_viewport(layout_mode, available_space);
  63. return;
  64. }
  65. if (root().children_are_inline())
  66. layout_inline_children(root(), layout_mode, available_space);
  67. else
  68. layout_block_level_children(root(), layout_mode, available_space);
  69. // Assign collapsed margin left after children layout of formatting context to the last child box
  70. if (m_margin_state.current_collapsed_margin() != 0) {
  71. for (auto* child_box = root().last_child_of_type<Box>(); child_box; child_box = child_box->previous_sibling_of_type<Box>()) {
  72. if (child_box->is_absolutely_positioned() || child_box->is_floating())
  73. continue;
  74. if (margins_collapse_through(*child_box, m_state))
  75. continue;
  76. m_state.get_mutable(*child_box).margin_bottom = m_margin_state.current_collapsed_margin();
  77. break;
  78. }
  79. }
  80. }
  81. void BlockFormattingContext::parent_context_did_dimension_child_root_box()
  82. {
  83. m_was_notified_after_parent_dimensioned_my_root_box = true;
  84. // Left-side floats: offset_from_edge is from left edge (0) to left content edge of floating_box.
  85. for (auto& floating_box : m_left_floats.all_boxes) {
  86. auto& box_state = m_state.get_mutable(floating_box->box);
  87. box_state.set_content_x(floating_box->offset_from_edge);
  88. }
  89. // Right-side floats: offset_from_edge is from right edge (float_containing_block_width) to the left content edge of floating_box.
  90. for (auto& floating_box : m_right_floats.all_boxes) {
  91. auto float_containing_block_width = containing_block_width_for(floating_box->box);
  92. auto& box_state = m_state.get_mutable(floating_box->box);
  93. box_state.set_content_x(float_containing_block_width - floating_box->offset_from_edge);
  94. }
  95. // We can also layout absolutely positioned boxes within this BFC.
  96. for (auto& box : m_absolutely_positioned_boxes) {
  97. auto& cb_state = m_state.get(*box->containing_block());
  98. auto available_width = AvailableSize::make_definite(cb_state.content_width() + cb_state.padding_left + cb_state.padding_right);
  99. auto available_height = AvailableSize::make_definite(cb_state.content_height() + cb_state.padding_top + cb_state.padding_bottom);
  100. layout_absolutely_positioned_element(box, AvailableSpace(available_width, available_height));
  101. }
  102. }
  103. void BlockFormattingContext::compute_width(Box const& box, AvailableSpace const& available_space, LayoutMode)
  104. {
  105. if (box.is_absolutely_positioned()) {
  106. compute_width_for_absolutely_positioned_element(box, available_space);
  107. return;
  108. }
  109. auto remaining_available_space = available_space;
  110. if (available_space.width.is_definite() && creates_block_formatting_context(box)) {
  111. // 9.5 Floats
  112. // The border box of a table, a block-level replaced element, or an element in the normal flow that establishes a
  113. // new block formatting context (such as an element with 'overflow' other than 'visible') must not overlap the margin
  114. // box of any floats in the same block formatting context as the element itself. If necessary, implementations should
  115. // clear the said element by placing it below any preceding floats, but may place it adjacent to such floats if there is
  116. // sufficient space. They may even make the border box of said element narrower than defined by section 10.3.3.
  117. // CSS2 does not define when a UA may put said element next to the float or by how much said element may
  118. // become narrower.
  119. auto intrusion = intrusion_by_floats_into_box(box, 0);
  120. auto remaining_width = available_space.width.to_px() - intrusion.left - intrusion.right;
  121. remaining_available_space.width = AvailableSize::make_definite(remaining_width);
  122. }
  123. if (box_is_sized_as_replaced_element(box)) {
  124. // FIXME: This should not be done *by* ReplacedBox
  125. if (is<ReplacedBox>(box)) {
  126. auto& replaced = verify_cast<ReplacedBox>(box);
  127. // FIXME: This const_cast is gross.
  128. const_cast<ReplacedBox&>(replaced).prepare_for_replaced_layout();
  129. }
  130. compute_width_for_block_level_replaced_element_in_normal_flow(box, remaining_available_space);
  131. if (box.is_floating()) {
  132. // 10.3.6 Floating, replaced elements:
  133. // https://www.w3.org/TR/CSS22/visudet.html#float-replaced-width
  134. return;
  135. }
  136. }
  137. if (box.is_floating()) {
  138. // 10.3.5 Floating, non-replaced elements:
  139. // https://www.w3.org/TR/CSS22/visudet.html#float-width
  140. compute_width_for_floating_box(box, available_space);
  141. return;
  142. }
  143. auto const& computed_values = box.computed_values();
  144. auto width_of_containing_block = remaining_available_space.width.to_px();
  145. auto width_of_containing_block_as_length_for_resolve = remaining_available_space.width.is_definite() ? CSS::Length::make_px(width_of_containing_block) : CSS::Length::make_px(0);
  146. auto zero_value = CSS::Length::make_px(0);
  147. auto margin_left = CSS::Length::make_auto();
  148. auto margin_right = CSS::Length::make_auto();
  149. auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block_as_length_for_resolve).to_px(box);
  150. auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block_as_length_for_resolve).to_px(box);
  151. auto& box_state = m_state.get_mutable(box);
  152. box_state.border_left = computed_values.border_left().width;
  153. box_state.border_right = computed_values.border_right().width;
  154. box_state.padding_left = padding_left;
  155. box_state.padding_right = padding_right;
  156. // NOTE: If we are calculating the min-content or max-content width of this box,
  157. // and the width should be treated as auto, then we can simply return here,
  158. // as the preferred width and min/max constraints are irrelevant for intrinsic sizing.
  159. if (box_state.width_constraint != SizeConstraint::None)
  160. return;
  161. auto try_compute_width = [&](auto const& a_width) {
  162. CSS::Length width = a_width;
  163. margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length_for_resolve);
  164. margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length_for_resolve);
  165. CSSPixels total_px = computed_values.border_left().width + computed_values.border_right().width;
  166. for (auto& value : { margin_left, CSS::Length::make_px(padding_left), width, CSS::Length::make_px(padding_right), margin_right }) {
  167. total_px += value.to_px(box);
  168. }
  169. if (!box.is_inline()) {
  170. // 10.3.3 Block-level, non-replaced elements in normal flow
  171. // 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.
  172. if (!width.is_auto() && total_px > width_of_containing_block) {
  173. if (margin_left.is_auto())
  174. margin_left = zero_value;
  175. if (margin_right.is_auto())
  176. margin_right = zero_value;
  177. }
  178. // 10.3.3 cont'd.
  179. auto underflow_px = width_of_containing_block - total_px;
  180. if (available_space.width.is_intrinsic_sizing_constraint())
  181. underflow_px = 0;
  182. if (width.is_auto()) {
  183. if (margin_left.is_auto())
  184. margin_left = zero_value;
  185. if (margin_right.is_auto())
  186. margin_right = zero_value;
  187. if (available_space.width.is_definite()) {
  188. if (underflow_px >= 0) {
  189. width = CSS::Length::make_px(underflow_px);
  190. } else {
  191. width = zero_value;
  192. margin_right = CSS::Length::make_px(margin_right.to_px(box) + underflow_px);
  193. }
  194. }
  195. } else {
  196. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  197. margin_right = CSS::Length::make_px(margin_right.to_px(box) + underflow_px);
  198. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  199. margin_right = CSS::Length::make_px(underflow_px);
  200. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  201. margin_left = CSS::Length::make_px(underflow_px);
  202. } else { // margin_left.is_auto() && margin_right.is_auto()
  203. auto half_of_the_underflow = CSS::Length::make_px(underflow_px / 2);
  204. margin_left = half_of_the_underflow;
  205. margin_right = half_of_the_underflow;
  206. }
  207. }
  208. }
  209. return width;
  210. };
  211. auto input_width = [&] {
  212. if (box_is_sized_as_replaced_element(box)) {
  213. // NOTE: Replaced elements had their width calculated independently above.
  214. // We use that width as the input here to ensure that margins get resolved.
  215. return CSS::Length::make_px(box_state.content_width());
  216. }
  217. if (is<TableWrapper>(box))
  218. return CSS::Length::make_px(compute_table_box_width_inside_table_wrapper(box, remaining_available_space));
  219. if (should_treat_width_as_auto(box, remaining_available_space))
  220. return CSS::Length::make_auto();
  221. return calculate_inner_width(box, remaining_available_space.width, computed_values.width());
  222. }();
  223. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  224. auto used_width = try_compute_width(input_width);
  225. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  226. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  227. if (!should_treat_max_width_as_none(box, available_space.width)) {
  228. auto max_width = calculate_inner_width(box, remaining_available_space.width, computed_values.max_width());
  229. auto used_width_px = used_width.is_auto() ? CSSPixels { 0 } : used_width.to_px(box);
  230. if (used_width_px > max_width.to_px(box)) {
  231. used_width = try_compute_width(max_width);
  232. }
  233. }
  234. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  235. // but this time using the value of 'min-width' as the computed value for 'width'.
  236. if (!computed_values.min_width().is_auto()) {
  237. auto min_width = calculate_inner_width(box, remaining_available_space.width, computed_values.min_width());
  238. auto used_width_px = used_width.is_auto() ? remaining_available_space.width.to_px() : used_width.to_px(box);
  239. if (used_width_px < min_width.to_px(box)) {
  240. used_width = try_compute_width(min_width);
  241. }
  242. }
  243. if (!box_is_sized_as_replaced_element(box) && !used_width.is_auto())
  244. box_state.set_content_width(used_width.to_px(box));
  245. box_state.margin_left = margin_left.to_px(box);
  246. box_state.margin_right = margin_right.to_px(box);
  247. }
  248. void BlockFormattingContext::compute_width_for_floating_box(Box const& box, AvailableSpace const& available_space)
  249. {
  250. // 10.3.5 Floating, non-replaced elements
  251. auto& computed_values = box.computed_values();
  252. auto zero_value = CSS::Length::make_px(0);
  253. auto width_of_containing_block = available_space.width.to_px();
  254. auto width_of_containing_block_as_length_for_resolve = CSS::Length::make_px(width_of_containing_block);
  255. if (!available_space.width.is_definite())
  256. width_of_containing_block_as_length_for_resolve = CSS::Length::make_px(0);
  257. auto margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length_for_resolve);
  258. auto margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length_for_resolve);
  259. auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block_as_length_for_resolve).to_px(box);
  260. auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block_as_length_for_resolve).to_px(box);
  261. // If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
  262. if (margin_left.is_auto())
  263. margin_left = zero_value;
  264. if (margin_right.is_auto())
  265. margin_right = zero_value;
  266. auto compute_width = [&](auto width) {
  267. // If 'width' is computed as 'auto', the used value is the "shrink-to-fit" width.
  268. if (width.is_auto()) {
  269. // Find the available width: in this case, this is the width of the containing
  270. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  271. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  272. auto available_width = width_of_containing_block
  273. - margin_left.to_px(box) - computed_values.border_left().width - padding_left
  274. - padding_right - computed_values.border_right().width - margin_right.to_px(box);
  275. auto result = calculate_shrink_to_fit_widths(box);
  276. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  277. width = CSS::Length::make_px(min(max(result.preferred_minimum_width, available_width), result.preferred_width));
  278. }
  279. return width;
  280. };
  281. auto input_width = [&] {
  282. if (should_treat_width_as_auto(box, available_space))
  283. return CSS::Length::make_auto();
  284. return calculate_inner_width(box, available_space.width, computed_values.width());
  285. }();
  286. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  287. auto width = compute_width(input_width);
  288. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  289. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  290. if (!should_treat_max_width_as_none(box, available_space.width)) {
  291. auto max_width = calculate_inner_width(box, available_space.width, computed_values.max_width());
  292. if (width.to_px(box) > max_width.to_px(box))
  293. width = compute_width(max_width);
  294. }
  295. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  296. // but this time using the value of 'min-width' as the computed value for 'width'.
  297. if (!computed_values.min_width().is_auto()) {
  298. auto min_width = calculate_inner_width(box, available_space.width, computed_values.min_width());
  299. if (width.to_px(box) < min_width.to_px(box))
  300. width = compute_width(min_width);
  301. }
  302. auto& box_state = m_state.get_mutable(box);
  303. box_state.set_content_width(width.to_px(box));
  304. box_state.margin_left = margin_left.to_px(box);
  305. box_state.margin_right = margin_right.to_px(box);
  306. box_state.border_left = computed_values.border_left().width;
  307. box_state.border_right = computed_values.border_right().width;
  308. box_state.padding_left = padding_left;
  309. box_state.padding_right = padding_right;
  310. }
  311. void BlockFormattingContext::compute_width_for_block_level_replaced_element_in_normal_flow(Box const& box, AvailableSpace const& available_space)
  312. {
  313. // 10.3.6 Floating, replaced elements
  314. auto& computed_values = box.computed_values();
  315. auto zero_value = CSS::Length::make_px(0);
  316. auto width_of_containing_block = available_space.width.to_px();
  317. auto width_of_containing_block_as_length_for_resolve = CSS::Length::make_px(width_of_containing_block);
  318. if (!available_space.width.is_definite())
  319. width_of_containing_block_as_length_for_resolve = CSS::Length::make_px(0);
  320. // 10.3.4 Block-level, replaced elements in normal flow
  321. // The used value of 'width' is determined as for inline replaced elements. Then the rules for
  322. // non-replaced block-level elements are applied to determine the margins.
  323. auto margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length_for_resolve);
  324. auto margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length_for_resolve);
  325. auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block_as_length_for_resolve).to_px(box);
  326. auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block_as_length_for_resolve).to_px(box);
  327. // If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
  328. if (margin_left.is_auto())
  329. margin_left = zero_value;
  330. if (margin_right.is_auto())
  331. margin_right = zero_value;
  332. auto& box_state = m_state.get_mutable(box);
  333. box_state.set_content_width(compute_width_for_replaced_element(box, available_space));
  334. box_state.margin_left = margin_left.to_px(box);
  335. box_state.margin_right = margin_right.to_px(box);
  336. box_state.border_left = computed_values.border_left().width;
  337. box_state.border_right = computed_values.border_right().width;
  338. box_state.padding_left = padding_left;
  339. box_state.padding_right = padding_right;
  340. }
  341. CSSPixels BlockFormattingContext::compute_table_box_width_inside_table_wrapper(Box const& box, AvailableSpace const& available_space)
  342. {
  343. // 17.5.2
  344. // Table wrapper width should be equal to width of table box it contains
  345. auto const& computed_values = box.computed_values();
  346. auto width_of_containing_block = available_space.width.to_px();
  347. 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);
  348. auto zero_value = CSS::Length::make_px(0);
  349. auto margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length_for_resolve);
  350. auto margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length_for_resolve);
  351. // If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
  352. if (margin_left.is_auto())
  353. margin_left = zero_value;
  354. if (margin_right.is_auto())
  355. margin_right = zero_value;
  356. // table-wrapper can't have borders or paddings but it might have margin taken from table-root.
  357. auto available_width = width_of_containing_block - margin_left.to_px(box) - margin_right.to_px(box);
  358. LayoutState throwaway_state(&m_state);
  359. if (available_space.width.is_definite())
  360. throwaway_state.get_mutable(box).set_content_width(available_width);
  361. else if (available_space.width.is_min_content())
  362. throwaway_state.get_mutable(box).set_min_content_width();
  363. else {
  364. VERIFY(available_space.width.is_max_content());
  365. throwaway_state.get_mutable(box).set_max_content_width();
  366. }
  367. auto context = create_independent_formatting_context_if_needed(throwaway_state, box);
  368. VERIFY(context);
  369. context->run(box, LayoutMode::IntrinsicSizing, m_state.get(box).available_inner_space_or_constraints_from(available_space));
  370. Optional<Box const&> table_box;
  371. box.for_each_in_subtree_of_type<Box>([&](Box const& child_box) {
  372. if (child_box.display().is_table_inside()) {
  373. table_box = child_box;
  374. return IterationDecision::Break;
  375. }
  376. return IterationDecision::Continue;
  377. });
  378. VERIFY(table_box.has_value());
  379. auto table_used_width = throwaway_state.get(*table_box).border_box_width();
  380. return available_space.width.is_definite() ? min(table_used_width, available_width) : table_used_width;
  381. }
  382. void BlockFormattingContext::compute_height(Box const& box, AvailableSpace const& available_space)
  383. {
  384. auto const& computed_values = box.computed_values();
  385. auto containing_block_height = CSS::Length::make_px(available_space.height.to_px());
  386. // Then work out what the height is, based on box type and CSS properties.
  387. CSSPixels height = 0;
  388. if (box_is_sized_as_replaced_element(box)) {
  389. height = compute_height_for_replaced_element(box, available_space);
  390. } else {
  391. if (should_treat_height_as_auto(box, available_space)) {
  392. height = compute_auto_height_for_block_level_element(box, m_state.get(box).available_inner_space_or_constraints_from(available_space));
  393. } else {
  394. height = calculate_inner_height(box, available_space.height, computed_values.height()).to_px(box);
  395. }
  396. }
  397. if (!should_treat_max_height_as_none(box, available_space.height)) {
  398. auto max_height = calculate_inner_height(box, available_space.height, computed_values.max_height());
  399. if (!max_height.is_auto())
  400. height = min(height, max_height.to_px(box));
  401. }
  402. if (!computed_values.min_height().is_auto()) {
  403. height = max(height, calculate_inner_height(box, available_space.height, computed_values.min_height()).to_px(box));
  404. }
  405. if (box.document().in_quirks_mode()
  406. && box.dom_node()
  407. && box.dom_node()->is_html_html_element()
  408. && box.computed_values().height().is_auto()) {
  409. // 3.6. The html element fills the viewport quirk
  410. // https://quirks.spec.whatwg.org/#the-html-element-fills-the-viewport-quirk
  411. // FIXME: Handle vertical writing mode.
  412. auto& box_state = m_state.get_mutable(box);
  413. // 1. Let margins be sum of the used values of the margin-left and margin-right properties of element
  414. // if element has a vertical writing mode, otherwise let margins be the sum of the used values of
  415. // the margin-top and margin-bottom properties of element.
  416. auto margins = box_state.margin_top + box_state.margin_bottom;
  417. // 2. Let size be the size of the initial containing block in the block flow direction minus margins.
  418. auto size = box_state.content_height() - margins;
  419. // 3. Return the bigger value of size and the normal border box size the element would have
  420. // according to the CSS specification.
  421. height = max(size, height);
  422. }
  423. m_state.get_mutable(box).set_content_height(height);
  424. }
  425. void BlockFormattingContext::layout_inline_children(BlockContainer const& block_container, LayoutMode layout_mode, AvailableSpace const& available_space)
  426. {
  427. VERIFY(block_container.children_are_inline());
  428. auto& block_container_state = m_state.get_mutable(block_container);
  429. InlineFormattingContext context(m_state, block_container, *this);
  430. context.run(
  431. block_container,
  432. layout_mode,
  433. available_space);
  434. if (!block_container_state.has_definite_width()) {
  435. // NOTE: min-width or max-width for boxes with inline children can only be applied after inside layout
  436. // is done and width of box content is known
  437. auto used_width_px = context.automatic_content_width();
  438. if (!should_treat_max_width_as_none(block_container, available_space.width)) {
  439. auto max_width_px = calculate_inner_width(block_container, available_space.width, block_container.computed_values().max_width()).to_px(block_container);
  440. if (used_width_px > max_width_px)
  441. used_width_px = max_width_px;
  442. }
  443. auto should_treat_min_width_as_auto = [&] {
  444. auto const& available_width = available_space.width;
  445. auto const& min_width = block_container.computed_values().min_width();
  446. if (min_width.is_auto())
  447. return true;
  448. if (min_width.is_fit_content() && available_width.is_intrinsic_sizing_constraint())
  449. return true;
  450. if (min_width.is_max_content() && available_width.is_max_content())
  451. return true;
  452. if (min_width.is_min_content() && available_width.is_min_content())
  453. return true;
  454. return false;
  455. }();
  456. if (!should_treat_min_width_as_auto) {
  457. auto min_width_px = calculate_inner_width(block_container, available_space.width, block_container.computed_values().min_width()).to_px(block_container);
  458. if (used_width_px < min_width_px)
  459. used_width_px = min_width_px;
  460. }
  461. block_container_state.set_content_width(used_width_px);
  462. }
  463. if (!block_container_state.has_definite_height())
  464. block_container_state.set_content_height(context.automatic_content_height());
  465. }
  466. CSSPixels BlockFormattingContext::compute_auto_height_for_block_level_element(Box const& box, AvailableSpace const& available_space)
  467. {
  468. if (creates_block_formatting_context(box)) {
  469. return compute_auto_height_for_block_formatting_context_root(box);
  470. }
  471. auto const& box_state = m_state.get(box);
  472. auto display = box.display();
  473. if (display.is_flex_inside()) {
  474. // https://drafts.csswg.org/css-flexbox-1/#algo-main-container
  475. // NOTE: The automatic block size of a block-level flex container is its max-content size.
  476. return calculate_max_content_height(box, available_space.width);
  477. }
  478. if (display.is_grid_inside()) {
  479. // https://www.w3.org/TR/css-grid-2/#intrinsic-sizes
  480. // In both inline and block formatting contexts, the grid container’s auto block size is its
  481. // max-content size.
  482. return calculate_max_content_height(box, available_space.width);
  483. }
  484. if (display.is_table_inside()) {
  485. return calculate_max_content_height(box, available_space.width);
  486. }
  487. // https://www.w3.org/TR/CSS22/visudet.html#normal-block
  488. // 10.6.3 Block-level non-replaced elements in normal flow when 'overflow' computes to 'visible'
  489. // The element's height is the distance from its top content edge to the first applicable of the following:
  490. // 1. the bottom edge of the last line box, if the box establishes a inline formatting context with one or more lines
  491. if (box.children_are_inline() && !box_state.line_boxes.is_empty())
  492. return box_state.line_boxes.last().bottom();
  493. // 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
  494. // 3. the bottom border edge of the last in-flow child whose top margin doesn't collapse with the element's bottom margin
  495. if (!box.children_are_inline()) {
  496. for (auto* child_box = box.last_child_of_type<Box>(); child_box; child_box = child_box->previous_sibling_of_type<Box>()) {
  497. if (child_box->is_absolutely_positioned() || child_box->is_floating())
  498. continue;
  499. // FIXME: This is hack. If the last child is a list-item marker box, we ignore it for purposes of height calculation.
  500. // Perhaps markers should not be considered in-flow(?) Perhaps they should always be the first child of the list-item
  501. // box instead of the last child.
  502. if (child_box->is_list_item_marker_box())
  503. continue;
  504. auto const& child_box_state = m_state.get(*child_box);
  505. if (margins_collapse_through(*child_box, m_state))
  506. continue;
  507. auto margin_bottom = m_margin_state.current_collapsed_margin();
  508. if (box_state.padding_bottom == 0 && box_state.border_bottom == 0) {
  509. m_margin_state.box_last_in_flow_child_margin_bottom_collapsed = true;
  510. margin_bottom = 0;
  511. }
  512. return max(CSSPixels(0), child_box_state.offset.y() + child_box_state.content_height() + child_box_state.border_box_bottom() + margin_bottom);
  513. }
  514. }
  515. // 4. zero, otherwise
  516. return 0;
  517. }
  518. 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)
  519. {
  520. auto& box_state = m_state.get_mutable(box);
  521. if (box.is_absolutely_positioned()) {
  522. m_absolutely_positioned_boxes.append(box);
  523. return;
  524. }
  525. // NOTE: ListItemMarkerBoxes are placed by their corresponding ListItemBox.
  526. if (is<ListItemMarkerBox>(box))
  527. return;
  528. resolve_vertical_box_model_metrics(box);
  529. if (box.is_floating()) {
  530. auto const y = m_y_offset_of_current_block_container.value();
  531. auto margin_top = !m_margin_state.has_block_container_waiting_for_final_y_position() ? m_margin_state.current_collapsed_margin() : 0;
  532. layout_floating_box(box, block_container, layout_mode, available_space, margin_top + y);
  533. bottom_of_lowest_margin_box = max(bottom_of_lowest_margin_box, box_state.offset.y() + box_state.content_height() + box_state.margin_box_bottom());
  534. return;
  535. }
  536. m_margin_state.add_margin(box_state.margin_top);
  537. auto introduce_clearance = clear_floating_boxes(box, {});
  538. if (introduce_clearance == DidIntroduceClearance::Yes)
  539. m_margin_state.reset();
  540. auto const y = m_y_offset_of_current_block_container.value();
  541. if (box_state.has_definite_height()) {
  542. compute_height(box, available_space);
  543. }
  544. auto independent_formatting_context = create_independent_formatting_context_if_needed(m_state, box);
  545. m_margin_state.update_block_waiting_for_final_y_position();
  546. CSSPixels margin_top = m_margin_state.current_collapsed_margin();
  547. if (m_margin_state.has_block_container_waiting_for_final_y_position()) {
  548. // If first child margin top will collapse with margin-top of containing block then margin-top of child is 0
  549. margin_top = 0;
  550. }
  551. if (independent_formatting_context) {
  552. // Margins of elements that establish new formatting contexts do not collapse with their in-flow children
  553. m_margin_state.reset();
  554. }
  555. place_block_level_element_in_normal_flow_vertically(box, y + margin_top);
  556. compute_width(box, available_space, layout_mode);
  557. place_block_level_element_in_normal_flow_horizontally(box, available_space);
  558. if (box.is_replaced_box())
  559. compute_height(box, available_space);
  560. if (independent_formatting_context) {
  561. // This box establishes a new formatting context. Pass control to it.
  562. independent_formatting_context->run(box, layout_mode, box_state.available_inner_space_or_constraints_from(available_space));
  563. } else {
  564. // This box participates in the current block container's flow.
  565. if (box.children_are_inline()) {
  566. layout_inline_children(verify_cast<BlockContainer>(box), layout_mode, box_state.available_inner_space_or_constraints_from(available_space));
  567. } else {
  568. if (box_state.border_top > 0 || box_state.padding_top > 0) {
  569. // margin-top of block container can't collapse with it's children if it has non zero border or padding
  570. m_margin_state.reset();
  571. } else if (!m_margin_state.has_block_container_waiting_for_final_y_position()) {
  572. // margin-top of block container can be updated during children layout hence it's final y position yet to be determined
  573. m_margin_state.register_block_container_y_position_update_callback([&](CSSPixels margin_top) {
  574. if (introduce_clearance == DidIntroduceClearance::No) {
  575. place_block_level_element_in_normal_flow_vertically(box, margin_top + y);
  576. }
  577. });
  578. }
  579. layout_block_level_children(verify_cast<BlockContainer>(box), layout_mode, box_state.available_inner_space_or_constraints_from(available_space));
  580. }
  581. }
  582. // Tables already set their height during the independent formatting context run. When multi-line text cells are involved, using different
  583. // available space here than during the independent formatting context run can result in different line breaks and thus a different height.
  584. if (!box.display().is_table_inside()) {
  585. compute_height(box, available_space);
  586. }
  587. if (independent_formatting_context || !margins_collapse_through(box, m_state)) {
  588. if (!m_margin_state.box_last_in_flow_child_margin_bottom_collapsed) {
  589. m_margin_state.reset();
  590. }
  591. m_y_offset_of_current_block_container = box_state.offset.y() + box_state.content_height() + box_state.border_box_bottom();
  592. }
  593. m_margin_state.box_last_in_flow_child_margin_bottom_collapsed = false;
  594. m_margin_state.add_margin(box_state.margin_bottom);
  595. m_margin_state.update_block_waiting_for_final_y_position();
  596. compute_inset(box);
  597. if (is<ListItemBox>(box)) {
  598. layout_list_item_marker(static_cast<ListItemBox const&>(box));
  599. }
  600. bottom_of_lowest_margin_box = max(bottom_of_lowest_margin_box, box_state.offset.y() + box_state.content_height() + box_state.margin_box_bottom());
  601. if (independent_formatting_context)
  602. independent_formatting_context->parent_context_did_dimension_child_root_box();
  603. }
  604. void BlockFormattingContext::layout_block_level_children(BlockContainer const& block_container, LayoutMode layout_mode, AvailableSpace const& available_space)
  605. {
  606. VERIFY(!block_container.children_are_inline());
  607. CSSPixels bottom_of_lowest_margin_box = 0;
  608. TemporaryChange<Optional<CSSPixels>> change { m_y_offset_of_current_block_container, CSSPixels(0) };
  609. block_container.for_each_child_of_type<Box>([&](Box& box) {
  610. layout_block_level_box(box, block_container, layout_mode, bottom_of_lowest_margin_box, available_space);
  611. return IterationDecision::Continue;
  612. });
  613. m_margin_state.block_container_y_position_update_callback = {};
  614. if (layout_mode == LayoutMode::IntrinsicSizing) {
  615. auto& block_container_state = m_state.get_mutable(block_container);
  616. if (!block_container_state.has_definite_width())
  617. block_container_state.set_content_width(greatest_child_width(block_container));
  618. if (!block_container_state.has_definite_height())
  619. block_container_state.set_content_height(bottom_of_lowest_margin_box);
  620. }
  621. }
  622. void BlockFormattingContext::resolve_vertical_box_model_metrics(Box const& box)
  623. {
  624. auto& box_state = m_state.get_mutable(box);
  625. auto const& computed_values = box.computed_values();
  626. auto width_of_containing_block = containing_block_width_for(box);
  627. box_state.margin_top = computed_values.margin().top().to_px(box, width_of_containing_block);
  628. box_state.margin_bottom = computed_values.margin().bottom().to_px(box, width_of_containing_block);
  629. box_state.border_top = computed_values.border_top().width;
  630. box_state.border_bottom = computed_values.border_bottom().width;
  631. box_state.padding_top = computed_values.padding().top().to_px(box, width_of_containing_block);
  632. box_state.padding_bottom = computed_values.padding().bottom().to_px(box, width_of_containing_block);
  633. }
  634. CSSPixels BlockFormattingContext::BlockMarginState::current_collapsed_margin() const
  635. {
  636. CSSPixels smallest_margin = 0;
  637. CSSPixels largest_margin = 0;
  638. size_t negative_margin_count = 0;
  639. for (auto margin : current_collapsible_margins) {
  640. if (margin < 0)
  641. ++negative_margin_count;
  642. largest_margin = max(largest_margin, margin);
  643. smallest_margin = min(smallest_margin, margin);
  644. }
  645. CSSPixels collapsed_margin = 0;
  646. if (negative_margin_count == current_collapsible_margins.size()) {
  647. // When all margins are negative, the size of the collapsed margin is the smallest (most negative) margin.
  648. collapsed_margin = smallest_margin;
  649. } else if (negative_margin_count > 0) {
  650. // 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.
  651. collapsed_margin = largest_margin + smallest_margin;
  652. } else {
  653. // Otherwise, collapse all the adjacent margins by using only the largest one.
  654. collapsed_margin = largest_margin;
  655. }
  656. return collapsed_margin;
  657. }
  658. BlockFormattingContext::DidIntroduceClearance BlockFormattingContext::clear_floating_boxes(Node const& child_box, Optional<InlineFormattingContext&> inline_formatting_context)
  659. {
  660. auto const& computed_values = child_box.computed_values();
  661. auto result = DidIntroduceClearance::No;
  662. auto clear_floating_boxes = [&](FloatSideData& float_side) {
  663. if (!float_side.current_boxes.is_empty()) {
  664. // NOTE: Floating boxes are globally relevant within this BFC, *but* their offset coordinates
  665. // are relative to their containing block.
  666. // This means that we have to first convert to a root-space Y coordinate before clearing,
  667. // and then convert back to a local Y coordinate when assigning the cleared offset to
  668. // the `child_box` layout state.
  669. // First, find the lowest margin box edge on this float side and calculate the Y offset just below it.
  670. CSSPixels clearance_y_in_root = 0;
  671. for (auto const& floating_box : float_side.current_boxes) {
  672. auto floating_box_rect_in_root = margin_box_rect_in_ancestor_coordinate_space(floating_box.box, root());
  673. clearance_y_in_root = max(clearance_y_in_root, floating_box_rect_in_root.bottom());
  674. }
  675. // Then, convert the clearance Y to a coordinate relative to the containing block of `child_box`.
  676. CSSPixels clearance_y_in_containing_block = clearance_y_in_root;
  677. for (auto* containing_block = child_box.containing_block(); containing_block && containing_block != &root(); containing_block = containing_block->containing_block())
  678. clearance_y_in_containing_block -= m_state.get(*containing_block).offset.y();
  679. if (inline_formatting_context.has_value()) {
  680. if (clearance_y_in_containing_block > inline_formatting_context->vertical_float_clearance()) {
  681. result = DidIntroduceClearance::Yes;
  682. inline_formatting_context->set_vertical_float_clearance(clearance_y_in_containing_block);
  683. }
  684. } else if (clearance_y_in_containing_block > m_y_offset_of_current_block_container.value()) {
  685. result = DidIntroduceClearance::Yes;
  686. m_y_offset_of_current_block_container = clearance_y_in_containing_block;
  687. }
  688. float_side.clear();
  689. }
  690. };
  691. // Flex-items don't float and also don't clear.
  692. if ((computed_values.clear() == CSS::Clear::Left || computed_values.clear() == CSS::Clear::Both) && !child_box.is_flex_item())
  693. clear_floating_boxes(m_left_floats);
  694. if ((computed_values.clear() == CSS::Clear::Right || computed_values.clear() == CSS::Clear::Both) && !child_box.is_flex_item())
  695. clear_floating_boxes(m_right_floats);
  696. return result;
  697. }
  698. void BlockFormattingContext::place_block_level_element_in_normal_flow_vertically(Box const& child_box, CSSPixels y)
  699. {
  700. auto& box_state = m_state.get_mutable(child_box);
  701. y += box_state.border_box_top();
  702. box_state.set_content_offset(CSSPixelPoint { box_state.offset.x(), y });
  703. }
  704. // Returns whether the given box has the given ancestor on the path to root, ignoring the anonymous blocks.
  705. static bool box_has_ancestor_in_non_anonymous_containing_block_chain(Box const* box, Box const& ancestor, Box const& root)
  706. {
  707. Box const* current_ancestor = box ? box->non_anonymous_containing_block() : &root;
  708. while (current_ancestor != &root) {
  709. if (current_ancestor == &ancestor)
  710. return true;
  711. current_ancestor = current_ancestor->non_anonymous_containing_block();
  712. }
  713. return false;
  714. }
  715. void BlockFormattingContext::place_block_level_element_in_normal_flow_horizontally(Box const& child_box, AvailableSpace const& available_space)
  716. {
  717. auto& box_state = m_state.get_mutable(child_box);
  718. CSSPixels x = 0;
  719. CSSPixels available_width_within_containing_block = available_space.width.to_px();
  720. if ((!m_left_floats.current_boxes.is_empty() || !m_right_floats.current_boxes.is_empty())
  721. && creates_block_formatting_context(child_box)) {
  722. auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(child_box, root());
  723. auto space_and_containing_margin = space_used_and_containing_margin_for_floats(box_in_root_rect.y());
  724. available_width_within_containing_block -= space_and_containing_margin.left_used_space + space_and_containing_margin.right_used_space;
  725. auto const& containing_box_state = m_state.get(*child_box.containing_block());
  726. if (box_has_ancestor_in_non_anonymous_containing_block_chain(space_and_containing_margin.matching_left_float_box, *child_box.non_anonymous_containing_block(), root()))
  727. x = space_and_containing_margin.left_used_space;
  728. else
  729. // If the floating box doesn't share a containing block with the child box, the child box margin should overlap with the width of the floating box.
  730. x = max(space_and_containing_margin.left_used_space - containing_box_state.margin_left, 0);
  731. }
  732. if (child_box.containing_block()->computed_values().text_align() == CSS::TextAlign::LibwebCenter) {
  733. x += (available_width_within_containing_block / 2) - box_state.content_width() / 2;
  734. } else if (child_box.containing_block()->computed_values().text_align() == CSS::TextAlign::LibwebRight) {
  735. // Subtracting the left margin here because left and right margins need to be swapped when aligning to the right
  736. x += available_width_within_containing_block - box_state.content_width() - box_state.margin_box_left();
  737. } else {
  738. x += box_state.margin_box_left();
  739. }
  740. box_state.set_content_offset({ x, box_state.offset.y() });
  741. }
  742. void BlockFormattingContext::layout_viewport(LayoutMode layout_mode, AvailableSpace const& available_space)
  743. {
  744. // NOTE: If we are laying out a standalone SVG document, we give it some special treatment:
  745. // The root <svg> container gets the same size as the viewport,
  746. // and we call directly into the SVG layout code from here.
  747. if (root().first_child() && root().first_child()->is_svg_svg_box()) {
  748. auto const& svg_root = verify_cast<SVGSVGBox>(*root().first_child());
  749. auto svg_formatting_context = create_independent_formatting_context_if_needed(m_state, svg_root);
  750. svg_formatting_context->run(svg_root, layout_mode, available_space);
  751. } else {
  752. if (root().children_are_inline())
  753. layout_inline_children(root(), layout_mode, available_space);
  754. else
  755. layout_block_level_children(root(), layout_mode, available_space);
  756. }
  757. }
  758. void BlockFormattingContext::layout_floating_box(Box const& box, BlockContainer const&, LayoutMode layout_mode, AvailableSpace const& available_space, CSSPixels y, LineBuilder* line_builder)
  759. {
  760. VERIFY(box.is_floating());
  761. auto& box_state = m_state.get_mutable(box);
  762. CSSPixels width_of_containing_block = available_space.width.to_px();
  763. resolve_vertical_box_model_metrics(box);
  764. compute_width(box, available_space, layout_mode);
  765. auto independent_formatting_context = layout_inside(box, layout_mode, box_state.available_inner_space_or_constraints_from(available_space));
  766. compute_height(box, available_space);
  767. // First we place the box normally (to get the right y coordinate.)
  768. // If we have a LineBuilder, we're in the middle of inline layout, otherwise this is block layout.
  769. if (line_builder) {
  770. auto y = max(line_builder->y_for_float_to_be_inserted_here(box), line_builder->inline_formatting_context().vertical_float_clearance());
  771. box_state.set_content_y(y + box_state.margin_box_top());
  772. } else {
  773. place_block_level_element_in_normal_flow_vertically(box, y + box_state.margin_top);
  774. place_block_level_element_in_normal_flow_horizontally(box, available_space);
  775. }
  776. // Then we float it to the left or right.
  777. auto float_box = [&](FloatSide side, FloatSideData& side_data, FloatSideData& other_side_data) {
  778. CSSPixels offset_from_edge = 0;
  779. auto float_to_edge = [&] {
  780. if (side == FloatSide::Left)
  781. offset_from_edge = box_state.margin_box_left();
  782. else
  783. offset_from_edge = box_state.content_width() + box_state.margin_box_right();
  784. };
  785. auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(box, root());
  786. CSSPixels y_in_root = box_in_root_rect.y();
  787. CSSPixels y = box_state.offset.y();
  788. if (side_data.current_boxes.is_empty()) {
  789. // This is the first floating box on this side. Go all the way to the edge.
  790. float_to_edge();
  791. side_data.y_offset = 0;
  792. } else {
  793. // NOTE: If we're in inline layout, the LineBuilder has already provided the right Y offset.
  794. // In block layout, we adjust by the side's current Y offset here.
  795. if (!line_builder)
  796. y_in_root += side_data.y_offset;
  797. bool did_touch_preceding_float = false;
  798. bool did_place_next_to_preceding_float = false;
  799. // Walk all currently tracked floats on the side we're floating towards.
  800. // We're looking for the innermost preceding float that intersects vertically with `box`.
  801. for (auto& preceding_float : side_data.current_boxes.in_reverse()) {
  802. auto const preceding_float_rect = margin_box_rect_in_ancestor_coordinate_space(preceding_float.box, root());
  803. if (!preceding_float_rect.contains_vertically(y_in_root))
  804. continue;
  805. // We found a preceding float that intersects vertically with the current float.
  806. // Now we need to find out if there's enough inline-axis space to stack them next to each other.
  807. auto const& preceding_float_state = m_state.get(preceding_float.box);
  808. CSSPixels tentative_offset_from_edge = 0;
  809. bool fits_next_to_preceding_float = false;
  810. if (side == FloatSide::Left) {
  811. 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();
  812. fits_next_to_preceding_float = (tentative_offset_from_edge + box_state.content_width() + box_state.margin_box_right()) <= width_of_containing_block;
  813. } else {
  814. tentative_offset_from_edge = preceding_float.offset_from_edge + preceding_float_state.margin_box_left() + box_state.margin_box_right() + box_state.content_width();
  815. fits_next_to_preceding_float = tentative_offset_from_edge >= 0;
  816. }
  817. did_touch_preceding_float = true;
  818. if (!fits_next_to_preceding_float)
  819. break;
  820. offset_from_edge = tentative_offset_from_edge;
  821. did_place_next_to_preceding_float = true;
  822. break;
  823. }
  824. if (!did_touch_preceding_float || !did_place_next_to_preceding_float) {
  825. // One of two things happened:
  826. // - This box does not touch another floating box.
  827. // - We ran out of horizontal space on this "float line", and need to break.
  828. // Either way, we float this box all the way to the edge.
  829. float_to_edge();
  830. CSSPixels lowest_margin_edge = 0;
  831. for (auto const& box : side_data.current_boxes) {
  832. auto const& box_state = m_state.get(box.box);
  833. lowest_margin_edge = max(lowest_margin_edge, box_state.margin_box_height());
  834. }
  835. side_data.y_offset += lowest_margin_edge;
  836. // Also, forget all previous boxes floated to this side while since they're no longer relevant.
  837. side_data.clear();
  838. }
  839. }
  840. // NOTE: If we're in inline layout, the LineBuilder has already provided the right Y offset.
  841. // In block layout, we adjust by the side's current Y offset here.
  842. // FIXME: It's annoying that we have different behavior for inline vs block here.
  843. // Find a way to unify the behavior so we don't need to branch here.
  844. if (!line_builder)
  845. y += side_data.y_offset;
  846. auto top_margin_edge = y - box_state.margin_box_top();
  847. side_data.all_boxes.append(adopt_own(*new FloatingBox {
  848. .box = box,
  849. .offset_from_edge = offset_from_edge,
  850. .top_margin_edge = top_margin_edge,
  851. .bottom_margin_edge = y + box_state.content_height() + box_state.margin_box_bottom(),
  852. }));
  853. side_data.current_boxes.append(*side_data.all_boxes.last());
  854. if (side == FloatSide::Left) {
  855. side_data.current_width = offset_from_edge + box_state.content_width() + box_state.margin_box_right();
  856. } else {
  857. side_data.current_width = offset_from_edge + box_state.margin_box_left();
  858. }
  859. side_data.max_width = max(side_data.current_width, side_data.max_width);
  860. // NOTE: We don't set the X position here, that happens later, once we know the root block width.
  861. // See parent_context_did_dimension_child_root_box() for that logic.
  862. box_state.set_content_y(y);
  863. // If the new box was inserted below the bottom of the opposite side,
  864. // we reset the other side back to its edge.
  865. if (top_margin_edge > other_side_data.y_offset)
  866. other_side_data.clear();
  867. };
  868. // Next, float to the left and/or right
  869. if (box.computed_values().float_() == CSS::Float::Left) {
  870. float_box(FloatSide::Left, m_left_floats, m_right_floats);
  871. } else if (box.computed_values().float_() == CSS::Float::Right) {
  872. float_box(FloatSide::Right, m_right_floats, m_left_floats);
  873. }
  874. m_state.get_mutable(root()).add_floating_descendant(box);
  875. if (line_builder)
  876. line_builder->recalculate_available_space();
  877. compute_inset(box);
  878. if (independent_formatting_context)
  879. independent_formatting_context->parent_context_did_dimension_child_root_box();
  880. }
  881. void BlockFormattingContext::layout_list_item_marker(ListItemBox const& list_item_box)
  882. {
  883. if (!list_item_box.marker())
  884. return;
  885. auto& marker = *list_item_box.marker();
  886. auto& marker_state = m_state.get_mutable(marker);
  887. auto& list_item_state = m_state.get_mutable(list_item_box);
  888. CSSPixels image_width = 0;
  889. CSSPixels image_height = 0;
  890. if (auto const* list_style_image = marker.list_style_image()) {
  891. image_width = list_style_image->natural_width().value_or(0);
  892. image_height = list_style_image->natural_height().value_or(0);
  893. }
  894. CSSPixels default_marker_width = max(4, marker.font().pixel_size_rounded_up() - 4);
  895. if (marker.text().is_empty()) {
  896. marker_state.set_content_width(image_width + default_marker_width);
  897. } else {
  898. auto text_width = marker.font().width(marker.text());
  899. marker_state.set_content_width(image_width + text_width);
  900. }
  901. marker_state.set_content_height(max(image_height, marker.font().pixel_size_rounded_up() + 1));
  902. if (marker.list_style_type() == CSS::ListStyleType::DisclosureClosed || marker.list_style_type() == CSS::ListStyleType::DisclosureOpen)
  903. marker_state.set_content_width(marker_state.content_height());
  904. auto final_marker_width = marker_state.content_width() + default_marker_width;
  905. if (marker.list_style_position() == CSS::ListStylePosition::Inside) {
  906. list_item_state.set_content_offset({ final_marker_width, list_item_state.offset.y() });
  907. list_item_state.set_content_width(list_item_state.content_width() - final_marker_width);
  908. }
  909. marker_state.set_content_offset({ -final_marker_width, max(CSSPixels(0), (CSSPixels(marker.line_height()) - marker_state.content_height()) / 2) });
  910. if (marker_state.content_height() > list_item_state.content_height())
  911. list_item_state.set_content_height(marker_state.content_height());
  912. }
  913. BlockFormattingContext::SpaceUsedAndContainingMarginForFloats BlockFormattingContext::space_used_and_containing_margin_for_floats(CSSPixels y) const
  914. {
  915. SpaceUsedAndContainingMarginForFloats space_and_containing_margin;
  916. for (auto const& floating_box_ptr : m_left_floats.all_boxes.in_reverse()) {
  917. auto const& floating_box = *floating_box_ptr;
  918. auto const& floating_box_state = m_state.get(floating_box.box);
  919. // NOTE: The floating box is *not* in the final horizontal position yet, but the size and vertical position is valid.
  920. auto rect = margin_box_rect_in_ancestor_coordinate_space(floating_box.box, root());
  921. if (rect.contains_vertically(y)) {
  922. CSSPixels offset_from_containing_block_chain_margins_between_here_and_root = 0;
  923. for (auto const* containing_block = floating_box.box->containing_block(); containing_block && containing_block != &root(); containing_block = containing_block->containing_block()) {
  924. auto const& containing_block_state = m_state.get(*containing_block);
  925. offset_from_containing_block_chain_margins_between_here_and_root += containing_block_state.margin_box_left();
  926. }
  927. space_and_containing_margin.left_used_space = floating_box.offset_from_edge
  928. + floating_box_state.content_width()
  929. + floating_box_state.margin_box_right();
  930. space_and_containing_margin.left_total_containing_margin = offset_from_containing_block_chain_margins_between_here_and_root;
  931. space_and_containing_margin.matching_left_float_box = floating_box.box.ptr();
  932. break;
  933. }
  934. }
  935. for (auto const& floating_box_ptr : m_right_floats.all_boxes.in_reverse()) {
  936. auto const& floating_box = *floating_box_ptr;
  937. auto const& floating_box_state = m_state.get(floating_box.box);
  938. // NOTE: The floating box is *not* in the final horizontal position yet, but the size and vertical position is valid.
  939. auto rect = margin_box_rect_in_ancestor_coordinate_space(floating_box.box, root());
  940. if (rect.contains_vertically(y)) {
  941. CSSPixels offset_from_containing_block_chain_margins_between_here_and_root = 0;
  942. for (auto const* containing_block = floating_box.box->containing_block(); containing_block && containing_block != &root(); containing_block = containing_block->containing_block()) {
  943. auto const& containing_block_state = m_state.get(*containing_block);
  944. offset_from_containing_block_chain_margins_between_here_and_root += containing_block_state.margin_box_right();
  945. }
  946. space_and_containing_margin.right_used_space = floating_box.offset_from_edge
  947. + floating_box_state.margin_box_left();
  948. space_and_containing_margin.right_total_containing_margin = offset_from_containing_block_chain_margins_between_here_and_root;
  949. break;
  950. }
  951. }
  952. return space_and_containing_margin;
  953. }
  954. FormattingContext::SpaceUsedByFloats BlockFormattingContext::intrusion_by_floats_into_box(Box const& box, CSSPixels y_in_box) const
  955. {
  956. // NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC.
  957. auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(box, root());
  958. CSSPixels y_in_root = box_in_root_rect.y() + y_in_box;
  959. auto space_and_containing_margin = space_used_and_containing_margin_for_floats(y_in_root);
  960. auto left_side_floats_limit_to_right = space_and_containing_margin.left_total_containing_margin + space_and_containing_margin.left_used_space;
  961. auto right_side_floats_limit_to_right = space_and_containing_margin.right_used_space + space_and_containing_margin.right_total_containing_margin;
  962. auto left_intrusion = max(CSSPixels(0), left_side_floats_limit_to_right - max(CSSPixels(0), box_in_root_rect.x()));
  963. CSSPixels offset_from_containing_block_chain_margins_between_here_and_root = 0;
  964. for (auto const* containing_block = static_cast<Box const*>(&box); containing_block && containing_block != &root(); containing_block = containing_block->containing_block()) {
  965. auto const& containing_block_state = m_state.get(*containing_block);
  966. offset_from_containing_block_chain_margins_between_here_and_root = max(offset_from_containing_block_chain_margins_between_here_and_root, containing_block_state.margin_box_right());
  967. }
  968. auto right_intrusion = max(CSSPixels(0), right_side_floats_limit_to_right - offset_from_containing_block_chain_margins_between_here_and_root);
  969. return { left_intrusion, right_intrusion };
  970. }
  971. CSSPixels BlockFormattingContext::greatest_child_width(Box const& box) const
  972. {
  973. // Similar to FormattingContext::greatest_child_width()
  974. // but this one takes floats into account!
  975. CSSPixels max_width = m_left_floats.max_width + m_right_floats.max_width;
  976. if (box.children_are_inline()) {
  977. for (auto const& line_box : m_state.get(verify_cast<BlockContainer>(box)).line_boxes) {
  978. CSSPixels width_here = line_box.width();
  979. CSSPixels extra_width_from_left_floats = 0;
  980. for (auto& left_float : m_left_floats.all_boxes) {
  981. // NOTE: Floats directly affect the automatic size of their containing block, but only indirectly anything above in the tree.
  982. if (left_float->box->containing_block() != &box)
  983. continue;
  984. if (line_box.baseline() >= left_float->top_margin_edge || line_box.baseline() <= left_float->bottom_margin_edge) {
  985. auto const& left_float_state = m_state.get(left_float->box);
  986. 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());
  987. }
  988. }
  989. CSSPixels extra_width_from_right_floats = 0;
  990. for (auto& right_float : m_right_floats.all_boxes) {
  991. // NOTE: Floats directly affect the automatic size of their containing block, but only indirectly anything above in the tree.
  992. if (right_float->box->containing_block() != &box)
  993. continue;
  994. if (line_box.baseline() >= right_float->top_margin_edge || line_box.baseline() <= right_float->bottom_margin_edge) {
  995. auto const& right_float_state = m_state.get(right_float->box);
  996. extra_width_from_right_floats = max(extra_width_from_right_floats, right_float->offset_from_edge + right_float_state.margin_box_left());
  997. }
  998. }
  999. width_here += extra_width_from_left_floats + extra_width_from_right_floats;
  1000. max_width = max(max_width, width_here);
  1001. }
  1002. } else {
  1003. box.for_each_child_of_type<Box>([&](Box const& child) {
  1004. if (!child.is_absolutely_positioned())
  1005. max_width = max(max_width, m_state.get(child).margin_box_width());
  1006. });
  1007. }
  1008. return max_width;
  1009. }
  1010. void BlockFormattingContext::determine_width_of_child(Box const&, AvailableSpace const&)
  1011. {
  1012. // NOTE: We don't do anything here, since we'll have already determined the width of the child
  1013. // before recursing into nested layout within the child.
  1014. }
  1015. void BlockFormattingContext::determine_height_of_child(Box const& box, AvailableSpace const& available_space)
  1016. {
  1017. compute_height(box, available_space);
  1018. }
  1019. }