FormattingContext.cpp 27 KB

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