BlockFormattingContext.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/CSS/Length.h>
  27. #include <LibWeb/DOM/Node.h>
  28. #include <LibWeb/Layout/BlockBox.h>
  29. #include <LibWeb/Layout/BlockFormattingContext.h>
  30. #include <LibWeb/Layout/Box.h>
  31. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  32. #include <LibWeb/Layout/InlineFormattingContext.h>
  33. #include <LibWeb/Layout/ListItemBox.h>
  34. #include <LibWeb/Layout/WidgetBox.h>
  35. #include <LibWeb/Page/Frame.h>
  36. namespace Web::Layout {
  37. BlockFormattingContext::BlockFormattingContext(Box& context_box, FormattingContext* parent)
  38. : FormattingContext(context_box, parent)
  39. {
  40. }
  41. BlockFormattingContext::~BlockFormattingContext()
  42. {
  43. }
  44. bool BlockFormattingContext::is_initial() const
  45. {
  46. return context_box().is_initial_containing_block();
  47. }
  48. void BlockFormattingContext::run(Box& box, LayoutMode layout_mode)
  49. {
  50. if (is_initial()) {
  51. layout_initial_containing_block(layout_mode);
  52. return;
  53. }
  54. // FIXME: BFC currently computes the width+height of the target box.
  55. // This is necessary to be able to place absolutely positioned descendants.
  56. // The same work is also done by the parent BFC for each of its blocks..
  57. if (layout_mode == LayoutMode::Default)
  58. compute_width(box);
  59. if (box.children_are_inline()) {
  60. layout_inline_children(box, layout_mode);
  61. } else {
  62. layout_block_level_children(box, layout_mode);
  63. }
  64. if (layout_mode == LayoutMode::Default)
  65. compute_height(box);
  66. }
  67. void BlockFormattingContext::compute_width(Box& box)
  68. {
  69. if (box.is_replaced()) {
  70. // FIXME: This should not be done *by* ReplacedBox
  71. auto& replaced = downcast<ReplacedBox>(box);
  72. replaced.prepare_for_replaced_layout();
  73. compute_width_for_block_level_replaced_element_in_normal_flow(replaced);
  74. return;
  75. }
  76. if (box.is_absolutely_positioned()) {
  77. compute_width_for_absolutely_positioned_block(box);
  78. return;
  79. }
  80. if (box.is_floating()) {
  81. compute_width_for_floating_box(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_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_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.to_px(box);
  177. box.box_model().margin.right = margin_right.to_px(box);
  178. box.box_model().border.left = style.border_left().width;
  179. box.box_model().border.right = style.border_right().width;
  180. box.box_model().padding.left = padding_left.to_px(box);
  181. box.box_model().padding.right = padding_right.to_px(box);
  182. }
  183. void BlockFormattingContext::compute_width_for_floating_box(Box& box)
  184. {
  185. // 10.3.5 Floating, non-replaced elements
  186. auto& style = box.style();
  187. float width_of_containing_block = box.width_of_logical_containing_block();
  188. auto zero_value = CSS::Length::make_px(0);
  189. auto margin_left = CSS::Length::make_auto();
  190. auto margin_right = CSS::Length::make_auto();
  191. const auto padding_left = style.padding().left.resolved_or_zero(box, width_of_containing_block);
  192. const auto padding_right = style.padding().right.resolved_or_zero(box, width_of_containing_block);
  193. // If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
  194. if (margin_left.is_auto())
  195. margin_left = zero_value;
  196. if (margin_right.is_auto())
  197. margin_right = zero_value;
  198. auto width = style.width().resolved_or_auto(box, width_of_containing_block);
  199. // If 'width' is computed as 'auto', the used value is the "shrink-to-fit" width.
  200. if (width.is_auto()) {
  201. // Find the available width: in this case, this is the width of the containing
  202. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  203. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  204. float available_width = width_of_containing_block
  205. - margin_left.to_px(box) - style.border_left().width - padding_left.to_px(box)
  206. - padding_right.to_px(box) - style.border_right().width - margin_right.to_px(box);
  207. auto result = calculate_shrink_to_fit_widths(box);
  208. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  209. width = CSS::Length(min(max(result.preferred_minimum_width, available_width), result.preferred_width), CSS::Length::Type::Px);
  210. }
  211. float final_width = width.resolved_or_zero(box, width_of_containing_block).to_px(box);
  212. box.set_width(final_width);
  213. }
  214. void BlockFormattingContext::compute_width_for_block_level_replaced_element_in_normal_flow(ReplacedBox& box)
  215. {
  216. box.set_width(compute_width_for_replaced_element(box));
  217. }
  218. void BlockFormattingContext::compute_height_for_block_level_replaced_element_in_normal_flow(ReplacedBox& box)
  219. {
  220. box.set_height(compute_height_for_replaced_element(box));
  221. }
  222. void BlockFormattingContext::compute_width_for_absolutely_positioned_block(Box& box)
  223. {
  224. auto& containing_block = context_box();
  225. auto& style = box.style();
  226. auto zero_value = CSS::Length::make_px(0);
  227. auto margin_left = CSS::Length::make_auto();
  228. auto margin_right = CSS::Length::make_auto();
  229. const auto border_left = style.border_left().width;
  230. const auto border_right = style.border_right().width;
  231. const auto padding_left = style.padding().left.resolved_or_zero(box, containing_block.width());
  232. const auto padding_right = style.padding().right.resolved_or_zero(box, containing_block.width());
  233. auto try_compute_width = [&](const auto& a_width) {
  234. margin_left = style.margin().left.resolved_or_zero(box, containing_block.width());
  235. margin_right = style.margin().right.resolved_or_zero(box, containing_block.width());
  236. auto left = style.offset().left.resolved_or_auto(box, containing_block.width());
  237. auto right = style.offset().right.resolved_or_auto(box, containing_block.width());
  238. auto width = a_width;
  239. auto solve_for_left = [&] {
  240. 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);
  241. };
  242. auto solve_for_width = [&] {
  243. 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);
  244. };
  245. auto solve_for_right = [&] {
  246. 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);
  247. };
  248. // If all three of 'left', 'width', and 'right' are 'auto':
  249. if (left.is_auto() && width.is_auto() && right.is_auto()) {
  250. // First set any 'auto' values for 'margin-left' and 'margin-right' to 0.
  251. if (margin_left.is_auto())
  252. margin_left = CSS::Length::make_px(0);
  253. if (margin_right.is_auto())
  254. margin_right = CSS::Length::make_px(0);
  255. // Then, if the 'direction' property of the element establishing the static-position containing block
  256. // is 'ltr' set 'left' to the static position and apply rule number three below;
  257. // otherwise, set 'right' to the static position and apply rule number one below.
  258. // FIXME: This is very hackish.
  259. left = CSS::Length::make_px(0);
  260. goto Rule3;
  261. }
  262. if (!left.is_auto() && !width.is_auto() && !right.is_auto()) {
  263. // FIXME: This should be solved in a more complicated way.
  264. return width;
  265. }
  266. if (margin_left.is_auto())
  267. margin_left = CSS::Length::make_px(0);
  268. if (margin_right.is_auto())
  269. margin_right = CSS::Length::make_px(0);
  270. // 1. 'left' and 'width' are 'auto' and 'right' is not 'auto',
  271. // then the width is shrink-to-fit. Then solve for 'left'
  272. if (left.is_auto() && width.is_auto() && !right.is_auto()) {
  273. auto result = calculate_shrink_to_fit_widths(box);
  274. solve_for_left();
  275. auto available_width = solve_for_width();
  276. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  277. }
  278. // 2. 'left' and 'right' are 'auto' and 'width' is not 'auto',
  279. // then if the 'direction' property of the element establishing
  280. // the static-position containing block is 'ltr' set 'left'
  281. // to the static position, otherwise set 'right' to the static position.
  282. // Then solve for 'left' (if 'direction is 'rtl') or 'right' (if 'direction' is 'ltr').
  283. else if (left.is_auto() && right.is_auto() && !width.is_auto()) {
  284. // FIXME: Check direction
  285. // FIXME: Use the static-position containing block
  286. left = zero_value;
  287. right = solve_for_right();
  288. }
  289. // 3. 'width' and 'right' are 'auto' and 'left' is not 'auto',
  290. // then the width is shrink-to-fit. Then solve for 'right'
  291. else if (width.is_auto() && right.is_auto() && !left.is_auto()) {
  292. Rule3:
  293. auto result = calculate_shrink_to_fit_widths(box);
  294. right = solve_for_right();
  295. auto available_width = solve_for_width();
  296. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  297. }
  298. // 4. 'left' is 'auto', 'width' and 'right' are not 'auto', then solve for 'left'
  299. else if (left.is_auto() && !width.is_auto() && !right.is_auto()) {
  300. left = solve_for_left();
  301. }
  302. // 5. 'width' is 'auto', 'left' and 'right' are not 'auto', then solve for 'width'
  303. else if (width.is_auto() && !left.is_auto() && !right.is_auto()) {
  304. width = solve_for_width();
  305. }
  306. // 6. 'right' is 'auto', 'left' and 'width' are not 'auto', then solve for 'right'
  307. else if (right.is_auto() && !left.is_auto() && !width.is_auto()) {
  308. right = solve_for_right();
  309. }
  310. return width;
  311. };
  312. auto specified_width = style.width().resolved_or_auto(box, containing_block.width());
  313. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  314. auto used_width = try_compute_width(specified_width);
  315. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  316. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  317. auto specified_max_width = style.max_width().resolved_or_auto(box, containing_block.width());
  318. if (!specified_max_width.is_auto()) {
  319. if (used_width.to_px(box) > specified_max_width.to_px(box)) {
  320. used_width = try_compute_width(specified_max_width);
  321. }
  322. }
  323. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  324. // but this time using the value of 'min-width' as the computed value for 'width'.
  325. auto specified_min_width = style.min_width().resolved_or_auto(box, containing_block.width());
  326. if (!specified_min_width.is_auto()) {
  327. if (used_width.to_px(box) < specified_min_width.to_px(box)) {
  328. used_width = try_compute_width(specified_min_width);
  329. }
  330. }
  331. box.set_width(used_width.to_px(box));
  332. box.box_model().margin.left = margin_left.to_px(box);
  333. box.box_model().margin.right = margin_right.to_px(box);
  334. box.box_model().border.left = border_left;
  335. box.box_model().border.right = border_right;
  336. box.box_model().padding.left = padding_left.to_px(box);
  337. box.box_model().padding.right = padding_right.to_px(box);
  338. }
  339. void BlockFormattingContext::compute_height(Box& box)
  340. {
  341. if (box.is_replaced()) {
  342. compute_height_for_block_level_replaced_element_in_normal_flow(downcast<ReplacedBox>(box));
  343. return;
  344. }
  345. auto& style = box.style();
  346. auto& containing_block = *box.containing_block();
  347. CSS::Length specified_height;
  348. if (style.height().is_percentage() && !containing_block.style().height().is_absolute()) {
  349. specified_height = CSS::Length::make_auto();
  350. } else {
  351. specified_height = style.height().resolved_or_auto(box, containing_block.height());
  352. }
  353. auto specified_max_height = style.max_height().resolved_or_auto(box, containing_block.height());
  354. box.box_model().margin.top = style.margin().top.resolved_or_zero(box, containing_block.width()).to_px(box);
  355. box.box_model().margin.bottom = style.margin().bottom.resolved_or_zero(box, containing_block.width()).to_px(box);
  356. box.box_model().border.top = style.border_top().width;
  357. box.box_model().border.bottom = style.border_bottom().width;
  358. box.box_model().padding.top = style.padding().top.resolved_or_zero(box, containing_block.width()).to_px(box);
  359. box.box_model().padding.bottom = style.padding().bottom.resolved_or_zero(box, containing_block.width()).to_px(box);
  360. if (!specified_height.is_auto()) {
  361. float used_height = specified_height.to_px(box);
  362. if (!specified_max_height.is_auto())
  363. used_height = min(used_height, specified_max_height.to_px(box));
  364. box.set_height(used_height);
  365. }
  366. }
  367. void BlockFormattingContext::layout_inline_children(Box& box, LayoutMode layout_mode)
  368. {
  369. InlineFormattingContext context(box, this);
  370. context.run(box, layout_mode);
  371. }
  372. void BlockFormattingContext::layout_block_level_children(Box& box, LayoutMode layout_mode)
  373. {
  374. float content_height = 0;
  375. float content_width = 0;
  376. box.for_each_child_of_type<Box>([&](auto& child_box) {
  377. if (child_box.is_absolutely_positioned()) {
  378. layout_absolutely_positioned_child(child_box);
  379. return IterationDecision::Continue;
  380. }
  381. if (child_box.is_floating()) {
  382. layout_floating_child(child_box, box);
  383. return IterationDecision::Continue;
  384. }
  385. compute_width(child_box);
  386. layout_inside(child_box, layout_mode);
  387. compute_height(child_box);
  388. if (child_box.is_replaced())
  389. place_block_level_replaced_element_in_normal_flow(child_box, box);
  390. else if (child_box.is_block())
  391. place_block_level_non_replaced_element_in_normal_flow(child_box, box);
  392. else
  393. dbgln("FIXME: BlockFormattingContext::layout_block_level_children() doesn't know how to place a {}", child_box.class_name());
  394. // FIXME: This should be factored differently. It's uncool that we mutate the tree *during* layout!
  395. // Instead, we should generate the marker box during the tree build.
  396. if (is<ListItemBox>(child_box))
  397. downcast<ListItemBox>(child_box).layout_marker();
  398. content_height = max(content_height, child_box.effective_offset().y() + child_box.height() + child_box.box_model().margin_box().bottom);
  399. content_width = max(content_width, downcast<Box>(child_box).width());
  400. return IterationDecision::Continue;
  401. });
  402. if (layout_mode != LayoutMode::Default) {
  403. if (box.style().width().is_undefined() || box.style().width().is_auto())
  404. box.set_width(content_width);
  405. }
  406. // FIXME: It's not right to always shrink-wrap the box to the content here.
  407. box.set_height(content_height);
  408. }
  409. void BlockFormattingContext::place_block_level_replaced_element_in_normal_flow(Box& child_box, Box& containing_block)
  410. {
  411. ASSERT(!containing_block.is_absolutely_positioned());
  412. auto& replaced_element_box_model = child_box.box_model();
  413. replaced_element_box_model.margin.top = child_box.style().margin().top.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  414. replaced_element_box_model.margin.bottom = child_box.style().margin().bottom.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  415. replaced_element_box_model.border.top = child_box.style().border_top().width;
  416. replaced_element_box_model.border.bottom = child_box.style().border_bottom().width;
  417. replaced_element_box_model.padding.top = child_box.style().padding().top.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  418. replaced_element_box_model.padding.bottom = child_box.style().padding().bottom.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  419. float x = replaced_element_box_model.margin.left
  420. + replaced_element_box_model.border.left
  421. + replaced_element_box_model.padding.left
  422. + replaced_element_box_model.offset.left;
  423. float y = replaced_element_box_model.margin_box().top + containing_block.box_model().offset.top;
  424. child_box.set_offset(x, y);
  425. }
  426. void BlockFormattingContext::place_block_level_non_replaced_element_in_normal_flow(Box& child_box, Box& containing_block)
  427. {
  428. auto& box_model = child_box.box_model();
  429. auto& style = child_box.style();
  430. box_model.margin.top = style.margin().top.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  431. box_model.margin.bottom = style.margin().bottom.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  432. box_model.border.top = style.border_top().width;
  433. box_model.border.bottom = style.border_bottom().width;
  434. box_model.padding.top = style.padding().top.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  435. box_model.padding.bottom = style.padding().bottom.resolved_or_zero(containing_block, containing_block.width()).to_px(child_box);
  436. float x = box_model.margin.left
  437. + box_model.border.left
  438. + box_model.padding.left
  439. + box_model.offset.left;
  440. if (containing_block.style().text_align() == CSS::TextAlign::VendorSpecificCenter) {
  441. x = (containing_block.width() / 2) - child_box.width() / 2;
  442. }
  443. float y = box_model.margin_box().top
  444. + box_model.offset.top;
  445. // NOTE: Empty (0-height) preceding siblings have their margins collapsed with *their* preceding sibling, etc.
  446. float collapsed_bottom_margin_of_preceding_siblings = 0;
  447. auto* relevant_sibling = child_box.previous_sibling_of_type<Layout::BlockBox>();
  448. while (relevant_sibling != nullptr) {
  449. if (!relevant_sibling->is_absolutely_positioned() && !relevant_sibling->is_floating()) {
  450. collapsed_bottom_margin_of_preceding_siblings = max(collapsed_bottom_margin_of_preceding_siblings, relevant_sibling->box_model().margin.bottom);
  451. if (relevant_sibling->height() > 0)
  452. break;
  453. }
  454. relevant_sibling = relevant_sibling->previous_sibling();
  455. }
  456. if (relevant_sibling) {
  457. y += relevant_sibling->effective_offset().y()
  458. + relevant_sibling->height()
  459. + relevant_sibling->box_model().border_box().bottom;
  460. // Collapse top margin with bottom margin of preceding siblings if needed
  461. float my_margin_top = box_model.margin.top;
  462. if (my_margin_top < 0 || collapsed_bottom_margin_of_preceding_siblings < 0) {
  463. // Negative margins present.
  464. float largest_negative_margin = -min(my_margin_top, collapsed_bottom_margin_of_preceding_siblings);
  465. 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);
  466. float final_margin = largest_positive_margin - largest_negative_margin;
  467. y += final_margin - my_margin_top;
  468. } else if (collapsed_bottom_margin_of_preceding_siblings > my_margin_top) {
  469. // Sibling's margin is larger than mine, adjust so we use sibling's.
  470. y += collapsed_bottom_margin_of_preceding_siblings - my_margin_top;
  471. }
  472. }
  473. if (child_box.style().clear() == CSS::Clear::Left || child_box.style().clear() == CSS::Clear::Both) {
  474. if (!m_left_floating_boxes.is_empty()) {
  475. float clearance_y = 0;
  476. for (auto* floating_box : m_left_floating_boxes) {
  477. clearance_y = max(clearance_y, floating_box->effective_offset().y() + floating_box->box_model().margin_box().bottom);
  478. }
  479. y = max(y, clearance_y);
  480. m_left_floating_boxes.clear();
  481. }
  482. }
  483. if (child_box.style().clear() == CSS::Clear::Right || child_box.style().clear() == CSS::Clear::Both) {
  484. if (!m_right_floating_boxes.is_empty()) {
  485. float clearance_y = 0;
  486. for (auto* floating_box : m_right_floating_boxes) {
  487. clearance_y = max(clearance_y, floating_box->effective_offset().y() + floating_box->box_model().margin_box().bottom);
  488. }
  489. y = max(y, clearance_y);
  490. m_right_floating_boxes.clear();
  491. }
  492. }
  493. child_box.set_offset(x, y);
  494. }
  495. void BlockFormattingContext::layout_initial_containing_block(LayoutMode layout_mode)
  496. {
  497. auto viewport_rect = context_box().frame().viewport_rect();
  498. auto& icb = downcast<Layout::InitialContainingBlockBox>(context_box());
  499. icb.build_stacking_context_tree();
  500. icb.set_width(viewport_rect.width());
  501. layout_block_level_children(context_box(), layout_mode);
  502. ASSERT(!icb.children_are_inline());
  503. // FIXME: The ICB should have the height of the viewport.
  504. // Instead of auto-sizing the ICB, we should spill into overflow.
  505. float lowest_bottom = 0;
  506. icb.for_each_child_of_type<Box>([&](auto& child) {
  507. lowest_bottom = max(lowest_bottom, child.absolute_rect().bottom());
  508. });
  509. icb.set_height(lowest_bottom);
  510. // FIXME: This is a total hack. Make sure any GUI::Widgets are moved into place after layout.
  511. // We should stop embedding GUI::Widgets entirely, since that won't work out-of-process.
  512. icb.for_each_in_subtree_of_type<Layout::WidgetBox>([&](auto& widget) {
  513. widget.update_widget();
  514. return IterationDecision::Continue;
  515. });
  516. }
  517. static Gfx::FloatRect rect_in_coordinate_space(const Box& box, const Box& context_box)
  518. {
  519. Gfx::FloatRect rect { box.effective_offset(), box.size() };
  520. for (auto* ancestor = box.parent(); ancestor; ancestor = ancestor->parent()) {
  521. if (is<Box>(*ancestor)) {
  522. auto offset = downcast<Box>(*ancestor).effective_offset();
  523. rect.move_by(offset);
  524. }
  525. if (ancestor == &context_box)
  526. break;
  527. }
  528. return rect;
  529. }
  530. void BlockFormattingContext::layout_floating_child(Box& box, Box& containing_block)
  531. {
  532. ASSERT(box.is_floating());
  533. compute_width(box);
  534. layout_inside(box, LayoutMode::Default);
  535. compute_height(box);
  536. // First we place the box normally (to get the right y coordinate.)
  537. place_block_level_non_replaced_element_in_normal_flow(box, containing_block);
  538. // Then we float it to the left or right.
  539. float x = box.effective_offset().x();
  540. auto box_in_context_rect = rect_in_coordinate_space(box, context_box());
  541. float y_in_context_box = box_in_context_rect.y();
  542. // Next, float to the left and/or right
  543. if (box.style().float_() == CSS::Float::Left) {
  544. if (!m_left_floating_boxes.is_empty()) {
  545. auto& previous_floating_box = *m_left_floating_boxes.last();
  546. auto previous_rect = rect_in_coordinate_space(previous_floating_box, context_box());
  547. if (previous_rect.contains_vertically(y_in_context_box)) {
  548. // This box touches another already floating box. Stack to the right.
  549. x = previous_floating_box.effective_offset().x() + previous_floating_box.width();
  550. } else {
  551. // This box does not touch another floating box, go all the way to the left.
  552. x = 0;
  553. // Also, forget all previous left-floating boxes while we're here since they're no longer relevant.
  554. m_left_floating_boxes.clear();
  555. }
  556. } else {
  557. // This is the first left-floating box. Go all the way to the left.
  558. x = 0;
  559. }
  560. m_left_floating_boxes.append(&box);
  561. } else if (box.style().float_() == CSS::Float::Right) {
  562. if (!m_right_floating_boxes.is_empty()) {
  563. auto& previous_floating_box = *m_right_floating_boxes.last();
  564. auto previous_rect = rect_in_coordinate_space(previous_floating_box, context_box());
  565. if (previous_rect.contains_vertically(y_in_context_box)) {
  566. // This box touches another already floating box. Stack to the left.
  567. x = previous_floating_box.effective_offset().x() - box.width();
  568. } else {
  569. // This box does not touch another floating box, go all the way to the right.
  570. x = containing_block.width() - box.width();
  571. // Also, forget all previous right-floating boxes while we're here since they're no longer relevant.
  572. m_right_floating_boxes.clear();
  573. }
  574. } else {
  575. // This is the first right-floating box. Go all the way to the right.
  576. x = containing_block.width() - box.width();
  577. }
  578. m_right_floating_boxes.append(&box);
  579. }
  580. box.set_offset(x, box.effective_offset().y());
  581. }
  582. void BlockFormattingContext::layout_absolutely_positioned_child(Box& box)
  583. {
  584. auto& containing_block = context_box();
  585. auto& box_model = box.box_model();
  586. auto specified_width = box.style().width().resolved_or_auto(box, containing_block.width());
  587. compute_width(box);
  588. layout_inside(box, LayoutMode::Default);
  589. compute_height(box);
  590. box_model.margin.left = box.style().margin().left.resolved_or_auto(box, containing_block.width()).to_px(box);
  591. box_model.margin.top = box.style().margin().top.resolved_or_auto(box, containing_block.height()).to_px(box);
  592. box_model.margin.right = box.style().margin().right.resolved_or_auto(box, containing_block.width()).to_px(box);
  593. box_model.margin.bottom = box.style().margin().bottom.resolved_or_auto(box, containing_block.height()).to_px(box);
  594. box_model.border.left = box.style().border_left().width;
  595. box_model.border.right = box.style().border_right().width;
  596. box_model.border.top = box.style().border_top().width;
  597. box_model.border.bottom = box.style().border_bottom().width;
  598. box_model.offset.left = box.style().offset().left.resolved_or_auto(box, containing_block.width()).to_px(box);
  599. box_model.offset.top = box.style().offset().top.resolved_or_auto(box, containing_block.height()).to_px(box);
  600. box_model.offset.right = box.style().offset().right.resolved_or_auto(box, containing_block.width()).to_px(box);
  601. box_model.offset.bottom = box.style().offset().bottom.resolved_or_auto(box, containing_block.height()).to_px(box);
  602. if (box.style().offset().left.is_auto() && specified_width.is_auto() && box.style().offset().right.is_auto()) {
  603. if (box.style().margin().left.is_auto())
  604. box_model.margin.left = 0;
  605. if (box.style().margin().right.is_auto())
  606. box_model.margin.right = 0;
  607. }
  608. Gfx::FloatPoint used_offset;
  609. if (!box.style().offset().left.is_auto()) {
  610. float x_offset = box_model.offset.left
  611. + box_model.border_box().left;
  612. used_offset.set_x(x_offset + box_model.margin.left);
  613. } else if (!box.style().offset().right.is_auto()) {
  614. float x_offset = 0
  615. - box.style().offset().right.to_px(box)
  616. - box_model.border_box().right;
  617. used_offset.set_x(containing_block.width() + x_offset - box.width() - box_model.margin.right);
  618. } else {
  619. float x_offset = box_model.margin_box().left;
  620. used_offset.set_x(x_offset);
  621. }
  622. if (!box.style().offset().top.is_auto()) {
  623. float y_offset = box_model.offset.top
  624. + box_model.border_box().top;
  625. used_offset.set_y(y_offset + box_model.margin.top);
  626. } else if (!box.style().offset().bottom.is_auto()) {
  627. float y_offset = 0
  628. - box.style().offset().bottom.to_px(box)
  629. - box_model.border_box().bottom;
  630. used_offset.set_y(containing_block.height() + y_offset - box.height() - box_model.margin.bottom);
  631. } else {
  632. float y_offset = box_model.margin_box().top;
  633. used_offset.set_y(y_offset);
  634. }
  635. box.set_offset(used_offset);
  636. }
  637. }