BlockFormattingContext.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/Length.h>
  7. #include <LibWeb/DOM/Node.h>
  8. #include <LibWeb/HTML/BrowsingContext.h>
  9. #include <LibWeb/Layout/BlockContainer.h>
  10. #include <LibWeb/Layout/BlockFormattingContext.h>
  11. #include <LibWeb/Layout/Box.h>
  12. #include <LibWeb/Layout/InitialContainingBlock.h>
  13. #include <LibWeb/Layout/InlineFormattingContext.h>
  14. #include <LibWeb/Layout/ListItemBox.h>
  15. #include <LibWeb/Layout/ListItemMarkerBox.h>
  16. #include <LibWeb/Layout/ReplacedBox.h>
  17. namespace Web::Layout {
  18. BlockFormattingContext::BlockFormattingContext(FormattingState& state, BlockContainer const& root, FormattingContext* parent)
  19. : FormattingContext(Type::Block, state, root, parent)
  20. {
  21. }
  22. BlockFormattingContext::~BlockFormattingContext()
  23. {
  24. if (!m_was_notified_after_parent_dimensioned_my_root_box) {
  25. // HACK: The parent formatting context never notified us after assigning dimensions to our root box.
  26. // Pretend that it did anyway, to make sure absolutely positioned children get laid out.
  27. // FIXME: Get rid of this hack once parent contexts behave properly.
  28. parent_context_did_dimension_child_root_box();
  29. }
  30. }
  31. bool BlockFormattingContext::is_initial() const
  32. {
  33. return is<InitialContainingBlock>(root());
  34. }
  35. void BlockFormattingContext::run(Box const&, LayoutMode layout_mode)
  36. {
  37. if (is_initial()) {
  38. layout_initial_containing_block(layout_mode);
  39. return;
  40. }
  41. if (root().children_are_inline())
  42. layout_inline_children(root(), layout_mode);
  43. else
  44. layout_block_level_children(root(), layout_mode);
  45. }
  46. void BlockFormattingContext::parent_context_did_dimension_child_root_box()
  47. {
  48. m_was_notified_after_parent_dimensioned_my_root_box = true;
  49. for (auto& box : m_absolutely_positioned_boxes)
  50. layout_absolutely_positioned_element(box);
  51. apply_transformations_to_children(root());
  52. }
  53. void BlockFormattingContext::apply_transformations_to_children(Box const& box)
  54. {
  55. box.for_each_child_of_type<Box>([&](auto& child_box) {
  56. float transform_y_offset = 0.0f;
  57. if (!child_box.computed_values().transformations().is_empty()) {
  58. // FIXME: All transformations can be interpreted as successive 3D-matrix operations on the box, we don't do that yet.
  59. // https://drafts.csswg.org/css-transforms/#serialization-of-the-computed-value
  60. for (auto transformation : child_box.computed_values().transformations()) {
  61. switch (transformation.function) {
  62. case CSS::TransformFunction::TranslateY:
  63. if (transformation.values.size() != 1)
  64. continue;
  65. transformation.values.first().visit(
  66. [&](CSS::Length& value) {
  67. transform_y_offset += value.to_px(child_box);
  68. },
  69. [&](float value) {
  70. transform_y_offset += value;
  71. },
  72. [&](auto&) {
  73. dbgln("FIXME: Implement unsupported transformation function value type!");
  74. });
  75. break;
  76. default:
  77. dbgln("FIXME: Implement missing transform function!");
  78. }
  79. }
  80. }
  81. auto& child_box_state = m_state.get_mutable(child_box);
  82. auto untransformed_offset = child_box_state.offset;
  83. child_box_state.offset = Gfx::FloatPoint { untransformed_offset.x(), untransformed_offset.y() + transform_y_offset };
  84. });
  85. }
  86. void BlockFormattingContext::compute_width(Box const& box)
  87. {
  88. if (box.is_absolutely_positioned()) {
  89. compute_width_for_absolutely_positioned_element(box);
  90. return;
  91. }
  92. if (is<ReplacedBox>(box)) {
  93. // FIXME: This should not be done *by* ReplacedBox
  94. auto& replaced = verify_cast<ReplacedBox>(box);
  95. // FIXME: This const_cast is gross.
  96. const_cast<ReplacedBox&>(replaced).prepare_for_replaced_layout();
  97. compute_width_for_block_level_replaced_element_in_normal_flow(replaced);
  98. return;
  99. }
  100. if (box.is_floating()) {
  101. compute_width_for_floating_box(box);
  102. return;
  103. }
  104. auto const& computed_values = box.computed_values();
  105. float width_of_containing_block = m_state.get(*box.containing_block()).content_width;
  106. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  107. auto zero_value = CSS::Length::make_px(0);
  108. auto margin_left = CSS::Length::make_auto();
  109. auto margin_right = CSS::Length::make_auto();
  110. const auto padding_left = computed_values.padding().left.resolved(box, width_of_containing_block_as_length).resolved(box);
  111. const auto padding_right = computed_values.padding().right.resolved(box, width_of_containing_block_as_length).resolved(box);
  112. auto try_compute_width = [&](const auto& a_width) {
  113. CSS::Length width = a_width;
  114. margin_left = computed_values.margin().left.resolved(box, width_of_containing_block_as_length).resolved(box);
  115. margin_right = computed_values.margin().right.resolved(box, width_of_containing_block_as_length).resolved(box);
  116. float total_px = computed_values.border_left().width + computed_values.border_right().width;
  117. for (auto& value : { margin_left, padding_left, width, padding_right, margin_right }) {
  118. total_px += value.to_px(box);
  119. }
  120. if (!box.is_inline()) {
  121. // 10.3.3 Block-level, non-replaced elements in normal flow
  122. // 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.
  123. if (width.is_auto() && total_px > width_of_containing_block) {
  124. if (margin_left.is_auto())
  125. margin_left = zero_value;
  126. if (margin_right.is_auto())
  127. margin_right = zero_value;
  128. }
  129. // 10.3.3 cont'd.
  130. auto underflow_px = width_of_containing_block - total_px;
  131. if (width.is_auto()) {
  132. if (margin_left.is_auto())
  133. margin_left = zero_value;
  134. if (margin_right.is_auto())
  135. margin_right = zero_value;
  136. if (underflow_px >= 0) {
  137. width = CSS::Length(underflow_px, CSS::Length::Type::Px);
  138. } else {
  139. width = zero_value;
  140. margin_right = CSS::Length(margin_right.to_px(box) + underflow_px, CSS::Length::Type::Px);
  141. }
  142. } else {
  143. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  144. margin_right = CSS::Length(margin_right.to_px(box) + underflow_px, CSS::Length::Type::Px);
  145. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  146. margin_right = CSS::Length(underflow_px, CSS::Length::Type::Px);
  147. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  148. margin_left = CSS::Length(underflow_px, CSS::Length::Type::Px);
  149. } else { // margin_left.is_auto() && margin_right.is_auto()
  150. auto half_of_the_underflow = CSS::Length(underflow_px / 2, CSS::Length::Type::Px);
  151. margin_left = half_of_the_underflow;
  152. margin_right = half_of_the_underflow;
  153. }
  154. }
  155. } else if (box.is_inline_block()) {
  156. // 10.3.9 'Inline-block', non-replaced elements in normal flow
  157. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  158. if (margin_left.is_auto())
  159. margin_left = zero_value;
  160. if (margin_right.is_auto())
  161. margin_right = zero_value;
  162. // If 'width' is 'auto', the used value is the shrink-to-fit width as for floating elements.
  163. if (width.is_auto()) {
  164. // Find the available width: in this case, this is the width of the containing
  165. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  166. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  167. float available_width = width_of_containing_block
  168. - margin_left.to_px(box) - computed_values.border_left().width - padding_left.to_px(box)
  169. - padding_right.to_px(box) - computed_values.border_right().width - margin_right.to_px(box);
  170. auto result = calculate_shrink_to_fit_widths(box);
  171. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  172. width = CSS::Length(min(max(result.preferred_minimum_width, available_width), result.preferred_width), CSS::Length::Type::Px);
  173. }
  174. }
  175. return width;
  176. };
  177. auto specified_width = computed_values.width().has_value() ? computed_values.width()->resolved(box, width_of_containing_block_as_length).resolved(box) : CSS::Length::make_auto();
  178. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  179. auto used_width = try_compute_width(specified_width);
  180. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  181. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  182. auto specified_max_width = computed_values.max_width().has_value() ? computed_values.max_width()->resolved(box, width_of_containing_block_as_length).resolved(box) : CSS::Length::make_auto();
  183. if (!specified_max_width.is_auto()) {
  184. if (used_width.to_px(box) > specified_max_width.to_px(box)) {
  185. used_width = try_compute_width(specified_max_width);
  186. }
  187. }
  188. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  189. // but this time using the value of 'min-width' as the computed value for 'width'.
  190. auto specified_min_width = computed_values.min_width().has_value() ? computed_values.min_width()->resolved(box, width_of_containing_block_as_length).resolved(box) : CSS::Length::make_auto();
  191. if (!specified_min_width.is_auto()) {
  192. if (used_width.to_px(box) < specified_min_width.to_px(box)) {
  193. used_width = try_compute_width(specified_min_width);
  194. }
  195. }
  196. auto& box_state = m_state.get_mutable(box);
  197. box_state.content_width = used_width.to_px(box);
  198. box_state.margin_left = margin_left.to_px(box);
  199. box_state.margin_right = margin_right.to_px(box);
  200. box_state.border_left = computed_values.border_left().width;
  201. box_state.border_right = computed_values.border_right().width;
  202. box_state.padding_left = padding_left.to_px(box);
  203. box_state.padding_right = padding_right.to_px(box);
  204. }
  205. void BlockFormattingContext::compute_width_for_floating_box(Box const& box)
  206. {
  207. // 10.3.5 Floating, non-replaced elements
  208. auto& computed_values = box.computed_values();
  209. auto& containing_block = *box.containing_block();
  210. float width_of_containing_block = m_state.get(containing_block).content_width;
  211. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  212. auto zero_value = CSS::Length::make_px(0);
  213. auto margin_left = computed_values.margin().left.resolved(box, width_of_containing_block_as_length).resolved(box);
  214. auto margin_right = computed_values.margin().right.resolved(box, width_of_containing_block_as_length).resolved(box);
  215. const auto padding_left = computed_values.padding().left.resolved(box, width_of_containing_block_as_length).resolved(box);
  216. const auto padding_right = computed_values.padding().right.resolved(box, width_of_containing_block_as_length).resolved(box);
  217. // If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
  218. if (margin_left.is_auto())
  219. margin_left = zero_value;
  220. if (margin_right.is_auto())
  221. margin_right = zero_value;
  222. auto width = computed_values.width().has_value() ? computed_values.width()->resolved(box, width_of_containing_block_as_length).resolved(box) : CSS::Length::make_auto();
  223. // If 'width' is computed as 'auto', the used value is the "shrink-to-fit" width.
  224. if (width.is_auto()) {
  225. // Find the available width: in this case, this is the width of the containing
  226. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  227. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  228. float available_width = width_of_containing_block
  229. - margin_left.to_px(box) - computed_values.border_left().width - padding_left.to_px(box)
  230. - padding_right.to_px(box) - computed_values.border_right().width - margin_right.to_px(box);
  231. auto result = calculate_shrink_to_fit_widths(box);
  232. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  233. width = CSS::Length(min(max(result.preferred_minimum_width, available_width), result.preferred_width), CSS::Length::Type::Px);
  234. }
  235. auto& box_state = m_state.get_mutable(box);
  236. box_state.content_width = width.to_px(box);
  237. box_state.margin_left = margin_left.to_px(box);
  238. box_state.margin_right = margin_right.to_px(box);
  239. box_state.border_left = computed_values.border_left().width;
  240. box_state.border_right = computed_values.border_right().width;
  241. box_state.padding_left = padding_left.to_px(box);
  242. box_state.padding_right = padding_right.to_px(box);
  243. }
  244. void BlockFormattingContext::compute_width_for_block_level_replaced_element_in_normal_flow(ReplacedBox const& box)
  245. {
  246. m_state.get_mutable(box).content_width = compute_width_for_replaced_element(m_state, box);
  247. }
  248. float BlockFormattingContext::compute_theoretical_height(FormattingState const& state, Box const& box)
  249. {
  250. auto const& computed_values = box.computed_values();
  251. auto const& containing_block = *box.containing_block();
  252. auto const& containing_block_state = state.get(containing_block);
  253. auto containing_block_height = CSS::Length::make_px(containing_block_state.content_height);
  254. auto is_absolute = [](Optional<CSS::LengthPercentage> const& length_percentage) {
  255. return length_percentage.has_value() && length_percentage->is_length() && length_percentage->length().is_absolute();
  256. };
  257. // Then work out what the height is, based on box type and CSS properties.
  258. float height = 0;
  259. if (is<ReplacedBox>(box)) {
  260. height = compute_height_for_replaced_element(state, verify_cast<ReplacedBox>(box));
  261. } else {
  262. if (!box.computed_values().height().has_value()
  263. || (box.computed_values().height()->is_length() && box.computed_values().height()->length().is_auto())
  264. || (computed_values.height().has_value() && computed_values.height()->is_percentage() && !is_absolute(containing_block.computed_values().height()))) {
  265. height = compute_auto_height_for_block_level_element(state, box);
  266. } else {
  267. height = computed_values.height().has_value() ? computed_values.height()->resolved(box, containing_block_height).to_px(box) : 0;
  268. }
  269. }
  270. auto specified_max_height = computed_values.max_height().has_value() ? computed_values.max_height()->resolved(box, containing_block_height).resolved(box) : CSS::Length::make_auto();
  271. if (!specified_max_height.is_auto()
  272. && !(computed_values.max_height().has_value() && computed_values.max_height()->is_percentage() && !is_absolute(containing_block.computed_values().height())))
  273. height = min(height, specified_max_height.to_px(box));
  274. auto specified_min_height = computed_values.min_height().has_value() ? computed_values.min_height()->resolved(box, containing_block_height).resolved(box) : CSS::Length::make_auto();
  275. if (!specified_min_height.is_auto()
  276. && !(computed_values.min_height().has_value() && computed_values.min_height()->is_percentage() && !is_absolute(containing_block.computed_values().height())))
  277. height = max(height, specified_min_height.to_px(box));
  278. return height;
  279. }
  280. void BlockFormattingContext::compute_height(Box const& box, FormattingState& state)
  281. {
  282. auto const& computed_values = box.computed_values();
  283. auto const& containing_block = *box.containing_block();
  284. auto width_of_containing_block_as_length = CSS::Length::make_px(state.get(containing_block).content_width);
  285. // First, resolve the top/bottom parts of the surrounding box model.
  286. auto& box_state = state.get_mutable(box);
  287. // FIXME: While negative values are generally allowed for margins, for now just ignore those for height calculation
  288. box_state.margin_top = max(computed_values.margin().top.resolved(box, width_of_containing_block_as_length).to_px(box), 0);
  289. box_state.margin_bottom = max(computed_values.margin().bottom.resolved(box, width_of_containing_block_as_length).to_px(box), 0);
  290. box_state.border_top = computed_values.border_top().width;
  291. box_state.border_bottom = computed_values.border_bottom().width;
  292. box_state.padding_top = computed_values.padding().top.resolved(box, width_of_containing_block_as_length).to_px(box);
  293. box_state.padding_bottom = computed_values.padding().bottom.resolved(box, width_of_containing_block_as_length).to_px(box);
  294. box_state.content_height = compute_theoretical_height(state, box);
  295. }
  296. void BlockFormattingContext::layout_inline_children(BlockContainer const& block_container, LayoutMode layout_mode)
  297. {
  298. VERIFY(block_container.children_are_inline());
  299. InlineFormattingContext context(m_state, block_container, *this);
  300. context.run(block_container, layout_mode);
  301. }
  302. void BlockFormattingContext::layout_block_level_children(BlockContainer const& block_container, LayoutMode layout_mode)
  303. {
  304. VERIFY(!block_container.children_are_inline());
  305. float content_height = 0;
  306. float content_width = 0;
  307. block_container.for_each_child_of_type<Box>([&](Box& child_box) {
  308. auto& box_state = m_state.get_mutable(child_box);
  309. if (child_box.is_absolutely_positioned()) {
  310. m_absolutely_positioned_boxes.append(child_box);
  311. return IterationDecision::Continue;
  312. }
  313. // NOTE: ListItemMarkerBoxes are placed by their corresponding ListItemBox.
  314. if (is<ListItemMarkerBox>(child_box))
  315. return IterationDecision::Continue;
  316. if (child_box.is_floating()) {
  317. layout_floating_child(child_box, block_container);
  318. return IterationDecision::Continue;
  319. }
  320. compute_width(child_box);
  321. if (is<ReplacedBox>(child_box) || is<BlockContainer>(child_box))
  322. place_block_level_element_in_normal_flow_vertically(child_box, block_container);
  323. if (child_box.has_definite_height()) {
  324. compute_height(child_box, m_state);
  325. }
  326. OwnPtr<FormattingContext> independent_formatting_context;
  327. if (child_box.can_have_children()) {
  328. independent_formatting_context = create_independent_formatting_context_if_needed(m_state, child_box);
  329. if (independent_formatting_context)
  330. independent_formatting_context->run(child_box, layout_mode);
  331. else
  332. layout_block_level_children(verify_cast<BlockContainer>(child_box), layout_mode);
  333. }
  334. compute_height(child_box, m_state);
  335. compute_position(child_box);
  336. if (is<ReplacedBox>(child_box) || is<BlockContainer>(child_box))
  337. place_block_level_element_in_normal_flow_horizontally(child_box, block_container);
  338. if (is<ListItemBox>(child_box)) {
  339. layout_list_item_marker(static_cast<ListItemBox const&>(child_box));
  340. }
  341. content_height = max(content_height, box_state.offset.y() + box_state.content_height + box_state.margin_box_bottom());
  342. content_width = max(content_width, box_state.border_box_width());
  343. if (independent_formatting_context)
  344. independent_formatting_context->parent_context_did_dimension_child_root_box();
  345. return IterationDecision::Continue;
  346. });
  347. if (layout_mode != LayoutMode::Default) {
  348. auto& width = block_container.computed_values().width();
  349. if (!width.has_value() || (width->is_length() && width->length().is_auto())) {
  350. auto& block_container_state = m_state.get_mutable(block_container);
  351. block_container_state.content_width = content_width;
  352. }
  353. }
  354. }
  355. void BlockFormattingContext::compute_vertical_box_model_metrics(Box const& box, BlockContainer const& containing_block)
  356. {
  357. auto& box_state = m_state.get_mutable(box);
  358. auto const& computed_values = box.computed_values();
  359. auto width_of_containing_block = CSS::Length::make_px(m_state.get(containing_block).content_width);
  360. box_state.margin_top = computed_values.margin().top.resolved(box, width_of_containing_block).resolved(containing_block).to_px(box);
  361. box_state.margin_bottom = computed_values.margin().bottom.resolved(box, width_of_containing_block).resolved(containing_block).to_px(box);
  362. box_state.border_top = computed_values.border_top().width;
  363. box_state.border_bottom = computed_values.border_bottom().width;
  364. box_state.padding_top = computed_values.padding().top.resolved(box, width_of_containing_block).resolved(containing_block).to_px(box);
  365. box_state.padding_bottom = computed_values.padding().bottom.resolved(box, width_of_containing_block).resolved(containing_block).to_px(box);
  366. }
  367. void BlockFormattingContext::place_block_level_element_in_normal_flow_vertically(Box const& child_box, BlockContainer const& containing_block)
  368. {
  369. auto& box_state = m_state.get_mutable(child_box);
  370. auto const& computed_values = child_box.computed_values();
  371. compute_vertical_box_model_metrics(child_box, containing_block);
  372. float y = box_state.margin_box_top()
  373. + box_state.offset_top;
  374. // NOTE: Empty (0-height) preceding siblings have their margins collapsed with *their* preceding sibling, etc.
  375. float collapsed_bottom_margin_of_preceding_siblings = 0;
  376. auto* relevant_sibling = child_box.previous_sibling_of_type<Layout::BlockContainer>();
  377. while (relevant_sibling != nullptr) {
  378. if (!relevant_sibling->is_absolutely_positioned() && !relevant_sibling->is_floating()) {
  379. auto const& relevant_sibling_state = m_state.get(*relevant_sibling);
  380. collapsed_bottom_margin_of_preceding_siblings = max(collapsed_bottom_margin_of_preceding_siblings, relevant_sibling_state.margin_bottom);
  381. if (relevant_sibling_state.border_box_height() > 0)
  382. break;
  383. }
  384. relevant_sibling = relevant_sibling->previous_sibling();
  385. }
  386. if (relevant_sibling) {
  387. auto const& relevant_sibling_state = m_state.get(*relevant_sibling);
  388. y += relevant_sibling_state.offset.y()
  389. + relevant_sibling_state.content_height
  390. + relevant_sibling_state.border_box_bottom();
  391. // Collapse top margin with bottom margin of preceding siblings if needed
  392. float my_margin_top = box_state.margin_top;
  393. if (my_margin_top < 0 || collapsed_bottom_margin_of_preceding_siblings < 0) {
  394. // Negative margins present.
  395. float largest_negative_margin = -min(my_margin_top, collapsed_bottom_margin_of_preceding_siblings);
  396. float largest_positive_margin = (my_margin_top < 0 && collapsed_bottom_margin_of_preceding_siblings < 0) ? 0 : max(my_margin_top, collapsed_bottom_margin_of_preceding_siblings);
  397. float final_margin = largest_positive_margin - largest_negative_margin;
  398. y += final_margin - my_margin_top;
  399. } else if (collapsed_bottom_margin_of_preceding_siblings > my_margin_top) {
  400. // Sibling's margin is larger than mine, adjust so we use sibling's.
  401. y += collapsed_bottom_margin_of_preceding_siblings - my_margin_top;
  402. }
  403. }
  404. auto clear_floating_boxes = [&](FloatSideData& float_side) {
  405. if (!float_side.boxes.is_empty()) {
  406. float clearance_y = 0;
  407. for (auto const& floating_box : float_side.boxes) {
  408. auto const& floating_box_state = m_state.get(floating_box);
  409. clearance_y = max(clearance_y, floating_box_state.offset.y() + floating_box_state.border_box_height());
  410. }
  411. y = max(y, clearance_y);
  412. float_side.boxes.clear();
  413. float_side.y_offset = 0;
  414. }
  415. };
  416. // Flex-items don't float and also don't clear.
  417. if ((computed_values.clear() == CSS::Clear::Left || computed_values.clear() == CSS::Clear::Both) && !child_box.is_flex_item())
  418. clear_floating_boxes(m_left_floats);
  419. if ((computed_values.clear() == CSS::Clear::Right || computed_values.clear() == CSS::Clear::Both) && !child_box.is_flex_item())
  420. clear_floating_boxes(m_right_floats);
  421. box_state.offset = Gfx::FloatPoint { box_state.offset.x(), y };
  422. }
  423. void BlockFormattingContext::place_block_level_element_in_normal_flow_horizontally(Box const& child_box, BlockContainer const& containing_block)
  424. {
  425. auto& box_state = m_state.get_mutable(child_box);
  426. auto const& containing_block_state = m_state.get(containing_block);
  427. float x = 0;
  428. if (containing_block.computed_values().text_align() == CSS::TextAlign::LibwebCenter) {
  429. x = (containing_block_state.content_width / 2) - box_state.content_width / 2;
  430. } else {
  431. x = box_state.margin_box_left() + box_state.offset_left;
  432. }
  433. box_state.offset = Gfx::FloatPoint { x, box_state.offset.y() };
  434. }
  435. void BlockFormattingContext::layout_initial_containing_block(LayoutMode layout_mode)
  436. {
  437. auto viewport_rect = root().browsing_context().viewport_rect();
  438. auto& icb = verify_cast<Layout::InitialContainingBlock>(root());
  439. auto& icb_state = m_state.get_mutable(icb);
  440. VERIFY(!icb.children_are_inline());
  441. layout_block_level_children(root(), layout_mode);
  442. // Compute scrollable overflow.
  443. float bottom_edge = 0;
  444. float right_edge = 0;
  445. icb.for_each_in_subtree_of_type<Box>([&](Box const& child) {
  446. auto const& child_state = m_state.get(child);
  447. auto child_rect = absolute_content_rect(child, m_state);
  448. child_rect.inflate(child_state.border_box_top(), child_state.border_box_right(), child_state.border_box_bottom(), child_state.border_box_left());
  449. bottom_edge = max(bottom_edge, child_rect.bottom());
  450. right_edge = max(right_edge, child_rect.right());
  451. return IterationDecision::Continue;
  452. });
  453. if (bottom_edge >= viewport_rect.height() || right_edge >= viewport_rect.width()) {
  454. // FIXME: Move overflow data to FormattingState!
  455. auto& overflow_data = icb_state.ensure_overflow_data();
  456. overflow_data.scrollable_overflow_rect = viewport_rect.to_type<float>();
  457. // NOTE: The edges are *within* the rectangle, so we add 1 to get the width and height.
  458. overflow_data.scrollable_overflow_rect.set_size(right_edge + 1, bottom_edge + 1);
  459. }
  460. }
  461. void BlockFormattingContext::layout_floating_child(Box const& box, BlockContainer const& containing_block)
  462. {
  463. VERIFY(box.is_floating());
  464. auto& box_state = m_state.get_mutable(box);
  465. auto containing_block_content_width = m_state.get(containing_block).content_width;
  466. compute_width(box);
  467. (void)layout_inside(box, LayoutMode::Default);
  468. compute_height(box, m_state);
  469. // First we place the box normally (to get the right y coordinate.)
  470. place_block_level_element_in_normal_flow_vertically(box, containing_block);
  471. place_block_level_element_in_normal_flow_horizontally(box, containing_block);
  472. auto float_box = [&](FloatSide side, FloatSideData& side_data) {
  473. auto first_edge = [&](FormattingState::NodeState const& thing) { return side == FloatSide::Left ? thing.margin_left : thing.margin_right; };
  474. auto second_edge = [&](FormattingState::NodeState const& thing) { return side == FloatSide::Right ? thing.margin_left : thing.margin_right; };
  475. auto edge_of_containing_block = [&] {
  476. if (side == FloatSide::Left)
  477. return box_state.margin_box_left();
  478. return containing_block_content_width - box_state.margin_box_right() - box_state.content_width;
  479. };
  480. // Then we float it to the left or right.
  481. auto box_in_root_rect = margin_box_rect_in_ancestor_coordinate_space(box, root(), m_state);
  482. float y_in_root = box_in_root_rect.y();
  483. float x = 0;
  484. float y = box_state.offset.y();
  485. if (side_data.boxes.is_empty()) {
  486. // This is the first floating box on this side. Go all the way to the edge.
  487. x = edge_of_containing_block();
  488. side_data.y_offset = 0;
  489. } else {
  490. auto& previous_box = side_data.boxes.last();
  491. auto const& previous_box_state = m_state.get(previous_box);
  492. auto previous_rect = margin_box_rect_in_ancestor_coordinate_space(previous_box, root(), m_state);
  493. auto margin_collapsed_with_previous = max(
  494. second_edge(previous_box_state),
  495. first_edge(box_state));
  496. float wanted_x = 0;
  497. bool fits_on_line = false;
  498. if (side == FloatSide::Left) {
  499. auto previous_right_border_edge = previous_box_state.offset.x()
  500. + previous_box_state.content_width
  501. + previous_box_state.padding_right
  502. + previous_box_state.border_right
  503. + margin_collapsed_with_previous;
  504. wanted_x = previous_right_border_edge + box_state.border_left + box_state.padding_left;
  505. fits_on_line = (wanted_x + box_state.content_width + box_state.padding_right + box_state.border_right + box_state.margin_right) <= containing_block_content_width;
  506. } else {
  507. auto previous_left_border_edge = previous_box_state.offset.x()
  508. - previous_box_state.padding_left
  509. - previous_box_state.border_left
  510. - margin_collapsed_with_previous;
  511. wanted_x = previous_left_border_edge - box_state.border_right - box_state.padding_right - box_state.content_width;
  512. fits_on_line = (wanted_x - box_state.padding_left - box_state.border_left - box_state.margin_left) >= 0;
  513. }
  514. if (fits_on_line) {
  515. if (previous_rect.contains_vertically(y_in_root + side_data.y_offset)) {
  516. // This box touches another already floating box. Stack after others.
  517. x = wanted_x;
  518. } else {
  519. // This box does not touch another floating box, go all the way to the edge.
  520. x = edge_of_containing_block();
  521. // Also, forget all previous boxes floated to this side while since they're no longer relevant.
  522. side_data.boxes.clear();
  523. }
  524. } else {
  525. // We ran out of horizontal space on this "float line", and need to break.
  526. x = edge_of_containing_block();
  527. float lowest_border_edge = 0;
  528. for (auto const& box : side_data.boxes) {
  529. auto const& box_state = m_state.get(box);
  530. lowest_border_edge = max(lowest_border_edge, box_state.border_box_height());
  531. }
  532. side_data.y_offset += lowest_border_edge;
  533. // Also, forget all previous boxes floated to this side while since they're no longer relevant.
  534. side_data.boxes.clear();
  535. }
  536. }
  537. y += side_data.y_offset;
  538. side_data.boxes.append(box);
  539. box_state.offset = Gfx::FloatPoint { x, y };
  540. };
  541. // Next, float to the left and/or right
  542. if (box.computed_values().float_() == CSS::Float::Left) {
  543. float_box(FloatSide::Left, m_left_floats);
  544. } else if (box.computed_values().float_() == CSS::Float::Right) {
  545. float_box(FloatSide::Right, m_right_floats);
  546. }
  547. }
  548. void BlockFormattingContext::layout_list_item_marker(ListItemBox const& list_item_box)
  549. {
  550. if (!list_item_box.marker())
  551. return;
  552. auto& marker = *list_item_box.marker();
  553. auto& marker_state = m_state.get_mutable(marker);
  554. auto& list_item_state = m_state.get_mutable(list_item_box);
  555. int image_width = 0;
  556. int image_height = 0;
  557. if (auto const* list_style_image = marker.list_style_image_bitmap()) {
  558. image_width = list_style_image->rect().width();
  559. image_height = list_style_image->rect().height();
  560. }
  561. if (marker.text().is_empty()) {
  562. marker_state.content_width = image_width + 4;
  563. } else {
  564. auto text_width = marker.font().width(marker.text());
  565. marker_state.content_width = image_width + text_width;
  566. }
  567. marker_state.content_height = max(image_height, marker.line_height());
  568. marker_state.offset = { -(marker_state.content_width + 4), 0 };
  569. if (marker_state.content_height > list_item_state.content_height)
  570. list_item_state.content_height = marker_state.content_height;
  571. }
  572. }