GridFormattingContext.cpp 101 KB

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