GridFormattingContext.cpp 88 KB

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