GridFormattingContext.cpp 88 KB

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