GridFormattingContext.cpp 85 KB

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