GridFormattingContext.cpp 101 KB

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