GridFormattingContext.cpp 105 KB

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