GridFormattingContext.cpp 111 KB

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