FormattingContext.cpp 74 KB

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