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