GridFormattingContext.cpp 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. /*
  2. * Copyright (c) 2022, Martin Falisse <mfalisse@outlook.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Node.h>
  7. #include <LibWeb/Layout/Box.h>
  8. #include <LibWeb/Layout/GridFormattingContext.h>
  9. namespace Web::Layout {
  10. GridFormattingContext::GridFormattingContext(LayoutState& state, BlockContainer const& block_container, FormattingContext* parent)
  11. : BlockFormattingContext(state, block_container, parent)
  12. {
  13. }
  14. GridFormattingContext::~GridFormattingContext() = default;
  15. void GridFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const& available_space)
  16. {
  17. auto& box_state = m_state.get_mutable(box);
  18. auto grid_template_columns = box.computed_values().grid_template_columns();
  19. auto grid_template_rows = box.computed_values().grid_template_rows();
  20. auto should_skip_is_anonymous_text_run = [&](Box& child_box) -> bool {
  21. if (child_box.is_anonymous() && !child_box.first_child_of_type<BlockContainer>()) {
  22. bool contains_only_white_space = true;
  23. child_box.for_each_in_subtree([&](auto const& node) {
  24. if (!is<TextNode>(node) || !static_cast<TextNode const&>(node).dom_node().data().is_whitespace()) {
  25. contains_only_white_space = false;
  26. return IterationDecision::Break;
  27. }
  28. return IterationDecision::Continue;
  29. });
  30. if (contains_only_white_space)
  31. return true;
  32. }
  33. return false;
  34. };
  35. auto resolve_definite_track_size = [&](CSS::GridSize const& grid_size) -> float {
  36. VERIFY(grid_size.is_definite());
  37. switch (grid_size.type()) {
  38. case CSS::GridSize::Type::Length:
  39. if (grid_size.length().is_auto())
  40. break;
  41. return grid_size.length().to_px(box);
  42. break;
  43. case CSS::GridSize::Type::Percentage:
  44. return grid_size.percentage().as_fraction() * box_state.content_width();
  45. break;
  46. default:
  47. VERIFY_NOT_REACHED();
  48. }
  49. return 0;
  50. };
  51. // https://drafts.csswg.org/css-grid/#overview-placement
  52. // 2.2. Placing Items
  53. // The contents of the grid container are organized into individual grid items (analogous to
  54. // flex items), which are then assigned to predefined areas in the grid. They can be explicitly
  55. // placed using coordinates through the grid-placement properties or implicitly placed into
  56. // empty areas using auto-placement.
  57. struct PositionedBox {
  58. Box const& box;
  59. int row { 0 };
  60. int row_span { 1 };
  61. int column { 0 };
  62. int column_span { 1 };
  63. float computed_height { 0 };
  64. };
  65. Vector<PositionedBox> positioned_boxes;
  66. Vector<Box const&> boxes_to_place;
  67. box.for_each_child_of_type<Box>([&](Box& child_box) {
  68. if (should_skip_is_anonymous_text_run(child_box))
  69. return IterationDecision::Continue;
  70. boxes_to_place.append(child_box);
  71. return IterationDecision::Continue;
  72. });
  73. auto column_count = 0;
  74. for (auto const& explicit_grid_track : grid_template_columns.track_list()) {
  75. if (explicit_grid_track.is_repeat() && explicit_grid_track.repeat().is_default())
  76. column_count += explicit_grid_track.repeat().repeat_count() * explicit_grid_track.repeat().grid_track_size_list().track_list().size();
  77. else
  78. column_count += 1;
  79. }
  80. auto row_count = 0;
  81. for (auto const& explicit_grid_track : grid_template_rows.track_list()) {
  82. if (explicit_grid_track.is_repeat() && explicit_grid_track.repeat().is_default())
  83. row_count += explicit_grid_track.repeat().repeat_count() * explicit_grid_track.repeat().grid_track_size_list().track_list().size();
  84. else
  85. row_count += 1;
  86. }
  87. // https://www.w3.org/TR/css-grid-2/#auto-repeat
  88. // 7.2.3.2. Repeat-to-fill: auto-fill and auto-fit repetitions
  89. // On a subgridded axis, the auto-fill keyword is only valid once per <line-name-list>, and repeats
  90. // enough times for the name list to match the subgrid’s specified grid span (falling back to 0 if
  91. // the span is already fulfilled).
  92. // Otherwise on a standalone axis, when auto-fill is given as the repetition number
  93. if (grid_template_columns.track_list().size() == 1
  94. && grid_template_columns.track_list().first().is_repeat()
  95. && (grid_template_columns.track_list().first().repeat().is_auto_fill() || grid_template_columns.track_list().first().repeat().is_auto_fit())) {
  96. // If the grid container has a definite size or max size in the relevant axis, then the number of
  97. // repetitions is the largest possible positive integer that does not cause the grid to overflow the
  98. // content box of its grid container
  99. auto sum_of_grid_track_sizes = 0;
  100. // (treating each track as its max track sizing function if that is definite or its minimum track sizing
  101. // function otherwise, flooring the max track sizing function by the min track sizing function if both
  102. // are definite, and taking gap into account)
  103. // FIXME: take gap into account
  104. for (auto& explicit_grid_track : grid_template_columns.track_list().first().repeat().grid_track_size_list().track_list()) {
  105. auto track_sizing_function = explicit_grid_track;
  106. if (track_sizing_function.is_minmax()) {
  107. if (track_sizing_function.minmax().max_grid_size().is_definite() && !track_sizing_function.minmax().min_grid_size().is_definite())
  108. sum_of_grid_track_sizes += resolve_definite_track_size(track_sizing_function.minmax().max_grid_size());
  109. else if (track_sizing_function.minmax().min_grid_size().is_definite() && !track_sizing_function.minmax().max_grid_size().is_definite())
  110. sum_of_grid_track_sizes += resolve_definite_track_size(track_sizing_function.minmax().min_grid_size());
  111. else if (track_sizing_function.minmax().min_grid_size().is_definite() && track_sizing_function.minmax().max_grid_size().is_definite())
  112. sum_of_grid_track_sizes += min(resolve_definite_track_size(track_sizing_function.minmax().min_grid_size()), resolve_definite_track_size(track_sizing_function.minmax().max_grid_size()));
  113. } else {
  114. sum_of_grid_track_sizes += min(resolve_definite_track_size(track_sizing_function.grid_size()), resolve_definite_track_size(track_sizing_function.grid_size()));
  115. }
  116. }
  117. column_count = max(1, static_cast<int>(get_free_space_x(box) / sum_of_grid_track_sizes));
  118. // For the purpose of finding the number of auto-repeated tracks in a standalone axis, the UA must
  119. // floor the track size to a UA-specified value to avoid division by zero. It is suggested that this
  120. // floor be 1px.
  121. }
  122. if (grid_template_rows.track_list().size() == 1
  123. && grid_template_rows.track_list().first().is_repeat()
  124. && (grid_template_rows.track_list().first().repeat().is_auto_fill() || grid_template_rows.track_list().first().repeat().is_auto_fit())) {
  125. // If the grid container has a definite size or max size in the relevant axis, then the number of
  126. // repetitions is the largest possible positive integer that does not cause the grid to overflow the
  127. // content box of its grid container
  128. auto sum_of_grid_track_sizes = 0;
  129. // (treating each track as its max track sizing function if that is definite or its minimum track sizing
  130. // function otherwise, flooring the max track sizing function by the min track sizing function if both
  131. // are definite, and taking gap into account)
  132. // FIXME: take gap into account
  133. for (auto& explicit_grid_track : grid_template_rows.track_list().first().repeat().grid_track_size_list().track_list()) {
  134. auto track_sizing_function = explicit_grid_track;
  135. if (track_sizing_function.is_minmax()) {
  136. if (track_sizing_function.minmax().max_grid_size().is_definite() && !track_sizing_function.minmax().min_grid_size().is_definite())
  137. sum_of_grid_track_sizes += resolve_definite_track_size(track_sizing_function.minmax().max_grid_size());
  138. else if (track_sizing_function.minmax().min_grid_size().is_definite() && !track_sizing_function.minmax().max_grid_size().is_definite())
  139. sum_of_grid_track_sizes += resolve_definite_track_size(track_sizing_function.minmax().min_grid_size());
  140. else if (track_sizing_function.minmax().min_grid_size().is_definite() && track_sizing_function.minmax().max_grid_size().is_definite())
  141. sum_of_grid_track_sizes += min(resolve_definite_track_size(track_sizing_function.minmax().min_grid_size()), resolve_definite_track_size(track_sizing_function.minmax().max_grid_size()));
  142. } else {
  143. sum_of_grid_track_sizes += min(resolve_definite_track_size(track_sizing_function.grid_size()), resolve_definite_track_size(track_sizing_function.grid_size()));
  144. }
  145. }
  146. row_count = max(1, static_cast<int>(get_free_space_y(box) / sum_of_grid_track_sizes));
  147. // The auto-fit keyword behaves the same as auto-fill, except that after grid item placement any
  148. // empty repeated tracks are collapsed. An empty track is one with no in-flow grid items placed into
  149. // or spanning across it. (This can result in all tracks being collapsed, if they’re all empty.)
  150. // A collapsed track is treated as having a fixed track sizing function of 0px, and the gutters on
  151. // either side of it—including any space allotted through distributed alignment—collapse.
  152. // For the purpose of finding the number of auto-repeated tracks in a standalone axis, the UA must
  153. // floor the track size to a UA-specified value to avoid division by zero. It is suggested that this
  154. // floor be 1px.
  155. }
  156. auto occupation_grid = OccupationGrid(column_count, row_count);
  157. // https://drafts.csswg.org/css-grid/#auto-placement-algo
  158. // 8.5. Grid Item Placement Algorithm
  159. // FIXME: 0. Generate anonymous grid items
  160. // 1. Position anything that's not auto-positioned.
  161. for (size_t i = 0; i < boxes_to_place.size(); i++) {
  162. auto const& child_box = boxes_to_place[i];
  163. if (is_auto_positioned_row(child_box.computed_values().grid_row_start(), child_box.computed_values().grid_row_end())
  164. || is_auto_positioned_column(child_box.computed_values().grid_column_start(), child_box.computed_values().grid_column_end()))
  165. continue;
  166. int row_start = child_box.computed_values().grid_row_start().raw_value();
  167. int row_end = child_box.computed_values().grid_row_end().raw_value();
  168. int column_start = child_box.computed_values().grid_column_start().raw_value();
  169. int column_end = child_box.computed_values().grid_column_end().raw_value();
  170. // https://www.w3.org/TR/css-grid-2/#line-placement
  171. // 8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
  172. // https://www.w3.org/TR/css-grid-2/#grid-placement-slot
  173. // First attempt to match the grid area’s edge to a named grid area: if there is a grid line whose
  174. // line name is <custom-ident>-start (for grid-*-start) / <custom-ident>-end (for grid-*-end),
  175. // contributes the first such line to the grid item’s placement.
  176. // Otherwise, treat this as if the integer 1 had been specified along with the <custom-ident>.
  177. // https://www.w3.org/TR/css-grid-2/#grid-placement-int
  178. // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
  179. // instead counts in reverse, starting from the end edge of the explicit grid.
  180. if (row_end < 0)
  181. row_end = occupation_grid.row_count() + row_end + 2;
  182. if (column_end < 0)
  183. column_end = occupation_grid.column_count() + column_end + 2;
  184. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  185. // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
  186. // of finding this position.
  187. // https://www.w3.org/TR/css-grid-2/#grid-placement-span-int
  188. // Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid
  189. // item’s grid area is N lines from its opposite edge in the corresponding direction. For example,
  190. // grid-column-end: span 2 indicates the second grid line in the endward direction from the
  191. // grid-column-start line.
  192. int row_span = 1;
  193. int column_span = 1;
  194. if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_span())
  195. row_span = child_box.computed_values().grid_row_end().raw_value();
  196. if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_span())
  197. column_span = child_box.computed_values().grid_column_end().raw_value();
  198. if (child_box.computed_values().grid_row_end().is_position() && child_box.computed_values().grid_row_start().is_span()) {
  199. row_span = child_box.computed_values().grid_row_start().raw_value();
  200. row_start = row_end - row_span;
  201. }
  202. if (child_box.computed_values().grid_column_end().is_position() && child_box.computed_values().grid_column_start().is_span()) {
  203. column_span = child_box.computed_values().grid_column_start().raw_value();
  204. column_start = column_end - column_span;
  205. }
  206. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  207. // lines with that name exist, all implicit grid lines on the side of the explicit grid
  208. // corresponding to the search direction are assumed to have that name for the purpose of counting
  209. // this span.
  210. // https://drafts.csswg.org/css-grid/#grid-placement-auto
  211. // auto
  212. // The property contributes nothing to the grid item’s placement, indicating auto-placement or a
  213. // default span of one. (See § 8 Placing Grid Items, above.)
  214. // https://www.w3.org/TR/css-grid-2/#common-uses-named-lines
  215. // 8.1.3. Named Lines and Spans
  216. // Instead of counting lines by number, lines can be referenced by their line name:
  217. if (child_box.computed_values().grid_column_start().has_line_name()) {
  218. auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_column_start().line_name(), grid_template_columns);
  219. if (found_flag_and_index > -1)
  220. column_start = 1 + found_flag_and_index;
  221. else
  222. column_start = 1; // FIXME
  223. }
  224. if (child_box.computed_values().grid_column_end().has_line_name()) {
  225. auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_column_end().line_name(), grid_template_columns);
  226. if (found_flag_and_index > -1) {
  227. column_end = 1 + found_flag_and_index;
  228. if (!child_box.computed_values().grid_column_start().is_position())
  229. column_start = column_end - column_span;
  230. } else {
  231. column_end = 2; // FIXME
  232. column_start = 1; // FIXME
  233. }
  234. }
  235. if (child_box.computed_values().grid_row_start().has_line_name()) {
  236. auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_row_start().line_name(), grid_template_rows);
  237. if (found_flag_and_index > -1)
  238. row_start = 1 + found_flag_and_index;
  239. else
  240. row_start = 1; // FIXME
  241. }
  242. if (child_box.computed_values().grid_row_end().has_line_name()) {
  243. auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_row_end().line_name(), grid_template_rows);
  244. if (found_flag_and_index > -1) {
  245. row_end = 1 + found_flag_and_index;
  246. if (!child_box.computed_values().grid_row_start().is_position())
  247. row_start = row_end - row_span;
  248. } else {
  249. row_end = 2; // FIXME
  250. row_start = 1; // FIXME
  251. }
  252. }
  253. // If there are multiple lines of the same name, they effectively establish a named set of grid
  254. // lines, which can be exclusively indexed by filtering the placement by name:
  255. // https://drafts.csswg.org/css-grid/#grid-placement-errors
  256. // 8.3.1. Grid Placement Conflict Handling
  257. // If the placement for a grid item contains two lines, and the start line is further end-ward than
  258. // the end line, swap the two lines. If the start line is equal to the end line, remove the end
  259. // line.
  260. if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_position()) {
  261. if (row_start > row_end)
  262. swap(row_start, row_end);
  263. if (row_start != row_end)
  264. row_span = row_end - row_start;
  265. }
  266. if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_position()) {
  267. if (column_start > column_end)
  268. swap(column_start, column_end);
  269. if (column_start != column_end)
  270. column_span = column_end - column_start;
  271. }
  272. // If the placement contains two spans, remove the one contributed by the end grid-placement
  273. // property.
  274. if (child_box.computed_values().grid_row_start().is_span() && child_box.computed_values().grid_row_end().is_span())
  275. row_span = child_box.computed_values().grid_row_start().raw_value();
  276. if (child_box.computed_values().grid_column_start().is_span() && child_box.computed_values().grid_column_end().is_span())
  277. column_span = child_box.computed_values().grid_column_start().raw_value();
  278. // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.
  279. row_start -= 1;
  280. column_start -= 1;
  281. positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
  282. occupation_grid.maybe_add_row(row_start + row_span);
  283. occupation_grid.maybe_add_column(column_start + column_span);
  284. occupation_grid.set_occupied(column_start, column_start + column_span, row_start, row_start + row_span);
  285. boxes_to_place.remove(i);
  286. i--;
  287. }
  288. // 2. Process the items locked to a given row.
  289. // FIXME: Do "dense" packing
  290. for (size_t i = 0; i < boxes_to_place.size(); i++) {
  291. auto const& child_box = boxes_to_place[i];
  292. if (is_auto_positioned_row(child_box.computed_values().grid_row_start(), child_box.computed_values().grid_row_end()))
  293. continue;
  294. int row_start = child_box.computed_values().grid_row_start().raw_value();
  295. int row_end = child_box.computed_values().grid_row_end().raw_value();
  296. // https://www.w3.org/TR/css-grid-2/#line-placement
  297. // 8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
  298. // https://www.w3.org/TR/css-grid-2/#grid-placement-slot
  299. // First attempt to match the grid area’s edge to a named grid area: if there is a grid line whose
  300. // line name is <custom-ident>-start (for grid-*-start) / <custom-ident>-end (for grid-*-end),
  301. // contributes the first such line to the grid item’s placement.
  302. // Otherwise, treat this as if the integer 1 had been specified along with the <custom-ident>.
  303. // https://www.w3.org/TR/css-grid-2/#grid-placement-int
  304. // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
  305. // instead counts in reverse, starting from the end edge of the explicit grid.
  306. if (row_end < 0)
  307. row_end = occupation_grid.row_count() + row_end + 2;
  308. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  309. // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
  310. // of finding this position.
  311. // https://www.w3.org/TR/css-grid-2/#grid-placement-span-int
  312. // Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid
  313. // item’s grid area is N lines from its opposite edge in the corresponding direction. For example,
  314. // grid-column-end: span 2 indicates the second grid line in the endward direction from the
  315. // grid-column-start line.
  316. int row_span = 1;
  317. if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_span())
  318. row_span = child_box.computed_values().grid_row_end().raw_value();
  319. if (child_box.computed_values().grid_row_end().is_position() && child_box.computed_values().grid_row_start().is_span()) {
  320. row_span = child_box.computed_values().grid_row_start().raw_value();
  321. row_start = row_end - row_span;
  322. // FIXME: Remove me once have implemented spans overflowing into negative indexes, e.g., grid-row: span 2 / 1
  323. if (row_start < 0)
  324. row_start = 1;
  325. }
  326. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  327. // lines with that name exist, all implicit grid lines on the side of the explicit grid
  328. // corresponding to the search direction are assumed to have that name for the purpose of counting
  329. // this span.
  330. // https://drafts.csswg.org/css-grid/#grid-placement-auto
  331. // auto
  332. // The property contributes nothing to the grid item’s placement, indicating auto-placement or a
  333. // default span of one. (See § 8 Placing Grid Items, above.)
  334. // https://www.w3.org/TR/css-grid-2/#common-uses-named-lines
  335. // 8.1.3. Named Lines and Spans
  336. // Instead of counting lines by number, lines can be referenced by their line name:
  337. if (child_box.computed_values().grid_row_start().has_line_name()) {
  338. auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_row_start().line_name(), grid_template_rows);
  339. if (found_flag_and_index > -1)
  340. row_start = 1 + found_flag_and_index;
  341. else
  342. row_start = 1; // FIXME
  343. }
  344. if (child_box.computed_values().grid_row_end().has_line_name()) {
  345. auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_row_end().line_name(), grid_template_rows);
  346. if (found_flag_and_index > -1) {
  347. row_end = 1 + found_flag_and_index;
  348. if (!child_box.computed_values().grid_row_start().is_position())
  349. row_start = row_end - row_span;
  350. } else {
  351. row_start = 1; // FIXME
  352. row_end = 2; // FIXME
  353. }
  354. }
  355. // If there are multiple lines of the same name, they effectively establish a named set of grid
  356. // lines, which can be exclusively indexed by filtering the placement by name:
  357. // https://drafts.csswg.org/css-grid/#grid-placement-errors
  358. // 8.3.1. Grid Placement Conflict Handling
  359. // If the placement for a grid item contains two lines, and the start line is further end-ward than
  360. // the end line, swap the two lines. If the start line is equal to the end line, remove the end
  361. // line.
  362. if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_position()) {
  363. if (row_start > row_end)
  364. swap(row_start, row_end);
  365. if (row_start != row_end)
  366. row_span = row_end - row_start;
  367. }
  368. // FIXME: Have yet to find the spec for this.
  369. if (!child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_position() && row_end == 1)
  370. row_start = 1;
  371. // If the placement contains two spans, remove the one contributed by the end grid-placement
  372. // property.
  373. if (child_box.computed_values().grid_row_start().is_span() && child_box.computed_values().grid_row_end().is_span())
  374. row_span = child_box.computed_values().grid_row_start().raw_value();
  375. // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.
  376. row_start -= 1;
  377. occupation_grid.maybe_add_row(row_start + row_span);
  378. int column_start = 0;
  379. auto column_span = child_box.computed_values().grid_column_start().is_span() ? child_box.computed_values().grid_column_start().raw_value() : 1;
  380. // https://drafts.csswg.org/css-grid/#auto-placement-algo
  381. // 8.5. Grid Item Placement Algorithm
  382. // 3.3. If the largest column span among all the items without a definite column position is larger
  383. // than the width of the implicit grid, add columns to the end of the implicit grid to accommodate
  384. // that column span.
  385. occupation_grid.maybe_add_column(column_span);
  386. bool found_available_column = false;
  387. for (int column_index = column_start; column_index < occupation_grid.column_count(); column_index++) {
  388. if (!occupation_grid.is_occupied(column_index, row_start)) {
  389. found_available_column = true;
  390. column_start = column_index;
  391. break;
  392. }
  393. }
  394. if (!found_available_column) {
  395. column_start = occupation_grid.column_count();
  396. occupation_grid.maybe_add_column(column_start + column_span);
  397. }
  398. occupation_grid.set_occupied(column_start, column_start + column_span, row_start, row_start + row_span);
  399. positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
  400. boxes_to_place.remove(i);
  401. i--;
  402. }
  403. // 3. Determine the columns in the implicit grid.
  404. // NOTE: "implicit grid" here is the same as the occupation_grid
  405. // 3.1. Start with the columns from the explicit grid.
  406. // NOTE: Done in step 1.
  407. // 3.2. Among all the items with a definite column position (explicitly positioned items, items
  408. // positioned in the previous step, and items not yet positioned but with a definite column) add
  409. // columns to the beginning and end of the implicit grid as necessary to accommodate those items.
  410. // NOTE: "Explicitly positioned items" and "items positioned in the previous step" done in step 1
  411. // and 2, respectively. Adding columns for "items not yet positioned but with a definite column"
  412. // will be done in step 4.
  413. // 4. Position the remaining grid items.
  414. // For each grid item that hasn't been positioned by the previous steps, in order-modified document
  415. // order:
  416. auto auto_placement_cursor_x = 0;
  417. auto auto_placement_cursor_y = 0;
  418. for (size_t i = 0; i < boxes_to_place.size(); i++) {
  419. auto const& child_box = boxes_to_place[i];
  420. // 4.1. For sparse packing:
  421. // FIXME: no distinction made. See #4.2
  422. // 4.1.1. If the item has a definite column position:
  423. if (!is_auto_positioned_column(child_box.computed_values().grid_column_start(), child_box.computed_values().grid_column_end())) {
  424. int column_start = child_box.computed_values().grid_column_start().raw_value();
  425. int column_end = child_box.computed_values().grid_column_end().raw_value();
  426. // https://www.w3.org/TR/css-grid-2/#line-placement
  427. // 8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
  428. // https://www.w3.org/TR/css-grid-2/#grid-placement-slot
  429. // First attempt to match the grid area’s edge to a named grid area: if there is a grid line whose
  430. // line name is <custom-ident>-start (for grid-*-start) / <custom-ident>-end (for grid-*-end),
  431. // contributes the first such line to the grid item’s placement.
  432. // Otherwise, treat this as if the integer 1 had been specified along with the <custom-ident>.
  433. // https://www.w3.org/TR/css-grid-2/#grid-placement-int
  434. // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
  435. // instead counts in reverse, starting from the end edge of the explicit grid.
  436. if (column_end < 0)
  437. column_end = occupation_grid.column_count() + column_end + 2;
  438. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  439. // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
  440. // of finding this position.
  441. // https://www.w3.org/TR/css-grid-2/#grid-placement-span-int
  442. // Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid
  443. // item’s grid area is N lines from its opposite edge in the corresponding direction. For example,
  444. // grid-column-end: span 2 indicates the second grid line in the endward direction from the
  445. // grid-column-start line.
  446. int column_span = 1;
  447. auto row_span = child_box.computed_values().grid_row_start().is_span() ? child_box.computed_values().grid_row_start().raw_value() : 1;
  448. if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_span())
  449. column_span = child_box.computed_values().grid_column_end().raw_value();
  450. if (child_box.computed_values().grid_column_end().is_position() && child_box.computed_values().grid_column_start().is_span()) {
  451. column_span = child_box.computed_values().grid_column_start().raw_value();
  452. column_start = column_end - column_span;
  453. // FIXME: Remove me once have implemented spans overflowing into negative indexes, e.g., grid-column: span 2 / 1
  454. if (column_start < 0)
  455. column_start = 1;
  456. }
  457. // FIXME: Have yet to find the spec for this.
  458. if (!child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_position() && column_end == 1)
  459. column_start = 1;
  460. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  461. // lines with that name exist, all implicit grid lines on the side of the explicit grid
  462. // corresponding to the search direction are assumed to have that name for the purpose of counting
  463. // this span.
  464. // https://drafts.csswg.org/css-grid/#grid-placement-auto
  465. // auto
  466. // The property contributes nothing to the grid item’s placement, indicating auto-placement or a
  467. // default span of one. (See § 8 Placing Grid Items, above.)
  468. // https://www.w3.org/TR/css-grid-2/#common-uses-named-lines
  469. // 8.1.3. Named Lines and Spans
  470. // Instead of counting lines by number, lines can be referenced by their line name:
  471. if (child_box.computed_values().grid_column_start().has_line_name()) {
  472. auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_column_start().line_name(), grid_template_columns);
  473. if (found_flag_and_index > -1)
  474. column_start = 1 + found_flag_and_index;
  475. else
  476. column_start = 1; // FIXME
  477. }
  478. if (child_box.computed_values().grid_column_end().has_line_name()) {
  479. auto found_flag_and_index = get_line_index_by_line_name(child_box.computed_values().grid_column_end().line_name(), grid_template_columns);
  480. if (found_flag_and_index > -1) {
  481. column_end = 1 + found_flag_and_index;
  482. if (!child_box.computed_values().grid_column_start().is_position())
  483. column_start = column_end - column_span;
  484. } else {
  485. column_end = 2; // FIXME
  486. column_start = 1; // FIXME
  487. }
  488. }
  489. // If there are multiple lines of the same name, they effectively establish a named set of grid
  490. // lines, which can be exclusively indexed by filtering the placement by name:
  491. // https://drafts.csswg.org/css-grid/#grid-placement-errors
  492. // 8.3.1. Grid Placement Conflict Handling
  493. // If the placement for a grid item contains two lines, and the start line is further end-ward than
  494. // the end line, swap the two lines. If the start line is equal to the end line, remove the end
  495. // line.
  496. if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_position()) {
  497. if (column_start > column_end)
  498. swap(column_start, column_end);
  499. if (column_start != column_end)
  500. column_span = column_end - column_start;
  501. }
  502. // If the placement contains two spans, remove the one contributed by the end grid-placement
  503. // property.
  504. if (child_box.computed_values().grid_column_start().is_span() && child_box.computed_values().grid_column_end().is_span())
  505. column_span = child_box.computed_values().grid_column_start().raw_value();
  506. // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.
  507. column_start -= 1;
  508. // 4.1.1.1. Set the column position of the cursor to the grid item's column-start line. If this is
  509. // less than the previous column position of the cursor, increment the row position by 1.
  510. if (column_start < auto_placement_cursor_x)
  511. auto_placement_cursor_y++;
  512. auto_placement_cursor_x = column_start;
  513. occupation_grid.maybe_add_column(auto_placement_cursor_x + column_span);
  514. occupation_grid.maybe_add_row(auto_placement_cursor_y + row_span);
  515. // 4.1.1.2. Increment the cursor's row position until a value is found where the grid item does not
  516. // overlap any occupied grid cells (creating new rows in the implicit grid as necessary).
  517. while (true) {
  518. if (!occupation_grid.is_occupied(column_start, auto_placement_cursor_y)) {
  519. break;
  520. }
  521. auto_placement_cursor_y++;
  522. occupation_grid.maybe_add_row(auto_placement_cursor_y + row_span);
  523. }
  524. // 4.1.1.3. Set the item's row-start line to the cursor's row position, and set the item's row-end
  525. // line according to its span from that position.
  526. occupation_grid.set_occupied(column_start, column_start + column_span, auto_placement_cursor_y, auto_placement_cursor_y + row_span);
  527. positioned_boxes.append({ child_box, auto_placement_cursor_y, row_span, column_start, column_span });
  528. }
  529. // 4.1.2. If the item has an automatic grid position in both axes:
  530. else {
  531. // 4.1.2.1. Increment the column position of the auto-placement cursor until either this item's grid
  532. // area does not overlap any occupied grid cells, or the cursor's column position, plus the item's
  533. // column span, overflow the number of columns in the implicit grid, as determined earlier in this
  534. // algorithm.
  535. auto column_start = 0;
  536. auto column_span = child_box.computed_values().grid_column_start().is_span() ? child_box.computed_values().grid_column_start().raw_value() : 1;
  537. // https://drafts.csswg.org/css-grid/#auto-placement-algo
  538. // 8.5. Grid Item Placement Algorithm
  539. // 3.3. If the largest column span among all the items without a definite column position is larger
  540. // than the width of the implicit grid, add columns to the end of the implicit grid to accommodate
  541. // that column span.
  542. occupation_grid.maybe_add_column(column_span);
  543. auto row_start = 0;
  544. auto row_span = child_box.computed_values().grid_row_start().is_span() ? child_box.computed_values().grid_row_start().raw_value() : 1;
  545. auto found_unoccupied_area = false;
  546. for (int row_index = auto_placement_cursor_y; row_index < occupation_grid.row_count(); row_index++) {
  547. for (int column_index = auto_placement_cursor_x; column_index < occupation_grid.column_count(); column_index++) {
  548. if (column_span + column_index <= occupation_grid.column_count()) {
  549. auto found_all_available = true;
  550. for (int span_index = 0; span_index < column_span; span_index++) {
  551. if (occupation_grid.is_occupied(column_index + span_index, row_index))
  552. found_all_available = false;
  553. }
  554. if (found_all_available) {
  555. found_unoccupied_area = true;
  556. column_start = column_index;
  557. row_start = row_index;
  558. goto finish;
  559. }
  560. }
  561. }
  562. auto_placement_cursor_x = 0;
  563. auto_placement_cursor_y++;
  564. }
  565. finish:
  566. // 4.1.2.2. If a non-overlapping position was found in the previous step, set the item's row-start
  567. // and column-start lines to the cursor's position. Otherwise, increment the auto-placement cursor's
  568. // row position (creating new rows in the implicit grid as necessary), set its column position to the
  569. // start-most column line in the implicit grid, and return to the previous step.
  570. if (!found_unoccupied_area) {
  571. row_start = occupation_grid.row_count();
  572. occupation_grid.maybe_add_row(occupation_grid.row_count() + 1);
  573. }
  574. occupation_grid.set_occupied(column_start, column_start + column_span, row_start, row_start + row_span);
  575. positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
  576. }
  577. boxes_to_place.remove(i);
  578. i--;
  579. // FIXME: 4.2. For dense packing:
  580. }
  581. for (auto& positioned_box : positioned_boxes) {
  582. auto& child_box_state = m_state.get_mutable(positioned_box.box);
  583. if (child_box_state.content_height() > positioned_box.computed_height)
  584. positioned_box.computed_height = child_box_state.content_height();
  585. if (child_box_state.content_height() > positioned_box.computed_height)
  586. positioned_box.computed_height = child_box_state.content_height();
  587. if (auto min_content_height = calculate_min_content_height(positioned_box.box, available_space.width); min_content_height > positioned_box.computed_height)
  588. positioned_box.computed_height = min_content_height;
  589. }
  590. // https://drafts.csswg.org/css-grid/#overview-sizing
  591. // 2.3. Sizing the Grid
  592. // Once the grid items have been placed, the sizes of the grid tracks (rows and columns) are
  593. // calculated, accounting for the sizes of their contents and/or available space as specified in
  594. // the grid definition.
  595. // https://www.w3.org/TR/css-grid-2/#layout-algorithm
  596. // 12. Grid Sizing
  597. // This section defines the grid sizing algorithm, which determines the size of all grid tracks and,
  598. // by extension, the entire grid.
  599. // Each track has specified minimum and maximum sizing functions (which may be the same). Each
  600. // sizing function is either:
  601. // - A fixed sizing function (<length> or resolvable <percentage>).
  602. // - An intrinsic sizing function (min-content, max-content, auto, fit-content()).
  603. // - A flexible sizing function (<flex>).
  604. // The grid sizing algorithm defines how to resolve these sizing constraints into used track sizes.
  605. for (auto const& track_in_list : grid_template_columns.track_list()) {
  606. auto repeat_count = (track_in_list.is_repeat() && track_in_list.repeat().is_default()) ? track_in_list.repeat().repeat_count() : 1;
  607. if (track_in_list.is_repeat()) {
  608. if (track_in_list.repeat().is_auto_fill() || track_in_list.repeat().is_auto_fit())
  609. repeat_count = column_count;
  610. }
  611. for (auto _ = 0; _ < repeat_count; _++) {
  612. switch (track_in_list.type()) {
  613. case CSS::ExplicitGridTrack::Type::MinMax:
  614. m_grid_columns.append({ track_in_list.minmax().min_grid_size(), track_in_list.minmax().max_grid_size() });
  615. break;
  616. case CSS::ExplicitGridTrack::Type::Repeat:
  617. for (auto& explicit_grid_track : track_in_list.repeat().grid_track_size_list().track_list()) {
  618. auto track_sizing_function = explicit_grid_track;
  619. if (track_sizing_function.is_minmax())
  620. m_grid_columns.append({ track_sizing_function.minmax().min_grid_size(), track_sizing_function.minmax().max_grid_size() });
  621. else
  622. m_grid_columns.append({ track_sizing_function.grid_size(), track_sizing_function.grid_size() });
  623. }
  624. break;
  625. case CSS::ExplicitGridTrack::Type::Default:
  626. m_grid_columns.append({ track_in_list.grid_size(), track_in_list.grid_size() });
  627. break;
  628. default:
  629. VERIFY_NOT_REACHED();
  630. }
  631. }
  632. }
  633. for (auto const& track_in_list : grid_template_rows.track_list()) {
  634. auto repeat_count = (track_in_list.is_repeat() && track_in_list.repeat().is_default()) ? track_in_list.repeat().repeat_count() : 1;
  635. if (track_in_list.is_repeat()) {
  636. if (track_in_list.repeat().is_auto_fill() || track_in_list.repeat().is_auto_fit())
  637. repeat_count = row_count;
  638. }
  639. for (auto _ = 0; _ < repeat_count; _++) {
  640. switch (track_in_list.type()) {
  641. case CSS::ExplicitGridTrack::Type::MinMax:
  642. m_grid_rows.append({ track_in_list.minmax().min_grid_size(), track_in_list.minmax().max_grid_size() });
  643. break;
  644. case CSS::ExplicitGridTrack::Type::Repeat:
  645. for (auto& explicit_grid_track : track_in_list.repeat().grid_track_size_list().track_list()) {
  646. auto track_sizing_function = explicit_grid_track;
  647. if (track_sizing_function.is_minmax())
  648. m_grid_rows.append({ track_sizing_function.minmax().min_grid_size(), track_sizing_function.minmax().max_grid_size() });
  649. else
  650. m_grid_rows.append({ track_sizing_function.grid_size(), track_sizing_function.grid_size() });
  651. }
  652. break;
  653. case CSS::ExplicitGridTrack::Type::Default:
  654. m_grid_rows.append({ track_in_list.grid_size(), track_in_list.grid_size() });
  655. break;
  656. default:
  657. VERIFY_NOT_REACHED();
  658. }
  659. }
  660. }
  661. for (int column_index = m_grid_columns.size(); column_index < occupation_grid.column_count(); column_index++)
  662. m_grid_columns.append({ CSS::GridSize::make_auto(), CSS::GridSize::make_auto() });
  663. for (int row_index = m_grid_rows.size(); row_index < occupation_grid.row_count(); row_index++)
  664. m_grid_rows.append({ CSS::GridSize::make_auto(), CSS::GridSize::make_auto() });
  665. // https://www.w3.org/TR/css-grid-2/#algo-overview
  666. // 12.1. Grid Sizing Algorithm
  667. // 1. First, the track sizing algorithm is used to resolve the sizes of the grid columns.
  668. // In this process, any grid item which is subgridded in the grid container’s inline axis is treated
  669. // as empty and its grid items (the grandchildren) are treated as direct children of the grid
  670. // container (their grandparent). This introspection is recursive.
  671. // Items which are subgridded only in the block axis, and whose grid container size in the inline
  672. // axis depends on the size of its contents are also introspected: since the size of the item in
  673. // this dimension can be dependent on the sizing of its subgridded tracks in the other, the size
  674. // contribution of any such item to this grid’s column sizing (see Resolve Intrinsic Track Sizes) is
  675. // taken under the provision of having determined its track sizing only up to the same point in the
  676. // Grid Sizing Algorithm as this itself. E.g. for the first pass through this step, the item will
  677. // have its tracks sized only through this first step; if a second pass of this step is triggered
  678. // then the item will have completed a first pass through steps 1-3 as well as the second pass of
  679. // this step prior to returning its size for consideration in this grid’s column sizing. Again, this
  680. // introspection is recursive.
  681. // If calculating the layout of a grid item in this step depends on the available space in the block
  682. // axis, assume the available space that it would have if any row with a definite max track sizing
  683. // function had that size and all other rows were infinite. If both the grid container and all
  684. // tracks have definite sizes, also apply align-content to find the final effective size of any gaps
  685. // spanned by such items; otherwise ignore the effects of track alignment in this estimation.
  686. // 2. Next, the track sizing algorithm resolves the sizes of the grid rows.
  687. // In this process, any grid item which is subgridded in the grid container’s block axis is treated
  688. // as empty and its grid items (the grandchildren) are treated as direct children of the grid
  689. // container (their grandparent). This introspection is recursive.
  690. // As with sizing columns, items which are subgridded only in the inline axis, and whose grid
  691. // container size in the block axis depends on the size of its contents are also introspected. (As
  692. // with sizing columns, the size contribution to this grid’s row sizing is taken under the provision
  693. // of having determined its track sizing only up to this corresponding point in the algorithm; and
  694. // again, this introspection is recursive.)
  695. // To find the inline-axis available space for any items whose block-axis size contributions require
  696. // it, use the grid column sizes calculated in the previous step. If the grid container’s inline
  697. // size is definite, also apply justify-content to account for the effective column gap sizes.
  698. // 3. Then, if the min-content contribution of any grid item has changed based on the row sizes and
  699. // alignment calculated in step 2, re-resolve the sizes of the grid columns with the new min-content
  700. // and max-content contributions (once only).
  701. // To find the block-axis available space for any items whose inline-axis size contributions require
  702. // it, use the grid row sizes calculated in the previous step. If the grid container’s block size is
  703. // definite, also apply align-content to account for the effective row gap sizes
  704. // 4. Next, if the min-content contribution of any grid item has changed based on the column sizes and
  705. // alignment calculated in step 3, re-resolve the sizes of the grid rows with the new min-content
  706. // and max-content contributions (once only).
  707. // To find the inline-axis available space for any items whose block-axis size contributions require
  708. // it, use the grid column sizes calculated in the previous step. If the grid container’s inline
  709. // size is definite, also apply justify-content to account for the effective column gap sizes.
  710. // 5. Finally, the grid container is sized using the resulting size of the grid as its content size,
  711. // and the tracks are aligned within the grid container according to the align-content and
  712. // justify-content properties.
  713. // Once the size of each grid area is thus established, the grid items are laid out into their
  714. // respective containing blocks. The grid area’s width and height are considered definite for this
  715. // purpose.
  716. // https://www.w3.org/TR/css-grid-2/#algo-track-sizing
  717. // 12.3. Track Sizing Algorithm
  718. // The remainder of this section is the track sizing algorithm, which calculates from the min and
  719. // max track sizing functions the used track size. Each track has a base size, a <length> which
  720. // grows throughout the algorithm and which will eventually be the track’s final size, and a growth
  721. // limit, a <length> which provides a desired maximum size for the base size. There are 5 steps:
  722. // 1. Initialize Track Sizes
  723. // 2. Resolve Intrinsic Track Sizes
  724. // 3. Maximize Tracks
  725. // 4. Expand Flexible Tracks
  726. // 5. Expand Stretched auto Tracks
  727. // https://www.w3.org/TR/css-grid-2/#algo-init
  728. // 12.4. Initialize Track Sizes
  729. // Initialize each track’s base size and growth limit.
  730. for (auto& grid_column : m_grid_columns) {
  731. // For each track, if the track’s min track sizing function is:
  732. switch (grid_column.min_track_sizing_function.type()) {
  733. // - A fixed sizing function
  734. // Resolve to an absolute length and use that size as the track’s initial base size.
  735. case CSS::GridSize::Type::Length:
  736. if (!grid_column.min_track_sizing_function.length().is_auto())
  737. grid_column.base_size = grid_column.min_track_sizing_function.length().to_px(box);
  738. break;
  739. case CSS::GridSize::Type::Percentage:
  740. grid_column.base_size = grid_column.min_track_sizing_function.percentage().as_fraction() * box_state.content_width();
  741. break;
  742. // - An intrinsic sizing function
  743. // Use an initial base size of zero.
  744. case CSS::GridSize::Type::FlexibleLength:
  745. break;
  746. default:
  747. VERIFY_NOT_REACHED();
  748. }
  749. // For each track, if the track’s max track sizing function is:
  750. switch (grid_column.max_track_sizing_function.type()) {
  751. // - A fixed sizing function
  752. // Resolve to an absolute length and use that size as the track’s initial growth limit.
  753. case CSS::GridSize::Type::Length:
  754. if (!grid_column.max_track_sizing_function.length().is_auto())
  755. grid_column.growth_limit = grid_column.max_track_sizing_function.length().to_px(box);
  756. else
  757. // - An intrinsic sizing function
  758. // Use an initial growth limit of infinity.
  759. grid_column.growth_limit = -1;
  760. break;
  761. case CSS::GridSize::Type::Percentage:
  762. grid_column.growth_limit = grid_column.max_track_sizing_function.percentage().as_fraction() * box_state.content_width();
  763. break;
  764. // - A flexible sizing function
  765. // Use an initial growth limit of infinity.
  766. case CSS::GridSize::Type::FlexibleLength:
  767. grid_column.growth_limit = -1;
  768. break;
  769. default:
  770. VERIFY_NOT_REACHED();
  771. }
  772. // In all cases, if the growth limit is less than the base size, increase the growth limit to match
  773. // the base size.
  774. if (grid_column.growth_limit != -1 && grid_column.growth_limit < grid_column.base_size)
  775. grid_column.growth_limit = grid_column.base_size;
  776. }
  777. // Initialize each track’s base size and growth limit.
  778. for (auto& grid_row : m_grid_rows) {
  779. // For each track, if the track’s min track sizing function is:
  780. switch (grid_row.min_track_sizing_function.type()) {
  781. // - A fixed sizing function
  782. // Resolve to an absolute length and use that size as the track’s initial base size.
  783. case CSS::GridSize::Type::Length:
  784. if (!grid_row.min_track_sizing_function.length().is_auto())
  785. grid_row.base_size = grid_row.min_track_sizing_function.length().to_px(box);
  786. break;
  787. case CSS::GridSize::Type::Percentage:
  788. grid_row.base_size = grid_row.min_track_sizing_function.percentage().as_fraction() * box_state.content_height();
  789. break;
  790. // - An intrinsic sizing function
  791. // Use an initial base size of zero.
  792. case CSS::GridSize::Type::FlexibleLength:
  793. break;
  794. default:
  795. VERIFY_NOT_REACHED();
  796. }
  797. // For each track, if the track’s max track sizing function is:
  798. switch (grid_row.max_track_sizing_function.type()) {
  799. // - A fixed sizing function
  800. // Resolve to an absolute length and use that size as the track’s initial growth limit.
  801. case CSS::GridSize::Type::Length:
  802. if (!grid_row.max_track_sizing_function.length().is_auto())
  803. grid_row.growth_limit = grid_row.max_track_sizing_function.length().to_px(box);
  804. else
  805. // - An intrinsic sizing function
  806. // Use an initial growth limit of infinity.
  807. grid_row.growth_limit = -1;
  808. break;
  809. case CSS::GridSize::Type::Percentage:
  810. grid_row.growth_limit = grid_row.max_track_sizing_function.percentage().as_fraction() * box_state.content_height();
  811. break;
  812. // - A flexible sizing function
  813. // Use an initial growth limit of infinity.
  814. case CSS::GridSize::Type::FlexibleLength:
  815. grid_row.growth_limit = -1;
  816. break;
  817. default:
  818. VERIFY_NOT_REACHED();
  819. }
  820. // In all cases, if the growth limit is less than the base size, increase the growth limit to match
  821. // the base size.
  822. if (grid_row.growth_limit != -1 && grid_row.growth_limit < grid_row.base_size)
  823. grid_row.growth_limit = grid_row.base_size;
  824. }
  825. // https://www.w3.org/TR/css-grid-2/#algo-content
  826. // 12.5. Resolve Intrinsic Track Sizes
  827. // This step resolves intrinsic track sizing functions to absolute lengths. First it resolves those
  828. // sizes based on items that are contained wholly within a single track. Then it gradually adds in
  829. // the space requirements of items that span multiple tracks, evenly distributing the extra space
  830. // across those tracks insofar as possible.
  831. // FIXME: 1. Shim baseline-aligned items so their intrinsic size contributions reflect their baseline
  832. // alignment. For the items in each baseline-sharing group, add a “shim” (effectively, additional
  833. // margin) on the start/end side (for first/last-baseline alignment) of each item so that, when
  834. // start/end-aligned together their baselines align as specified.
  835. // Consider these “shims” as part of the items’ intrinsic size contribution for the purpose of track
  836. // sizing, below. If an item uses multiple intrinsic size contributions, it can have different shims
  837. // for each one.
  838. // 2. Size tracks to fit non-spanning items: For each track with an intrinsic track sizing function and
  839. // not a flexible sizing function, consider the items in it with a span of 1:
  840. int index = 0;
  841. for (auto& grid_column : m_grid_columns) {
  842. if (!grid_column.min_track_sizing_function.is_intrinsic_track_sizing()) {
  843. ++index;
  844. continue;
  845. }
  846. Vector<Box const&> boxes_of_column;
  847. for (auto& positioned_box : positioned_boxes) {
  848. if (positioned_box.column == index && positioned_box.column_span == 1)
  849. boxes_of_column.append(positioned_box.box);
  850. }
  851. // - For min-content minimums:
  852. // If the track has a min-content min track sizing function, set its base size to the maximum of the
  853. // items’ min-content contributions, floored at zero.
  854. // FIXME: Not implemented yet min-content.
  855. // - For max-content minimums:
  856. // If the track has a max-content min track sizing function, set its base size to the maximum of the
  857. // items’ max-content contributions, floored at zero.
  858. // FIXME: Not implemented yet max-content.
  859. // - For auto minimums:
  860. // If the track has an auto min track sizing function and the grid container is being sized under a
  861. // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
  862. // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
  863. // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
  864. // limited by the max track sizing function (which could be the argument to a fit-content() track
  865. // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
  866. // below).
  867. // FIXME: Not implemented yet min-/max-content.
  868. // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
  869. // at zero. The minimum contribution of an item is the smallest outer size it can have.
  870. // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
  871. // containing block in the relevant axis, its minimum contribution is the outer size that would
  872. // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
  873. // contribution is its min-content contribution. Because the minimum contribution often depends on
  874. // the size of the item’s content, it is considered a type of intrinsic size contribution.
  875. float grid_column_width = 0;
  876. for (auto& box_of_column : boxes_of_column)
  877. grid_column_width = max(grid_column_width, calculate_min_content_width(box_of_column));
  878. grid_column.base_size = grid_column_width;
  879. // - For min-content maximums:
  880. // If the track has a min-content max track sizing function, set its growth limit to the maximum of
  881. // the items’ min-content contributions.
  882. // FIXME: Not implemented yet min-content maximums.
  883. // - For max-content maximums:
  884. // If the track has a max-content max track sizing function, set its growth limit to the maximum of
  885. // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
  886. // limit by the fit-content() argument.
  887. // FIXME: Not implemented yet max-content maximums.
  888. // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
  889. // to match the base size.
  890. if (grid_column.growth_limit != -1 && grid_column.growth_limit < grid_column.base_size)
  891. grid_column.growth_limit = grid_column.base_size;
  892. ++index;
  893. }
  894. index = 0;
  895. for (auto& grid_row : m_grid_rows) {
  896. if (!grid_row.min_track_sizing_function.is_intrinsic_track_sizing()) {
  897. ++index;
  898. continue;
  899. }
  900. Vector<PositionedBox&> positioned_boxes_of_row;
  901. for (auto& positioned_box : positioned_boxes) {
  902. if (positioned_box.row == index && positioned_box.row_span == 1)
  903. positioned_boxes_of_row.append(positioned_box);
  904. }
  905. // - For min-content minimums:
  906. // If the track has a min-content min track sizing function, set its base size to the maximum of the
  907. // items’ min-content contributions, floored at zero.
  908. // FIXME: Not implemented yet min-content.
  909. // - For max-content minimums:
  910. // If the track has a max-content min track sizing function, set its base size to the maximum of the
  911. // items’ max-content contributions, floored at zero.
  912. // FIXME: Not implemented yet max-content.
  913. // - For auto minimums:
  914. // If the track has an auto min track sizing function and the grid container is being sized under a
  915. // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
  916. // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
  917. // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
  918. // limited by the max track sizing function (which could be the argument to a fit-content() track
  919. // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
  920. // below).
  921. // FIXME: Not implemented yet min-/max-content.
  922. // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
  923. // at zero. The minimum contribution of an item is the smallest outer size it can have.
  924. // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
  925. // containing block in the relevant axis, its minimum contribution is the outer size that would
  926. // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
  927. // contribution is its min-content contribution. Because the minimum contribution often depends on
  928. // the size of the item’s content, it is considered a type of intrinsic size contribution.
  929. float grid_row_height = 0;
  930. for (auto& positioned_box : positioned_boxes_of_row)
  931. grid_row_height = max(grid_row_height, positioned_box.computed_height);
  932. grid_row.base_size = grid_row_height;
  933. // - For min-content maximums:
  934. // If the track has a min-content max track sizing function, set its growth limit to the maximum of
  935. // the items’ min-content contributions.
  936. // FIXME: Not implemented yet min-content maximums.
  937. // - For max-content maximums:
  938. // If the track has a max-content max track sizing function, set its growth limit to the maximum of
  939. // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
  940. // limit by the fit-content() argument.
  941. // FIXME: Not implemented yet max-content maximums.
  942. // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
  943. // to match the base size.
  944. if (grid_row.growth_limit != -1 && grid_row.growth_limit < grid_row.base_size)
  945. grid_row.growth_limit = grid_row.base_size;
  946. ++index;
  947. }
  948. // https://www.w3.org/TR/css-grid-2/#auto-repeat
  949. // The auto-fit keyword behaves the same as auto-fill, except that after grid item placement any
  950. // empty repeated tracks are collapsed. An empty track is one with no in-flow grid items placed into
  951. // or spanning across it. (This can result in all tracks being collapsed, if they’re all empty.)
  952. if (grid_template_columns.track_list().size() == 1
  953. && grid_template_columns.track_list().first().is_repeat()
  954. && grid_template_columns.track_list().first().repeat().is_auto_fit()) {
  955. auto idx = 0;
  956. for (auto& grid_column : m_grid_columns) {
  957. // A collapsed track is treated as having a fixed track sizing function of 0px, and the gutters on
  958. // either side of it—including any space allotted through distributed alignment—collapse.
  959. if (!occupation_grid.is_occupied(idx, 0)) {
  960. grid_column.base_size = 0;
  961. grid_column.growth_limit = 0;
  962. }
  963. idx++;
  964. }
  965. }
  966. // 3. Increase sizes to accommodate spanning items crossing content-sized tracks: Next, consider the
  967. // items with a span of 2 that do not span a track with a flexible sizing function.
  968. // FIXME: Content-sized tracks not implemented (min-content, etc.)
  969. // 3.1. For intrinsic minimums: First increase the base size of tracks with an intrinsic min track sizing
  970. // function by distributing extra space as needed to accommodate these items’ minimum contributions.
  971. // If the grid container is being sized under a min- or max-content constraint, use the items’
  972. // limited min-content contributions in place of their minimum contributions here. (For an item
  973. // spanning multiple tracks, the upper limit used to calculate its limited min-/max-content
  974. // contribution is the sum of the fixed max track sizing functions of any tracks it spans, and is
  975. // applied if it only spans such tracks.)
  976. // 3.2. For content-based minimums: Next continue to increase the base size of tracks with a min track
  977. // sizing function of min-content or max-content by distributing extra space as needed to account
  978. // for these items' min-content contributions.
  979. // 3.3. For max-content minimums: Next, if the grid container is being sized under a max-content
  980. // constraint, continue to increase the base size of tracks with a min track sizing function of auto
  981. // or max-content by distributing extra space as needed to account for these items' limited
  982. // max-content contributions.
  983. // In all cases, continue to increase the base size of tracks with a min track sizing function of
  984. // max-content by distributing extra space as needed to account for these items' max-content
  985. // contributions.
  986. // 3.4. If at this point any track’s growth limit is now less than its base size, increase its growth
  987. // limit to match its base size.
  988. // 3.5. For intrinsic maximums: Next increase the growth limit of tracks with an intrinsic max track
  989. // sizing function by distributing extra space as needed to account for these items' min-content
  990. // contributions. Mark any tracks whose growth limit changed from infinite to finite in this step as
  991. // infinitely growable for the next step.
  992. // 3.6. For max-content maximums: Lastly continue to increase the growth limit of tracks with a max track
  993. // sizing function of max-content by distributing extra space as needed to account for these items'
  994. // max-content contributions. However, limit the growth of any fit-content() tracks by their
  995. // fit-content() argument.
  996. // Repeat incrementally for items with greater spans until all items have been considered.
  997. // FIXME: 4. Increase sizes to accommodate spanning items crossing flexible tracks: Next, repeat the previous
  998. // step instead considering (together, rather than grouped by span size) all items that do span a
  999. // track with a flexible sizing function while
  1000. // - distributing space only to flexible tracks (i.e. treating all other tracks as having a fixed
  1001. // sizing function)
  1002. // - if the sum of the flexible sizing functions of all flexible tracks spanned by the item is greater
  1003. // than zero, distributing space to such tracks according to the ratios of their flexible sizing
  1004. // functions rather than distributing space equally
  1005. // FIXME: 5. If any track still has an infinite growth limit (because, for example, it had no items placed in
  1006. // it or it is a flexible track), set its growth limit to its base size.
  1007. // https://www.w3.org/TR/css-grid-2/#extra-space
  1008. // 12.5.1. Distributing Extra Space Across Spanned Tracks
  1009. // To distribute extra space by increasing the affected sizes of a set of tracks as required by a
  1010. // set of intrinsic size contributions,
  1011. float sum_of_track_sizes = 0;
  1012. for (auto& it : m_grid_columns)
  1013. sum_of_track_sizes += it.base_size;
  1014. // 1. Maintain separately for each affected base size or growth limit a planned increase, initially
  1015. // set to 0. (This prevents the size increases from becoming order-dependent.)
  1016. // 2. For each considered item,
  1017. // 2.1. Find the space to distribute: Subtract the corresponding size (base size or growth limit) of
  1018. // every spanned track from the item’s size contribution to find the item’s remaining size
  1019. // contribution. (For infinite growth limits, substitute the track’s base size.) This is the space
  1020. // to distribute. Floor it at zero.
  1021. // For base sizes, the limit is its growth limit. For growth limits, the limit is infinity if it is
  1022. // marked as infinitely growable, and equal to the growth limit otherwise. If the affected size was
  1023. // a growth limit and the track is not marked infinitely growable, then each item-incurred increase
  1024. // will be zero.
  1025. // extra-space = max(0, size-contribution - ∑track-sizes)
  1026. for (auto& grid_column : m_grid_columns)
  1027. grid_column.space_to_distribute = max(0, (grid_column.growth_limit == -1 ? grid_column.base_size : grid_column.growth_limit) - grid_column.base_size);
  1028. auto remaining_free_space = box_state.content_width() - sum_of_track_sizes;
  1029. // 2.2. Distribute space up to limits: Find the item-incurred increase for each spanned track with an
  1030. // affected size by: distributing the space equally among such tracks, freezing a track’s
  1031. // item-incurred increase as its affected size + item-incurred increase reaches its limit (and
  1032. // continuing to grow the unfrozen tracks as needed).
  1033. auto count_of_unfrozen_tracks = 0;
  1034. for (auto& grid_column : m_grid_columns) {
  1035. if (grid_column.space_to_distribute > 0)
  1036. count_of_unfrozen_tracks++;
  1037. }
  1038. while (remaining_free_space > 0) {
  1039. if (count_of_unfrozen_tracks == 0)
  1040. break;
  1041. auto free_space_to_distribute_per_track = remaining_free_space / count_of_unfrozen_tracks;
  1042. for (auto& grid_column : m_grid_columns) {
  1043. if (grid_column.space_to_distribute == 0)
  1044. continue;
  1045. // 2.4. For each affected track, if the track’s item-incurred increase is larger than the track’s planned
  1046. // increase set the track’s planned increase to that value.
  1047. if (grid_column.space_to_distribute <= free_space_to_distribute_per_track) {
  1048. grid_column.planned_increase += grid_column.space_to_distribute;
  1049. remaining_free_space -= grid_column.space_to_distribute;
  1050. grid_column.space_to_distribute = 0;
  1051. } else {
  1052. grid_column.space_to_distribute -= free_space_to_distribute_per_track;
  1053. grid_column.planned_increase += free_space_to_distribute_per_track;
  1054. remaining_free_space -= free_space_to_distribute_per_track;
  1055. }
  1056. }
  1057. count_of_unfrozen_tracks = 0;
  1058. for (auto& grid_column : m_grid_columns) {
  1059. if (grid_column.space_to_distribute > 0)
  1060. count_of_unfrozen_tracks++;
  1061. }
  1062. if (remaining_free_space == 0)
  1063. break;
  1064. }
  1065. // 2.3. Distribute space beyond limits: If space remains after all tracks are frozen, unfreeze and
  1066. // continue to distribute space to the item-incurred increase of…
  1067. // - when accommodating minimum contributions or accommodating min-content contributions: any affected
  1068. // track that happens to also have an intrinsic max track sizing function; if there are no such
  1069. // tracks, then all affected tracks.
  1070. // - when accommodating max-content contributions: any affected track that happens to also have a
  1071. // max-content max track sizing function; if there are no such tracks, then all affected tracks.
  1072. // - when handling any intrinsic growth limit: all affected tracks.
  1073. // For this purpose, the max track sizing function of a fit-content() track is treated as
  1074. // max-content until it reaches the limit specified as the fit-content() argument, after which it is
  1075. // treated as having a fixed sizing function of that argument.
  1076. // This step prioritizes the distribution of space for accommodating space required by the
  1077. // tracks’ min track sizing functions beyond their current growth limits based on the types of their
  1078. // max track sizing functions.
  1079. // 3. Update the tracks' affected sizes by adding in the planned increase so that the next round of
  1080. // space distribution will account for the increase. (If the affected size is an infinite growth
  1081. // limit, set it to the track’s base size plus the planned increase.)
  1082. for (auto& grid_column : m_grid_columns)
  1083. grid_column.base_size += grid_column.planned_increase;
  1084. // FIXME: Do for rows.
  1085. // https://www.w3.org/TR/css-grid-2/#algo-grow-tracks
  1086. // 12.6. Maximize Tracks
  1087. // If the free space is positive, distribute it equally to the base sizes of all tracks, freezing
  1088. // tracks as they reach their growth limits (and continuing to grow the unfrozen tracks as needed).
  1089. auto free_space = get_free_space_x(box);
  1090. while (free_space > 0) {
  1091. auto free_space_to_distribute_per_track = free_space / m_grid_columns.size();
  1092. for (auto& grid_column : m_grid_columns) {
  1093. if (grid_column.growth_limit != -1)
  1094. grid_column.base_size = min(grid_column.growth_limit, grid_column.base_size + free_space_to_distribute_per_track);
  1095. else
  1096. grid_column.base_size = grid_column.base_size + free_space_to_distribute_per_track;
  1097. }
  1098. if (get_free_space_x(box) == free_space)
  1099. break;
  1100. free_space = get_free_space_x(box);
  1101. }
  1102. free_space = get_free_space_y(box);
  1103. while (free_space > 0) {
  1104. auto free_space_to_distribute_per_track = free_space / m_grid_rows.size();
  1105. for (auto& grid_row : m_grid_rows)
  1106. grid_row.base_size = min(grid_row.growth_limit, grid_row.base_size + free_space_to_distribute_per_track);
  1107. if (get_free_space_y(box) == free_space)
  1108. break;
  1109. free_space = get_free_space_y(box);
  1110. }
  1111. if (free_space == -1) {
  1112. for (auto& grid_row : m_grid_rows) {
  1113. if (grid_row.growth_limit != -1)
  1114. grid_row.base_size = grid_row.growth_limit;
  1115. }
  1116. }
  1117. // For the purpose of this step: if sizing the grid container under a max-content constraint, the
  1118. // free space is infinite; if sizing under a min-content constraint, the free space is zero.
  1119. // If this would cause the grid to be larger than the grid container’s inner size as limited by its
  1120. // max-width/height, then redo this step, treating the available grid space as equal to the grid
  1121. // container’s inner size when it’s sized to its max-width/height.
  1122. // https://drafts.csswg.org/css-grid/#algo-flex-tracks
  1123. // 12.7. Expand Flexible Tracks
  1124. // This step sizes flexible tracks using the largest value it can assign to an fr without exceeding
  1125. // the available space.
  1126. // First, find the grid’s used flex fraction:
  1127. auto column_flex_factor_sum = 0;
  1128. for (auto& grid_column : m_grid_columns) {
  1129. if (grid_column.min_track_sizing_function.is_flexible_length())
  1130. column_flex_factor_sum++;
  1131. }
  1132. // See 12.7.1.
  1133. // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  1134. // than 1, set it to 1 instead.
  1135. if (column_flex_factor_sum < 1)
  1136. column_flex_factor_sum = 1;
  1137. // See 12.7.1.
  1138. float sized_column_widths = 0;
  1139. for (auto& grid_column : m_grid_columns) {
  1140. if (!grid_column.min_track_sizing_function.is_flexible_length())
  1141. sized_column_widths += grid_column.base_size;
  1142. }
  1143. // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  1144. double free_horizontal_space = box_state.content_width() - sized_column_widths;
  1145. // If the free space is zero or if sizing the grid container under a min-content constraint:
  1146. // The used flex fraction is zero.
  1147. // FIXME: Add min-content constraint check.
  1148. // Otherwise, if the free space is a definite length:
  1149. // The used flex fraction is the result of finding the size of an fr using all of the grid tracks
  1150. // and a space to fill of the available grid space.
  1151. if (free_horizontal_space > 0) {
  1152. for (auto& grid_column : m_grid_columns) {
  1153. if (grid_column.min_track_sizing_function.is_flexible_length()) {
  1154. // See 12.7.1.
  1155. // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  1156. auto hypothetical_fr_size = static_cast<double>(1.0 / column_flex_factor_sum) * free_horizontal_space;
  1157. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  1158. // greater than the track’s base size, set its base size to that product.
  1159. grid_column.base_size = max(grid_column.base_size, hypothetical_fr_size);
  1160. }
  1161. }
  1162. }
  1163. // First, find the grid’s used flex fraction:
  1164. auto row_flex_factor_sum = 0;
  1165. for (auto& grid_row : m_grid_rows) {
  1166. if (grid_row.min_track_sizing_function.is_flexible_length())
  1167. row_flex_factor_sum++;
  1168. }
  1169. // See 12.7.1.
  1170. // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  1171. // than 1, set it to 1 instead.
  1172. if (row_flex_factor_sum < 1)
  1173. row_flex_factor_sum = 1;
  1174. // See 12.7.1.
  1175. float sized_row_heights = 0;
  1176. for (auto& grid_row : m_grid_rows) {
  1177. if (!grid_row.min_track_sizing_function.is_flexible_length())
  1178. sized_row_heights += grid_row.base_size;
  1179. }
  1180. // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  1181. double free_vertical_space = box_state.content_height() - sized_row_heights;
  1182. // If the free space is zero or if sizing the grid container under a min-content constraint:
  1183. // The used flex fraction is zero.
  1184. // FIXME: Add min-content constraint check.
  1185. // Otherwise, if the free space is a definite length:
  1186. // The used flex fraction is the result of finding the size of an fr using all of the grid tracks
  1187. // and a space to fill of the available grid space.
  1188. if (free_vertical_space > 0) {
  1189. for (auto& grid_row : m_grid_rows) {
  1190. if (grid_row.min_track_sizing_function.is_flexible_length()) {
  1191. // See 12.7.1.
  1192. // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  1193. auto hypothetical_fr_size = static_cast<double>(1.0 / row_flex_factor_sum) * free_vertical_space;
  1194. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  1195. // greater than the track’s base size, set its base size to that product.
  1196. grid_row.base_size = max(grid_row.base_size, hypothetical_fr_size);
  1197. }
  1198. }
  1199. }
  1200. // Otherwise, if the free space is an indefinite length:
  1201. // FIXME: No tracks will have indefinite length as per current implementation.
  1202. // The used flex fraction is the maximum of:
  1203. // For each flexible track, if the flexible track’s flex factor is greater than one, the result of
  1204. // dividing the track’s base size by its flex factor; otherwise, the track’s base size.
  1205. // For each grid item that crosses a flexible track, the result of finding the size of an fr using
  1206. // all the grid tracks that the item crosses and a space to fill of the item’s max-content
  1207. // contribution.
  1208. // If using this flex fraction would cause the grid to be smaller than the grid container’s
  1209. // min-width/height (or larger than the grid container’s max-width/height), then redo this step,
  1210. // treating the free space as definite and the available grid space as equal to the grid container’s
  1211. // inner size when it’s sized to its min-width/height (max-width/height).
  1212. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  1213. // greater than the track’s base size, set its base size to that product.
  1214. // https://drafts.csswg.org/css-grid/#algo-find-fr-size
  1215. // 12.7.1. Find the Size of an fr
  1216. // This algorithm finds the largest size that an fr unit can be without exceeding the target size.
  1217. // It must be called with a set of grid tracks and some quantity of space to fill.
  1218. // 1. Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  1219. // 2. Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  1220. // than 1, set it to 1 instead.
  1221. // 3. Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  1222. // FIXME: 4. If the product of the hypothetical fr size and a flexible track’s flex factor is less than the
  1223. // track’s base size, restart this algorithm treating all such tracks as inflexible.
  1224. // 5. Return the hypothetical fr size.
  1225. // https://drafts.csswg.org/css-grid/#algo-stretch
  1226. // 12.8. Stretch auto Tracks
  1227. // When the content-distribution property of the grid container is normal or stretch in this axis,
  1228. // this step expands tracks that have an auto max track sizing function by dividing any remaining
  1229. // positive, definite free space equally amongst them. If the free space is indefinite, but the grid
  1230. // container has a definite min-width/height, use that size to calculate the free space for this
  1231. // step instead.
  1232. float used_horizontal_space = 0;
  1233. for (auto& grid_column : m_grid_columns) {
  1234. if (!(grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto()))
  1235. used_horizontal_space += grid_column.base_size;
  1236. }
  1237. float remaining_horizontal_space = box_state.content_width() - used_horizontal_space;
  1238. auto count_of_auto_max_column_tracks = 0;
  1239. for (auto& grid_column : m_grid_columns) {
  1240. if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
  1241. count_of_auto_max_column_tracks++;
  1242. }
  1243. for (auto& grid_column : m_grid_columns) {
  1244. if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
  1245. grid_column.base_size = max(grid_column.base_size, remaining_horizontal_space / count_of_auto_max_column_tracks);
  1246. }
  1247. float used_vertical_space = 0;
  1248. for (auto& grid_row : m_grid_rows) {
  1249. if (!(grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto()))
  1250. used_vertical_space += grid_row.base_size;
  1251. }
  1252. float remaining_vertical_space = box_state.content_height() - used_vertical_space;
  1253. auto count_of_auto_max_row_tracks = 0;
  1254. for (auto& grid_row : m_grid_rows) {
  1255. if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
  1256. count_of_auto_max_row_tracks++;
  1257. }
  1258. for (auto& grid_row : m_grid_rows) {
  1259. if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
  1260. grid_row.base_size = max(grid_row.base_size, remaining_vertical_space / count_of_auto_max_row_tracks);
  1261. }
  1262. auto layout_box = [&](int row_start, int row_end, int column_start, int column_end, Box const& child_box) -> void {
  1263. auto& child_box_state = m_state.get_mutable(child_box);
  1264. float x_start = 0;
  1265. float x_end = 0;
  1266. float y_start = 0;
  1267. float y_end = 0;
  1268. for (int i = 0; i < column_start; i++)
  1269. x_start += m_grid_columns[i].base_size;
  1270. for (int i = 0; i < column_end; i++)
  1271. x_end += m_grid_columns[i].base_size;
  1272. for (int i = 0; i < row_start; i++)
  1273. y_start += m_grid_rows[i].base_size;
  1274. for (int i = 0; i < row_end; i++)
  1275. y_end += m_grid_rows[i].base_size;
  1276. child_box_state.set_content_width(x_end - x_start);
  1277. child_box_state.set_content_height(y_end - y_start);
  1278. child_box_state.offset = { x_start, y_start };
  1279. auto available_space_for_children = AvailableSpace(AvailableSize::make_definite(child_box_state.content_width()), AvailableSize::make_definite(child_box_state.content_height()));
  1280. if (auto independent_formatting_context = layout_inside(child_box, LayoutMode::Normal, available_space_for_children))
  1281. independent_formatting_context->parent_context_did_dimension_child_root_box();
  1282. };
  1283. for (auto& positioned_box : positioned_boxes) {
  1284. auto resolved_span = positioned_box.row + positioned_box.row_span > static_cast<int>(m_grid_rows.size()) ? static_cast<int>(m_grid_rows.size()) - positioned_box.row : positioned_box.row_span;
  1285. layout_box(positioned_box.row, positioned_box.row + resolved_span, positioned_box.column, positioned_box.column + positioned_box.column_span, positioned_box.box);
  1286. }
  1287. float total_y = 0;
  1288. for (auto& grid_row : m_grid_rows)
  1289. total_y += grid_row.base_size;
  1290. m_automatic_content_height = total_y;
  1291. }
  1292. float GridFormattingContext::automatic_content_height() const
  1293. {
  1294. return m_automatic_content_height;
  1295. }
  1296. bool GridFormattingContext::is_auto_positioned_row(CSS::GridTrackPlacement const& grid_row_start, CSS::GridTrackPlacement const& grid_row_end) const
  1297. {
  1298. return is_auto_positioned_track(grid_row_start, grid_row_end);
  1299. }
  1300. bool GridFormattingContext::is_auto_positioned_column(CSS::GridTrackPlacement const& grid_column_start, CSS::GridTrackPlacement const& grid_column_end) const
  1301. {
  1302. return is_auto_positioned_track(grid_column_start, grid_column_end);
  1303. }
  1304. bool GridFormattingContext::is_auto_positioned_track(CSS::GridTrackPlacement const& grid_track_start, CSS::GridTrackPlacement const& grid_track_end) const
  1305. {
  1306. return grid_track_start.is_auto_positioned() && grid_track_end.is_auto_positioned();
  1307. }
  1308. float GridFormattingContext::get_free_space_x(Box const& box)
  1309. {
  1310. // https://www.w3.org/TR/css-grid-2/#algo-terms
  1311. // free space: Equal to the available grid space minus the sum of the base sizes of all the grid
  1312. // tracks (including gutters), floored at zero. If available grid space is indefinite, the free
  1313. // space is indefinite as well.
  1314. // FIXME: do indefinite space
  1315. auto sum_base_sizes = 0;
  1316. for (auto& grid_column : m_grid_columns)
  1317. sum_base_sizes += grid_column.base_size;
  1318. auto& box_state = m_state.get_mutable(box);
  1319. return max(0, box_state.content_width() - sum_base_sizes);
  1320. }
  1321. float GridFormattingContext::get_free_space_y(Box const& box)
  1322. {
  1323. // https://www.w3.org/TR/css-grid-2/#algo-terms
  1324. // free space: Equal to the available grid space minus the sum of the base sizes of all the grid
  1325. // tracks (including gutters), floored at zero. If available grid space is indefinite, the free
  1326. // space is indefinite as well.
  1327. auto sum_base_sizes = 0;
  1328. for (auto& grid_row : m_grid_rows)
  1329. sum_base_sizes += grid_row.base_size;
  1330. auto& box_state = m_state.get_mutable(box);
  1331. if (box_state.has_definite_height())
  1332. return max(0, absolute_content_rect(box, m_state).height() - sum_base_sizes);
  1333. return -1;
  1334. }
  1335. int GridFormattingContext::get_line_index_by_line_name(String const& needle, CSS::GridTrackSizeList grid_track_size_list)
  1336. {
  1337. if (grid_track_size_list.track_list().size() == 0)
  1338. return -1;
  1339. auto repeated_tracks_count = 0;
  1340. for (size_t x = 0; x < grid_track_size_list.track_list().size(); x++) {
  1341. if (grid_track_size_list.track_list()[x].is_repeat()) {
  1342. // FIXME: Calculate amount of columns/rows if auto-fill/fit
  1343. if (!grid_track_size_list.track_list()[x].repeat().is_default())
  1344. return -1;
  1345. auto repeat = grid_track_size_list.track_list()[x].repeat().grid_track_size_list();
  1346. for (size_t y = 0; y < repeat.track_list().size(); y++) {
  1347. for (size_t z = 0; z < repeat.line_names()[y].size(); z++) {
  1348. if (repeat.line_names()[y][z] == needle)
  1349. return x + repeated_tracks_count;
  1350. repeated_tracks_count++;
  1351. }
  1352. }
  1353. } else {
  1354. for (size_t y = 0; y < grid_track_size_list.line_names()[x].size(); y++) {
  1355. if (grid_track_size_list.line_names()[x][y] == needle)
  1356. return x + repeated_tracks_count;
  1357. }
  1358. }
  1359. }
  1360. for (size_t y = 0; y < grid_track_size_list.line_names()[grid_track_size_list.track_list().size()].size(); y++) {
  1361. if (grid_track_size_list.line_names()[grid_track_size_list.track_list().size()][y] == needle)
  1362. return grid_track_size_list.track_list().size() + repeated_tracks_count;
  1363. }
  1364. return -1;
  1365. }
  1366. OccupationGrid::OccupationGrid(int column_count, int row_count)
  1367. {
  1368. Vector<bool> occupation_grid_row;
  1369. for (int column_index = 0; column_index < max(column_count, 1); column_index++)
  1370. occupation_grid_row.append(false);
  1371. for (int row_index = 0; row_index < max(row_count, 1); row_index++)
  1372. m_occupation_grid.append(occupation_grid_row);
  1373. }
  1374. void OccupationGrid::maybe_add_column(int needed_number_of_columns)
  1375. {
  1376. if (needed_number_of_columns <= column_count())
  1377. return;
  1378. auto column_count_before_modification = column_count();
  1379. for (auto& occupation_grid_row : m_occupation_grid)
  1380. for (int idx = 0; idx < needed_number_of_columns - column_count_before_modification; idx++)
  1381. occupation_grid_row.append(false);
  1382. }
  1383. void OccupationGrid::maybe_add_row(int needed_number_of_rows)
  1384. {
  1385. if (needed_number_of_rows <= row_count())
  1386. return;
  1387. Vector<bool> new_occupation_grid_row;
  1388. for (int idx = 0; idx < column_count(); idx++)
  1389. new_occupation_grid_row.append(false);
  1390. for (int idx = 0; idx < needed_number_of_rows - row_count(); idx++)
  1391. m_occupation_grid.append(new_occupation_grid_row);
  1392. }
  1393. void OccupationGrid::set_occupied(int column_start, int column_end, int row_start, int row_end)
  1394. {
  1395. for (int row_index = 0; row_index < row_count(); row_index++) {
  1396. if (row_index >= row_start && row_index < row_end) {
  1397. for (int column_index = 0; column_index < column_count(); column_index++) {
  1398. if (column_index >= column_start && column_index < column_end)
  1399. set_occupied(column_index, row_index);
  1400. }
  1401. }
  1402. }
  1403. }
  1404. void OccupationGrid::set_occupied(int column_index, int row_index)
  1405. {
  1406. m_occupation_grid[row_index][column_index] = true;
  1407. }
  1408. bool OccupationGrid::is_occupied(int column_index, int row_index)
  1409. {
  1410. return m_occupation_grid[row_index][column_index];
  1411. }
  1412. }