BlockFormattingContext.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/CSS/Length.h>
  27. #include <LibWeb/DOM/Node.h>
  28. #include <LibWeb/Layout/BlockFormattingContext.h>
  29. #include <LibWeb/Layout/InlineFormattingContext.h>
  30. #include <LibWeb/Layout/LayoutBlock.h>
  31. #include <LibWeb/Layout/LayoutBox.h>
  32. #include <LibWeb/Layout/LayoutDocument.h>
  33. #include <LibWeb/Layout/LayoutListItem.h>
  34. #include <LibWeb/Layout/LayoutWidget.h>
  35. #include <LibWeb/Page/Frame.h>
  36. namespace Web::Layout {
  37. BlockFormattingContext::BlockFormattingContext(LayoutBox& context_box)
  38. : FormattingContext(context_box)
  39. {
  40. }
  41. BlockFormattingContext::~BlockFormattingContext()
  42. {
  43. }
  44. bool BlockFormattingContext::is_initial() const
  45. {
  46. return context_box().is_root();
  47. }
  48. void BlockFormattingContext::run(LayoutMode layout_mode)
  49. {
  50. if (is_initial()) {
  51. layout_initial_containing_block(layout_mode);
  52. return;
  53. }
  54. // FIXME: BFC currently computes the width+height of the context box.
  55. // This is necessary to be able to place absolutely positioned descendants.
  56. // The same work is also done by the parent BFC for each of its blocks..
  57. if (layout_mode == LayoutMode::Default)
  58. compute_width(context_box());
  59. if (context_box().children_are_inline()) {
  60. layout_inline_children(layout_mode);
  61. } else {
  62. layout_block_level_children(layout_mode);
  63. }
  64. if (layout_mode == LayoutMode::Default)
  65. compute_height(context_box());
  66. // No need to layout absolute positioned boxes during shrink-to-fit layouts.
  67. if (layout_mode == LayoutMode::Default)
  68. layout_absolutely_positioned_descendants();
  69. }
  70. void BlockFormattingContext::compute_width(LayoutBox& box)
  71. {
  72. if (box.is_replaced()) {
  73. // FIXME: This should not be done *by* LayoutReplaced
  74. auto& replaced = downcast<LayoutReplaced>(box);
  75. replaced.prepare_for_replaced_layout();
  76. auto width = replaced.calculate_width();
  77. replaced.set_width(width);
  78. return;
  79. }
  80. if (box.is_absolutely_positioned()) {
  81. compute_width_for_absolutely_positioned_block(box);
  82. return;
  83. }
  84. auto& style = box.style();
  85. float width_of_containing_block = box.width_of_logical_containing_block();
  86. auto zero_value = CSS::Length::make_px(0);
  87. auto margin_left = CSS::Length::make_auto();
  88. auto margin_right = CSS::Length::make_auto();
  89. const auto padding_left = style.padding().left.resolved_or_zero(box, width_of_containing_block);
  90. const auto padding_right = style.padding().right.resolved_or_zero(box, width_of_containing_block);
  91. auto try_compute_width = [&](const auto& a_width) {
  92. CSS::Length width = a_width;
  93. margin_left = style.margin().left.resolved_or_zero(box, width_of_containing_block);
  94. margin_right = style.margin().right.resolved_or_zero(box, width_of_containing_block);
  95. float total_px = style.border_left().width + style.border_right().width;
  96. for (auto& value : { margin_left, padding_left, width, padding_right, margin_right }) {
  97. total_px += value.to_px(box);
  98. }
  99. if (!box.is_replaced() && !box.is_inline()) {
  100. // 10.3.3 Block-level, non-replaced elements in normal flow
  101. // 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.
  102. if (width.is_auto() && total_px > width_of_containing_block) {
  103. if (margin_left.is_auto())
  104. margin_left = zero_value;
  105. if (margin_right.is_auto())
  106. margin_right = zero_value;
  107. }
  108. // 10.3.3 cont'd.
  109. auto underflow_px = width_of_containing_block - total_px;
  110. if (width.is_auto()) {
  111. if (margin_left.is_auto())
  112. margin_left = zero_value;
  113. if (margin_right.is_auto())
  114. margin_right = zero_value;
  115. if (underflow_px >= 0) {
  116. width = CSS::Length(underflow_px, CSS::Length::Type::Px);
  117. } else {
  118. width = zero_value;
  119. margin_right = CSS::Length(margin_right.to_px(box) + underflow_px, CSS::Length::Type::Px);
  120. }
  121. } else {
  122. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  123. margin_right = CSS::Length(margin_right.to_px(box) + underflow_px, CSS::Length::Type::Px);
  124. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  125. margin_right = CSS::Length(underflow_px, CSS::Length::Type::Px);
  126. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  127. margin_left = CSS::Length(underflow_px, CSS::Length::Type::Px);
  128. } else { // margin_left.is_auto() && margin_right.is_auto()
  129. auto half_of_the_underflow = CSS::Length(underflow_px / 2, CSS::Length::Type::Px);
  130. margin_left = half_of_the_underflow;
  131. margin_right = half_of_the_underflow;
  132. }
  133. }
  134. } else if (!box.is_replaced() && box.is_inline_block()) {
  135. // 10.3.9 'Inline-block', non-replaced elements in normal flow
  136. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  137. if (margin_left.is_auto())
  138. margin_left = zero_value;
  139. if (margin_right.is_auto())
  140. margin_right = zero_value;
  141. // If 'width' is 'auto', the used value is the shrink-to-fit width as for floating elements.
  142. if (width.is_auto()) {
  143. // Find the available width: in this case, this is the width of the containing
  144. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  145. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  146. float available_width = width_of_containing_block
  147. - margin_left.to_px(box) - style.border_left().width - padding_left.to_px(box)
  148. - padding_right.to_px(box) - style.border_right().width - margin_right.to_px(box);
  149. auto result = calculate_shrink_to_fit_widths(box);
  150. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  151. width = CSS::Length(min(max(result.preferred_minimum_width, available_width), result.preferred_width), CSS::Length::Type::Px);
  152. }
  153. }
  154. return width;
  155. };
  156. auto specified_width = style.width().resolved_or_auto(box, width_of_containing_block);
  157. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  158. auto used_width = try_compute_width(specified_width);
  159. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  160. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  161. auto specified_max_width = style.max_width().resolved_or_auto(box, width_of_containing_block);
  162. if (!specified_max_width.is_auto()) {
  163. if (used_width.to_px(box) > specified_max_width.to_px(box)) {
  164. used_width = try_compute_width(specified_max_width);
  165. }
  166. }
  167. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  168. // but this time using the value of 'min-width' as the computed value for 'width'.
  169. auto specified_min_width = style.min_width().resolved_or_auto(box, width_of_containing_block);
  170. if (!specified_min_width.is_auto()) {
  171. if (used_width.to_px(box) < specified_min_width.to_px(box)) {
  172. used_width = try_compute_width(specified_min_width);
  173. }
  174. }
  175. box.set_width(used_width.to_px(box));
  176. box.box_model().margin.left = margin_left;
  177. box.box_model().margin.right = margin_right;
  178. box.box_model().border.left = CSS::Length::make_px(style.border_left().width);
  179. box.box_model().border.right = CSS::Length::make_px(style.border_right().width);
  180. box.box_model().padding.left = padding_left;
  181. box.box_model().padding.right = padding_right;
  182. }
  183. void BlockFormattingContext::compute_width_for_absolutely_positioned_block(LayoutBox& box)
  184. {
  185. auto& containing_block = context_box();
  186. auto& style = box.style();
  187. auto zero_value = CSS::Length::make_px(0);
  188. auto margin_left = CSS::Length::make_auto();
  189. auto margin_right = CSS::Length::make_auto();
  190. const auto border_left = style.border_left().width;
  191. const auto border_right = style.border_right().width;
  192. const auto padding_left = style.padding().left.resolved(zero_value, box, containing_block.width());
  193. const auto padding_right = style.padding().right.resolved(zero_value, box, containing_block.width());
  194. auto try_compute_width = [&](const auto& a_width) {
  195. margin_left = style.margin().left.resolved(zero_value, box, containing_block.width());
  196. margin_right = style.margin().right.resolved(zero_value, box, containing_block.width());
  197. auto left = style.offset().left.resolved_or_auto(box, containing_block.width());
  198. auto right = style.offset().right.resolved_or_auto(box, containing_block.width());
  199. auto width = a_width;
  200. auto solve_for_left = [&] {
  201. return CSS::Length(containing_block.width() - margin_left.to_px(box) - border_left - padding_left.to_px(box) - width.to_px(box) - padding_right.to_px(box) - border_right - margin_right.to_px(box) - right.to_px(box), CSS::Length::Type::Px);
  202. };
  203. auto solve_for_width = [&] {
  204. return CSS::Length(containing_block.width() - left.to_px(box) - margin_left.to_px(box) - border_left - padding_left.to_px(box) - padding_right.to_px(box) - border_right - margin_right.to_px(box) - right.to_px(box), CSS::Length::Type::Px);
  205. };
  206. auto solve_for_right = [&] {
  207. return CSS::Length(containing_block.width() - left.to_px(box) - margin_left.to_px(box) - border_left - padding_left.to_px(box) - width.to_px(box) - padding_right.to_px(box) - border_right - margin_right.to_px(box), CSS::Length::Type::Px);
  208. };
  209. // If all three of 'left', 'width', and 'right' are 'auto':
  210. if (left.is_auto() && width.is_auto() && right.is_auto()) {
  211. // First set any 'auto' values for 'margin-left' and 'margin-right' to 0.
  212. if (margin_left.is_auto())
  213. margin_left = CSS::Length::make_px(0);
  214. if (margin_right.is_auto())
  215. margin_right = CSS::Length::make_px(0);
  216. // Then, if the 'direction' property of the element establishing the static-position containing block
  217. // is 'ltr' set 'left' to the static position and apply rule number three below;
  218. // otherwise, set 'right' to the static position and apply rule number one below.
  219. // FIXME: This is very hackish.
  220. left = CSS::Length::make_px(0);
  221. goto Rule3;
  222. }
  223. if (!left.is_auto() && !width.is_auto() && !right.is_auto()) {
  224. // FIXME: This should be solved in a more complicated way.
  225. return width;
  226. }
  227. if (margin_left.is_auto())
  228. margin_left = CSS::Length::make_px(0);
  229. if (margin_right.is_auto())
  230. margin_right = CSS::Length::make_px(0);
  231. // 1. 'left' and 'width' are 'auto' and 'right' is not 'auto',
  232. // then the width is shrink-to-fit. Then solve for 'left'
  233. if (left.is_auto() && width.is_auto() && !right.is_auto()) {
  234. auto result = calculate_shrink_to_fit_widths(box);
  235. solve_for_left();
  236. auto available_width = solve_for_width();
  237. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  238. }
  239. // 2. 'left' and 'right' are 'auto' and 'width' is not 'auto',
  240. // then if the 'direction' property of the element establishing
  241. // the static-position containing block is 'ltr' set 'left'
  242. // to the static position, otherwise set 'right' to the static position.
  243. // Then solve for 'left' (if 'direction is 'rtl') or 'right' (if 'direction' is 'ltr').
  244. else if (left.is_auto() && right.is_auto() && !width.is_auto()) {
  245. // FIXME: Check direction
  246. // FIXME: Use the static-position containing block
  247. left = zero_value;
  248. right = solve_for_right();
  249. }
  250. // 3. 'width' and 'right' are 'auto' and 'left' is not 'auto',
  251. // then the width is shrink-to-fit. Then solve for 'right'
  252. else if (width.is_auto() && right.is_auto() && !left.is_auto()) {
  253. Rule3:
  254. auto result = calculate_shrink_to_fit_widths(box);
  255. right = solve_for_right();
  256. auto available_width = solve_for_width();
  257. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  258. }
  259. // 4. 'left' is 'auto', 'width' and 'right' are not 'auto', then solve for 'left'
  260. else if (left.is_auto() && !width.is_auto() && !right.is_auto()) {
  261. left = solve_for_left();
  262. }
  263. // 5. 'width' is 'auto', 'left' and 'right' are not 'auto', then solve for 'width'
  264. else if (width.is_auto() && !left.is_auto() && !right.is_auto()) {
  265. width = solve_for_width();
  266. }
  267. // 6. 'right' is 'auto', 'left' and 'width' are not 'auto', then solve for 'right'
  268. else if (right.is_auto() && !left.is_auto() && !width.is_auto()) {
  269. right = solve_for_right();
  270. }
  271. return width;
  272. };
  273. auto specified_width = style.width().resolved_or_auto(box, containing_block.width());
  274. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  275. auto used_width = try_compute_width(specified_width);
  276. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  277. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  278. auto specified_max_width = style.max_width().resolved_or_auto(box, containing_block.width());
  279. if (!specified_max_width.is_auto()) {
  280. if (used_width.to_px(box) > specified_max_width.to_px(box)) {
  281. used_width = try_compute_width(specified_max_width);
  282. }
  283. }
  284. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  285. // but this time using the value of 'min-width' as the computed value for 'width'.
  286. auto specified_min_width = style.min_width().resolved_or_auto(box, containing_block.width());
  287. if (!specified_min_width.is_auto()) {
  288. if (used_width.to_px(box) < specified_min_width.to_px(box)) {
  289. used_width = try_compute_width(specified_min_width);
  290. }
  291. }
  292. box.set_width(used_width.to_px(box));
  293. box.box_model().margin.left = margin_left;
  294. box.box_model().margin.right = margin_right;
  295. box.box_model().border.left = CSS::Length::make_px(border_left);
  296. box.box_model().border.right = CSS::Length::make_px(border_right);
  297. box.box_model().padding.left = padding_left;
  298. box.box_model().padding.right = padding_right;
  299. }
  300. void BlockFormattingContext::compute_height(LayoutBox& box)
  301. {
  302. if (box.is_replaced()) {
  303. // FIXME: This should not be done *by* LayoutReplaced
  304. auto height = downcast<LayoutReplaced>(box).calculate_height();
  305. box.set_height(height);
  306. return;
  307. }
  308. auto& style = box.style();
  309. auto& containing_block = context_box();
  310. CSS::Length specified_height;
  311. if (style.height().is_percentage() && !containing_block.style().height().is_absolute()) {
  312. specified_height = CSS::Length::make_auto();
  313. } else {
  314. specified_height = style.height().resolved_or_auto(box, containing_block.height());
  315. }
  316. auto specified_max_height = style.max_height().resolved_or_auto(box, containing_block.height());
  317. box.box_model().margin.top = style.margin().top.resolved_or_zero(box, containing_block.width());
  318. box.box_model().margin.bottom = style.margin().bottom.resolved_or_zero(box, containing_block.width());
  319. box.box_model().border.top = CSS::Length::make_px(style.border_top().width);
  320. box.box_model().border.bottom = CSS::Length::make_px(style.border_bottom().width);
  321. box.box_model().padding.top = style.padding().top.resolved_or_zero(box, containing_block.width());
  322. box.box_model().padding.bottom = style.padding().bottom.resolved_or_zero(box, containing_block.width());
  323. if (!specified_height.is_auto()) {
  324. float used_height = specified_height.to_px(box);
  325. if (!specified_max_height.is_auto())
  326. used_height = min(used_height, specified_max_height.to_px(box));
  327. box.set_height(used_height);
  328. }
  329. }
  330. void BlockFormattingContext::layout_inline_children(LayoutMode layout_mode)
  331. {
  332. InlineFormattingContext context(context_box());
  333. context.run(layout_mode);
  334. }
  335. void BlockFormattingContext::layout_block_level_children(LayoutMode layout_mode)
  336. {
  337. float content_height = 0;
  338. float content_width = 0;
  339. context_box().for_each_in_subtree_of_type<LayoutBox>([&](auto& box) {
  340. if (box.is_absolutely_positioned() || box.containing_block() != &context_box())
  341. return IterationDecision::Continue;
  342. compute_width(box);
  343. layout_inside(box, layout_mode);
  344. compute_height(box);
  345. if (box.is_replaced())
  346. place_block_level_replaced_element_in_normal_flow(box);
  347. else if (box.is_block())
  348. place_block_level_non_replaced_element_in_normal_flow(box);
  349. else
  350. dbgln("FIXME: LayoutBlock::layout_contained_boxes doesn't know how to place a {}", box.class_name());
  351. // FIXME: This should be factored differently. It's uncool that we mutate the tree *during* layout!
  352. // Instead, we should generate the marker box during the tree build.
  353. if (is<LayoutListItem>(box))
  354. downcast<LayoutListItem>(box).layout_marker();
  355. content_height = max(content_height, box.effective_offset().y() + box.height() + box.box_model().margin_box(box).bottom);
  356. content_width = max(content_width, downcast<LayoutBox>(box).width());
  357. return IterationDecision::Continue;
  358. });
  359. if (layout_mode != LayoutMode::Default) {
  360. if (context_box().style().width().is_undefined() || context_box().style().width().is_auto())
  361. context_box().set_width(content_width);
  362. }
  363. // FIXME: It's not right to always shrink-wrap the context box to the content here.
  364. context_box().set_height(content_height);
  365. }
  366. void BlockFormattingContext::place_block_level_replaced_element_in_normal_flow(LayoutBox& box)
  367. {
  368. auto& containing_block = context_box();
  369. ASSERT(!containing_block.is_absolutely_positioned());
  370. auto& replaced_element_box_model = box.box_model();
  371. replaced_element_box_model.margin.top = box.style().margin().top.resolved_or_zero(context_box(), containing_block.width());
  372. replaced_element_box_model.margin.bottom = box.style().margin().bottom.resolved_or_zero(context_box(), containing_block.width());
  373. replaced_element_box_model.border.top = CSS::Length::make_px(box.style().border_top().width);
  374. replaced_element_box_model.border.bottom = CSS::Length::make_px(box.style().border_bottom().width);
  375. replaced_element_box_model.padding.top = box.style().padding().top.resolved_or_zero(context_box(), containing_block.width());
  376. replaced_element_box_model.padding.bottom = box.style().padding().bottom.resolved_or_zero(context_box(), containing_block.width());
  377. float x = replaced_element_box_model.margin.left.to_px(context_box())
  378. + replaced_element_box_model.border.left.to_px(context_box())
  379. + replaced_element_box_model.padding.left.to_px(context_box())
  380. + replaced_element_box_model.offset.left.to_px(context_box());
  381. float y = replaced_element_box_model.margin_box(context_box()).top + context_box().box_model().offset.top.to_px(context_box());
  382. box.set_offset(x, y);
  383. }
  384. void BlockFormattingContext::place_block_level_non_replaced_element_in_normal_flow(LayoutBox& box)
  385. {
  386. auto zero_value = CSS::Length::make_px(0);
  387. auto& containing_block = context_box();
  388. auto& box_model = box.box_model();
  389. auto& style = box.style();
  390. box_model.margin.top = style.margin().top.resolved(zero_value, containing_block, containing_block.width());
  391. box_model.margin.bottom = style.margin().bottom.resolved(zero_value, containing_block, containing_block.width());
  392. box_model.border.top = CSS::Length::make_px(style.border_top().width);
  393. box_model.border.bottom = CSS::Length::make_px(style.border_bottom().width);
  394. box_model.padding.top = style.padding().top.resolved(zero_value, containing_block, containing_block.width());
  395. box_model.padding.bottom = style.padding().bottom.resolved(zero_value, containing_block, containing_block.width());
  396. float x = box_model.margin.left.to_px(containing_block)
  397. + box_model.border.left.to_px(containing_block)
  398. + box_model.padding.left.to_px(containing_block)
  399. + box_model.offset.left.to_px(containing_block);
  400. if (containing_block.style().text_align() == CSS::TextAlign::VendorSpecificCenter) {
  401. x = (containing_block.width() / 2) - box.width() / 2;
  402. }
  403. float y = box_model.margin_box(containing_block).top
  404. + box_model.offset.top.to_px(containing_block);
  405. // NOTE: Empty (0-height) preceding siblings have their margins collapsed with *their* preceding sibling, etc.
  406. float collapsed_bottom_margin_of_preceding_siblings = 0;
  407. auto* relevant_sibling = box.previous_sibling_of_type<LayoutBlock>();
  408. while (relevant_sibling != nullptr) {
  409. if (!relevant_sibling->is_absolutely_positioned() && !relevant_sibling->is_floating()) {
  410. collapsed_bottom_margin_of_preceding_siblings = max(collapsed_bottom_margin_of_preceding_siblings, relevant_sibling->box_model().margin.bottom.to_px(*relevant_sibling));
  411. if (relevant_sibling->height() > 0)
  412. break;
  413. }
  414. relevant_sibling = relevant_sibling->previous_sibling();
  415. }
  416. if (relevant_sibling) {
  417. y += relevant_sibling->effective_offset().y() + relevant_sibling->height() + relevant_sibling->box_model().padding.bottom.to_px(*relevant_sibling);
  418. // Collapse top margin with bottom margin of preceding siblings if needed
  419. float my_margin_top = box_model.margin.top.to_px(containing_block);
  420. if (my_margin_top < 0 || collapsed_bottom_margin_of_preceding_siblings < 0) {
  421. // Negative margins present.
  422. float largest_negative_margin = -min(my_margin_top, collapsed_bottom_margin_of_preceding_siblings);
  423. 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);
  424. float final_margin = largest_positive_margin - largest_negative_margin;
  425. y += final_margin - my_margin_top;
  426. } else if (collapsed_bottom_margin_of_preceding_siblings > my_margin_top) {
  427. // Sibling's margin is larger than mine, adjust so we use sibling's.
  428. y += collapsed_bottom_margin_of_preceding_siblings - my_margin_top;
  429. }
  430. }
  431. box.set_offset(x, y);
  432. }
  433. void BlockFormattingContext::layout_initial_containing_block(LayoutMode layout_mode)
  434. {
  435. auto viewport_rect = context_box().frame().viewport_rect();
  436. auto& icb = downcast<LayoutDocument>(context_box());
  437. icb.build_stacking_context_tree();
  438. icb.set_width(viewport_rect.width());
  439. layout_block_level_children(layout_mode);
  440. ASSERT(!icb.children_are_inline());
  441. // FIXME: The ICB should have the height of the viewport.
  442. // Instead of auto-sizing the ICB, we should spill into overflow.
  443. float lowest_bottom = 0;
  444. icb.for_each_child_of_type<LayoutBox>([&](auto& child) {
  445. lowest_bottom = max(lowest_bottom, child.absolute_rect().bottom());
  446. });
  447. icb.set_height(lowest_bottom);
  448. // No need to layout absolute positioned boxes during shrink-to-fit layouts.
  449. if (layout_mode == LayoutMode::Default)
  450. layout_absolutely_positioned_descendants();
  451. // FIXME: This is a total hack. Make sure any GUI::Widgets are moved into place after layout.
  452. // We should stop embedding GUI::Widgets entirely, since that won't work out-of-process.
  453. icb.for_each_in_subtree_of_type<LayoutWidget>([&](auto& widget) {
  454. widget.update_widget();
  455. return IterationDecision::Continue;
  456. });
  457. }
  458. void BlockFormattingContext::layout_absolutely_positioned_descendants()
  459. {
  460. context_box().for_each_in_subtree_of_type<LayoutBox>([&](auto& box) {
  461. if (box.is_absolutely_positioned() && box.containing_block() == &context_box()) {
  462. layout_absolutely_positioned_descendant(box);
  463. }
  464. return IterationDecision::Continue;
  465. });
  466. }
  467. void BlockFormattingContext::layout_absolutely_positioned_descendant(LayoutBox& box)
  468. {
  469. auto& containing_block = context_box();
  470. auto& box_model = box.box_model();
  471. auto zero_value = CSS::Length::make_px(0);
  472. auto specified_width = box.style().width().resolved_or_auto(box, containing_block.width());
  473. compute_width(box);
  474. layout_inside(box, LayoutMode::Default);
  475. compute_height(box);
  476. box_model.margin.left = box.style().margin().left.resolved_or_auto(box, containing_block.width());
  477. box_model.margin.top = box.style().margin().top.resolved_or_auto(box, containing_block.height());
  478. box_model.margin.right = box.style().margin().right.resolved_or_auto(box, containing_block.width());
  479. box_model.margin.bottom = box.style().margin().bottom.resolved_or_auto(box, containing_block.height());
  480. box_model.border.left = CSS::Length::make_px(box.style().border_left().width);
  481. box_model.border.right = CSS::Length::make_px(box.style().border_right().width);
  482. box_model.border.top = CSS::Length::make_px(box.style().border_top().width);
  483. box_model.border.bottom = CSS::Length::make_px(box.style().border_bottom().width);
  484. box_model.offset.left = box.style().offset().left.resolved_or_auto(box, containing_block.width());
  485. box_model.offset.top = box.style().offset().top.resolved_or_auto(box, containing_block.height());
  486. box_model.offset.right = box.style().offset().right.resolved_or_auto(box, containing_block.width());
  487. box_model.offset.bottom = box.style().offset().bottom.resolved_or_auto(box, containing_block.height());
  488. if (box_model.offset.left.is_auto() && specified_width.is_auto() && box_model.offset.right.is_auto()) {
  489. if (box_model.margin.left.is_auto())
  490. box_model.margin.left = zero_value;
  491. if (box_model.margin.right.is_auto())
  492. box_model.margin.right = zero_value;
  493. }
  494. Gfx::FloatPoint used_offset;
  495. if (!box_model.offset.left.is_auto()) {
  496. float x_offset = box_model.offset.left.to_px(box)
  497. + box_model.border_box(box).left;
  498. used_offset.set_x(x_offset + box_model.margin.left.to_px(box));
  499. } else if (!box_model.offset.right.is_auto()) {
  500. float x_offset = 0
  501. - box_model.offset.right.to_px(box)
  502. - box_model.border_box(box).right;
  503. used_offset.set_x(containing_block.width() + x_offset - box.width() - box_model.margin.right.to_px(box));
  504. } else {
  505. float x_offset = box_model.margin_box(box).left;
  506. used_offset.set_x(x_offset);
  507. }
  508. if (!box_model.offset.top.is_auto()) {
  509. float y_offset = box_model.offset.top.to_px(box)
  510. + box_model.border_box(box).top;
  511. used_offset.set_y(y_offset + box_model.margin.top.to_px(box));
  512. } else if (!box_model.offset.bottom.is_auto()) {
  513. float y_offset = 0
  514. - box_model.offset.bottom.to_px(box)
  515. - box_model.border_box(box).bottom;
  516. used_offset.set_y(containing_block.height() + y_offset - box.height() - box_model.margin.bottom.to_px(box));
  517. } else {
  518. float y_offset = box_model.margin_box(box).top;
  519. used_offset.set_y(y_offset);
  520. }
  521. box.set_offset(used_offset);
  522. }
  523. }