FormattingContext.cpp 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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. // https://www.w3.org/TR/CSS22/visudet.html#normal-block
  235. // 10.6.3 Block-level non-replaced elements in normal flow when 'overflow' computes to 'visible'
  236. // The element's height is the distance from its top content edge to the first applicable of the following:
  237. // 1. the bottom edge of the last line box, if the box establishes a inline formatting context with one or more lines
  238. if (box.children_are_inline() && !box_state.line_boxes.is_empty())
  239. return box_state.line_boxes.last().bottom();
  240. // 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
  241. // FIXME: 3. the bottom border edge of the last in-flow child whose top margin doesn't collapse with the element's bottom margin
  242. if (!box.children_are_inline()) {
  243. for (auto* child_box = box.last_child_of_type<Box>(); child_box; child_box = child_box->previous_sibling_of_type<Box>()) {
  244. if (child_box->is_absolutely_positioned() || child_box->is_floating())
  245. continue;
  246. // FIXME: This is hack. If the last child is a list-item marker box, we ignore it for purposes of height calculation.
  247. // Perhaps markers should not be considered in-flow(?) Perhaps they should always be the first child of the list-item
  248. // box instead of the last child.
  249. if (child_box->is_list_item_marker_box())
  250. continue;
  251. auto const& child_box_state = m_state.get(*child_box);
  252. // Ignore anonymous block containers with no lines. These don't count as in-flow block boxes.
  253. if (child_box->is_anonymous() && child_box->is_block_container() && child_box_state.line_boxes.is_empty())
  254. continue;
  255. // FIXME: Handle margin collapsing.
  256. return max(0.0f, child_box_state.offset.y() + child_box_state.content_height() + child_box_state.margin_box_bottom());
  257. }
  258. }
  259. // 4. zero, otherwise
  260. return 0;
  261. }
  262. // https://www.w3.org/TR/CSS22/visudet.html#root-height
  263. float FormattingContext::compute_auto_height_for_block_formatting_context_root(BlockContainer const& root) const
  264. {
  265. // 10.6.7 'Auto' heights for block formatting context roots
  266. Optional<float> top;
  267. Optional<float> bottom;
  268. if (root.children_are_inline()) {
  269. // If it only has inline-level children, the height is the distance between
  270. // the top content edge and the bottom of the bottommost line box.
  271. auto const& line_boxes = m_state.get(root).line_boxes;
  272. top = 0;
  273. if (!line_boxes.is_empty())
  274. bottom = line_boxes.last().bottom();
  275. } else {
  276. // If it has block-level children, the height is the distance between
  277. // the top margin-edge of the topmost block-level child box
  278. // and the bottom margin-edge of the bottommost block-level child box.
  279. root.for_each_child_of_type<Box>([&](Layout::Box& child_box) {
  280. // Absolutely positioned children are ignored,
  281. // and relatively positioned boxes are considered without their offset.
  282. // Note that the child box may be an anonymous block box.
  283. if (child_box.is_absolutely_positioned())
  284. return IterationDecision::Continue;
  285. // FIXME: This doesn't look right.
  286. if ((root.computed_values().overflow_y() == CSS::Overflow::Visible) && child_box.is_floating())
  287. return IterationDecision::Continue;
  288. auto const& child_box_state = m_state.get(child_box);
  289. float child_box_top = child_box_state.offset.y() - child_box_state.margin_box_top();
  290. float child_box_bottom = child_box_state.offset.y() + child_box_state.content_height() + child_box_state.margin_box_bottom();
  291. if (!top.has_value() || child_box_top < top.value())
  292. top = child_box_top;
  293. if (!bottom.has_value() || child_box_bottom > bottom.value())
  294. bottom = child_box_bottom;
  295. return IterationDecision::Continue;
  296. });
  297. }
  298. // In addition, if the element has any floating descendants
  299. // whose bottom margin edge is below the element's bottom content edge,
  300. // then the height is increased to include those edges.
  301. for (auto* floating_box : m_state.get(root).floating_descendants()) {
  302. // NOTE: Floating box coordinates are relative to their own containing block,
  303. // which may or may not be the BFC root.
  304. auto margin_box = margin_box_rect_in_ancestor_coordinate_space(*floating_box, root, m_state);
  305. float floating_box_bottom_margin_edge = margin_box.bottom() + 1;
  306. if (!bottom.has_value() || floating_box_bottom_margin_edge > bottom.value())
  307. bottom = floating_box_bottom_margin_edge;
  308. }
  309. return max(0.0f, bottom.value_or(0) - top.value_or(0));
  310. }
  311. // 10.3.2 Inline, replaced elements, https://www.w3.org/TR/CSS22/visudet.html#inline-replaced-width
  312. float FormattingContext::tentative_width_for_replaced_element(LayoutState const& state, ReplacedBox const& box, CSS::Size const& computed_width, AvailableSpace const& available_space)
  313. {
  314. // Treat percentages of indefinite containing block widths as 0 (the initial width).
  315. if (computed_width.is_percentage() && !state.get(*box.containing_block()).has_definite_width())
  316. return 0;
  317. auto height_of_containing_block = CSS::Length::make_px(containing_block_height_for(box, state));
  318. auto const& computed_height = box.computed_values().height();
  319. float used_width = computed_width.resolved(box, CSS::Length::make_px(available_space.width.to_px())).to_px(box);
  320. // If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic width,
  321. // then that intrinsic width is the used value of 'width'.
  322. if (computed_height.is_auto() && computed_width.is_auto() && box.has_intrinsic_width())
  323. return box.intrinsic_width().value();
  324. // If 'height' and 'width' both have computed values of 'auto' and the element has no intrinsic width,
  325. // but does have an intrinsic height and intrinsic ratio;
  326. // or if 'width' has a computed value of 'auto',
  327. // 'height' has some other computed value, and the element does have an intrinsic ratio; then the used value of 'width' is:
  328. //
  329. // (used height) * (intrinsic ratio)
  330. if ((computed_height.is_auto() && computed_width.is_auto() && !box.has_intrinsic_width() && box.has_intrinsic_height() && box.has_intrinsic_aspect_ratio())
  331. || (computed_width.is_auto() && !computed_height.is_auto() && box.has_intrinsic_aspect_ratio())) {
  332. return compute_height_for_replaced_element(state, box, available_space) * box.intrinsic_aspect_ratio().value();
  333. }
  334. // If 'height' and 'width' both have computed values of 'auto' and the element has an intrinsic ratio but no intrinsic height or width,
  335. // 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
  336. // depend on the replaced element's width, then the used value of 'width' is calculated from the constraint equation used for block-level,
  337. // non-replaced elements in normal flow.
  338. // 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'.
  339. if (computed_width.is_auto() && box.has_intrinsic_width())
  340. return box.intrinsic_width().value();
  341. // 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.
  342. // 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.
  343. if (computed_width.is_auto())
  344. return 300;
  345. return used_width;
  346. }
  347. void FormattingContext::compute_width_for_absolutely_positioned_element(Box const& box, AvailableSpace const& available_space)
  348. {
  349. if (is<ReplacedBox>(box))
  350. compute_width_for_absolutely_positioned_replaced_element(verify_cast<ReplacedBox>(box), available_space);
  351. else
  352. compute_width_for_absolutely_positioned_non_replaced_element(box, available_space);
  353. }
  354. void FormattingContext::compute_height_for_absolutely_positioned_element(Box const& box, AvailableSpace const& available_space)
  355. {
  356. if (is<ReplacedBox>(box))
  357. compute_height_for_absolutely_positioned_replaced_element(static_cast<ReplacedBox const&>(box), available_space);
  358. else
  359. compute_height_for_absolutely_positioned_non_replaced_element(box, available_space);
  360. }
  361. float FormattingContext::compute_width_for_replaced_element(LayoutState const& state, ReplacedBox const& box, AvailableSpace const& available_space)
  362. {
  363. // 10.3.4 Block-level, replaced elements in normal flow...
  364. // 10.3.2 Inline, replaced elements
  365. auto zero_value = CSS::Length::make_px(0);
  366. auto width_of_containing_block_as_length = CSS::Length::make_px(available_space.width.to_px());
  367. auto margin_left = box.computed_values().margin().left().resolved(box, width_of_containing_block_as_length).resolved(box);
  368. auto margin_right = box.computed_values().margin().right().resolved(box, width_of_containing_block_as_length).resolved(box);
  369. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  370. if (margin_left.is_auto())
  371. margin_left = zero_value;
  372. if (margin_right.is_auto())
  373. margin_right = zero_value;
  374. auto computed_width = box.computed_values().width();
  375. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  376. auto used_width = tentative_width_for_replaced_element(state, box, computed_width, available_space);
  377. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  378. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  379. auto computed_max_width = box.computed_values().max_width();
  380. if (!computed_max_width.is_none()) {
  381. if (used_width > computed_max_width.resolved(box, width_of_containing_block_as_length).to_px(box)) {
  382. used_width = tentative_width_for_replaced_element(state, box, computed_max_width, available_space);
  383. }
  384. }
  385. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  386. // but this time using the value of 'min-width' as the computed value for 'width'.
  387. auto computed_min_width = box.computed_values().min_width();
  388. if (!computed_min_width.is_auto()) {
  389. if (used_width < computed_min_width.resolved(box, width_of_containing_block_as_length).to_px(box)) {
  390. used_width = tentative_width_for_replaced_element(state, box, computed_min_width, available_space);
  391. }
  392. }
  393. return used_width;
  394. }
  395. // 10.6.2 Inline replaced elements, block-level replaced elements in normal flow, 'inline-block' replaced elements in normal flow and floating replaced elements
  396. // https://www.w3.org/TR/CSS22/visudet.html#inline-replaced-height
  397. float FormattingContext::tentative_height_for_replaced_element(LayoutState const& state, ReplacedBox const& box, CSS::Size const& computed_height, AvailableSpace const& available_space)
  398. {
  399. // Treat percentages of indefinite containing block heights as 0 (the initial height).
  400. if (computed_height.is_percentage() && !state.get(*box.containing_block()).has_definite_height())
  401. return 0;
  402. auto const& computed_width = box.computed_values().width();
  403. // If 'height' and 'width' both have computed values of 'auto' and the element also has
  404. // an intrinsic height, then that intrinsic height is the used value of 'height'.
  405. if (computed_width.is_auto() && computed_height.is_auto() && box.has_intrinsic_height())
  406. return box.intrinsic_height().value();
  407. // Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic ratio then the used value of 'height' is:
  408. //
  409. // (used width) / (intrinsic ratio)
  410. if (computed_height.is_auto() && box.has_intrinsic_aspect_ratio())
  411. return compute_width_for_replaced_element(state, box, available_space) / box.intrinsic_aspect_ratio().value();
  412. // 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'.
  413. if (computed_height.is_auto() && box.has_intrinsic_height())
  414. return box.intrinsic_height().value();
  415. // Otherwise, if 'height' has a computed value of 'auto', but none of the conditions above are met,
  416. // 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,
  417. // and has a width not greater than the device width.
  418. if (computed_height.is_auto())
  419. return 150;
  420. return computed_height.resolved(box, CSS::Length::make_px(available_space.height.to_px())).to_px(box);
  421. }
  422. float FormattingContext::compute_height_for_replaced_element(LayoutState const& state, ReplacedBox const& box, AvailableSpace const& available_space)
  423. {
  424. // 10.6.2 Inline replaced elements, block-level replaced elements in normal flow,
  425. // 'inline-block' replaced elements in normal flow and floating replaced elements
  426. auto width_of_containing_block_as_length = CSS::Length::make_px(available_space.width.to_px());
  427. auto height_of_containing_block_as_length = CSS::Length::make_px(available_space.height.to_px());
  428. auto computed_width = box.computed_values().width();
  429. auto computed_height = box.computed_values().height();
  430. float used_height = tentative_height_for_replaced_element(state, box, computed_height, available_space);
  431. if (computed_width.is_auto() && computed_height.is_auto() && box.has_intrinsic_aspect_ratio()) {
  432. float w = tentative_width_for_replaced_element(state, box, computed_width, available_space);
  433. float h = used_height;
  434. used_height = solve_replaced_size_constraint(state, w, h, box).height();
  435. }
  436. return used_height;
  437. }
  438. void FormattingContext::compute_width_for_absolutely_positioned_non_replaced_element(Box const& box, AvailableSpace const& available_space)
  439. {
  440. auto width_of_containing_block = available_space.width.to_px();
  441. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  442. auto& computed_values = box.computed_values();
  443. auto zero_value = CSS::Length::make_px(0);
  444. auto margin_left = CSS::Length::make_auto();
  445. auto margin_right = CSS::Length::make_auto();
  446. auto const border_left = computed_values.border_left().width;
  447. auto const border_right = computed_values.border_right().width;
  448. auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block_as_length).to_px(box);
  449. auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block_as_length).to_px(box);
  450. auto try_compute_width = [&](auto const& a_width) {
  451. margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length).resolved(box);
  452. margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length).resolved(box);
  453. auto left = computed_values.inset().left().resolved(box, width_of_containing_block_as_length).resolved(box);
  454. auto right = computed_values.inset().right().resolved(box, width_of_containing_block_as_length).resolved(box);
  455. auto width = a_width;
  456. auto solve_for_left = [&] {
  457. 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);
  458. };
  459. auto solve_for_width = [&] {
  460. 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);
  461. };
  462. auto solve_for_right = [&] {
  463. 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);
  464. };
  465. // If all three of 'left', 'width', and 'right' are 'auto':
  466. if (left.is_auto() && width.is_auto() && right.is_auto()) {
  467. // First set any 'auto' values for 'margin-left' and 'margin-right' to 0.
  468. if (margin_left.is_auto())
  469. margin_left = CSS::Length::make_px(0);
  470. if (margin_right.is_auto())
  471. margin_right = CSS::Length::make_px(0);
  472. // Then, if the 'direction' property of the element establishing the static-position containing block
  473. // is 'ltr' set 'left' to the static position and apply rule number three below;
  474. // otherwise, set 'right' to the static position and apply rule number one below.
  475. // FIXME: This is very hackish.
  476. left = CSS::Length::make_px(0);
  477. goto Rule3;
  478. }
  479. if (!left.is_auto() && !width.is_auto() && !right.is_auto()) {
  480. // FIXME: This should be solved in a more complicated way.
  481. return width;
  482. }
  483. if (margin_left.is_auto())
  484. margin_left = CSS::Length::make_px(0);
  485. if (margin_right.is_auto())
  486. margin_right = CSS::Length::make_px(0);
  487. // 1. 'left' and 'width' are 'auto' and 'right' is not 'auto',
  488. // then the width is shrink-to-fit. Then solve for 'left'
  489. if (left.is_auto() && width.is_auto() && !right.is_auto()) {
  490. auto result = calculate_shrink_to_fit_widths(box);
  491. solve_for_left();
  492. auto available_width = solve_for_width();
  493. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  494. }
  495. // 2. 'left' and 'right' are 'auto' and 'width' is not 'auto',
  496. // then if the 'direction' property of the element establishing
  497. // the static-position containing block is 'ltr' set 'left'
  498. // to the static position, otherwise set 'right' to the static position.
  499. // Then solve for 'left' (if 'direction is 'rtl') or 'right' (if 'direction' is 'ltr').
  500. else if (left.is_auto() && right.is_auto() && !width.is_auto()) {
  501. // FIXME: Check direction
  502. // FIXME: Use the static-position containing block
  503. left = zero_value;
  504. right = solve_for_right();
  505. }
  506. // 3. 'width' and 'right' are 'auto' and 'left' is not 'auto',
  507. // then the width is shrink-to-fit. Then solve for 'right'
  508. else if (width.is_auto() && right.is_auto() && !left.is_auto()) {
  509. Rule3:
  510. auto result = calculate_shrink_to_fit_widths(box);
  511. auto available_width = solve_for_width();
  512. width = CSS::Length(min(max(result.preferred_minimum_width, available_width.to_px(box)), result.preferred_width), CSS::Length::Type::Px);
  513. right = solve_for_right();
  514. }
  515. // 4. 'left' is 'auto', 'width' and 'right' are not 'auto', then solve for 'left'
  516. else if (left.is_auto() && !width.is_auto() && !right.is_auto()) {
  517. left = solve_for_left();
  518. }
  519. // 5. 'width' is 'auto', 'left' and 'right' are not 'auto', then solve for 'width'
  520. else if (width.is_auto() && !left.is_auto() && !right.is_auto()) {
  521. width = solve_for_width();
  522. }
  523. // 6. 'right' is 'auto', 'left' and 'width' are not 'auto', then solve for 'right'
  524. else if (right.is_auto() && !left.is_auto() && !width.is_auto()) {
  525. right = solve_for_right();
  526. }
  527. return width;
  528. };
  529. auto specified_width = computed_values.width().resolved(box, width_of_containing_block_as_length).resolved(box);
  530. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  531. auto used_width = try_compute_width(specified_width);
  532. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  533. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  534. if (!computed_values.max_width().is_none()) {
  535. auto max_width = computed_values.max_width().resolved(box, width_of_containing_block_as_length).resolved(box);
  536. if (used_width.to_px(box) > max_width.to_px(box)) {
  537. used_width = try_compute_width(max_width);
  538. }
  539. }
  540. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  541. // but this time using the value of 'min-width' as the computed value for 'width'.
  542. if (!computed_values.min_width().is_auto()) {
  543. auto min_width = computed_values.min_width().resolved(box, width_of_containing_block_as_length).resolved(box);
  544. if (used_width.to_px(box) < min_width.to_px(box)) {
  545. used_width = try_compute_width(min_width);
  546. }
  547. }
  548. auto& box_state = m_state.get_mutable(box);
  549. box_state.set_content_width(used_width.to_px(box));
  550. box_state.margin_left = margin_left.to_px(box);
  551. box_state.margin_right = margin_right.to_px(box);
  552. box_state.border_left = border_left;
  553. box_state.border_right = border_right;
  554. box_state.padding_left = padding_left;
  555. box_state.padding_right = padding_right;
  556. }
  557. void FormattingContext::compute_width_for_absolutely_positioned_replaced_element(ReplacedBox const& box, AvailableSpace const& available_space)
  558. {
  559. // 10.3.8 Absolutely positioned, replaced elements
  560. // The used value of 'width' is determined as for inline replaced elements.
  561. // FIXME: This const_cast is gross.
  562. const_cast<ReplacedBox&>(box).prepare_for_replaced_layout();
  563. m_state.get_mutable(box).set_content_width(compute_width_for_replaced_element(m_state, box, available_space));
  564. }
  565. // https://drafts.csswg.org/css-position-3/#abs-non-replaced-height
  566. void FormattingContext::compute_height_for_absolutely_positioned_non_replaced_element(Box const& box, AvailableSpace const& available_space)
  567. {
  568. // 5.3. The Height Of Absolutely Positioned, Non-Replaced Elements
  569. // For absolutely positioned elements, the used values of the vertical dimensions must satisfy this constraint:
  570. // top + margin-top + border-top-width + padding-top + height + padding-bottom + border-bottom-width + margin-bottom + bottom = height of containing block
  571. auto margin_top = box.computed_values().margin().top();
  572. auto margin_bottom = box.computed_values().margin().bottom();
  573. auto top = box.computed_values().inset().top();
  574. auto bottom = box.computed_values().inset().bottom();
  575. auto height = box.computed_values().height();
  576. auto height_of_containing_block = available_space.height.to_px();
  577. auto height_of_containing_block_as_length = CSS::Length::make_px(height_of_containing_block);
  578. auto solve_for = [&](CSS::Length length) {
  579. return CSS::Length::make_px(
  580. height_of_containing_block
  581. - top.resolved(box, height_of_containing_block_as_length).to_px(box)
  582. - margin_top.length().to_px(box)
  583. - box.computed_values().border_top().width
  584. - box.computed_values().padding().top().length().to_px(box)
  585. - height.resolved(box, height_of_containing_block_as_length).to_px(box)
  586. - box.computed_values().padding().bottom().length().to_px(box)
  587. - box.computed_values().border_bottom().width
  588. - margin_bottom.length().to_px(box)
  589. - bottom.resolved(box, height_of_containing_block_as_length).to_px(box)
  590. + length.to_px(box));
  591. };
  592. auto solve_for_top = [&] {
  593. top = solve_for(top.resolved(box, height_of_containing_block_as_length));
  594. };
  595. auto solve_for_bottom = [&] {
  596. bottom = solve_for(bottom.resolved(box, height_of_containing_block_as_length));
  597. };
  598. auto solve_for_height = [&] {
  599. height = CSS::Size::make_length(solve_for(height.resolved(box, height_of_containing_block_as_length)));
  600. };
  601. auto solve_for_margin_top = [&] {
  602. margin_top = solve_for(margin_top.length());
  603. };
  604. auto solve_for_margin_bottom = [&] {
  605. margin_bottom = solve_for(margin_bottom.length());
  606. };
  607. auto solve_for_margin_top_and_margin_bottom = [&] {
  608. auto remainder = solve_for(CSS::Length::make_px(margin_top.length().to_px(box) + margin_bottom.length().to_px(box))).to_px(box);
  609. margin_top = CSS::Length::make_px(remainder / 2);
  610. margin_bottom = CSS::Length::make_px(remainder / 2);
  611. };
  612. // If all three of top, height, and bottom are auto:
  613. if (top.is_auto() && height.is_auto() && bottom.is_auto()) {
  614. // First set any auto values for margin-top and margin-bottom to 0,
  615. if (margin_top.is_auto())
  616. margin_top = CSS::Length::make_px(0);
  617. if (margin_bottom.is_auto())
  618. margin_bottom = CSS::Length::make_px(0);
  619. // then set top to the static position,
  620. auto static_position = calculate_static_position(box);
  621. top = CSS::Length::make_px(static_position.y());
  622. // and finally apply rule number three below.
  623. height = CSS::Size::make_px(compute_auto_height_for_block_formatting_context_root(verify_cast<BlockContainer>(box)));
  624. solve_for_bottom();
  625. }
  626. // If none of the three are auto:
  627. else if (!top.is_auto() && !height.is_auto() && !bottom.is_auto()) {
  628. // If both margin-top and margin-bottom are auto,
  629. if (margin_top.is_auto() && margin_bottom.is_auto()) {
  630. // solve the equation under the extra constraint that the two margins get equal values.
  631. solve_for_margin_top_and_margin_bottom();
  632. }
  633. // If one of margin-top or margin-bottom is auto,
  634. else if (margin_top.is_auto() || margin_bottom.is_auto()) {
  635. // solve the equation for that value.
  636. if (margin_top.is_auto())
  637. solve_for_margin_top();
  638. else
  639. solve_for_margin_bottom();
  640. }
  641. // If the values are over-constrained,
  642. else {
  643. // ignore the value for bottom and solve for that value.
  644. solve_for_bottom();
  645. }
  646. }
  647. // Otherwise,
  648. else {
  649. // set auto values for margin-top and margin-bottom to 0,
  650. if (margin_top.is_auto())
  651. margin_top = CSS::Length::make_px(0);
  652. if (margin_bottom.is_auto())
  653. margin_bottom = CSS::Length::make_px(0);
  654. // and pick one of the following six rules that apply.
  655. // 1. If top and height are auto and bottom is not auto,
  656. if (top.is_auto() && height.is_auto() && !bottom.is_auto()) {
  657. // then the height is based on the Auto heights for block formatting context roots,
  658. height = CSS::Size::make_px(compute_auto_height_for_block_formatting_context_root(verify_cast<BlockContainer>(box)));
  659. // and solve for top.
  660. solve_for_top();
  661. }
  662. // 2. If top and bottom are auto and height is not auto,
  663. else if (top.is_auto() && bottom.is_auto() && !height.is_auto()) {
  664. // then set top to the static position,
  665. top = CSS::Length::make_px(calculate_static_position(box).y());
  666. // then solve for bottom.
  667. solve_for_bottom();
  668. }
  669. // 3. If height and bottom are auto and top is not auto,
  670. else if (height.is_auto() && bottom.is_auto() && !top.is_auto()) {
  671. // then the height is based on the Auto heights for block formatting context roots,
  672. height = CSS::Size::make_px(compute_auto_height_for_block_formatting_context_root(verify_cast<BlockContainer>(box)));
  673. // and solve for bottom.
  674. solve_for_bottom();
  675. }
  676. // 4. If top is auto, height and bottom are not auto,
  677. else if (top.is_auto() && !height.is_auto() && !bottom.is_auto()) {
  678. // then solve for top.
  679. solve_for_top();
  680. }
  681. // 5. If height is auto, top and bottom are not auto,
  682. else if (height.is_auto() && !top.is_auto() && !bottom.is_auto()) {
  683. // then solve for height.
  684. solve_for_height();
  685. }
  686. // 6. If bottom is auto, top and height are not auto,
  687. else if (bottom.is_auto() && !top.is_auto() && !height.is_auto()) {
  688. // then solve for bottom.
  689. solve_for_bottom();
  690. }
  691. }
  692. auto used_height = height.resolved(box, height_of_containing_block_as_length).to_px(box);
  693. auto const& computed_min_height = box.computed_values().min_height();
  694. auto const& computed_max_height = box.computed_values().max_height();
  695. if (!computed_max_height.is_none())
  696. used_height = min(used_height, computed_max_height.resolved(box, height_of_containing_block_as_length).resolved(box).to_px(box));
  697. if (!computed_min_height.is_auto())
  698. used_height = max(used_height, computed_min_height.resolved(box, height_of_containing_block_as_length).resolved(box).to_px(box));
  699. // NOTE: The following is not directly part of any spec, but this is where we resolve
  700. // the final used values for vertical margin/border/padding.
  701. auto width_of_containing_block = containing_block_width_for(box);
  702. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  703. auto& box_state = m_state.get_mutable(box);
  704. box_state.margin_top = margin_top.resolved(box, width_of_containing_block_as_length).to_px(box);
  705. box_state.margin_bottom = margin_bottom.resolved(box, width_of_containing_block_as_length).to_px(box);
  706. box_state.border_top = box.computed_values().border_top().width;
  707. box_state.border_bottom = box.computed_values().border_bottom().width;
  708. box_state.padding_top = box.computed_values().padding().top().resolved(box, width_of_containing_block_as_length).to_px(box);
  709. box_state.padding_bottom = box.computed_values().padding().bottom().resolved(box, width_of_containing_block_as_length).to_px(box);
  710. // And here is where we assign the box's content height.
  711. box_state.set_content_height(used_height);
  712. }
  713. // 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.
  714. static Gfx::FloatRect content_box_rect_in_static_position_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  715. {
  716. auto rect = content_box_rect(box, state);
  717. if (&box == &ancestor_box)
  718. return rect;
  719. for (auto const* current = box.parent(); current; current = current->parent()) {
  720. if (current == &ancestor_box)
  721. return rect;
  722. auto const& current_state = state.get(static_cast<Box const&>(*current));
  723. rect.translate_by(current_state.offset);
  724. }
  725. // If we get here, ancestor_box was not an ancestor of `box`!
  726. VERIFY_NOT_REACHED();
  727. }
  728. // https://www.w3.org/TR/css-position-3/#staticpos-rect
  729. Gfx::FloatPoint FormattingContext::calculate_static_position(Box const& box) const
  730. {
  731. // NOTE: This is very ad-hoc.
  732. // The purpose of this function is to calculate the approximate position that `box`
  733. // would have had if it were position:static.
  734. float x = 0.0f;
  735. float y = 0.0f;
  736. VERIFY(box.parent());
  737. if (box.parent()->children_are_inline()) {
  738. // We're an abspos box with inline siblings. This is gonna get messy!
  739. if (auto* sibling = box.previous_sibling()) {
  740. // Hard case: there's a previous sibling. This means there's already inline content
  741. // preceding the hypothetical static position of `box` within its containing block.
  742. // If we had been position:static, that inline content would have been wrapped in
  743. // anonymous block box, so now we get to imagine what the world might have looked like
  744. // in that scenario..
  745. // Basically, we find its last associated line box fragment and place `box` under it.
  746. // FIXME: I'm 100% sure this can be smarter, better and faster.
  747. LineBoxFragment const* last_fragment = nullptr;
  748. auto& cb_state = m_state.get(*sibling->containing_block());
  749. for (auto& line_box : cb_state.line_boxes) {
  750. for (auto& fragment : line_box.fragments()) {
  751. if (&fragment.layout_node() == sibling)
  752. last_fragment = &fragment;
  753. }
  754. }
  755. if (last_fragment) {
  756. y = last_fragment->offset().y() + last_fragment->height();
  757. }
  758. } else {
  759. // Easy case: no previous sibling, we're at the top of the containing block.
  760. }
  761. } else {
  762. // We're among block siblings, Y can be calculated easily.
  763. y = compute_box_y_position_with_respect_to_siblings(box);
  764. }
  765. auto offset_to_static_parent = content_box_rect_in_static_position_ancestor_coordinate_space(box, *box.containing_block(), m_state);
  766. return offset_to_static_parent.location().translated(x, y);
  767. }
  768. void FormattingContext::layout_absolutely_positioned_element(Box const& box, AvailableSpace const& available_space)
  769. {
  770. auto& containing_block_state = m_state.get_mutable(*box.containing_block());
  771. auto& box_state = m_state.get_mutable(box);
  772. auto width_of_containing_block = available_space.width.to_px();
  773. auto height_of_containing_block = available_space.height.to_px();
  774. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  775. auto height_of_containing_block_as_length = CSS::Length::make_px(height_of_containing_block);
  776. auto specified_width = box.computed_values().width().resolved(box, width_of_containing_block_as_length).resolved(box);
  777. compute_width_for_absolutely_positioned_element(box, available_space);
  778. auto independent_formatting_context = layout_inside(box, LayoutMode::Normal, box_state.available_inner_space_or_constraints_from(available_space));
  779. compute_height_for_absolutely_positioned_element(box, available_space);
  780. box_state.margin_left = box.computed_values().margin().left().resolved(box, width_of_containing_block_as_length).to_px(box);
  781. box_state.margin_top = box.computed_values().margin().top().resolved(box, width_of_containing_block_as_length).to_px(box);
  782. box_state.margin_right = box.computed_values().margin().right().resolved(box, width_of_containing_block_as_length).to_px(box);
  783. box_state.margin_bottom = box.computed_values().margin().bottom().resolved(box, width_of_containing_block_as_length).to_px(box);
  784. box_state.border_left = box.computed_values().border_left().width;
  785. box_state.border_right = box.computed_values().border_right().width;
  786. box_state.border_top = box.computed_values().border_top().width;
  787. box_state.border_bottom = box.computed_values().border_bottom().width;
  788. auto const& computed_left = box.computed_values().inset().left();
  789. auto const& computed_right = box.computed_values().inset().right();
  790. auto const& computed_top = box.computed_values().inset().top();
  791. auto const& computed_bottom = box.computed_values().inset().bottom();
  792. box_state.inset_left = computed_left.resolved(box, width_of_containing_block_as_length).to_px(box);
  793. box_state.inset_top = computed_top.resolved(box, height_of_containing_block_as_length).to_px(box);
  794. box_state.inset_right = computed_right.resolved(box, width_of_containing_block_as_length).to_px(box);
  795. box_state.inset_bottom = computed_bottom.resolved(box, height_of_containing_block_as_length).to_px(box);
  796. if (computed_left.is_auto() && box.computed_values().width().is_auto() && computed_right.is_auto()) {
  797. if (box.computed_values().margin().left().is_auto())
  798. box_state.margin_left = 0;
  799. if (box.computed_values().margin().right().is_auto())
  800. box_state.margin_right = 0;
  801. }
  802. auto static_position = calculate_static_position(box);
  803. Gfx::FloatPoint used_offset;
  804. if (!computed_left.is_auto()) {
  805. float x_offset = box_state.inset_left
  806. + box_state.border_box_left();
  807. used_offset.set_x(x_offset + box_state.margin_left);
  808. } else if (!computed_right.is_auto()) {
  809. float x_offset = 0
  810. - box_state.inset_right
  811. - box_state.border_box_right();
  812. used_offset.set_x(width_of_containing_block + x_offset - box_state.content_width() - box_state.margin_right);
  813. } else {
  814. float x_offset = box_state.margin_box_left()
  815. + static_position.x();
  816. used_offset.set_x(x_offset);
  817. }
  818. if (!computed_top.is_auto()) {
  819. float y_offset = box_state.inset_top
  820. + box_state.border_box_top();
  821. used_offset.set_y(y_offset + box_state.margin_top);
  822. } else if (!computed_bottom.is_auto()) {
  823. float y_offset = 0
  824. - box_state.inset_bottom
  825. - box_state.border_box_bottom();
  826. used_offset.set_y(height_of_containing_block + y_offset - box_state.content_height() - box_state.margin_bottom);
  827. } else {
  828. float y_offset = box_state.margin_box_top()
  829. + static_position.y();
  830. used_offset.set_y(y_offset);
  831. }
  832. // NOTE: Absolutely positioned boxes are relative to the *padding edge* of the containing block.
  833. used_offset.translate_by(-containing_block_state.padding_left, -containing_block_state.padding_top);
  834. box_state.set_content_offset(used_offset);
  835. if (independent_formatting_context)
  836. independent_formatting_context->parent_context_did_dimension_child_root_box();
  837. }
  838. void FormattingContext::compute_height_for_absolutely_positioned_replaced_element(ReplacedBox const& box, AvailableSpace const& available_space)
  839. {
  840. // 10.6.5 Absolutely positioned, replaced elements
  841. // The used value of 'height' is determined as for inline replaced elements.
  842. m_state.get_mutable(box).set_content_height(compute_height_for_replaced_element(m_state, box, available_space));
  843. }
  844. // https://www.w3.org/TR/css-position-3/#relpos-insets
  845. void FormattingContext::compute_inset(Box const& box)
  846. {
  847. if (box.computed_values().position() != CSS::Position::Relative)
  848. return;
  849. 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) {
  850. auto resolved_first = computed_start.resolved(box, CSS::Length::make_px(reference_for_percentage)).resolved(box);
  851. auto resolved_second = computed_end.resolved(box, CSS::Length::make_px(reference_for_percentage)).resolved(box);
  852. if (resolved_first.is_auto() && resolved_second.is_auto()) {
  853. // If opposing inset properties in an axis both compute to auto (their initial values),
  854. // their used values are zero (i.e., the boxes stay in their original position in that axis).
  855. used_start = 0;
  856. used_end = 0;
  857. } else if (resolved_first.is_auto() || resolved_second.is_auto()) {
  858. // If only one is auto, its used value becomes the negation of the other, and the box is shifted by the specified amount.
  859. if (resolved_first.is_auto()) {
  860. used_end = resolved_second.to_px(box);
  861. used_start = 0 - used_end;
  862. } else {
  863. used_start = resolved_first.to_px(box);
  864. used_end = 0 - used_start;
  865. }
  866. } else {
  867. // If neither is auto, the position is over-constrained; (with respect to the writing mode of its containing block)
  868. // the computed end side value is ignored, and its used value becomes the negation of the start side.
  869. used_start = resolved_first.to_px(box);
  870. used_end = 0 - used_start;
  871. }
  872. };
  873. auto& box_state = m_state.get_mutable(box);
  874. auto const& computed_values = box.computed_values();
  875. // FIXME: Respect the containing block's writing-mode.
  876. 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));
  877. 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));
  878. }
  879. float FormattingContext::calculate_fit_content_size(float min_content_size, float max_content_size, AvailableSize const& available_size) const
  880. {
  881. // If the available space in a given axis is definite, equal to clamp(min-content size, stretch-fit size, max-content size)
  882. // (i.e. max(min-content size, min(max-content size, stretch-fit size))).
  883. if (available_size.is_definite()) {
  884. // FIXME: Compute the real stretch-fit size.
  885. auto stretch_fit_size = available_size.to_px();
  886. auto s = max(min_content_size, min(max_content_size, stretch_fit_size));
  887. return s;
  888. }
  889. // When sizing under a min-content constraint, equal to the min-content size.
  890. if (available_size.is_min_content())
  891. return min_content_size;
  892. // Otherwise, equal to the max-content size in that axis.
  893. return max_content_size;
  894. }
  895. float FormattingContext::calculate_fit_content_width(Layout::Box const& box, AvailableSpace const& available_space) const
  896. {
  897. // When sizing under a min-content constraint, equal to the min-content size.
  898. // NOTE: We check this first, to avoid needlessly calculating the max-content size.
  899. if (available_space.width.is_min_content())
  900. return calculate_min_content_width(box);
  901. if (available_space.width.is_max_content())
  902. return calculate_max_content_width(box);
  903. return calculate_fit_content_size(calculate_min_content_width(box), calculate_max_content_width(box), available_space.width);
  904. }
  905. float FormattingContext::calculate_fit_content_height(Layout::Box const& box, AvailableSpace const& available_space) const
  906. {
  907. // When sizing under a min-content constraint, equal to the min-content size.
  908. // NOTE: We check this first, to avoid needlessly calculating the max-content size.
  909. if (available_space.height.is_min_content())
  910. return calculate_min_content_height(box, available_space.width);
  911. if (available_space.height.is_max_content())
  912. return calculate_max_content_height(box, available_space.width);
  913. return calculate_fit_content_size(calculate_min_content_height(box, available_space.width), calculate_max_content_height(box, available_space.width), available_space.height);
  914. }
  915. float FormattingContext::calculate_min_content_width(Layout::Box const& box) const
  916. {
  917. if (box.has_intrinsic_width())
  918. return *box.intrinsic_width();
  919. auto& root_state = m_state.m_root;
  920. auto& cache = *root_state.intrinsic_sizes.ensure(&box, [] { return adopt_own(*new LayoutState::IntrinsicSizes); });
  921. if (cache.min_content_width.has_value())
  922. return *cache.min_content_width;
  923. LayoutState throwaway_state(&m_state);
  924. auto& box_state = throwaway_state.get_mutable(box);
  925. box_state.width_constraint = SizeConstraint::MinContent;
  926. auto context = const_cast<FormattingContext*>(this)->create_independent_formatting_context_if_needed(throwaway_state, box);
  927. VERIFY(context);
  928. auto available_width = AvailableSize::make_min_content();
  929. auto available_height = AvailableSize::make_indefinite();
  930. context->run(box, LayoutMode::IntrinsicSizing, AvailableSpace(available_width, available_height));
  931. if (context->type() == FormattingContext::Type::Flex) {
  932. cache.min_content_width = box_state.content_width();
  933. } else {
  934. cache.min_content_width = context->greatest_child_width(box);
  935. }
  936. if (!isfinite(*cache.min_content_width)) {
  937. // HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
  938. dbgln("FIXME: Calculated non-finite min-content width for {}", box.debug_description());
  939. cache.min_content_width = 0;
  940. }
  941. return *cache.min_content_width;
  942. }
  943. float FormattingContext::calculate_max_content_width(Layout::Box const& box) const
  944. {
  945. if (box.has_intrinsic_width())
  946. return *box.intrinsic_width();
  947. auto& root_state = m_state.m_root;
  948. auto& cache = *root_state.intrinsic_sizes.ensure(&box, [] { return adopt_own(*new LayoutState::IntrinsicSizes); });
  949. if (cache.max_content_width.has_value())
  950. return *cache.max_content_width;
  951. LayoutState throwaway_state(&m_state);
  952. auto& box_state = throwaway_state.get_mutable(box);
  953. box_state.width_constraint = SizeConstraint::MaxContent;
  954. auto context = const_cast<FormattingContext*>(this)->create_independent_formatting_context_if_needed(throwaway_state, box);
  955. VERIFY(context);
  956. auto available_width = AvailableSize::make_max_content();
  957. auto available_height = AvailableSize::make_indefinite();
  958. context->run(box, LayoutMode::IntrinsicSizing, AvailableSpace(available_width, available_height));
  959. if (context->type() == FormattingContext::Type::Flex) {
  960. cache.max_content_width = box_state.content_width();
  961. } else {
  962. cache.max_content_width = context->greatest_child_width(box);
  963. }
  964. if (!isfinite(*cache.max_content_width)) {
  965. // HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
  966. dbgln("FIXME: Calculated non-finite max-content width for {}", box.debug_description());
  967. cache.max_content_width = 0;
  968. }
  969. return *cache.max_content_width;
  970. }
  971. float FormattingContext::calculate_min_content_height(Layout::Box const& box, AvailableSize const& available_width) const
  972. {
  973. if (box.has_intrinsic_height())
  974. return *box.intrinsic_height();
  975. bool is_cacheable = available_width.is_definite() || available_width.is_intrinsic_sizing_constraint();
  976. Optional<float>* cache_slot = nullptr;
  977. if (is_cacheable) {
  978. auto& root_state = m_state.m_root;
  979. auto& cache = *root_state.intrinsic_sizes.ensure(&box, [] { return adopt_own(*new LayoutState::IntrinsicSizes); });
  980. if (available_width.is_definite()) {
  981. cache_slot = &cache.min_content_height_with_definite_available_width;
  982. } else if (available_width.is_min_content()) {
  983. cache_slot = &cache.min_content_height_with_min_content_available_width;
  984. } else if (available_width.is_max_content()) {
  985. cache_slot = &cache.min_content_height_with_max_content_available_width;
  986. }
  987. }
  988. if (cache_slot && cache_slot->has_value())
  989. return cache_slot->value();
  990. LayoutState throwaway_state(&m_state);
  991. auto& box_state = throwaway_state.get_mutable(box);
  992. box_state.height_constraint = SizeConstraint::MinContent;
  993. auto context = const_cast<FormattingContext*>(this)->create_independent_formatting_context_if_needed(throwaway_state, box);
  994. VERIFY(context);
  995. context->run(box, LayoutMode::IntrinsicSizing, AvailableSpace(available_width, AvailableSize::make_min_content()));
  996. auto min_content_height = context->automatic_content_height();
  997. if (!isfinite(min_content_height)) {
  998. // HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
  999. dbgln("FIXME: Calculated non-finite min-content height for {}", box.debug_description());
  1000. min_content_height = 0;
  1001. }
  1002. if (cache_slot) {
  1003. *cache_slot = min_content_height;
  1004. }
  1005. return min_content_height;
  1006. }
  1007. float FormattingContext::calculate_max_content_height(Layout::Box const& box, AvailableSize const& available_width) const
  1008. {
  1009. if (box.has_intrinsic_height())
  1010. return *box.intrinsic_height();
  1011. bool is_cacheable = available_width.is_definite() || available_width.is_intrinsic_sizing_constraint();
  1012. Optional<float>* cache_slot = nullptr;
  1013. if (is_cacheable) {
  1014. auto& root_state = m_state.m_root;
  1015. auto& cache = *root_state.intrinsic_sizes.ensure(&box, [] { return adopt_own(*new LayoutState::IntrinsicSizes); });
  1016. if (available_width.is_definite()) {
  1017. cache_slot = &cache.max_content_height_with_definite_available_width;
  1018. } else if (available_width.is_min_content()) {
  1019. cache_slot = &cache.max_content_height_with_min_content_available_width;
  1020. } else if (available_width.is_max_content()) {
  1021. cache_slot = &cache.max_content_height_with_max_content_available_width;
  1022. }
  1023. }
  1024. if (cache_slot && cache_slot->has_value())
  1025. return cache_slot->value();
  1026. LayoutState throwaway_state(&m_state);
  1027. auto& box_state = throwaway_state.get_mutable(box);
  1028. box_state.height_constraint = SizeConstraint::MaxContent;
  1029. auto context = const_cast<FormattingContext*>(this)->create_independent_formatting_context_if_needed(throwaway_state, box);
  1030. VERIFY(context);
  1031. context->run(box, LayoutMode::IntrinsicSizing, AvailableSpace(available_width, AvailableSize::make_max_content()));
  1032. auto max_content_height = context->automatic_content_height();
  1033. if (!isfinite(max_content_height)) {
  1034. // HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
  1035. dbgln("FIXME: Calculated non-finite max-content height for {}", box.debug_description());
  1036. max_content_height = 0;
  1037. }
  1038. if (cache_slot) {
  1039. *cache_slot = max_content_height;
  1040. }
  1041. return max_content_height;
  1042. }
  1043. float FormattingContext::containing_block_width_for(Box const& box, LayoutState const& state)
  1044. {
  1045. auto& containing_block_state = state.get(*box.containing_block());
  1046. auto& box_state = state.get(box);
  1047. switch (box_state.width_constraint) {
  1048. case SizeConstraint::MinContent:
  1049. return 0;
  1050. case SizeConstraint::MaxContent:
  1051. return INFINITY;
  1052. case SizeConstraint::None:
  1053. return containing_block_state.content_width();
  1054. }
  1055. VERIFY_NOT_REACHED();
  1056. }
  1057. float FormattingContext::containing_block_height_for(Box const& box, LayoutState const& state)
  1058. {
  1059. auto& containing_block_state = state.get(*box.containing_block());
  1060. auto& box_state = state.get(box);
  1061. switch (box_state.height_constraint) {
  1062. case SizeConstraint::MinContent:
  1063. return 0;
  1064. case SizeConstraint::MaxContent:
  1065. return INFINITY;
  1066. case SizeConstraint::None:
  1067. return containing_block_state.content_height();
  1068. }
  1069. VERIFY_NOT_REACHED();
  1070. }
  1071. static Box const* previous_block_level_sibling(Box const& box)
  1072. {
  1073. for (auto* sibling = box.previous_sibling_of_type<Box>(); sibling; sibling = sibling->previous_sibling_of_type<Box>()) {
  1074. if (sibling->display().is_block_outside())
  1075. return sibling;
  1076. }
  1077. return nullptr;
  1078. }
  1079. float FormattingContext::compute_box_y_position_with_respect_to_siblings(Box const& box) const
  1080. {
  1081. auto const& box_state = m_state.get(box);
  1082. float y = box_state.border_box_top();
  1083. Vector<float> collapsible_margins;
  1084. auto* relevant_sibling = previous_block_level_sibling(box);
  1085. while (relevant_sibling != nullptr) {
  1086. if (!relevant_sibling->is_absolutely_positioned() && !relevant_sibling->is_floating()) {
  1087. auto const& relevant_sibling_state = m_state.get(*relevant_sibling);
  1088. collapsible_margins.append(relevant_sibling_state.margin_bottom);
  1089. // NOTE: Empty (0-height) preceding siblings have their margins collapsed with *their* preceding sibling, etc.
  1090. if (relevant_sibling_state.border_box_height() > 0)
  1091. break;
  1092. collapsible_margins.append(relevant_sibling_state.margin_top);
  1093. }
  1094. relevant_sibling = previous_block_level_sibling(*relevant_sibling);
  1095. }
  1096. if (relevant_sibling) {
  1097. // Collapse top margin with the collapsed margin(s) of preceding siblings.
  1098. collapsible_margins.append(box_state.margin_top);
  1099. float smallest_margin = 0;
  1100. float largest_margin = 0;
  1101. size_t negative_margin_count = 0;
  1102. for (auto margin : collapsible_margins) {
  1103. if (margin < 0)
  1104. ++negative_margin_count;
  1105. largest_margin = max(largest_margin, margin);
  1106. smallest_margin = min(smallest_margin, margin);
  1107. }
  1108. float collapsed_margin = 0;
  1109. if (negative_margin_count == collapsible_margins.size()) {
  1110. // When all margins are negative, the size of the collapsed margin is the smallest (most negative) margin.
  1111. collapsed_margin = smallest_margin;
  1112. } else if (negative_margin_count > 0) {
  1113. // 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.
  1114. collapsed_margin = largest_margin + smallest_margin;
  1115. } else {
  1116. // Otherwise, collapse all the adjacent margins by using only the largest one.
  1117. collapsed_margin = largest_margin;
  1118. }
  1119. auto const& relevant_sibling_state = m_state.get(*relevant_sibling);
  1120. return y + relevant_sibling_state.offset.y()
  1121. + relevant_sibling_state.content_height()
  1122. + relevant_sibling_state.border_box_bottom()
  1123. + collapsed_margin;
  1124. } else {
  1125. return y + box_state.margin_top;
  1126. }
  1127. }
  1128. // https://drafts.csswg.org/css-sizing-3/#stretch-fit-size
  1129. float FormattingContext::calculate_stretch_fit_width(Box const& box, AvailableSize const& available_width) const
  1130. {
  1131. // The size a box would take if its outer size filled the available space in the given axis;
  1132. // in other words, the stretch fit into the available space, if that is definite.
  1133. // Undefined if the available space is indefinite.
  1134. auto const& box_state = m_state.get(box);
  1135. return available_width.to_px()
  1136. - box_state.margin_left
  1137. - box_state.margin_right
  1138. - box_state.padding_left
  1139. - box_state.padding_right
  1140. - box_state.border_left
  1141. - box_state.border_right;
  1142. }
  1143. }