FormattingContext.cpp 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /*
  2. * Copyright (c) 2020-2022, 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/GridFormattingContext.h>
  12. #include <LibWeb/Layout/InitialContainingBlock.h>
  13. #include <LibWeb/Layout/ReplacedBox.h>
  14. #include <LibWeb/Layout/SVGFormattingContext.h>
  15. #include <LibWeb/Layout/SVGSVGBox.h>
  16. #include <LibWeb/Layout/TableBox.h>
  17. #include <LibWeb/Layout/TableCellBox.h>
  18. #include <LibWeb/Layout/TableFormattingContext.h>
  19. namespace Web::Layout {
  20. FormattingContext::FormattingContext(Type type, LayoutState& state, Box const& context_box, FormattingContext* parent)
  21. : m_type(type)
  22. , m_parent(parent)
  23. , m_context_box(context_box)
  24. , m_state(state)
  25. {
  26. }
  27. FormattingContext::~FormattingContext() = default;
  28. // https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context
  29. bool FormattingContext::creates_block_formatting_context(Box const& box)
  30. {
  31. // NOTE: Replaced elements never create a BFC.
  32. if (box.is_replaced_box())
  33. return false;
  34. // NOTE: This function uses MDN as a reference, not because it's authoritative,
  35. // but because they've gathered all the conditions in one convenient location.
  36. // The root element of the document (<html>).
  37. if (box.is_root_element())
  38. return true;
  39. // Floats (elements where float isn't none).
  40. if (box.is_floating())
  41. return true;
  42. // Absolutely positioned elements (elements where position is absolute or fixed).
  43. if (box.is_absolutely_positioned())
  44. return true;
  45. // Inline-blocks (elements with display: inline-block).
  46. if (box.display().is_inline_block())
  47. return true;
  48. // Table cells (elements with display: table-cell, which is the default for HTML table cells).
  49. if (box.display().is_table_cell())
  50. return true;
  51. // Table captions (elements with display: table-caption, which is the default for HTML table captions).
  52. if (box.display().is_table_caption())
  53. return true;
  54. // FIXME: Anonymous table cells implicitly created by the elements with display: table, table-row, table-row-group, table-header-group, table-footer-group
  55. // (which is the default for HTML tables, table rows, table bodies, table headers, and table footers, respectively), or inline-table.
  56. // Block elements where overflow has a value other than visible and clip.
  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. // display: flow-root.
  64. if (box.display().is_flow_root_inside())
  65. return true;
  66. // FIXME: Elements with contain: layout, content, or paint.
  67. if (box.parent()) {
  68. auto parent_display = box.parent()->display();
  69. // Flex items (direct children of the element with display: flex or inline-flex) if they are neither flex nor grid nor table containers themselves.
  70. if (parent_display.is_flex_inside()) {
  71. if (!box.display().is_flex_inside())
  72. return true;
  73. }
  74. // Grid items (direct children of the element with display: grid or inline-grid) if they are neither flex nor grid nor table containers themselves.
  75. if (parent_display.is_grid_inside()) {
  76. if (!box.display().is_grid_inside()) {
  77. return true;
  78. }
  79. }
  80. }
  81. // FIXME: Multicol containers (elements where column-count or column-width isn't auto, including elements with column-count: 1).
  82. // FIXME: column-span: all should always create a new formatting context, even when the column-span: all element isn't contained by a multicol container (Spec change, Chrome bug).
  83. return false;
  84. }
  85. OwnPtr<FormattingContext> FormattingContext::create_independent_formatting_context_if_needed(LayoutState& state, Box const& child_box)
  86. {
  87. if (child_box.is_replaced_box() && !child_box.can_have_children()) {
  88. // NOTE: This is a bit strange.
  89. // Basically, we create a pretend formatting context for replaced elements that does nothing.
  90. // This allows other formatting contexts to treat them like elements that actually need inside layout
  91. // without having separate code to handle replaced elements.
  92. // FIXME: Find a better abstraction for this.
  93. struct ReplacedFormattingContext : public FormattingContext {
  94. ReplacedFormattingContext(LayoutState& state, Box const& box)
  95. : FormattingContext(Type::Block, state, box)
  96. {
  97. }
  98. virtual float automatic_content_height() const override { return 0; };
  99. virtual void run(Box const&, LayoutMode, AvailableSpace const&) override { }
  100. };
  101. return make<ReplacedFormattingContext>(state, child_box);
  102. }
  103. if (!child_box.can_have_children())
  104. return {};
  105. auto child_display = child_box.display();
  106. if (is<SVGSVGBox>(child_box))
  107. return make<SVGFormattingContext>(state, child_box, this);
  108. if (child_display.is_flex_inside())
  109. return make<FlexFormattingContext>(state, child_box, this);
  110. if (creates_block_formatting_context(child_box))
  111. return make<BlockFormattingContext>(state, verify_cast<BlockContainer>(child_box), this);
  112. if (child_display.is_table_inside())
  113. return make<TableFormattingContext>(state, verify_cast<TableBox>(child_box), this);
  114. if (child_display.is_grid_inside()) {
  115. return make<GridFormattingContext>(state, verify_cast<BlockContainer>(child_box), this);
  116. }
  117. VERIFY(is_block_formatting_context());
  118. VERIFY(!child_box.children_are_inline());
  119. // The child box is a block container that doesn't create its own BFC.
  120. // It will be formatted by this BFC.
  121. if (!child_display.is_flow_inside()) {
  122. dbgln("FIXME: Child box doesn't create BFC, but inside is also not flow! display={}", child_display.to_string());
  123. // HACK: Instead of crashing, create a dummy formatting context that does nothing.
  124. // FIXME: Remove this once it's no longer needed. It currently swallows problem with standalone
  125. // table-related boxes that don't get fixed up by CSS anonymous table box generation.
  126. struct DummyFormattingContext : public FormattingContext {
  127. DummyFormattingContext(LayoutState& state, Box const& box)
  128. : FormattingContext(Type::Block, state, box)
  129. {
  130. }
  131. virtual float automatic_content_height() const override { return 0; };
  132. virtual void run(Box const&, LayoutMode, AvailableSpace const&) override { }
  133. };
  134. return make<DummyFormattingContext>(state, child_box);
  135. }
  136. VERIFY(child_box.is_block_container());
  137. VERIFY(child_display.is_flow_inside());
  138. return {};
  139. }
  140. OwnPtr<FormattingContext> FormattingContext::layout_inside(Box const& child_box, LayoutMode layout_mode, AvailableSpace const& available_space)
  141. {
  142. {
  143. // OPTIMIZATION: If we're doing intrinsic sizing and `child_box` has definite size in both axes,
  144. // we don't need to layout its insides. The size is resolvable without learning
  145. // the metrics of whatever's inside the box.
  146. auto const& used_values = m_state.get(child_box);
  147. if (layout_mode == LayoutMode::IntrinsicSizing
  148. && used_values.width_constraint == SizeConstraint::None
  149. && used_values.height_constraint == SizeConstraint::None
  150. && used_values.has_definite_width()
  151. && used_values.has_definite_height()) {
  152. return nullptr;
  153. }
  154. }
  155. if (!child_box.can_have_children())
  156. return {};
  157. auto independent_formatting_context = create_independent_formatting_context_if_needed(m_state, child_box);
  158. if (independent_formatting_context)
  159. independent_formatting_context->run(child_box, layout_mode, available_space);
  160. else
  161. run(child_box, layout_mode, available_space);
  162. return independent_formatting_context;
  163. }
  164. float FormattingContext::greatest_child_width(Box const& box)
  165. {
  166. float max_width = 0;
  167. if (box.children_are_inline()) {
  168. for (auto& line_box : m_state.get(box).line_boxes) {
  169. max_width = max(max_width, line_box.width());
  170. }
  171. } else {
  172. box.for_each_child_of_type<Box>([&](Box const& child) {
  173. if (!child.is_absolutely_positioned())
  174. max_width = max(max_width, m_state.get(child).border_box_width());
  175. });
  176. }
  177. return max_width;
  178. }
  179. FormattingContext::ShrinkToFitResult FormattingContext::calculate_shrink_to_fit_widths(Box const& box)
  180. {
  181. return {
  182. .preferred_width = calculate_max_content_width(box),
  183. .preferred_minimum_width = calculate_min_content_width(box),
  184. };
  185. }
  186. static Gfx::FloatSize solve_replaced_size_constraint(LayoutState const& state, float w, float h, ReplacedBox const& box)
  187. {
  188. // 10.4 Minimum and maximum widths: 'min-width' and 'max-width'
  189. auto const& containing_block = *box.containing_block();
  190. auto const& containing_block_state = state.get(containing_block);
  191. auto width_of_containing_block = CSS::Length::make_px(containing_block_state.content_width());
  192. auto height_of_containing_block = CSS::Length::make_px(containing_block_state.content_height());
  193. auto specified_min_width = box.computed_values().min_width().is_auto() ? 0 : box.computed_values().min_width().resolved(box, width_of_containing_block).to_px(box);
  194. auto specified_max_width = box.computed_values().max_width().is_none() ? w : box.computed_values().max_width().resolved(box, width_of_containing_block).to_px(box);
  195. auto specified_min_height = box.computed_values().min_height().is_auto() ? 0 : box.computed_values().min_height().resolved(box, height_of_containing_block).to_px(box);
  196. auto specified_max_height = box.computed_values().max_height().is_none() ? h : box.computed_values().max_height().resolved(box, height_of_containing_block).to_px(box);
  197. auto min_width = min(specified_min_width, specified_max_width);
  198. auto max_width = max(specified_min_width, specified_max_width);
  199. auto min_height = min(specified_min_height, specified_max_height);
  200. auto max_height = max(specified_min_height, specified_max_height);
  201. if (w > max_width)
  202. return { w, max(max_width * h / w, min_height) };
  203. if (w < min_width)
  204. return { max_width, min(min_width * h / w, max_height) };
  205. if (h > max_height)
  206. return { max(max_height * w / h, min_width), max_height };
  207. if (h < min_height)
  208. return { min(min_height * w / h, max_width), min_height };
  209. if ((w > max_width && h > max_height) && (max_width / w < max_height / h))
  210. return { max_width, max(min_height, max_width * h / w) };
  211. if ((w > max_width && h > max_height) && (max_width / w > max_height / h))
  212. return { max(min_width, max_height * w / h), max_height };
  213. if ((w < min_width && h < min_height) && (min_width / w < min_height / h))
  214. return { min(max_width, min_height * w / h), min_height };
  215. if ((w < min_width && h < min_height) && (min_width / w > min_height / h))
  216. return { min_width, min(max_height, min_width * h / w) };
  217. if (w < min_width && h > max_height)
  218. return { min_width, max_height };
  219. if (w > max_width && h < min_height)
  220. return { max_width, min_height };
  221. return { w, h };
  222. }
  223. float FormattingContext::compute_auto_height_for_block_level_element(Box const& box, AvailableSpace const& available_space) const
  224. {
  225. if (creates_block_formatting_context(box))
  226. return compute_auto_height_for_block_formatting_context_root(verify_cast<BlockContainer>(box));
  227. auto const& box_state = m_state.get(box);
  228. auto display = box.display();
  229. if (display.is_flex_inside()) {
  230. // https://drafts.csswg.org/css-flexbox-1/#algo-main-container
  231. // NOTE: The automatic block size of a block-level flex container is its max-content size.
  232. return calculate_max_content_height(box, available_space.width);
  233. }
  234. if (display.is_grid_inside()) {
  235. // https://www.w3.org/TR/css-grid-2/#intrinsic-sizes
  236. // In both inline and block formatting contexts, the grid container’s auto block size is its
  237. // max-content size.
  238. return calculate_max_content_height(box, available_space.width);
  239. }
  240. // https://www.w3.org/TR/CSS22/visudet.html#normal-block
  241. // 10.6.3 Block-level non-replaced elements in normal flow when 'overflow' computes to 'visible'
  242. // The element's height is the distance from its top content edge to the first applicable of the following:
  243. // 1. the bottom edge of the last line box, if the box establishes a inline formatting context with one or more lines
  244. if (box.children_are_inline() && !box_state.line_boxes.is_empty())
  245. return box_state.line_boxes.last().bottom();
  246. // 2. the bottom edge of the bottom (possibly collapsed) margin of its last in-flow child, if the child's bottom margin does not collapse with the element's bottom margin
  247. // FIXME: 3. the bottom border edge of the last in-flow child whose top margin doesn't collapse with the element's bottom margin
  248. if (!box.children_are_inline()) {
  249. for (auto* child_box = box.last_child_of_type<Box>(); child_box; child_box = child_box->previous_sibling_of_type<Box>()) {
  250. if (child_box->is_absolutely_positioned() || child_box->is_floating())
  251. continue;
  252. // FIXME: This is hack. If the last child is a list-item marker box, we ignore it for purposes of height calculation.
  253. // Perhaps markers should not be considered in-flow(?) Perhaps they should always be the first child of the list-item
  254. // box instead of the last child.
  255. if (child_box->is_list_item_marker_box())
  256. continue;
  257. auto const& child_box_state = m_state.get(*child_box);
  258. // Ignore anonymous block containers with no lines. These don't count as in-flow block boxes.
  259. if (child_box->is_anonymous() && child_box->is_block_container() && child_box_state.line_boxes.is_empty())
  260. continue;
  261. // FIXME: Handle margin collapsing.
  262. return max(0.0f, child_box_state.offset.y() + child_box_state.content_height() + child_box_state.margin_box_bottom());
  263. }
  264. }
  265. // 4. zero, otherwise
  266. return 0;
  267. }
  268. // https://www.w3.org/TR/CSS22/visudet.html#root-height
  269. float FormattingContext::compute_auto_height_for_block_formatting_context_root(BlockContainer const& root) const
  270. {
  271. // 10.6.7 'Auto' heights for block formatting context roots
  272. Optional<float> top;
  273. Optional<float> bottom;
  274. if (root.children_are_inline()) {
  275. // If it only has inline-level children, the height is the distance between
  276. // the top content edge and the bottom of the bottommost line box.
  277. auto const& line_boxes = m_state.get(root).line_boxes;
  278. top = 0;
  279. if (!line_boxes.is_empty())
  280. bottom = line_boxes.last().bottom();
  281. } else {
  282. // If it has block-level children, the height is the distance between
  283. // the top margin-edge of the topmost block-level child box
  284. // and the bottom margin-edge of the bottommost block-level child box.
  285. root.for_each_child_of_type<Box>([&](Layout::Box& child_box) {
  286. // Absolutely positioned children are ignored,
  287. // and relatively positioned boxes are considered without their offset.
  288. // Note that the child box may be an anonymous block box.
  289. if (child_box.is_absolutely_positioned())
  290. return IterationDecision::Continue;
  291. // FIXME: This doesn't look right.
  292. if ((root.computed_values().overflow_y() == CSS::Overflow::Visible) && child_box.is_floating())
  293. return IterationDecision::Continue;
  294. auto const& child_box_state = m_state.get(child_box);
  295. float child_box_top = child_box_state.offset.y() - child_box_state.margin_box_top();
  296. float child_box_bottom = child_box_state.offset.y() + child_box_state.content_height() + child_box_state.margin_box_bottom();
  297. if (!top.has_value() || child_box_top < top.value())
  298. top = child_box_top;
  299. if (!bottom.has_value() || child_box_bottom > bottom.value())
  300. bottom = child_box_bottom;
  301. return IterationDecision::Continue;
  302. });
  303. }
  304. // In addition, if the element has any floating descendants
  305. // whose bottom margin edge is below the element's bottom content edge,
  306. // then the height is increased to include those edges.
  307. for (auto* floating_box : m_state.get(root).floating_descendants()) {
  308. // NOTE: Floating box coordinates are relative to their own containing block,
  309. // which may or may not be the BFC root.
  310. auto margin_box = margin_box_rect_in_ancestor_coordinate_space(*floating_box, root, m_state);
  311. float floating_box_bottom_margin_edge = margin_box.bottom() + 1;
  312. if (!bottom.has_value() || floating_box_bottom_margin_edge > bottom.value())
  313. bottom = floating_box_bottom_margin_edge;
  314. }
  315. return max(0.0f, bottom.value_or(0) - top.value_or(0));
  316. }
  317. // 10.3.2 Inline, replaced elements, https://www.w3.org/TR/CSS22/visudet.html#inline-replaced-width
  318. float FormattingContext::tentative_width_for_replaced_element(LayoutState const& state, ReplacedBox const& box, CSS::Size const& computed_width, AvailableSpace const& available_space)
  319. {
  320. // Treat percentages of indefinite containing block widths as 0 (the initial width).
  321. if (computed_width.is_percentage() && !state.get(*box.containing_block()).has_definite_width())
  322. return 0;
  323. auto height_of_containing_block = CSS::Length::make_px(containing_block_height_for(box, state));
  324. auto const& computed_height = box.computed_values().height();
  325. float used_width = computed_width.resolved(box, CSS::Length::make_px(available_space.width.to_px())).to_px(box);
  326. // If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic width,
  327. // then that intrinsic width is the used value of 'width'.
  328. if (computed_height.is_auto() && computed_width.is_auto() && box.has_intrinsic_width())
  329. return box.intrinsic_width().value();
  330. // If 'height' and 'width' both have computed values of 'auto' and the element has no intrinsic width,
  331. // but does have an intrinsic height and intrinsic ratio;
  332. // or if 'width' has a computed value of 'auto',
  333. // 'height' has some other computed value, and the element does have an intrinsic ratio; then the used value of 'width' is:
  334. //
  335. // (used height) * (intrinsic ratio)
  336. if ((computed_height.is_auto() && computed_width.is_auto() && !box.has_intrinsic_width() && box.has_intrinsic_height() && box.has_intrinsic_aspect_ratio())
  337. || (computed_width.is_auto() && !computed_height.is_auto() && box.has_intrinsic_aspect_ratio())) {
  338. return compute_height_for_replaced_element(state, box, available_space) * box.intrinsic_aspect_ratio().value();
  339. }
  340. // If 'height' and 'width' both have computed values of 'auto' and the element has an intrinsic ratio but no intrinsic height or width,
  341. // then the used value of 'width' is undefined in CSS 2.2. However, it is suggested that, if the containing block's width does not itself
  342. // depend on the replaced element's width, then the used value of 'width' is calculated from the constraint equation used for block-level,
  343. // non-replaced elements in normal flow.
  344. // Otherwise, if 'width' has a computed value of 'auto', and the element has an intrinsic width, then that intrinsic width is the used value of 'width'.
  345. if (computed_width.is_auto() && box.has_intrinsic_width())
  346. return box.intrinsic_width().value();
  347. // Otherwise, if 'width' has a computed value of 'auto', but none of the conditions above are met, then the used value of 'width' becomes 300px.
  348. // If 300px is too wide to fit the device, UAs should use the width of the largest rectangle that has a 2:1 ratio and fits the device instead.
  349. if (computed_width.is_auto())
  350. return 300;
  351. return used_width;
  352. }
  353. void FormattingContext::compute_width_for_absolutely_positioned_element(Box const& box, AvailableSpace const& available_space)
  354. {
  355. if (is<ReplacedBox>(box))
  356. compute_width_for_absolutely_positioned_replaced_element(verify_cast<ReplacedBox>(box), available_space);
  357. else
  358. compute_width_for_absolutely_positioned_non_replaced_element(box, available_space);
  359. }
  360. void FormattingContext::compute_height_for_absolutely_positioned_element(Box const& box, AvailableSpace const& available_space)
  361. {
  362. if (is<ReplacedBox>(box))
  363. compute_height_for_absolutely_positioned_replaced_element(static_cast<ReplacedBox const&>(box), available_space);
  364. else
  365. compute_height_for_absolutely_positioned_non_replaced_element(box, available_space);
  366. }
  367. float FormattingContext::compute_width_for_replaced_element(LayoutState const& state, ReplacedBox const& box, AvailableSpace const& available_space)
  368. {
  369. // 10.3.4 Block-level, replaced elements in normal flow...
  370. // 10.3.2 Inline, replaced elements
  371. auto zero_value = CSS::Length::make_px(0);
  372. auto width_of_containing_block_as_length = CSS::Length::make_px(available_space.width.to_px());
  373. auto margin_left = box.computed_values().margin().left().resolved(box, width_of_containing_block_as_length).resolved(box);
  374. auto margin_right = box.computed_values().margin().right().resolved(box, width_of_containing_block_as_length).resolved(box);
  375. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  376. if (margin_left.is_auto())
  377. margin_left = zero_value;
  378. if (margin_right.is_auto())
  379. margin_right = zero_value;
  380. auto computed_width = box.computed_values().width();
  381. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  382. auto used_width = tentative_width_for_replaced_element(state, box, computed_width, available_space);
  383. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  384. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  385. auto computed_max_width = box.computed_values().max_width();
  386. if (!computed_max_width.is_none()) {
  387. if (used_width > computed_max_width.resolved(box, width_of_containing_block_as_length).to_px(box)) {
  388. used_width = tentative_width_for_replaced_element(state, box, computed_max_width, available_space);
  389. }
  390. }
  391. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  392. // but this time using the value of 'min-width' as the computed value for 'width'.
  393. auto computed_min_width = box.computed_values().min_width();
  394. if (!computed_min_width.is_auto()) {
  395. if (used_width < computed_min_width.resolved(box, width_of_containing_block_as_length).to_px(box)) {
  396. used_width = tentative_width_for_replaced_element(state, box, computed_min_width, available_space);
  397. }
  398. }
  399. return used_width;
  400. }
  401. // 10.6.2 Inline replaced elements, block-level replaced elements in normal flow, 'inline-block' replaced elements in normal flow and floating replaced elements
  402. // https://www.w3.org/TR/CSS22/visudet.html#inline-replaced-height
  403. float FormattingContext::tentative_height_for_replaced_element(LayoutState const& state, ReplacedBox const& box, CSS::Size const& computed_height, AvailableSpace const& available_space)
  404. {
  405. // Treat percentages of indefinite containing block heights as 0 (the initial height).
  406. if (computed_height.is_percentage() && !state.get(*box.containing_block()).has_definite_height())
  407. return 0;
  408. auto const& computed_width = box.computed_values().width();
  409. // If 'height' and 'width' both have computed values of 'auto' and the element also has
  410. // an intrinsic height, then that intrinsic height is the used value of 'height'.
  411. if (computed_width.is_auto() && computed_height.is_auto() && box.has_intrinsic_height())
  412. return box.intrinsic_height().value();
  413. // Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic ratio then the used value of 'height' is:
  414. //
  415. // (used width) / (intrinsic ratio)
  416. if (computed_height.is_auto() && box.has_intrinsic_aspect_ratio())
  417. return compute_width_for_replaced_element(state, box, available_space) / box.intrinsic_aspect_ratio().value();
  418. // Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic height, then that intrinsic height is the used value of 'height'.
  419. if (computed_height.is_auto() && box.has_intrinsic_height())
  420. return box.intrinsic_height().value();
  421. // Otherwise, if 'height' has a computed value of 'auto', but none of the conditions above are met,
  422. // then the used value of 'height' must be set to the height of the largest rectangle that has a 2:1 ratio, has a height not greater than 150px,
  423. // and has a width not greater than the device width.
  424. if (computed_height.is_auto())
  425. return 150;
  426. return computed_height.resolved(box, CSS::Length::make_px(available_space.height.to_px())).to_px(box);
  427. }
  428. float FormattingContext::compute_height_for_replaced_element(LayoutState const& state, ReplacedBox const& box, AvailableSpace const& available_space)
  429. {
  430. // 10.6.2 Inline replaced elements, block-level replaced elements in normal flow,
  431. // 'inline-block' replaced elements in normal flow and floating replaced elements
  432. auto width_of_containing_block_as_length = CSS::Length::make_px(available_space.width.to_px());
  433. auto height_of_containing_block_as_length = CSS::Length::make_px(available_space.height.to_px());
  434. auto computed_width = box.computed_values().width();
  435. auto computed_height = box.computed_values().height();
  436. float used_height = tentative_height_for_replaced_element(state, box, computed_height, available_space);
  437. if (computed_width.is_auto() && computed_height.is_auto() && box.has_intrinsic_aspect_ratio()) {
  438. float w = tentative_width_for_replaced_element(state, box, computed_width, available_space);
  439. float h = used_height;
  440. used_height = solve_replaced_size_constraint(state, w, h, box).height();
  441. }
  442. return used_height;
  443. }
  444. void FormattingContext::compute_width_for_absolutely_positioned_non_replaced_element(Box const& box, AvailableSpace const& available_space)
  445. {
  446. auto width_of_containing_block = available_space.width.to_px();
  447. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  448. auto& computed_values = box.computed_values();
  449. auto zero_value = CSS::Length::make_px(0);
  450. auto margin_left = CSS::Length::make_auto();
  451. auto margin_right = CSS::Length::make_auto();
  452. auto const border_left = computed_values.border_left().width;
  453. auto const border_right = computed_values.border_right().width;
  454. auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block_as_length).to_px(box);
  455. auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block_as_length).to_px(box);
  456. auto try_compute_width = [&](auto const& a_width) {
  457. margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length).resolved(box);
  458. margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length).resolved(box);
  459. auto left = computed_values.inset().left().resolved(box, width_of_containing_block_as_length).resolved(box);
  460. auto right = computed_values.inset().right().resolved(box, width_of_containing_block_as_length).resolved(box);
  461. auto width = a_width;
  462. auto solve_for_left = [&] {
  463. return CSS::Length(width_of_containing_block - margin_left.to_px(box) - border_left - padding_left - width.to_px(box) - padding_right - border_right - margin_right.to_px(box) - right.to_px(box), CSS::Length::Type::Px);
  464. };
  465. auto solve_for_width = [&] {
  466. return CSS::Length(width_of_containing_block - left.to_px(box) - margin_left.to_px(box) - border_left - padding_left - padding_right - border_right - margin_right.to_px(box) - right.to_px(box), CSS::Length::Type::Px);
  467. };
  468. auto solve_for_right = [&] {
  469. return CSS::Length(width_of_containing_block - left.to_px(box) - margin_left.to_px(box) - border_left - padding_left - width.to_px(box) - padding_right - border_right - margin_right.to_px(box), CSS::Length::Type::Px);
  470. };
  471. // If all three of 'left', 'width', and 'right' are 'auto':
  472. if (left.is_auto() && width.is_auto() && right.is_auto()) {
  473. // First set any 'auto' values for 'margin-left' and 'margin-right' to 0.
  474. if (margin_left.is_auto())
  475. margin_left = CSS::Length::make_px(0);
  476. if (margin_right.is_auto())
  477. margin_right = CSS::Length::make_px(0);
  478. // Then, if the 'direction' property of the element establishing the static-position containing block
  479. // is 'ltr' set 'left' to the static position and apply rule number three below;
  480. // otherwise, set 'right' to the static position and apply rule number one below.
  481. // FIXME: This is very hackish.
  482. left = CSS::Length::make_px(0);
  483. goto Rule3;
  484. }
  485. if (!left.is_auto() && !width.is_auto() && !right.is_auto()) {
  486. // FIXME: This should be solved in a more complicated way.
  487. return width;
  488. }
  489. if (margin_left.is_auto())
  490. margin_left = CSS::Length::make_px(0);
  491. if (margin_right.is_auto())
  492. margin_right = CSS::Length::make_px(0);
  493. // 1. 'left' and 'width' are 'auto' and 'right' is not 'auto',
  494. // then the width is shrink-to-fit. Then solve for 'left'
  495. if (left.is_auto() && width.is_auto() && !right.is_auto()) {
  496. auto result = calculate_shrink_to_fit_widths(box);
  497. solve_for_left();
  498. auto available_width = solve_for_width();
  499. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  500. }
  501. // 2. 'left' and 'right' are 'auto' and 'width' is not 'auto',
  502. // then if the 'direction' property of the element establishing
  503. // the static-position containing block is 'ltr' set 'left'
  504. // to the static position, otherwise set 'right' to the static position.
  505. // Then solve for 'left' (if 'direction is 'rtl') or 'right' (if 'direction' is 'ltr').
  506. else if (left.is_auto() && right.is_auto() && !width.is_auto()) {
  507. // FIXME: Check direction
  508. // FIXME: Use the static-position containing block
  509. left = zero_value;
  510. right = solve_for_right();
  511. }
  512. // 3. 'width' and 'right' are 'auto' and 'left' is not 'auto',
  513. // then the width is shrink-to-fit. Then solve for 'right'
  514. else if (width.is_auto() && right.is_auto() && !left.is_auto()) {
  515. Rule3:
  516. auto result = calculate_shrink_to_fit_widths(box);
  517. auto available_width = solve_for_width();
  518. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  519. right = solve_for_right();
  520. }
  521. // 4. 'left' is 'auto', 'width' and 'right' are not 'auto', then solve for 'left'
  522. else if (left.is_auto() && !width.is_auto() && !right.is_auto()) {
  523. left = solve_for_left();
  524. }
  525. // 5. 'width' is 'auto', 'left' and 'right' are not 'auto', then solve for 'width'
  526. else if (width.is_auto() && !left.is_auto() && !right.is_auto()) {
  527. width = solve_for_width();
  528. }
  529. // 6. 'right' is 'auto', 'left' and 'width' are not 'auto', then solve for 'right'
  530. else if (right.is_auto() && !left.is_auto() && !width.is_auto()) {
  531. right = solve_for_right();
  532. }
  533. return width;
  534. };
  535. auto specified_width = computed_values.width().resolved(box, width_of_containing_block_as_length).resolved(box);
  536. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  537. auto used_width = try_compute_width(specified_width);
  538. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  539. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  540. if (!computed_values.max_width().is_none()) {
  541. auto max_width = computed_values.max_width().resolved(box, width_of_containing_block_as_length).resolved(box);
  542. if (used_width.to_px(box) > max_width.to_px(box)) {
  543. used_width = try_compute_width(max_width);
  544. }
  545. }
  546. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  547. // but this time using the value of 'min-width' as the computed value for 'width'.
  548. if (!computed_values.min_width().is_auto()) {
  549. auto min_width = computed_values.min_width().resolved(box, width_of_containing_block_as_length).resolved(box);
  550. if (used_width.to_px(box) < min_width.to_px(box)) {
  551. used_width = try_compute_width(min_width);
  552. }
  553. }
  554. auto& box_state = m_state.get_mutable(box);
  555. box_state.set_content_width(used_width.to_px(box));
  556. box_state.margin_left = margin_left.to_px(box);
  557. box_state.margin_right = margin_right.to_px(box);
  558. box_state.border_left = border_left;
  559. box_state.border_right = border_right;
  560. box_state.padding_left = padding_left;
  561. box_state.padding_right = padding_right;
  562. }
  563. void FormattingContext::compute_width_for_absolutely_positioned_replaced_element(ReplacedBox const& box, AvailableSpace const& available_space)
  564. {
  565. // 10.3.8 Absolutely positioned, replaced elements
  566. // The used value of 'width' is determined as for inline replaced elements.
  567. // FIXME: This const_cast is gross.
  568. const_cast<ReplacedBox&>(box).prepare_for_replaced_layout();
  569. m_state.get_mutable(box).set_content_width(compute_width_for_replaced_element(m_state, box, available_space));
  570. }
  571. // https://drafts.csswg.org/css-position-3/#abs-non-replaced-height
  572. void FormattingContext::compute_height_for_absolutely_positioned_non_replaced_element(Box const& box, AvailableSpace const& available_space)
  573. {
  574. // 5.3. The Height Of Absolutely Positioned, Non-Replaced Elements
  575. // For absolutely positioned elements, the used values of the vertical dimensions must satisfy this constraint:
  576. // top + margin-top + border-top-width + padding-top + height + padding-bottom + border-bottom-width + margin-bottom + bottom = height of containing block
  577. auto margin_top = box.computed_values().margin().top();
  578. auto margin_bottom = box.computed_values().margin().bottom();
  579. auto top = box.computed_values().inset().top();
  580. auto bottom = box.computed_values().inset().bottom();
  581. auto height = box.computed_values().height();
  582. auto width_of_containing_block = containing_block_width_for(box);
  583. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  584. auto height_of_containing_block = available_space.height.to_px();
  585. auto height_of_containing_block_as_length = CSS::Length::make_px(height_of_containing_block);
  586. auto solve_for = [&](CSS::Length length) {
  587. return CSS::Length::make_px(
  588. height_of_containing_block
  589. - top.resolved(box, height_of_containing_block_as_length).to_px(box)
  590. - margin_top.resolved(box, width_of_containing_block_as_length).to_px(box)
  591. - box.computed_values().border_top().width
  592. - box.computed_values().padding().top().resolved(box, width_of_containing_block_as_length).to_px(box)
  593. - height.resolved(box, height_of_containing_block_as_length).to_px(box)
  594. - box.computed_values().padding().bottom().resolved(box, width_of_containing_block_as_length).to_px(box)
  595. - box.computed_values().border_bottom().width
  596. - margin_bottom.resolved(box, width_of_containing_block_as_length).to_px(box)
  597. - bottom.resolved(box, height_of_containing_block_as_length).to_px(box)
  598. + length.to_px(box));
  599. };
  600. auto solve_for_top = [&] {
  601. top = solve_for(top.resolved(box, height_of_containing_block_as_length));
  602. };
  603. auto solve_for_bottom = [&] {
  604. bottom = solve_for(bottom.resolved(box, height_of_containing_block_as_length));
  605. };
  606. auto solve_for_height = [&] {
  607. height = CSS::Size::make_length(solve_for(height.resolved(box, height_of_containing_block_as_length)));
  608. };
  609. auto solve_for_margin_top = [&] {
  610. margin_top = solve_for(margin_top.resolved(box, width_of_containing_block_as_length));
  611. };
  612. auto solve_for_margin_bottom = [&] {
  613. margin_bottom = solve_for(margin_bottom.resolved(box, width_of_containing_block_as_length));
  614. };
  615. auto solve_for_margin_top_and_margin_bottom = [&] {
  616. auto remainder = solve_for(CSS::Length::make_px(margin_top.resolved(box, width_of_containing_block_as_length).to_px(box) + margin_bottom.resolved(box, width_of_containing_block_as_length).to_px(box))).to_px(box);
  617. margin_top = CSS::Length::make_px(remainder / 2);
  618. margin_bottom = CSS::Length::make_px(remainder / 2);
  619. };
  620. // If all three of top, height, and bottom are auto:
  621. if (top.is_auto() && height.is_auto() && bottom.is_auto()) {
  622. // First set any auto values for margin-top and margin-bottom to 0,
  623. if (margin_top.is_auto())
  624. margin_top = CSS::Length::make_px(0);
  625. if (margin_bottom.is_auto())
  626. margin_bottom = CSS::Length::make_px(0);
  627. // then set top to the static position,
  628. auto static_position = calculate_static_position(box);
  629. top = CSS::Length::make_px(static_position.y());
  630. // and finally apply rule number three below.
  631. height = CSS::Size::make_px(compute_auto_height_for_block_formatting_context_root(verify_cast<BlockContainer>(box)));
  632. solve_for_bottom();
  633. }
  634. // If none of the three are auto:
  635. else if (!top.is_auto() && !height.is_auto() && !bottom.is_auto()) {
  636. // If both margin-top and margin-bottom are auto,
  637. if (margin_top.is_auto() && margin_bottom.is_auto()) {
  638. // solve the equation under the extra constraint that the two margins get equal values.
  639. solve_for_margin_top_and_margin_bottom();
  640. }
  641. // If one of margin-top or margin-bottom is auto,
  642. else if (margin_top.is_auto() || margin_bottom.is_auto()) {
  643. // solve the equation for that value.
  644. if (margin_top.is_auto())
  645. solve_for_margin_top();
  646. else
  647. solve_for_margin_bottom();
  648. }
  649. // If the values are over-constrained,
  650. else {
  651. // ignore the value for bottom and solve for that value.
  652. solve_for_bottom();
  653. }
  654. }
  655. // Otherwise,
  656. else {
  657. // set auto values for margin-top and margin-bottom to 0,
  658. if (margin_top.is_auto())
  659. margin_top = CSS::Length::make_px(0);
  660. if (margin_bottom.is_auto())
  661. margin_bottom = CSS::Length::make_px(0);
  662. // and pick one of the following six rules that apply.
  663. // 1. If top and height are auto and bottom is not auto,
  664. if (top.is_auto() && height.is_auto() && !bottom.is_auto()) {
  665. // then the height is based on the Auto heights for block formatting context roots,
  666. height = CSS::Size::make_px(compute_auto_height_for_block_formatting_context_root(verify_cast<BlockContainer>(box)));
  667. // and solve for top.
  668. solve_for_top();
  669. }
  670. // 2. If top and bottom are auto and height is not auto,
  671. else if (top.is_auto() && bottom.is_auto() && !height.is_auto()) {
  672. // then set top to the static position,
  673. top = CSS::Length::make_px(calculate_static_position(box).y());
  674. // then solve for bottom.
  675. solve_for_bottom();
  676. }
  677. // 3. If height and bottom are auto and top is not auto,
  678. else if (height.is_auto() && bottom.is_auto() && !top.is_auto()) {
  679. // then the height is based on the Auto heights for block formatting context roots,
  680. height = CSS::Size::make_px(compute_auto_height_for_block_formatting_context_root(verify_cast<BlockContainer>(box)));
  681. // and solve for bottom.
  682. solve_for_bottom();
  683. }
  684. // 4. If top is auto, height and bottom are not auto,
  685. else if (top.is_auto() && !height.is_auto() && !bottom.is_auto()) {
  686. // then solve for top.
  687. solve_for_top();
  688. }
  689. // 5. If height is auto, top and bottom are not auto,
  690. else if (height.is_auto() && !top.is_auto() && !bottom.is_auto()) {
  691. // then solve for height.
  692. solve_for_height();
  693. }
  694. // 6. If bottom is auto, top and height are not auto,
  695. else if (bottom.is_auto() && !top.is_auto() && !height.is_auto()) {
  696. // then solve for bottom.
  697. solve_for_bottom();
  698. }
  699. }
  700. auto used_height = height.resolved(box, height_of_containing_block_as_length).to_px(box);
  701. auto const& computed_min_height = box.computed_values().min_height();
  702. auto const& computed_max_height = box.computed_values().max_height();
  703. if (!computed_max_height.is_none())
  704. used_height = min(used_height, computed_max_height.resolved(box, height_of_containing_block_as_length).resolved(box).to_px(box));
  705. if (!computed_min_height.is_auto())
  706. used_height = max(used_height, computed_min_height.resolved(box, height_of_containing_block_as_length).resolved(box).to_px(box));
  707. // NOTE: The following is not directly part of any spec, but this is where we resolve
  708. // the final used values for vertical margin/border/padding.
  709. auto& box_state = m_state.get_mutable(box);
  710. box_state.margin_top = margin_top.resolved(box, width_of_containing_block_as_length).to_px(box);
  711. box_state.margin_bottom = margin_bottom.resolved(box, width_of_containing_block_as_length).to_px(box);
  712. box_state.border_top = box.computed_values().border_top().width;
  713. box_state.border_bottom = box.computed_values().border_bottom().width;
  714. box_state.padding_top = box.computed_values().padding().top().resolved(box, width_of_containing_block_as_length).to_px(box);
  715. box_state.padding_bottom = box.computed_values().padding().bottom().resolved(box, width_of_containing_block_as_length).to_px(box);
  716. // And here is where we assign the box's content height.
  717. box_state.set_content_height(used_height);
  718. }
  719. // NOTE: This is different from content_box_rect_in_ancestor_coordinate_space() as this does *not* follow the containing block chain up, but rather the parent() chain.
  720. static Gfx::FloatRect content_box_rect_in_static_position_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  721. {
  722. auto rect = content_box_rect(box, state);
  723. if (&box == &ancestor_box)
  724. return rect;
  725. for (auto const* current = box.parent(); current; current = current->parent()) {
  726. if (current == &ancestor_box)
  727. return rect;
  728. auto const& current_state = state.get(static_cast<Box const&>(*current));
  729. rect.translate_by(current_state.offset);
  730. }
  731. // If we get here, ancestor_box was not an ancestor of `box`!
  732. VERIFY_NOT_REACHED();
  733. }
  734. // https://www.w3.org/TR/css-position-3/#staticpos-rect
  735. Gfx::FloatPoint FormattingContext::calculate_static_position(Box const& box) const
  736. {
  737. // NOTE: This is very ad-hoc.
  738. // The purpose of this function is to calculate the approximate position that `box`
  739. // would have had if it were position:static.
  740. float x = 0.0f;
  741. float y = 0.0f;
  742. VERIFY(box.parent());
  743. if (box.parent()->children_are_inline()) {
  744. // We're an abspos box with inline siblings. This is gonna get messy!
  745. if (auto* sibling = box.previous_sibling()) {
  746. // Hard case: there's a previous sibling. This means there's already inline content
  747. // preceding the hypothetical static position of `box` within its containing block.
  748. // If we had been position:static, that inline content would have been wrapped in
  749. // anonymous block box, so now we get to imagine what the world might have looked like
  750. // in that scenario..
  751. // Basically, we find its last associated line box fragment and place `box` under it.
  752. // FIXME: I'm 100% sure this can be smarter, better and faster.
  753. LineBoxFragment const* last_fragment = nullptr;
  754. auto& cb_state = m_state.get(*sibling->containing_block());
  755. for (auto& line_box : cb_state.line_boxes) {
  756. for (auto& fragment : line_box.fragments()) {
  757. if (&fragment.layout_node() == sibling)
  758. last_fragment = &fragment;
  759. }
  760. }
  761. if (last_fragment) {
  762. y = last_fragment->offset().y() + last_fragment->height();
  763. }
  764. } else {
  765. // Easy case: no previous sibling, we're at the top of the containing block.
  766. }
  767. } else {
  768. // We're among block siblings, Y can be calculated easily.
  769. y = compute_box_y_position_with_respect_to_siblings(box);
  770. }
  771. auto offset_to_static_parent = content_box_rect_in_static_position_ancestor_coordinate_space(box, *box.containing_block(), m_state);
  772. return offset_to_static_parent.location().translated(x, y);
  773. }
  774. void FormattingContext::layout_absolutely_positioned_element(Box const& box, AvailableSpace const& available_space)
  775. {
  776. auto& containing_block_state = m_state.get_mutable(*box.containing_block());
  777. auto& box_state = m_state.get_mutable(box);
  778. auto width_of_containing_block = available_space.width.to_px();
  779. auto height_of_containing_block = available_space.height.to_px();
  780. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  781. auto height_of_containing_block_as_length = CSS::Length::make_px(height_of_containing_block);
  782. auto specified_width = box.computed_values().width().resolved(box, width_of_containing_block_as_length).resolved(box);
  783. compute_width_for_absolutely_positioned_element(box, available_space);
  784. auto independent_formatting_context = layout_inside(box, LayoutMode::Normal, box_state.available_inner_space_or_constraints_from(available_space));
  785. compute_height_for_absolutely_positioned_element(box, available_space);
  786. box_state.margin_left = box.computed_values().margin().left().resolved(box, width_of_containing_block_as_length).to_px(box);
  787. box_state.margin_top = box.computed_values().margin().top().resolved(box, width_of_containing_block_as_length).to_px(box);
  788. box_state.margin_right = box.computed_values().margin().right().resolved(box, width_of_containing_block_as_length).to_px(box);
  789. box_state.margin_bottom = box.computed_values().margin().bottom().resolved(box, width_of_containing_block_as_length).to_px(box);
  790. box_state.border_left = box.computed_values().border_left().width;
  791. box_state.border_right = box.computed_values().border_right().width;
  792. box_state.border_top = box.computed_values().border_top().width;
  793. box_state.border_bottom = box.computed_values().border_bottom().width;
  794. auto const& computed_left = box.computed_values().inset().left();
  795. auto const& computed_right = box.computed_values().inset().right();
  796. auto const& computed_top = box.computed_values().inset().top();
  797. auto const& computed_bottom = box.computed_values().inset().bottom();
  798. box_state.inset_left = computed_left.resolved(box, width_of_containing_block_as_length).to_px(box);
  799. box_state.inset_top = computed_top.resolved(box, height_of_containing_block_as_length).to_px(box);
  800. box_state.inset_right = computed_right.resolved(box, width_of_containing_block_as_length).to_px(box);
  801. box_state.inset_bottom = computed_bottom.resolved(box, height_of_containing_block_as_length).to_px(box);
  802. if (computed_left.is_auto() && box.computed_values().width().is_auto() && computed_right.is_auto()) {
  803. if (box.computed_values().margin().left().is_auto())
  804. box_state.margin_left = 0;
  805. if (box.computed_values().margin().right().is_auto())
  806. box_state.margin_right = 0;
  807. }
  808. auto static_position = calculate_static_position(box);
  809. Gfx::FloatPoint used_offset;
  810. if (!computed_left.is_auto()) {
  811. float x_offset = box_state.inset_left
  812. + box_state.border_box_left();
  813. used_offset.set_x(x_offset + box_state.margin_left);
  814. } else if (!computed_right.is_auto()) {
  815. float x_offset = 0
  816. - box_state.inset_right
  817. - box_state.border_box_right();
  818. used_offset.set_x(width_of_containing_block + x_offset - box_state.content_width() - box_state.margin_right);
  819. } else {
  820. float x_offset = box_state.margin_box_left()
  821. + static_position.x();
  822. used_offset.set_x(x_offset);
  823. }
  824. if (!computed_top.is_auto()) {
  825. float y_offset = box_state.inset_top
  826. + box_state.border_box_top();
  827. used_offset.set_y(y_offset + box_state.margin_top);
  828. } else if (!computed_bottom.is_auto()) {
  829. float y_offset = 0
  830. - box_state.inset_bottom
  831. - box_state.border_box_bottom();
  832. used_offset.set_y(height_of_containing_block + y_offset - box_state.content_height() - box_state.margin_bottom);
  833. } else {
  834. float y_offset = box_state.margin_box_top()
  835. + static_position.y();
  836. used_offset.set_y(y_offset);
  837. }
  838. // NOTE: Absolutely positioned boxes are relative to the *padding edge* of the containing block.
  839. used_offset.translate_by(-containing_block_state.padding_left, -containing_block_state.padding_top);
  840. box_state.set_content_offset(used_offset);
  841. if (independent_formatting_context)
  842. independent_formatting_context->parent_context_did_dimension_child_root_box();
  843. }
  844. void FormattingContext::compute_height_for_absolutely_positioned_replaced_element(ReplacedBox const& box, AvailableSpace const& available_space)
  845. {
  846. // 10.6.5 Absolutely positioned, replaced elements
  847. // The used value of 'height' is determined as for inline replaced elements.
  848. m_state.get_mutable(box).set_content_height(compute_height_for_replaced_element(m_state, box, available_space));
  849. }
  850. // https://www.w3.org/TR/css-position-3/#relpos-insets
  851. void FormattingContext::compute_inset(Box const& box)
  852. {
  853. if (box.computed_values().position() != CSS::Position::Relative)
  854. return;
  855. auto resolve_two_opposing_insets = [&](CSS::LengthPercentage const& computed_start, CSS::LengthPercentage const& computed_end, float& used_start, float& used_end, float reference_for_percentage) {
  856. auto resolved_first = computed_start.resolved(box, CSS::Length::make_px(reference_for_percentage)).resolved(box);
  857. auto resolved_second = computed_end.resolved(box, CSS::Length::make_px(reference_for_percentage)).resolved(box);
  858. if (resolved_first.is_auto() && resolved_second.is_auto()) {
  859. // If opposing inset properties in an axis both compute to auto (their initial values),
  860. // their used values are zero (i.e., the boxes stay in their original position in that axis).
  861. used_start = 0;
  862. used_end = 0;
  863. } else if (resolved_first.is_auto() || resolved_second.is_auto()) {
  864. // If only one is auto, its used value becomes the negation of the other, and the box is shifted by the specified amount.
  865. if (resolved_first.is_auto()) {
  866. used_end = resolved_second.to_px(box);
  867. used_start = 0 - used_end;
  868. } else {
  869. used_start = resolved_first.to_px(box);
  870. used_end = 0 - used_start;
  871. }
  872. } else {
  873. // If neither is auto, the position is over-constrained; (with respect to the writing mode of its containing block)
  874. // the computed end side value is ignored, and its used value becomes the negation of the start side.
  875. used_start = resolved_first.to_px(box);
  876. used_end = 0 - used_start;
  877. }
  878. };
  879. auto& box_state = m_state.get_mutable(box);
  880. auto const& computed_values = box.computed_values();
  881. // FIXME: Respect the containing block's writing-mode.
  882. resolve_two_opposing_insets(computed_values.inset().left(), computed_values.inset().right(), box_state.inset_left, box_state.inset_right, containing_block_width_for(box));
  883. resolve_two_opposing_insets(computed_values.inset().top(), computed_values.inset().bottom(), box_state.inset_top, box_state.inset_bottom, containing_block_height_for(box));
  884. }
  885. // https://drafts.csswg.org/css-sizing-3/#fit-content-size
  886. float FormattingContext::calculate_fit_content_width(Layout::Box const& box, AvailableSpace const& available_space) const
  887. {
  888. // If the available space in a given axis is definite,
  889. // equal to clamp(min-content size, stretch-fit size, max-content size)
  890. // (i.e. max(min-content size, min(max-content size, stretch-fit size))).
  891. if (available_space.width.is_definite()) {
  892. return max(calculate_min_content_width(box),
  893. min(calculate_stretch_fit_width(box, available_space.width),
  894. calculate_max_content_width(box)));
  895. }
  896. // When sizing under a min-content constraint, equal to the min-content size.
  897. if (available_space.width.is_min_content())
  898. return calculate_min_content_width(box);
  899. // Otherwise, equal to the max-content size in that axis.
  900. return calculate_max_content_width(box);
  901. }
  902. // https://drafts.csswg.org/css-sizing-3/#fit-content-size
  903. float FormattingContext::calculate_fit_content_height(Layout::Box const& box, AvailableSpace const& available_space) const
  904. {
  905. // If the available space in a given axis is definite,
  906. // equal to clamp(min-content size, stretch-fit size, max-content size)
  907. // (i.e. max(min-content size, min(max-content size, stretch-fit size))).
  908. if (available_space.height.is_definite()) {
  909. return max(calculate_min_content_height(box, available_space.width),
  910. min(calculate_stretch_fit_height(box, available_space.height),
  911. calculate_max_content_height(box, available_space.width)));
  912. }
  913. // When sizing under a min-content constraint, equal to the min-content size.
  914. if (available_space.height.is_min_content())
  915. return calculate_min_content_height(box, available_space.width);
  916. // Otherwise, equal to the max-content size in that axis.
  917. return calculate_max_content_height(box, available_space.width);
  918. }
  919. float FormattingContext::calculate_min_content_width(Layout::Box const& box) const
  920. {
  921. if (box.has_intrinsic_width())
  922. return *box.intrinsic_width();
  923. auto& root_state = m_state.m_root;
  924. auto& cache = *root_state.intrinsic_sizes.ensure(&box, [] { return adopt_own(*new LayoutState::IntrinsicSizes); });
  925. if (cache.min_content_width.has_value())
  926. return *cache.min_content_width;
  927. LayoutState throwaway_state(&m_state);
  928. auto& box_state = throwaway_state.get_mutable(box);
  929. box_state.width_constraint = SizeConstraint::MinContent;
  930. auto context = const_cast<FormattingContext*>(this)->create_independent_formatting_context_if_needed(throwaway_state, box);
  931. VERIFY(context);
  932. auto available_width = AvailableSize::make_min_content();
  933. auto available_height = AvailableSize::make_indefinite();
  934. context->run(box, LayoutMode::IntrinsicSizing, AvailableSpace(available_width, available_height));
  935. if (context->type() == FormattingContext::Type::Flex) {
  936. cache.min_content_width = box_state.content_width();
  937. } else {
  938. cache.min_content_width = context->greatest_child_width(box);
  939. }
  940. if (!isfinite(*cache.min_content_width)) {
  941. // HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
  942. dbgln("FIXME: Calculated non-finite min-content width for {}", box.debug_description());
  943. cache.min_content_width = 0;
  944. }
  945. return *cache.min_content_width;
  946. }
  947. float FormattingContext::calculate_max_content_width(Layout::Box const& box) const
  948. {
  949. if (box.has_intrinsic_width())
  950. return *box.intrinsic_width();
  951. auto& root_state = m_state.m_root;
  952. auto& cache = *root_state.intrinsic_sizes.ensure(&box, [] { return adopt_own(*new LayoutState::IntrinsicSizes); });
  953. if (cache.max_content_width.has_value())
  954. return *cache.max_content_width;
  955. LayoutState throwaway_state(&m_state);
  956. auto& box_state = throwaway_state.get_mutable(box);
  957. box_state.width_constraint = SizeConstraint::MaxContent;
  958. auto context = const_cast<FormattingContext*>(this)->create_independent_formatting_context_if_needed(throwaway_state, box);
  959. VERIFY(context);
  960. auto available_width = AvailableSize::make_max_content();
  961. auto available_height = AvailableSize::make_indefinite();
  962. context->run(box, LayoutMode::IntrinsicSizing, AvailableSpace(available_width, available_height));
  963. if (context->type() == FormattingContext::Type::Flex) {
  964. cache.max_content_width = box_state.content_width();
  965. } else {
  966. cache.max_content_width = context->greatest_child_width(box);
  967. }
  968. if (!isfinite(*cache.max_content_width)) {
  969. // HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
  970. dbgln("FIXME: Calculated non-finite max-content width for {}", box.debug_description());
  971. cache.max_content_width = 0;
  972. }
  973. return *cache.max_content_width;
  974. }
  975. float FormattingContext::calculate_min_content_height(Layout::Box const& box, AvailableSize const& available_width) const
  976. {
  977. if (box.has_intrinsic_height())
  978. return *box.intrinsic_height();
  979. bool is_cacheable = available_width.is_definite() || available_width.is_intrinsic_sizing_constraint();
  980. Optional<float>* cache_slot = nullptr;
  981. if (is_cacheable) {
  982. auto& root_state = m_state.m_root;
  983. auto& cache = *root_state.intrinsic_sizes.ensure(&box, [] { return adopt_own(*new LayoutState::IntrinsicSizes); });
  984. if (available_width.is_definite()) {
  985. cache_slot = &cache.min_content_height_with_definite_available_width.ensure(available_width.to_px());
  986. } else if (available_width.is_min_content()) {
  987. cache_slot = &cache.min_content_height_with_min_content_available_width;
  988. } else if (available_width.is_max_content()) {
  989. cache_slot = &cache.min_content_height_with_max_content_available_width;
  990. }
  991. }
  992. if (cache_slot && cache_slot->has_value())
  993. return cache_slot->value();
  994. LayoutState throwaway_state(&m_state);
  995. auto& box_state = throwaway_state.get_mutable(box);
  996. box_state.height_constraint = SizeConstraint::MinContent;
  997. auto context = const_cast<FormattingContext*>(this)->create_independent_formatting_context_if_needed(throwaway_state, box);
  998. VERIFY(context);
  999. context->run(box, LayoutMode::IntrinsicSizing, AvailableSpace(available_width, AvailableSize::make_min_content()));
  1000. auto min_content_height = context->automatic_content_height();
  1001. if (!isfinite(min_content_height)) {
  1002. // HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
  1003. dbgln("FIXME: Calculated non-finite min-content height for {}", box.debug_description());
  1004. min_content_height = 0;
  1005. }
  1006. if (cache_slot) {
  1007. *cache_slot = min_content_height;
  1008. }
  1009. return min_content_height;
  1010. }
  1011. float FormattingContext::calculate_max_content_height(Layout::Box const& box, AvailableSize const& available_width) const
  1012. {
  1013. if (box.has_intrinsic_height())
  1014. return *box.intrinsic_height();
  1015. bool is_cacheable = available_width.is_definite() || available_width.is_intrinsic_sizing_constraint();
  1016. Optional<float>* cache_slot = nullptr;
  1017. if (is_cacheable) {
  1018. auto& root_state = m_state.m_root;
  1019. auto& cache = *root_state.intrinsic_sizes.ensure(&box, [] { return adopt_own(*new LayoutState::IntrinsicSizes); });
  1020. if (available_width.is_definite()) {
  1021. cache_slot = &cache.max_content_height_with_definite_available_width.ensure(available_width.to_px());
  1022. } else if (available_width.is_min_content()) {
  1023. cache_slot = &cache.max_content_height_with_min_content_available_width;
  1024. } else if (available_width.is_max_content()) {
  1025. cache_slot = &cache.max_content_height_with_max_content_available_width;
  1026. }
  1027. }
  1028. if (cache_slot && cache_slot->has_value())
  1029. return cache_slot->value();
  1030. LayoutState throwaway_state(&m_state);
  1031. auto& box_state = throwaway_state.get_mutable(box);
  1032. box_state.height_constraint = SizeConstraint::MaxContent;
  1033. auto context = const_cast<FormattingContext*>(this)->create_independent_formatting_context_if_needed(throwaway_state, box);
  1034. VERIFY(context);
  1035. context->run(box, LayoutMode::IntrinsicSizing, AvailableSpace(available_width, AvailableSize::make_max_content()));
  1036. auto max_content_height = context->automatic_content_height();
  1037. if (!isfinite(max_content_height)) {
  1038. // HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
  1039. dbgln("FIXME: Calculated non-finite max-content height for {}", box.debug_description());
  1040. max_content_height = 0;
  1041. }
  1042. if (cache_slot) {
  1043. *cache_slot = max_content_height;
  1044. }
  1045. return max_content_height;
  1046. }
  1047. float FormattingContext::containing_block_width_for(Box const& box, LayoutState const& state)
  1048. {
  1049. auto& containing_block_state = state.get(*box.containing_block());
  1050. auto& box_state = state.get(box);
  1051. switch (box_state.width_constraint) {
  1052. case SizeConstraint::MinContent:
  1053. return 0;
  1054. case SizeConstraint::MaxContent:
  1055. return INFINITY;
  1056. case SizeConstraint::None:
  1057. return containing_block_state.content_width();
  1058. }
  1059. VERIFY_NOT_REACHED();
  1060. }
  1061. float FormattingContext::containing_block_height_for(Box const& box, LayoutState const& state)
  1062. {
  1063. auto& containing_block_state = state.get(*box.containing_block());
  1064. auto& box_state = state.get(box);
  1065. switch (box_state.height_constraint) {
  1066. case SizeConstraint::MinContent:
  1067. return 0;
  1068. case SizeConstraint::MaxContent:
  1069. return INFINITY;
  1070. case SizeConstraint::None:
  1071. return containing_block_state.content_height();
  1072. }
  1073. VERIFY_NOT_REACHED();
  1074. }
  1075. static Box const* previous_block_level_sibling(Box const& box)
  1076. {
  1077. for (auto* sibling = box.previous_sibling_of_type<Box>(); sibling; sibling = sibling->previous_sibling_of_type<Box>()) {
  1078. if (sibling->display().is_block_outside())
  1079. return sibling;
  1080. }
  1081. return nullptr;
  1082. }
  1083. float FormattingContext::compute_box_y_position_with_respect_to_siblings(Box const& box) const
  1084. {
  1085. auto const& box_state = m_state.get(box);
  1086. float y = box_state.border_box_top();
  1087. Vector<float> collapsible_margins;
  1088. auto* relevant_sibling = previous_block_level_sibling(box);
  1089. while (relevant_sibling != nullptr) {
  1090. if (!relevant_sibling->is_absolutely_positioned() && !relevant_sibling->is_floating()) {
  1091. auto const& relevant_sibling_state = m_state.get(*relevant_sibling);
  1092. collapsible_margins.append(relevant_sibling_state.margin_bottom);
  1093. // NOTE: Empty (0-height) preceding siblings have their margins collapsed with *their* preceding sibling, etc.
  1094. if (relevant_sibling_state.border_box_height() > 0)
  1095. break;
  1096. collapsible_margins.append(relevant_sibling_state.margin_top);
  1097. }
  1098. relevant_sibling = previous_block_level_sibling(*relevant_sibling);
  1099. }
  1100. if (relevant_sibling) {
  1101. // Collapse top margin with the collapsed margin(s) of preceding siblings.
  1102. collapsible_margins.append(box_state.margin_top);
  1103. float smallest_margin = 0;
  1104. float largest_margin = 0;
  1105. size_t negative_margin_count = 0;
  1106. for (auto margin : collapsible_margins) {
  1107. if (margin < 0)
  1108. ++negative_margin_count;
  1109. largest_margin = max(largest_margin, margin);
  1110. smallest_margin = min(smallest_margin, margin);
  1111. }
  1112. float collapsed_margin = 0;
  1113. if (negative_margin_count == collapsible_margins.size()) {
  1114. // When all margins are negative, the size of the collapsed margin is the smallest (most negative) margin.
  1115. collapsed_margin = smallest_margin;
  1116. } else if (negative_margin_count > 0) {
  1117. // When negative margins are involved, the size of the collapsed margin is the sum of the largest positive margin and the smallest (most negative) negative margin.
  1118. collapsed_margin = largest_margin + smallest_margin;
  1119. } else {
  1120. // Otherwise, collapse all the adjacent margins by using only the largest one.
  1121. collapsed_margin = largest_margin;
  1122. }
  1123. auto const& relevant_sibling_state = m_state.get(*relevant_sibling);
  1124. return y + relevant_sibling_state.offset.y()
  1125. + relevant_sibling_state.content_height()
  1126. + relevant_sibling_state.border_box_bottom()
  1127. + collapsed_margin;
  1128. } else {
  1129. return y + box_state.margin_top;
  1130. }
  1131. }
  1132. // https://drafts.csswg.org/css-sizing-3/#stretch-fit-size
  1133. float FormattingContext::calculate_stretch_fit_width(Box const& box, AvailableSize const& available_width) const
  1134. {
  1135. // The size a box would take if its outer size filled the available space in the given axis;
  1136. // in other words, the stretch fit into the available space, if that is definite.
  1137. // Undefined if the available space is indefinite.
  1138. auto const& box_state = m_state.get(box);
  1139. return available_width.to_px()
  1140. - box_state.margin_left
  1141. - box_state.margin_right
  1142. - box_state.padding_left
  1143. - box_state.padding_right
  1144. - box_state.border_left
  1145. - box_state.border_right;
  1146. }
  1147. // https://drafts.csswg.org/css-sizing-3/#stretch-fit-size
  1148. float FormattingContext::calculate_stretch_fit_height(Box const& box, AvailableSize const& available_height) const
  1149. {
  1150. // The size a box would take if its outer size filled the available space in the given axis;
  1151. // in other words, the stretch fit into the available space, if that is definite.
  1152. // Undefined if the available space is indefinite.
  1153. auto const& box_state = m_state.get(box);
  1154. return available_height.to_px()
  1155. - box_state.margin_top
  1156. - box_state.margin_bottom
  1157. - box_state.padding_top
  1158. - box_state.padding_bottom
  1159. - box_state.border_top
  1160. - box_state.border_bottom;
  1161. }
  1162. bool FormattingContext::should_treat_width_as_auto(Box const& box, AvailableSpace const& available_space)
  1163. {
  1164. return box.computed_values().width().is_auto()
  1165. || (box.computed_values().width().contains_percentage() && !available_space.width.is_definite());
  1166. }
  1167. bool FormattingContext::should_treat_height_as_auto(Box const& box, AvailableSpace const& available_space)
  1168. {
  1169. return box.computed_values().height().is_auto()
  1170. || (box.computed_values().height().contains_percentage() && !available_space.height.is_definite());
  1171. }
  1172. }