BlockFormattingContext.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. layout_floating_children(box);
  60. if (box.children_are_inline()) {
  61. layout_inline_children(box, layout_mode);
  62. } else {
  63. layout_block_level_children(box, layout_mode);
  64. }
  65. if (layout_mode == LayoutMode::Default)
  66. compute_height(box);
  67. }
  68. void BlockFormattingContext::compute_width(Box& box)
  69. {
  70. if (box.is_replaced()) {
  71. // FIXME: This should not be done *by* ReplacedBox
  72. auto& replaced = downcast<ReplacedBox>(box);
  73. replaced.prepare_for_replaced_layout();
  74. compute_width_for_block_level_replaced_element_in_normal_flow(replaced);
  75. return;
  76. }
  77. if (box.is_absolutely_positioned()) {
  78. compute_width_for_absolutely_positioned_block(box);
  79. return;
  80. }
  81. if (box.is_floating()) {
  82. compute_width_for_floating_box(box);
  83. return;
  84. }
  85. auto& style = box.style();
  86. float width_of_containing_block = box.width_of_logical_containing_block();
  87. auto zero_value = CSS::Length::make_px(0);
  88. auto margin_left = CSS::Length::make_auto();
  89. auto margin_right = CSS::Length::make_auto();
  90. const auto padding_left = style.padding().left.resolved_or_zero(box, width_of_containing_block);
  91. const auto padding_right = style.padding().right.resolved_or_zero(box, width_of_containing_block);
  92. auto try_compute_width = [&](const auto& a_width) {
  93. CSS::Length width = a_width;
  94. margin_left = style.margin().left.resolved_or_zero(box, width_of_containing_block);
  95. margin_right = style.margin().right.resolved_or_zero(box, width_of_containing_block);
  96. float total_px = style.border_left().width + style.border_right().width;
  97. for (auto& value : { margin_left, padding_left, width, padding_right, margin_right }) {
  98. total_px += value.to_px(box);
  99. }
  100. if (!box.is_replaced() && !box.is_inline()) {
  101. // 10.3.3 Block-level, non-replaced elements in normal flow
  102. // 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.
  103. if (width.is_auto() && total_px > width_of_containing_block) {
  104. if (margin_left.is_auto())
  105. margin_left = zero_value;
  106. if (margin_right.is_auto())
  107. margin_right = zero_value;
  108. }
  109. // 10.3.3 cont'd.
  110. auto underflow_px = width_of_containing_block - total_px;
  111. if (width.is_auto()) {
  112. if (margin_left.is_auto())
  113. margin_left = zero_value;
  114. if (margin_right.is_auto())
  115. margin_right = zero_value;
  116. if (underflow_px >= 0) {
  117. width = CSS::Length(underflow_px, CSS::Length::Type::Px);
  118. } else {
  119. width = zero_value;
  120. margin_right = CSS::Length(margin_right.to_px(box) + underflow_px, CSS::Length::Type::Px);
  121. }
  122. } else {
  123. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  124. margin_right = CSS::Length(margin_right.to_px(box) + underflow_px, CSS::Length::Type::Px);
  125. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  126. margin_right = CSS::Length(underflow_px, CSS::Length::Type::Px);
  127. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  128. margin_left = CSS::Length(underflow_px, CSS::Length::Type::Px);
  129. } else { // margin_left.is_auto() && margin_right.is_auto()
  130. auto half_of_the_underflow = CSS::Length(underflow_px / 2, CSS::Length::Type::Px);
  131. margin_left = half_of_the_underflow;
  132. margin_right = half_of_the_underflow;
  133. }
  134. }
  135. } else if (!box.is_replaced() && box.is_inline_block()) {
  136. // 10.3.9 'Inline-block', non-replaced elements in normal flow
  137. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  138. if (margin_left.is_auto())
  139. margin_left = zero_value;
  140. if (margin_right.is_auto())
  141. margin_right = zero_value;
  142. // If 'width' is 'auto', the used value is the shrink-to-fit width as for floating elements.
  143. if (width.is_auto()) {
  144. // Find the available width: in this case, this is the width of the containing
  145. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  146. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  147. float available_width = width_of_containing_block
  148. - margin_left.to_px(box) - style.border_left().width - padding_left.to_px(box)
  149. - padding_right.to_px(box) - style.border_right().width - margin_right.to_px(box);
  150. auto result = calculate_shrink_to_fit_widths(box);
  151. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  152. width = CSS::Length(min(max(result.preferred_minimum_width, available_width), result.preferred_width), CSS::Length::Type::Px);
  153. }
  154. }
  155. return width;
  156. };
  157. auto specified_width = style.width().resolved_or_auto(box, width_of_containing_block);
  158. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  159. auto used_width = try_compute_width(specified_width);
  160. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  161. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  162. auto specified_max_width = style.max_width().resolved_or_auto(box, width_of_containing_block);
  163. if (!specified_max_width.is_auto()) {
  164. if (used_width.to_px(box) > specified_max_width.to_px(box)) {
  165. used_width = try_compute_width(specified_max_width);
  166. }
  167. }
  168. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  169. // but this time using the value of 'min-width' as the computed value for 'width'.
  170. auto specified_min_width = style.min_width().resolved_or_auto(box, width_of_containing_block);
  171. if (!specified_min_width.is_auto()) {
  172. if (used_width.to_px(box) < specified_min_width.to_px(box)) {
  173. used_width = try_compute_width(specified_min_width);
  174. }
  175. }
  176. box.set_width(used_width.to_px(box));
  177. box.box_model().margin.left = margin_left;
  178. box.box_model().margin.right = margin_right;
  179. box.box_model().border.left = CSS::Length::make_px(style.border_left().width);
  180. box.box_model().border.right = CSS::Length::make_px(style.border_right().width);
  181. box.box_model().padding.left = padding_left;
  182. box.box_model().padding.right = padding_right;
  183. }
  184. void BlockFormattingContext::compute_width_for_floating_box(Box& box)
  185. {
  186. // 10.3.5 Floating, non-replaced elements
  187. auto& style = box.style();
  188. float width_of_containing_block = box.width_of_logical_containing_block();
  189. auto zero_value = CSS::Length::make_px(0);
  190. auto margin_left = CSS::Length::make_auto();
  191. auto margin_right = CSS::Length::make_auto();
  192. const auto padding_left = style.padding().left.resolved_or_zero(box, width_of_containing_block);
  193. const auto padding_right = style.padding().right.resolved_or_zero(box, width_of_containing_block);
  194. // If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
  195. if (margin_left.is_auto())
  196. margin_left = zero_value;
  197. if (margin_right.is_auto())
  198. margin_right = zero_value;
  199. auto width = style.width().resolved_or_auto(box, width_of_containing_block);
  200. // If 'width' is computed as 'auto', the used value is the "shrink-to-fit" width.
  201. if (width.is_auto()) {
  202. // Find the available width: in this case, this is the width of the containing
  203. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  204. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  205. float available_width = width_of_containing_block
  206. - margin_left.to_px(box) - style.border_left().width - padding_left.to_px(box)
  207. - padding_right.to_px(box) - style.border_right().width - margin_right.to_px(box);
  208. auto result = calculate_shrink_to_fit_widths(box);
  209. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  210. width = CSS::Length(min(max(result.preferred_minimum_width, available_width), result.preferred_width), CSS::Length::Type::Px);
  211. }
  212. float final_width = width.resolved_or_zero(box, width_of_containing_block).to_px(box);
  213. box.set_width(final_width);
  214. }
  215. void BlockFormattingContext::compute_width_for_block_level_replaced_element_in_normal_flow(ReplacedBox& box)
  216. {
  217. box.set_width(compute_width_for_replaced_element(box));
  218. }
  219. void BlockFormattingContext::compute_height_for_block_level_replaced_element_in_normal_flow(ReplacedBox& box)
  220. {
  221. box.set_height(compute_height_for_replaced_element(box));
  222. }
  223. void BlockFormattingContext::compute_width_for_absolutely_positioned_block(Box& box)
  224. {
  225. auto& containing_block = context_box();
  226. auto& style = box.style();
  227. auto zero_value = CSS::Length::make_px(0);
  228. auto margin_left = CSS::Length::make_auto();
  229. auto margin_right = CSS::Length::make_auto();
  230. const auto border_left = style.border_left().width;
  231. const auto border_right = style.border_right().width;
  232. const auto padding_left = style.padding().left.resolved_or_zero(box, containing_block.width());
  233. const auto padding_right = style.padding().right.resolved_or_zero(box, containing_block.width());
  234. auto try_compute_width = [&](const auto& a_width) {
  235. margin_left = style.margin().left.resolved_or_zero(box, containing_block.width());
  236. margin_right = style.margin().right.resolved_or_zero(box, containing_block.width());
  237. auto left = style.offset().left.resolved_or_auto(box, containing_block.width());
  238. auto right = style.offset().right.resolved_or_auto(box, containing_block.width());
  239. auto width = a_width;
  240. auto solve_for_left = [&] {
  241. 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);
  242. };
  243. auto solve_for_width = [&] {
  244. 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);
  245. };
  246. auto solve_for_right = [&] {
  247. 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);
  248. };
  249. // If all three of 'left', 'width', and 'right' are 'auto':
  250. if (left.is_auto() && width.is_auto() && right.is_auto()) {
  251. // First set any 'auto' values for 'margin-left' and 'margin-right' to 0.
  252. if (margin_left.is_auto())
  253. margin_left = CSS::Length::make_px(0);
  254. if (margin_right.is_auto())
  255. margin_right = CSS::Length::make_px(0);
  256. // Then, if the 'direction' property of the element establishing the static-position containing block
  257. // is 'ltr' set 'left' to the static position and apply rule number three below;
  258. // otherwise, set 'right' to the static position and apply rule number one below.
  259. // FIXME: This is very hackish.
  260. left = CSS::Length::make_px(0);
  261. goto Rule3;
  262. }
  263. if (!left.is_auto() && !width.is_auto() && !right.is_auto()) {
  264. // FIXME: This should be solved in a more complicated way.
  265. return width;
  266. }
  267. if (margin_left.is_auto())
  268. margin_left = CSS::Length::make_px(0);
  269. if (margin_right.is_auto())
  270. margin_right = CSS::Length::make_px(0);
  271. // 1. 'left' and 'width' are 'auto' and 'right' is not 'auto',
  272. // then the width is shrink-to-fit. Then solve for 'left'
  273. if (left.is_auto() && width.is_auto() && !right.is_auto()) {
  274. auto result = calculate_shrink_to_fit_widths(box);
  275. solve_for_left();
  276. auto available_width = solve_for_width();
  277. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  278. }
  279. // 2. 'left' and 'right' are 'auto' and 'width' is not 'auto',
  280. // then if the 'direction' property of the element establishing
  281. // the static-position containing block is 'ltr' set 'left'
  282. // to the static position, otherwise set 'right' to the static position.
  283. // Then solve for 'left' (if 'direction is 'rtl') or 'right' (if 'direction' is 'ltr').
  284. else if (left.is_auto() && right.is_auto() && !width.is_auto()) {
  285. // FIXME: Check direction
  286. // FIXME: Use the static-position containing block
  287. left = zero_value;
  288. right = solve_for_right();
  289. }
  290. // 3. 'width' and 'right' are 'auto' and 'left' is not 'auto',
  291. // then the width is shrink-to-fit. Then solve for 'right'
  292. else if (width.is_auto() && right.is_auto() && !left.is_auto()) {
  293. Rule3:
  294. auto result = calculate_shrink_to_fit_widths(box);
  295. right = solve_for_right();
  296. auto available_width = solve_for_width();
  297. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  298. }
  299. // 4. 'left' is 'auto', 'width' and 'right' are not 'auto', then solve for 'left'
  300. else if (left.is_auto() && !width.is_auto() && !right.is_auto()) {
  301. left = solve_for_left();
  302. }
  303. // 5. 'width' is 'auto', 'left' and 'right' are not 'auto', then solve for 'width'
  304. else if (width.is_auto() && !left.is_auto() && !right.is_auto()) {
  305. width = solve_for_width();
  306. }
  307. // 6. 'right' is 'auto', 'left' and 'width' are not 'auto', then solve for 'right'
  308. else if (right.is_auto() && !left.is_auto() && !width.is_auto()) {
  309. right = solve_for_right();
  310. }
  311. return width;
  312. };
  313. auto specified_width = style.width().resolved_or_auto(box, containing_block.width());
  314. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  315. auto used_width = try_compute_width(specified_width);
  316. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  317. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  318. auto specified_max_width = style.max_width().resolved_or_auto(box, containing_block.width());
  319. if (!specified_max_width.is_auto()) {
  320. if (used_width.to_px(box) > specified_max_width.to_px(box)) {
  321. used_width = try_compute_width(specified_max_width);
  322. }
  323. }
  324. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  325. // but this time using the value of 'min-width' as the computed value for 'width'.
  326. auto specified_min_width = style.min_width().resolved_or_auto(box, containing_block.width());
  327. if (!specified_min_width.is_auto()) {
  328. if (used_width.to_px(box) < specified_min_width.to_px(box)) {
  329. used_width = try_compute_width(specified_min_width);
  330. }
  331. }
  332. box.set_width(used_width.to_px(box));
  333. box.box_model().margin.left = margin_left;
  334. box.box_model().margin.right = margin_right;
  335. box.box_model().border.left = CSS::Length::make_px(border_left);
  336. box.box_model().border.right = CSS::Length::make_px(border_right);
  337. box.box_model().padding.left = padding_left;
  338. box.box_model().padding.right = padding_right;
  339. }
  340. void BlockFormattingContext::compute_height(Box& box)
  341. {
  342. if (box.is_replaced()) {
  343. compute_height_for_block_level_replaced_element_in_normal_flow(downcast<ReplacedBox>(box));
  344. return;
  345. }
  346. auto& style = box.style();
  347. auto& containing_block = *box.containing_block();
  348. CSS::Length specified_height;
  349. if (style.height().is_percentage() && !containing_block.style().height().is_absolute()) {
  350. specified_height = CSS::Length::make_auto();
  351. } else {
  352. specified_height = style.height().resolved_or_auto(box, containing_block.height());
  353. }
  354. auto specified_max_height = style.max_height().resolved_or_auto(box, containing_block.height());
  355. box.box_model().margin.top = style.margin().top.resolved_or_zero(box, containing_block.width());
  356. box.box_model().margin.bottom = style.margin().bottom.resolved_or_zero(box, containing_block.width());
  357. box.box_model().border.top = CSS::Length::make_px(style.border_top().width);
  358. box.box_model().border.bottom = CSS::Length::make_px(style.border_bottom().width);
  359. box.box_model().padding.top = style.padding().top.resolved_or_zero(box, containing_block.width());
  360. box.box_model().padding.bottom = style.padding().bottom.resolved_or_zero(box, containing_block.width());
  361. if (!specified_height.is_auto()) {
  362. float used_height = specified_height.to_px(box);
  363. if (!specified_max_height.is_auto())
  364. used_height = min(used_height, specified_max_height.to_px(box));
  365. box.set_height(used_height);
  366. }
  367. }
  368. void BlockFormattingContext::layout_inline_children(Box& box, LayoutMode layout_mode)
  369. {
  370. InlineFormattingContext context(box, this);
  371. context.run(box, layout_mode);
  372. }
  373. void BlockFormattingContext::layout_block_level_children(Box& box, LayoutMode layout_mode)
  374. {
  375. float content_height = 0;
  376. float content_width = 0;
  377. box.for_each_child_of_type<Box>([&](auto& child_box) {
  378. if (child_box.is_absolutely_positioned()) {
  379. layout_absolutely_positioned_child(child_box);
  380. return IterationDecision::Continue;
  381. }
  382. if (child_box.is_floating())
  383. return IterationDecision::Continue;
  384. compute_width(child_box);
  385. layout_inside(child_box, layout_mode);
  386. compute_height(child_box);
  387. if (child_box.is_replaced())
  388. place_block_level_replaced_element_in_normal_flow(child_box, box);
  389. else if (child_box.is_block())
  390. place_block_level_non_replaced_element_in_normal_flow(child_box, box);
  391. else
  392. dbgln("FIXME: BlockFormattingContext::layout_block_level_children() doesn't know how to place a {}", child_box.class_name());
  393. // FIXME: This should be factored differently. It's uncool that we mutate the tree *during* layout!
  394. // Instead, we should generate the marker box during the tree build.
  395. if (is<ListItemBox>(child_box))
  396. downcast<ListItemBox>(child_box).layout_marker();
  397. content_height = max(content_height, child_box.effective_offset().y() + child_box.height() + child_box.box_model().margin_box(child_box).bottom);
  398. content_width = max(content_width, downcast<Box>(child_box).width());
  399. return IterationDecision::Continue;
  400. });
  401. if (layout_mode != LayoutMode::Default) {
  402. if (box.style().width().is_undefined() || box.style().width().is_auto())
  403. box.set_width(content_width);
  404. }
  405. // FIXME: It's not right to always shrink-wrap the box to the content here.
  406. box.set_height(content_height);
  407. }
  408. void BlockFormattingContext::place_block_level_replaced_element_in_normal_flow(Box& child_box, Box& containing_block)
  409. {
  410. ASSERT(!containing_block.is_absolutely_positioned());
  411. auto& replaced_element_box_model = child_box.box_model();
  412. replaced_element_box_model.margin.top = child_box.style().margin().top.resolved_or_zero(containing_block, containing_block.width());
  413. replaced_element_box_model.margin.bottom = child_box.style().margin().bottom.resolved_or_zero(containing_block, containing_block.width());
  414. replaced_element_box_model.border.top = CSS::Length::make_px(child_box.style().border_top().width);
  415. replaced_element_box_model.border.bottom = CSS::Length::make_px(child_box.style().border_bottom().width);
  416. replaced_element_box_model.padding.top = child_box.style().padding().top.resolved_or_zero(containing_block, containing_block.width());
  417. replaced_element_box_model.padding.bottom = child_box.style().padding().bottom.resolved_or_zero(containing_block, containing_block.width());
  418. float x = replaced_element_box_model.margin.left.to_px(child_box)
  419. + replaced_element_box_model.border.left.to_px(child_box)
  420. + replaced_element_box_model.padding.left.to_px(child_box)
  421. + replaced_element_box_model.offset.left.to_px(child_box);
  422. float y = replaced_element_box_model.margin_box(child_box).top + containing_block.box_model().offset.top.to_px(child_box);
  423. child_box.set_offset(x, y);
  424. }
  425. void BlockFormattingContext::place_block_level_non_replaced_element_in_normal_flow(Box& child_box, Box& containing_block)
  426. {
  427. auto& box_model = child_box.box_model();
  428. auto& style = child_box.style();
  429. box_model.margin.top = style.margin().top.resolved_or_zero(containing_block, containing_block.width());
  430. box_model.margin.bottom = style.margin().bottom.resolved_or_zero(containing_block, containing_block.width());
  431. box_model.border.top = CSS::Length::make_px(style.border_top().width);
  432. box_model.border.bottom = CSS::Length::make_px(style.border_bottom().width);
  433. box_model.padding.top = style.padding().top.resolved_or_zero(containing_block, containing_block.width());
  434. box_model.padding.bottom = style.padding().bottom.resolved_or_zero(containing_block, containing_block.width());
  435. float x = box_model.margin.left.to_px(child_box)
  436. + box_model.border.left.to_px(child_box)
  437. + box_model.padding.left.to_px(child_box)
  438. + box_model.offset.left.to_px(child_box);
  439. if (containing_block.style().text_align() == CSS::TextAlign::VendorSpecificCenter) {
  440. x = (containing_block.width() / 2) - child_box.width() / 2;
  441. }
  442. float y = box_model.margin_box(child_box).top
  443. + box_model.offset.top.to_px(child_box);
  444. // NOTE: Empty (0-height) preceding siblings have their margins collapsed with *their* preceding sibling, etc.
  445. float collapsed_bottom_margin_of_preceding_siblings = 0;
  446. auto* relevant_sibling = child_box.previous_sibling_of_type<Layout::BlockBox>();
  447. while (relevant_sibling != nullptr) {
  448. if (!relevant_sibling->is_absolutely_positioned() && !relevant_sibling->is_floating()) {
  449. collapsed_bottom_margin_of_preceding_siblings = max(collapsed_bottom_margin_of_preceding_siblings, relevant_sibling->box_model().margin.bottom.to_px(*relevant_sibling));
  450. if (relevant_sibling->height() > 0)
  451. break;
  452. }
  453. relevant_sibling = relevant_sibling->previous_sibling();
  454. }
  455. if (relevant_sibling) {
  456. y += relevant_sibling->effective_offset().y()
  457. + relevant_sibling->height()
  458. + relevant_sibling->box_model().border_box(*relevant_sibling).bottom;
  459. // Collapse top margin with bottom margin of preceding siblings if needed
  460. float my_margin_top = box_model.margin.top.to_px(child_box);
  461. if (my_margin_top < 0 || collapsed_bottom_margin_of_preceding_siblings < 0) {
  462. // Negative margins present.
  463. float largest_negative_margin = -min(my_margin_top, collapsed_bottom_margin_of_preceding_siblings);
  464. 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);
  465. float final_margin = largest_positive_margin - largest_negative_margin;
  466. y += final_margin - my_margin_top;
  467. } else if (collapsed_bottom_margin_of_preceding_siblings > my_margin_top) {
  468. // Sibling's margin is larger than mine, adjust so we use sibling's.
  469. y += collapsed_bottom_margin_of_preceding_siblings - my_margin_top;
  470. }
  471. }
  472. if (child_box.style().clear() == CSS::Clear::Left || child_box.style().clear() == CSS::Clear::Both) {
  473. if (!m_left_floating_boxes.is_empty()) {
  474. float clearance_y = 0;
  475. for (auto* floating_box : m_left_floating_boxes) {
  476. clearance_y = max(clearance_y, floating_box->effective_offset().y() + floating_box->box_model().margin_box(*floating_box).bottom);
  477. }
  478. y = max(y, clearance_y);
  479. m_left_floating_boxes.clear();
  480. }
  481. }
  482. if (child_box.style().clear() == CSS::Clear::Right || child_box.style().clear() == CSS::Clear::Both) {
  483. if (!m_right_floating_boxes.is_empty()) {
  484. float clearance_y = 0;
  485. for (auto* floating_box : m_right_floating_boxes) {
  486. clearance_y = max(clearance_y, floating_box->effective_offset().y() + floating_box->box_model().margin_box(*floating_box).bottom);
  487. }
  488. y = max(y, clearance_y);
  489. m_right_floating_boxes.clear();
  490. }
  491. }
  492. child_box.set_offset(x, y);
  493. }
  494. void BlockFormattingContext::layout_initial_containing_block(LayoutMode layout_mode)
  495. {
  496. auto viewport_rect = context_box().frame().viewport_rect();
  497. auto& icb = downcast<Layout::InitialContainingBlockBox>(context_box());
  498. icb.build_stacking_context_tree();
  499. icb.set_width(viewport_rect.width());
  500. layout_floating_children(context_box());
  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. void BlockFormattingContext::layout_floating_children(Box& box)
  518. {
  519. box.for_each_child_of_type<Box>([&](auto& child_box) {
  520. if (child_box.is_floating())
  521. layout_floating_child(child_box, box);
  522. return IterationDecision::Continue;
  523. });
  524. }
  525. static Gfx::FloatRect rect_in_coordinate_space(const Box& box, const Box& context_box)
  526. {
  527. Gfx::FloatRect rect { box.effective_offset(), box.size() };
  528. for (auto* ancestor = box.parent(); ancestor; ancestor = ancestor->parent()) {
  529. if (is<Box>(*ancestor)) {
  530. auto offset = downcast<Box>(*ancestor).effective_offset();
  531. rect.move_by(offset);
  532. }
  533. if (ancestor == &context_box)
  534. break;
  535. }
  536. return rect;
  537. }
  538. void BlockFormattingContext::layout_floating_child(Box& box, Box& containing_block)
  539. {
  540. ASSERT(box.is_floating());
  541. compute_width(box);
  542. layout_inside(box, LayoutMode::Default);
  543. compute_height(box);
  544. // First we place the box normally (to get the right y coordinate.)
  545. place_block_level_non_replaced_element_in_normal_flow(box, containing_block);
  546. // Then we float it to the left or right.
  547. float x = box.effective_offset().x();
  548. auto box_in_context_rect = rect_in_coordinate_space(box, context_box());
  549. float y_in_context_box = box_in_context_rect.y();
  550. // Next, float to the left and/or right
  551. if (box.style().float_() == CSS::Float::Left) {
  552. if (!m_left_floating_boxes.is_empty()) {
  553. auto& previous_floating_box = *m_left_floating_boxes.last();
  554. auto previous_rect = rect_in_coordinate_space(previous_floating_box, context_box());
  555. if (previous_rect.contains_vertically(y_in_context_box)) {
  556. // This box touches another already floating box. Stack to the right.
  557. x = previous_floating_box.effective_offset().x() + previous_floating_box.width();
  558. } else {
  559. // This box does not touch another floating box, go all the way to the left.
  560. x = 0;
  561. // Also, forget all previous left-floating boxes while we're here since they're no longer relevant.
  562. m_left_floating_boxes.clear();
  563. }
  564. } else {
  565. // This is the first left-floating box. Go all the way to the left.
  566. x = 0;
  567. }
  568. m_left_floating_boxes.append(&box);
  569. } else if (box.style().float_() == CSS::Float::Right) {
  570. if (!m_right_floating_boxes.is_empty()) {
  571. auto& previous_floating_box = *m_right_floating_boxes.last();
  572. auto previous_rect = rect_in_coordinate_space(previous_floating_box, context_box());
  573. if (previous_rect.contains_vertically(y_in_context_box)) {
  574. // This box touches another already floating box. Stack to the left.
  575. x = previous_floating_box.effective_offset().x() - box.width();
  576. } else {
  577. // This box does not touch another floating box, go all the way to the right.
  578. x = containing_block.width() - box.width();
  579. // Also, forget all previous right-floating boxes while we're here since they're no longer relevant.
  580. m_right_floating_boxes.clear();
  581. }
  582. } else {
  583. // This is the first right-floating box. Go all the way to the right.
  584. x = containing_block.width() - box.width();
  585. }
  586. m_right_floating_boxes.append(&box);
  587. }
  588. box.set_offset(x, box.effective_offset().y());
  589. }
  590. void BlockFormattingContext::layout_absolutely_positioned_child(Box& box)
  591. {
  592. auto& containing_block = context_box();
  593. auto& box_model = box.box_model();
  594. auto zero_value = CSS::Length::make_px(0);
  595. auto specified_width = box.style().width().resolved_or_auto(box, containing_block.width());
  596. compute_width(box);
  597. layout_inside(box, LayoutMode::Default);
  598. compute_height(box);
  599. box_model.margin.left = box.style().margin().left.resolved_or_auto(box, containing_block.width());
  600. box_model.margin.top = box.style().margin().top.resolved_or_auto(box, containing_block.height());
  601. box_model.margin.right = box.style().margin().right.resolved_or_auto(box, containing_block.width());
  602. box_model.margin.bottom = box.style().margin().bottom.resolved_or_auto(box, containing_block.height());
  603. box_model.border.left = CSS::Length::make_px(box.style().border_left().width);
  604. box_model.border.right = CSS::Length::make_px(box.style().border_right().width);
  605. box_model.border.top = CSS::Length::make_px(box.style().border_top().width);
  606. box_model.border.bottom = CSS::Length::make_px(box.style().border_bottom().width);
  607. box_model.offset.left = box.style().offset().left.resolved_or_auto(box, containing_block.width());
  608. box_model.offset.top = box.style().offset().top.resolved_or_auto(box, containing_block.height());
  609. box_model.offset.right = box.style().offset().right.resolved_or_auto(box, containing_block.width());
  610. box_model.offset.bottom = box.style().offset().bottom.resolved_or_auto(box, containing_block.height());
  611. if (box_model.offset.left.is_auto() && specified_width.is_auto() && box_model.offset.right.is_auto()) {
  612. if (box_model.margin.left.is_auto())
  613. box_model.margin.left = zero_value;
  614. if (box_model.margin.right.is_auto())
  615. box_model.margin.right = zero_value;
  616. }
  617. Gfx::FloatPoint used_offset;
  618. if (!box_model.offset.left.is_auto()) {
  619. float x_offset = box_model.offset.left.to_px(box)
  620. + box_model.border_box(box).left;
  621. used_offset.set_x(x_offset + box_model.margin.left.to_px(box));
  622. } else if (!box_model.offset.right.is_auto()) {
  623. float x_offset = 0
  624. - box_model.offset.right.to_px(box)
  625. - box_model.border_box(box).right;
  626. used_offset.set_x(containing_block.width() + x_offset - box.width() - box_model.margin.right.to_px(box));
  627. } else {
  628. float x_offset = box_model.margin_box(box).left;
  629. used_offset.set_x(x_offset);
  630. }
  631. if (!box_model.offset.top.is_auto()) {
  632. float y_offset = box_model.offset.top.to_px(box)
  633. + box_model.border_box(box).top;
  634. used_offset.set_y(y_offset + box_model.margin.top.to_px(box));
  635. } else if (!box_model.offset.bottom.is_auto()) {
  636. float y_offset = 0
  637. - box_model.offset.bottom.to_px(box)
  638. - box_model.border_box(box).bottom;
  639. used_offset.set_y(containing_block.height() + y_offset - box.height() - box_model.margin.bottom.to_px(box));
  640. } else {
  641. float y_offset = box_model.margin_box(box).top;
  642. used_offset.set_y(y_offset);
  643. }
  644. box.set_offset(used_offset);
  645. }
  646. }