FormattingContext.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Dump.h>
  7. #include <LibWeb/Layout/BlockFormattingContext.h>
  8. #include <LibWeb/Layout/Box.h>
  9. #include <LibWeb/Layout/FlexFormattingContext.h>
  10. #include <LibWeb/Layout/FormattingContext.h>
  11. #include <LibWeb/Layout/InlineFormattingContext.h>
  12. #include <LibWeb/Layout/ReplacedBox.h>
  13. #include <LibWeb/Layout/SVGFormattingContext.h>
  14. #include <LibWeb/Layout/SVGSVGBox.h>
  15. #include <LibWeb/Layout/TableBox.h>
  16. #include <LibWeb/Layout/TableCellBox.h>
  17. #include <LibWeb/Layout/TableFormattingContext.h>
  18. namespace Web::Layout {
  19. FormattingContext::FormattingContext(Box& context_box, FormattingContext* parent)
  20. : m_parent(parent)
  21. , m_context_box(&context_box)
  22. {
  23. }
  24. FormattingContext::~FormattingContext()
  25. {
  26. }
  27. bool FormattingContext::creates_block_formatting_context(const Box& box)
  28. {
  29. if (box.is_root_element())
  30. return true;
  31. if (box.is_floating())
  32. return true;
  33. if (box.is_absolutely_positioned())
  34. return true;
  35. if (box.is_inline_block())
  36. return true;
  37. if (is<TableCellBox>(box))
  38. return true;
  39. CSS::Overflow overflow_x = box.computed_values().overflow_x();
  40. if ((overflow_x != CSS::Overflow::Visible) && (overflow_x != CSS::Overflow::Clip))
  41. return true;
  42. CSS::Overflow overflow_y = box.computed_values().overflow_y();
  43. if ((overflow_y != CSS::Overflow::Visible) && (overflow_y != CSS::Overflow::Clip))
  44. return true;
  45. // FIXME: inline-flex as well
  46. if (box.parent() && box.parent()->computed_values().display() == CSS::Display::Flex) {
  47. // FIXME: Flex items (direct children of the element with display: flex or inline-flex) if they are neither flex nor grid nor table containers themselves.
  48. if (box.computed_values().display() != CSS::Display::Flex)
  49. return true;
  50. }
  51. // FIXME: table-caption
  52. // FIXME: anonymous table cells
  53. // FIXME: display: flow-root
  54. // FIXME: Elements with contain: layout, content, or paint.
  55. // FIXME: grid
  56. // FIXME: multicol
  57. // FIXME: column-span: all
  58. return false;
  59. }
  60. void FormattingContext::layout_inside(Box& box, LayoutMode layout_mode)
  61. {
  62. if (is<SVGSVGBox>(box)) {
  63. SVGFormattingContext context(box, this);
  64. context.run(box, layout_mode);
  65. return;
  66. }
  67. if (creates_block_formatting_context(box)) {
  68. BlockFormattingContext context(box, this);
  69. context.run(box, layout_mode);
  70. return;
  71. }
  72. if (box.computed_values().display() == CSS::Display::Flex) {
  73. FlexFormattingContext context(box, this);
  74. context.run(box, layout_mode);
  75. return;
  76. }
  77. if (is<TableBox>(box)) {
  78. TableFormattingContext context(box, this);
  79. context.run(box, layout_mode);
  80. } else if (box.children_are_inline()) {
  81. InlineFormattingContext context(box, this);
  82. context.run(box, layout_mode);
  83. } else {
  84. // FIXME: This needs refactoring!
  85. VERIFY(is_block_formatting_context());
  86. run(box, layout_mode);
  87. }
  88. }
  89. static float greatest_child_width(const Box& box)
  90. {
  91. float max_width = 0;
  92. if (box.children_are_inline()) {
  93. for (auto& child : box.line_boxes()) {
  94. max_width = max(max_width, child.width());
  95. }
  96. } else {
  97. box.for_each_child_of_type<Box>([&](auto& child) {
  98. max_width = max(max_width, child.border_box_width());
  99. });
  100. }
  101. return max_width;
  102. }
  103. FormattingContext::ShrinkToFitResult FormattingContext::calculate_shrink_to_fit_widths(Box& box)
  104. {
  105. // Calculate the preferred width by formatting the content without breaking lines
  106. // other than where explicit line breaks occur.
  107. layout_inside(box, LayoutMode::OnlyRequiredLineBreaks);
  108. float preferred_width = greatest_child_width(box);
  109. // Also calculate the preferred minimum width, e.g., by trying all possible line breaks.
  110. // CSS 2.2 does not define the exact algorithm.
  111. layout_inside(box, LayoutMode::AllPossibleLineBreaks);
  112. float preferred_minimum_width = greatest_child_width(box);
  113. return { preferred_width, preferred_minimum_width };
  114. }
  115. static Gfx::FloatSize solve_replaced_size_constraint(float w, float h, const ReplacedBox& box)
  116. {
  117. // 10.4 Minimum and maximum widths: 'min-width' and 'max-width'
  118. auto& containing_block = *box.containing_block();
  119. auto specified_min_width = box.computed_values().min_width().resolved_or_zero(box, containing_block.width()).to_px(box);
  120. auto specified_max_width = box.computed_values().max_width().resolved(CSS::Length::make_px(w), box, containing_block.width()).to_px(box);
  121. auto specified_min_height = box.computed_values().min_height().resolved_or_auto(box, containing_block.height()).to_px(box);
  122. auto specified_max_height = box.computed_values().max_height().resolved(CSS::Length::make_px(h), box, containing_block.height()).to_px(box);
  123. auto min_width = min(specified_min_width, specified_max_width);
  124. auto max_width = max(specified_min_width, specified_max_width);
  125. auto min_height = min(specified_min_height, specified_max_height);
  126. auto max_height = max(specified_min_height, specified_max_height);
  127. if (w > max_width)
  128. return { w, max(max_width * h / w, min_height) };
  129. if (w < min_width)
  130. return { max_width, min(min_width * h / w, max_height) };
  131. if (h > max_height)
  132. return { max(max_height * w / h, min_width), max_height };
  133. if (h < min_height)
  134. return { min(min_height * w / h, max_width), min_height };
  135. if ((w > max_width && h > max_height) && (max_width / w < max_height / h))
  136. return { max_width, max(min_height, max_width * h / w) };
  137. if ((w > max_width && h > max_height) && (max_width / w > max_height / h))
  138. return { max(min_width, max_height * w / h), max_height };
  139. if ((w < min_width && h < min_height) && (min_width / w < min_height / h))
  140. return { min(max_width, min_height * w / h), min_height };
  141. if ((w < min_width && h < min_height) && (min_width / w > min_height / h))
  142. return { min_width, min(max_height, min_width * h / w) };
  143. if (w < min_width && h > max_height)
  144. return { min_width, max_height };
  145. if (w > max_width && h < min_height)
  146. return { max_width, min_height };
  147. return { w, h };
  148. }
  149. static float compute_auto_height_for_block_level_element(const Box& box)
  150. {
  151. Optional<float> top;
  152. Optional<float> bottom;
  153. if (box.children_are_inline()) {
  154. // If it only has inline-level children, the height is the distance between
  155. // the top of the topmost line box and the bottom of the bottommost line box.
  156. if (!box.line_boxes().is_empty()) {
  157. for (auto& fragment : box.line_boxes().first().fragments()) {
  158. if (!top.has_value() || fragment.offset().y() < top.value())
  159. top = fragment.offset().y();
  160. }
  161. for (auto& fragment : box.line_boxes().last().fragments()) {
  162. if (!bottom.has_value() || (fragment.offset().y() + fragment.height()) > bottom.value())
  163. bottom = fragment.offset().y() + fragment.height();
  164. }
  165. }
  166. } else {
  167. // If it has block-level children, the height is the distance between
  168. // the top margin-edge of the topmost block-level child box
  169. // and the bottom margin-edge of the bottommost block-level child box.
  170. box.for_each_child_of_type<Box>([&](Layout::Box& child_box) {
  171. if (child_box.is_absolutely_positioned())
  172. return IterationDecision::Continue;
  173. if ((box.computed_values().overflow_y() == CSS::Overflow::Visible) && child_box.is_floating())
  174. return IterationDecision::Continue;
  175. float child_box_top = child_box.effective_offset().y() - child_box.box_model().margin_box().top;
  176. float child_box_bottom = child_box.effective_offset().y() + child_box.height() + child_box.box_model().margin_box().bottom;
  177. if (!top.has_value() || child_box_top < top.value())
  178. top = child_box_top;
  179. if (!bottom.has_value() || child_box_bottom > bottom.value())
  180. bottom = child_box_bottom;
  181. return IterationDecision::Continue;
  182. });
  183. // In addition, if the element has any floating descendants
  184. // whose bottom margin edge is below the element's bottom content edge,
  185. // then the height is increased to include those edges.
  186. box.for_each_child_of_type<Box>([&](Layout::Box& child_box) {
  187. if (!child_box.is_floating())
  188. return IterationDecision::Continue;
  189. float child_box_bottom = child_box.effective_offset().y() + child_box.height();
  190. if (!bottom.has_value() || child_box_bottom > bottom.value())
  191. bottom = child_box_bottom;
  192. return IterationDecision::Continue;
  193. });
  194. }
  195. return bottom.value_or(0) - top.value_or(0);
  196. }
  197. float FormattingContext::tentative_width_for_replaced_element(const ReplacedBox& box, const CSS::Length& width)
  198. {
  199. auto& containing_block = *box.containing_block();
  200. auto specified_height = box.computed_values().height().resolved_or_auto(box, containing_block.height());
  201. float used_width = width.to_px(box);
  202. // If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic width,
  203. // then that intrinsic width is the used value of 'width'.
  204. if (specified_height.is_auto() && width.is_auto() && box.has_intrinsic_width()) {
  205. used_width = box.intrinsic_width();
  206. }
  207. // If 'height' and 'width' both have computed values of 'auto' and the element has no intrinsic width,
  208. // but does have an intrinsic height and intrinsic ratio;
  209. // or if 'width' has a computed value of 'auto',
  210. // 'height' has some other computed value, and the element does have an intrinsic ratio; then the used value of 'width' is:
  211. //
  212. // (used height) * (intrinsic ratio)
  213. else if ((specified_height.is_auto() && width.is_auto() && !box.has_intrinsic_width() && box.has_intrinsic_height() && box.has_intrinsic_ratio()) || (width.is_auto() && box.has_intrinsic_ratio())) {
  214. used_width = compute_height_for_replaced_element(box) * box.intrinsic_ratio();
  215. }
  216. else if (width.is_auto() && box.has_intrinsic_width()) {
  217. used_width = box.intrinsic_width();
  218. }
  219. else if (width.is_auto()) {
  220. used_width = 300;
  221. }
  222. return used_width;
  223. }
  224. void FormattingContext::compute_width_for_absolutely_positioned_element(Box& box)
  225. {
  226. if (is<ReplacedBox>(box))
  227. compute_width_for_absolutely_positioned_replaced_element(verify_cast<ReplacedBox>(box));
  228. else
  229. compute_width_for_absolutely_positioned_non_replaced_element(box);
  230. }
  231. void FormattingContext::compute_height_for_absolutely_positioned_element(Box& box)
  232. {
  233. if (is<ReplacedBox>(box))
  234. compute_height_for_absolutely_positioned_replaced_element(verify_cast<ReplacedBox>(box));
  235. else
  236. compute_height_for_absolutely_positioned_non_replaced_element(box);
  237. }
  238. float FormattingContext::compute_width_for_replaced_element(const ReplacedBox& box)
  239. {
  240. // 10.3.4 Block-level, replaced elements in normal flow...
  241. // 10.3.2 Inline, replaced elements
  242. auto zero_value = CSS::Length::make_px(0);
  243. auto& containing_block = *box.containing_block();
  244. auto margin_left = box.computed_values().margin().left.resolved_or_zero(box, containing_block.width());
  245. auto margin_right = box.computed_values().margin().right.resolved_or_zero(box, containing_block.width());
  246. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  247. if (margin_left.is_auto())
  248. margin_left = zero_value;
  249. if (margin_right.is_auto())
  250. margin_right = zero_value;
  251. auto specified_width = box.computed_values().width().resolved_or_auto(box, containing_block.width());
  252. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  253. auto used_width = tentative_width_for_replaced_element(box, specified_width);
  254. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  255. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  256. auto specified_max_width = box.computed_values().max_width().resolved_or_auto(box, containing_block.width());
  257. if (!specified_max_width.is_auto()) {
  258. if (used_width > specified_max_width.to_px(box)) {
  259. used_width = tentative_width_for_replaced_element(box, specified_max_width);
  260. }
  261. }
  262. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  263. // but this time using the value of 'min-width' as the computed value for 'width'.
  264. auto specified_min_width = box.computed_values().min_width().resolved_or_auto(box, containing_block.width());
  265. if (!specified_min_width.is_auto()) {
  266. if (used_width < specified_min_width.to_px(box)) {
  267. used_width = tentative_width_for_replaced_element(box, specified_min_width);
  268. }
  269. }
  270. return used_width;
  271. }
  272. float FormattingContext::tentative_height_for_replaced_element(const ReplacedBox& box, const CSS::Length& height)
  273. {
  274. auto& containing_block = *box.containing_block();
  275. auto specified_width = box.computed_values().width().resolved_or_auto(box, containing_block.width());
  276. float used_height = height.to_px(box);
  277. // If 'height' and 'width' both have computed values of 'auto' and the element also has
  278. // an intrinsic height, then that intrinsic height is the used value of 'height'.
  279. if (specified_width.is_auto() && height.is_auto() && box.has_intrinsic_height())
  280. used_height = box.intrinsic_height();
  281. else if (height.is_auto() && box.has_intrinsic_ratio())
  282. used_height = compute_width_for_replaced_element(box) / box.intrinsic_ratio();
  283. else if (height.is_auto() && box.has_intrinsic_height())
  284. used_height = box.intrinsic_height();
  285. else if (height.is_auto())
  286. used_height = 150;
  287. return used_height;
  288. }
  289. float FormattingContext::compute_height_for_replaced_element(const ReplacedBox& box)
  290. {
  291. // 10.6.2 Inline replaced elements, block-level replaced elements in normal flow,
  292. // 'inline-block' replaced elements in normal flow and floating replaced elements
  293. auto& containing_block = *box.containing_block();
  294. auto specified_width = box.computed_values().width().resolved_or_auto(box, containing_block.width());
  295. auto specified_height = box.computed_values().height().resolved_or_auto(box, containing_block.height());
  296. float used_height = tentative_height_for_replaced_element(box, specified_height);
  297. if (specified_width.is_auto() && specified_height.is_auto() && box.has_intrinsic_ratio()) {
  298. float w = tentative_width_for_replaced_element(box, specified_width);
  299. float h = used_height;
  300. used_height = solve_replaced_size_constraint(w, h, box).height();
  301. }
  302. return used_height;
  303. }
  304. void FormattingContext::compute_width_for_absolutely_positioned_non_replaced_element(Box& box)
  305. {
  306. auto& containing_block = *box.containing_block();
  307. auto& computed_values = box.computed_values();
  308. auto zero_value = CSS::Length::make_px(0);
  309. auto margin_left = CSS::Length::make_auto();
  310. auto margin_right = CSS::Length::make_auto();
  311. const auto border_left = computed_values.border_left().width;
  312. const auto border_right = computed_values.border_right().width;
  313. const auto padding_left = computed_values.padding().left.resolved_or_zero(box, containing_block.width());
  314. const auto padding_right = computed_values.padding().right.resolved_or_zero(box, containing_block.width());
  315. auto try_compute_width = [&](const auto& a_width) {
  316. margin_left = computed_values.margin().left.resolved_or_zero(box, containing_block.width());
  317. margin_right = computed_values.margin().right.resolved_or_zero(box, containing_block.width());
  318. auto left = computed_values.offset().left.resolved_or_auto(box, containing_block.width());
  319. auto right = computed_values.offset().right.resolved_or_auto(box, containing_block.width());
  320. auto width = a_width;
  321. auto solve_for_left = [&] {
  322. 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);
  323. };
  324. auto solve_for_width = [&] {
  325. 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);
  326. };
  327. auto solve_for_right = [&] {
  328. 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);
  329. };
  330. // If all three of 'left', 'width', and 'right' are 'auto':
  331. if (left.is_auto() && width.is_auto() && right.is_auto()) {
  332. // First set any 'auto' values for 'margin-left' and 'margin-right' to 0.
  333. if (margin_left.is_auto())
  334. margin_left = CSS::Length::make_px(0);
  335. if (margin_right.is_auto())
  336. margin_right = CSS::Length::make_px(0);
  337. // Then, if the 'direction' property of the element establishing the static-position containing block
  338. // is 'ltr' set 'left' to the static position and apply rule number three below;
  339. // otherwise, set 'right' to the static position and apply rule number one below.
  340. // FIXME: This is very hackish.
  341. left = CSS::Length::make_px(0);
  342. goto Rule3;
  343. }
  344. if (!left.is_auto() && !width.is_auto() && !right.is_auto()) {
  345. // FIXME: This should be solved in a more complicated way.
  346. return width;
  347. }
  348. if (margin_left.is_auto())
  349. margin_left = CSS::Length::make_px(0);
  350. if (margin_right.is_auto())
  351. margin_right = CSS::Length::make_px(0);
  352. // 1. 'left' and 'width' are 'auto' and 'right' is not 'auto',
  353. // then the width is shrink-to-fit. Then solve for 'left'
  354. if (left.is_auto() && width.is_auto() && !right.is_auto()) {
  355. auto result = calculate_shrink_to_fit_widths(box);
  356. solve_for_left();
  357. auto available_width = solve_for_width();
  358. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  359. }
  360. // 2. 'left' and 'right' are 'auto' and 'width' is not 'auto',
  361. // then if the 'direction' property of the element establishing
  362. // the static-position containing block is 'ltr' set 'left'
  363. // to the static position, otherwise set 'right' to the static position.
  364. // Then solve for 'left' (if 'direction is 'rtl') or 'right' (if 'direction' is 'ltr').
  365. else if (left.is_auto() && right.is_auto() && !width.is_auto()) {
  366. // FIXME: Check direction
  367. // FIXME: Use the static-position containing block
  368. left = zero_value;
  369. right = solve_for_right();
  370. }
  371. // 3. 'width' and 'right' are 'auto' and 'left' is not 'auto',
  372. // then the width is shrink-to-fit. Then solve for 'right'
  373. else if (width.is_auto() && right.is_auto() && !left.is_auto()) {
  374. Rule3:
  375. auto result = calculate_shrink_to_fit_widths(box);
  376. auto available_width = solve_for_width();
  377. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  378. right = solve_for_right();
  379. }
  380. // 4. 'left' is 'auto', 'width' and 'right' are not 'auto', then solve for 'left'
  381. else if (left.is_auto() && !width.is_auto() && !right.is_auto()) {
  382. left = solve_for_left();
  383. }
  384. // 5. 'width' is 'auto', 'left' and 'right' are not 'auto', then solve for 'width'
  385. else if (width.is_auto() && !left.is_auto() && !right.is_auto()) {
  386. width = solve_for_width();
  387. }
  388. // 6. 'right' is 'auto', 'left' and 'width' are not 'auto', then solve for 'right'
  389. else if (right.is_auto() && !left.is_auto() && !width.is_auto()) {
  390. right = solve_for_right();
  391. }
  392. return width;
  393. };
  394. auto specified_width = computed_values.width().resolved_or_auto(box, containing_block.width());
  395. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  396. auto used_width = try_compute_width(specified_width);
  397. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  398. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  399. auto specified_max_width = computed_values.max_width().resolved_or_auto(box, containing_block.width());
  400. if (!specified_max_width.is_auto()) {
  401. if (used_width.to_px(box) > specified_max_width.to_px(box)) {
  402. used_width = try_compute_width(specified_max_width);
  403. }
  404. }
  405. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  406. // but this time using the value of 'min-width' as the computed value for 'width'.
  407. auto specified_min_width = computed_values.min_width().resolved_or_auto(box, containing_block.width());
  408. if (!specified_min_width.is_auto()) {
  409. if (used_width.to_px(box) < specified_min_width.to_px(box)) {
  410. used_width = try_compute_width(specified_min_width);
  411. }
  412. }
  413. box.set_width(used_width.to_px(box));
  414. box.box_model().margin.left = margin_left.to_px(box);
  415. box.box_model().margin.right = margin_right.to_px(box);
  416. box.box_model().border.left = border_left;
  417. box.box_model().border.right = border_right;
  418. box.box_model().padding.left = padding_left.to_px(box);
  419. box.box_model().padding.right = padding_right.to_px(box);
  420. }
  421. void FormattingContext::compute_width_for_absolutely_positioned_replaced_element(ReplacedBox& box)
  422. {
  423. // 10.3.8 Absolutely positioned, replaced elements
  424. // The used value of 'width' is determined as for inline replaced elements.
  425. box.prepare_for_replaced_layout();
  426. box.set_width(compute_width_for_replaced_element(box));
  427. }
  428. void FormattingContext::compute_height_for_absolutely_positioned_non_replaced_element(Box& box)
  429. {
  430. auto& computed_values = box.computed_values();
  431. auto& containing_block = *box.containing_block();
  432. CSS::Length specified_top = computed_values.offset().top.resolved_or_auto(box, containing_block.height());
  433. CSS::Length specified_bottom = computed_values.offset().bottom.resolved_or_auto(box, containing_block.height());
  434. CSS::Length specified_height;
  435. if (computed_values.height().is_percentage() && !containing_block.computed_values().height().is_absolute()) {
  436. specified_height = CSS::Length::make_auto();
  437. } else {
  438. specified_height = computed_values.height().resolved_or_auto(box, containing_block.height());
  439. }
  440. auto specified_max_height = computed_values.max_height().resolved_or_auto(box, containing_block.height());
  441. auto specified_min_height = computed_values.min_height().resolved_or_auto(box, containing_block.height());
  442. box.box_model().margin.top = computed_values.margin().top.resolved_or_zero(box, containing_block.width()).to_px(box);
  443. box.box_model().margin.bottom = computed_values.margin().bottom.resolved_or_zero(box, containing_block.width()).to_px(box);
  444. box.box_model().border.top = computed_values.border_top().width;
  445. box.box_model().border.bottom = computed_values.border_bottom().width;
  446. box.box_model().padding.top = computed_values.padding().top.resolved_or_zero(box, containing_block.width()).to_px(box);
  447. box.box_model().padding.bottom = computed_values.padding().bottom.resolved_or_zero(box, containing_block.width()).to_px(box);
  448. if (specified_height.is_auto() && !specified_top.is_auto() && specified_bottom.is_auto()) {
  449. const auto& margin = box.box_model().margin;
  450. const auto& padding = box.box_model().padding;
  451. const auto& border = box.box_model().border;
  452. specified_height = CSS::Length(compute_auto_height_for_block_level_element(box), CSS::Length::Type::Px);
  453. box.box_model().offset.bottom = containing_block.height() - specified_height.to_px(box) - specified_top.to_px(box) - margin.top - padding.top - border.top - margin.bottom - padding.bottom - border.bottom;
  454. }
  455. else if (specified_height.is_auto() && !specified_top.is_auto() && !specified_bottom.is_auto()) {
  456. const auto& margin = box.box_model().margin;
  457. const auto& padding = box.box_model().padding;
  458. const auto& border = box.box_model().border;
  459. specified_height = CSS::Length(containing_block.height() - specified_top.to_px(box) - margin.top - padding.top - border.top - specified_bottom.to_px(box) - margin.bottom - padding.bottom - border.bottom, CSS::Length::Type::Px);
  460. }
  461. if (!specified_height.is_auto()) {
  462. float used_height = specified_height.to_px(box);
  463. if (!specified_max_height.is_auto())
  464. used_height = min(used_height, specified_max_height.to_px(box));
  465. if (!specified_min_height.is_auto())
  466. used_height = max(used_height, specified_min_height.to_px(box));
  467. box.set_height(used_height);
  468. }
  469. }
  470. void FormattingContext::layout_absolutely_positioned_element(Box& box)
  471. {
  472. auto& containing_block = *box.containing_block();
  473. auto& box_model = box.box_model();
  474. auto specified_width = box.computed_values().width().resolved_or_auto(box, containing_block.width());
  475. compute_width_for_absolutely_positioned_element(box);
  476. layout_inside(box, LayoutMode::Default);
  477. compute_height_for_absolutely_positioned_element(box);
  478. box_model.margin.left = box.computed_values().margin().left.resolved_or_auto(box, containing_block.width()).to_px(box);
  479. box_model.margin.top = box.computed_values().margin().top.resolved_or_auto(box, containing_block.height()).to_px(box);
  480. box_model.margin.right = box.computed_values().margin().right.resolved_or_auto(box, containing_block.width()).to_px(box);
  481. box_model.margin.bottom = box.computed_values().margin().bottom.resolved_or_auto(box, containing_block.height()).to_px(box);
  482. box_model.border.left = box.computed_values().border_left().width;
  483. box_model.border.right = box.computed_values().border_right().width;
  484. box_model.border.top = box.computed_values().border_top().width;
  485. box_model.border.bottom = box.computed_values().border_bottom().width;
  486. box_model.offset.left = box.computed_values().offset().left.resolved_or_auto(box, containing_block.width()).to_px(box);
  487. box_model.offset.top = box.computed_values().offset().top.resolved_or_auto(box, containing_block.height()).to_px(box);
  488. box_model.offset.right = box.computed_values().offset().right.resolved_or_auto(box, containing_block.width()).to_px(box);
  489. box_model.offset.bottom = box.computed_values().offset().bottom.resolved_or_auto(box, containing_block.height()).to_px(box);
  490. if (box.computed_values().offset().left.is_auto() && specified_width.is_auto() && box.computed_values().offset().right.is_auto()) {
  491. if (box.computed_values().margin().left.is_auto())
  492. box_model.margin.left = 0;
  493. if (box.computed_values().margin().right.is_auto())
  494. box_model.margin.right = 0;
  495. }
  496. Gfx::FloatPoint used_offset;
  497. if (!box.computed_values().offset().left.is_auto()) {
  498. float x_offset = box_model.offset.left
  499. + box_model.border_box().left;
  500. used_offset.set_x(x_offset + box_model.margin.left);
  501. } else if (!box.computed_values().offset().right.is_auto()) {
  502. float x_offset = 0
  503. - box_model.offset.right
  504. - box_model.border_box().right;
  505. used_offset.set_x(containing_block.width() + x_offset - box.width() - box_model.margin.right);
  506. } else {
  507. float x_offset = box_model.margin_box().left;
  508. used_offset.set_x(x_offset);
  509. }
  510. if (!box.computed_values().offset().top.is_auto()) {
  511. float y_offset = box_model.offset.top
  512. + box_model.border_box().top;
  513. used_offset.set_y(y_offset + box_model.margin.top);
  514. } else if (!box.computed_values().offset().bottom.is_auto()) {
  515. float y_offset = 0
  516. - box_model.offset.bottom
  517. - box_model.border_box().bottom;
  518. used_offset.set_y(containing_block.height() + y_offset - box.height() - box_model.margin.bottom);
  519. } else {
  520. float y_offset = box_model.margin_box().top;
  521. used_offset.set_y(y_offset);
  522. }
  523. box.set_offset(used_offset);
  524. }
  525. void FormattingContext::compute_height_for_absolutely_positioned_replaced_element(ReplacedBox& box)
  526. {
  527. // 10.6.5 Absolutely positioned, replaced elements
  528. // The used value of 'height' is determined as for inline replaced elements.
  529. box.set_height(compute_height_for_replaced_element(box));
  530. }
  531. }