GridFormattingContext.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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 should_skip_is_anonymous_text_run = [&](Box& child_box) -> bool {
  18. if (child_box.is_anonymous() && !child_box.first_child_of_type<BlockContainer>()) {
  19. bool contains_only_white_space = true;
  20. child_box.for_each_in_subtree([&](auto const& node) {
  21. if (!is<TextNode>(node) || !static_cast<TextNode const&>(node).dom_node().data().is_whitespace()) {
  22. contains_only_white_space = false;
  23. return IterationDecision::Break;
  24. }
  25. return IterationDecision::Continue;
  26. });
  27. if (contains_only_white_space)
  28. return true;
  29. }
  30. return false;
  31. };
  32. // https://drafts.csswg.org/css-grid/#overview-placement
  33. // 2.2. Placing Items
  34. // The contents of the grid container are organized into individual grid items (analogous to
  35. // flex items), which are then assigned to predefined areas in the grid. They can be explicitly
  36. // placed using coordinates through the grid-placement properties or implicitly placed into
  37. // empty areas using auto-placement.
  38. struct PositionedBox {
  39. Box const& box;
  40. int row { 0 };
  41. int row_span { 1 };
  42. int column { 0 };
  43. int column_span { 1 };
  44. float computed_height { 0 };
  45. };
  46. Vector<PositionedBox> positioned_boxes;
  47. Vector<Box const&> boxes_to_place;
  48. box.for_each_child_of_type<Box>([&](Box& child_box) {
  49. if (should_skip_is_anonymous_text_run(child_box))
  50. return IterationDecision::Continue;
  51. boxes_to_place.append(child_box);
  52. return IterationDecision::Continue;
  53. });
  54. auto occupation_grid = OccupationGrid(static_cast<int>(box.computed_values().grid_template_columns().size()), static_cast<int>(box.computed_values().grid_template_rows().size()));
  55. // https://drafts.csswg.org/css-grid/#auto-placement-algo
  56. // 8.5. Grid Item Placement Algorithm
  57. // FIXME: 0. Generate anonymous grid items
  58. // 1. Position anything that's not auto-positioned.
  59. for (size_t i = 0; i < boxes_to_place.size(); i++) {
  60. auto const& child_box = boxes_to_place[i];
  61. if (is_auto_positioned_row(child_box.computed_values().grid_row_start(), child_box.computed_values().grid_row_end())
  62. || is_auto_positioned_column(child_box.computed_values().grid_column_start(), child_box.computed_values().grid_column_end()))
  63. continue;
  64. int row_start = child_box.computed_values().grid_row_start().raw_value();
  65. int row_end = child_box.computed_values().grid_row_end().raw_value();
  66. int column_start = child_box.computed_values().grid_column_start().raw_value();
  67. int column_end = child_box.computed_values().grid_column_end().raw_value();
  68. // https://drafts.csswg.org/css-grid/#line-placement
  69. // 8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
  70. // https://drafts.csswg.org/css-grid/#grid-placement-slot
  71. // FIXME: <custom-ident>
  72. // First attempt to match the grid area’s edge to a named grid area: if there is a grid line whose
  73. // line name is <custom-ident>-start (for grid-*-start) / <custom-ident>-end (for grid-*-end),
  74. // contributes the first such line to the grid item’s placement.
  75. // Note: Named grid areas automatically generate implicitly-assigned line names of this form, so
  76. // specifying grid-row-start: foo will choose the start edge of that named grid area (unless another
  77. // line named foo-start was explicitly specified before it).
  78. // Otherwise, treat this as if the integer 1 had been specified along with the <custom-ident>.
  79. // https://drafts.csswg.org/css-grid/#grid-placement-int
  80. // [ <integer [−∞,−1]> | <integer [1,∞]> ] && <custom-ident>?
  81. // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
  82. // instead counts in reverse, starting from the end edge of the explicit grid.
  83. if (row_end < 0)
  84. row_end = occupation_grid.row_count() + row_end + 2;
  85. if (column_end < 0)
  86. column_end = occupation_grid.column_count() + column_end + 2;
  87. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  88. // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
  89. // of finding this position.
  90. // An <integer> value of zero makes the declaration invalid.
  91. // https://drafts.csswg.org/css-grid/#grid-placement-span-int
  92. // span && [ <integer [1,∞]> || <custom-ident> ]
  93. // Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid
  94. // item’s grid area is N lines from its opposite edge in the corresponding direction. For example,
  95. // grid-column-end: span 2 indicates the second grid line in the endward direction from the
  96. // grid-column-start line.
  97. int row_span = 1;
  98. int column_span = 1;
  99. if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_span())
  100. row_span = child_box.computed_values().grid_row_end().raw_value();
  101. if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_span())
  102. column_span = child_box.computed_values().grid_column_end().raw_value();
  103. if (child_box.computed_values().grid_row_end().is_position() && child_box.computed_values().grid_row_start().is_span()) {
  104. row_span = child_box.computed_values().grid_row_start().raw_value();
  105. row_start = row_end - row_span;
  106. }
  107. if (child_box.computed_values().grid_column_end().is_position() && child_box.computed_values().grid_column_start().is_span()) {
  108. column_span = child_box.computed_values().grid_column_start().raw_value();
  109. column_start = column_end - column_span;
  110. }
  111. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  112. // lines with that name exist, all implicit grid lines on the side of the explicit grid
  113. // corresponding to the search direction are assumed to have that name for the purpose of counting
  114. // this span.
  115. // https://drafts.csswg.org/css-grid/#grid-placement-auto
  116. // auto
  117. // The property contributes nothing to the grid item’s placement, indicating auto-placement or a
  118. // default span of one. (See § 8 Placing Grid Items, above.)
  119. // https://drafts.csswg.org/css-grid/#grid-placement-errors
  120. // 8.3.1. Grid Placement Conflict Handling
  121. // If the placement for a grid item contains two lines, and the start line is further end-ward than
  122. // the end line, swap the two lines. If the start line is equal to the end line, remove the end
  123. // line.
  124. if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_position()) {
  125. if (row_start > row_end)
  126. swap(row_start, row_end);
  127. if (row_start != row_end)
  128. row_span = row_end - row_start;
  129. }
  130. if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_position()) {
  131. if (column_start > column_end)
  132. swap(column_start, column_end);
  133. if (column_start != column_end)
  134. column_span = column_end - column_start;
  135. }
  136. // If the placement contains two spans, remove the one contributed by the end grid-placement
  137. // property.
  138. if (child_box.computed_values().grid_row_start().is_span() && child_box.computed_values().grid_row_end().is_span())
  139. row_span = child_box.computed_values().grid_row_start().raw_value();
  140. if (child_box.computed_values().grid_column_start().is_span() && child_box.computed_values().grid_column_end().is_span())
  141. column_span = child_box.computed_values().grid_column_start().raw_value();
  142. // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.
  143. row_start -= 1;
  144. column_start -= 1;
  145. positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
  146. occupation_grid.maybe_add_row(row_start + row_span);
  147. occupation_grid.maybe_add_column(column_start + column_span);
  148. occupation_grid.set_occupied(column_start, column_start + column_span, row_start, row_start + row_span);
  149. boxes_to_place.remove(i);
  150. i--;
  151. }
  152. // 2. Process the items locked to a given row.
  153. // FIXME: Do "dense" packing
  154. for (size_t i = 0; i < boxes_to_place.size(); i++) {
  155. auto const& child_box = boxes_to_place[i];
  156. if (is_auto_positioned_row(child_box.computed_values().grid_row_start(), child_box.computed_values().grid_row_end()))
  157. continue;
  158. int row_start = child_box.computed_values().grid_row_start().raw_value();
  159. int row_end = child_box.computed_values().grid_row_end().raw_value();
  160. // https://drafts.csswg.org/css-grid/#line-placement
  161. // 8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
  162. // https://drafts.csswg.org/css-grid/#grid-placement-slot
  163. // FIXME: <custom-ident>
  164. // First attempt to match the grid area’s edge to a named grid area: if there is a grid line whose
  165. // line name is <custom-ident>-start (for grid-*-start) / <custom-ident>-end (for grid-*-end),
  166. // contributes the first such line to the grid item’s placement.
  167. // Note: Named grid areas automatically generate implicitly-assigned line names of this form, so
  168. // specifying grid-row-start: foo will choose the start edge of that named grid area (unless another
  169. // line named foo-start was explicitly specified before it).
  170. // Otherwise, treat this as if the integer 1 had been specified along with the <custom-ident>.
  171. // https://drafts.csswg.org/css-grid/#grid-placement-int
  172. // [ <integer [−∞,−1]> | <integer [1,∞]> ] && <custom-ident>?
  173. // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
  174. // instead counts in reverse, starting from the end edge of the explicit grid.
  175. if (row_end < 0)
  176. row_end = occupation_grid.row_count() + row_end + 2;
  177. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  178. // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
  179. // of finding this position.
  180. // An <integer> value of zero makes the declaration invalid.
  181. // https://drafts.csswg.org/css-grid/#grid-placement-span-int
  182. // span && [ <integer [1,∞]> || <custom-ident> ]
  183. // Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid
  184. // item’s grid area is N lines from its opposite edge in the corresponding direction. For example,
  185. // grid-column-end: span 2 indicates the second grid line in the endward direction from the
  186. // grid-column-start line.
  187. int row_span = 1;
  188. if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_span())
  189. row_span = child_box.computed_values().grid_row_end().raw_value();
  190. if (child_box.computed_values().grid_row_end().is_position() && child_box.computed_values().grid_row_start().is_span()) {
  191. row_span = child_box.computed_values().grid_row_start().raw_value();
  192. row_start = row_end - row_span;
  193. // FIXME: Remove me once have implemented spans overflowing into negative indexes, e.g., grid-row: span 2 / 1
  194. if (row_start < 0)
  195. row_start = 1;
  196. }
  197. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  198. // lines with that name exist, all implicit grid lines on the side of the explicit grid
  199. // corresponding to the search direction are assumed to have that name for the purpose of counting
  200. // this span.
  201. // https://drafts.csswg.org/css-grid/#grid-placement-auto
  202. // auto
  203. // The property contributes nothing to the grid item’s placement, indicating auto-placement or a
  204. // default span of one. (See § 8 Placing Grid Items, above.)
  205. // https://drafts.csswg.org/css-grid/#grid-placement-errors
  206. // 8.3.1. Grid Placement Conflict Handling
  207. // If the placement for a grid item contains two lines, and the start line is further end-ward than
  208. // the end line, swap the two lines. If the start line is equal to the end line, remove the end
  209. // line.
  210. if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_position()) {
  211. if (row_start > row_end)
  212. swap(row_start, row_end);
  213. if (row_start != row_end)
  214. row_span = row_end - row_start;
  215. }
  216. // If the placement contains two spans, remove the one contributed by the end grid-placement
  217. // property.
  218. if (child_box.computed_values().grid_row_start().is_span() && child_box.computed_values().grid_row_end().is_span())
  219. row_span = child_box.computed_values().grid_row_start().raw_value();
  220. // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.
  221. row_start -= 1;
  222. occupation_grid.maybe_add_row(row_start + row_span);
  223. int column_start = 0;
  224. auto column_span = child_box.computed_values().grid_column_start().is_span() ? child_box.computed_values().grid_column_start().raw_value() : 1;
  225. bool found_available_column = false;
  226. for (int column_index = column_start; column_index < occupation_grid.column_count(); column_index++) {
  227. if (!occupation_grid.is_occupied(column_index, row_start)) {
  228. found_available_column = true;
  229. column_start = column_index;
  230. break;
  231. }
  232. }
  233. if (!found_available_column) {
  234. column_start = occupation_grid.column_count();
  235. occupation_grid.maybe_add_column(column_start + column_span);
  236. }
  237. occupation_grid.set_occupied(column_start, column_start + column_span, row_start, row_start + row_span);
  238. positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
  239. boxes_to_place.remove(i);
  240. i--;
  241. }
  242. // 3. Determine the columns in the implicit grid.
  243. // NOTE: "implicit grid" here is the same as the occupation_grid
  244. // 3.1. Start with the columns from the explicit grid.
  245. // NOTE: Done in step 1.
  246. // 3.2. Among all the items with a definite column position (explicitly positioned items, items
  247. // positioned in the previous step, and items not yet positioned but with a definite column) add
  248. // columns to the beginning and end of the implicit grid as necessary to accommodate those items.
  249. // NOTE: "Explicitly positioned items" and "items positioned in the previous step" done in step 1
  250. // and 2, respectively. Adding columns for "items not yet positioned but with a definite column"
  251. // will be done in step 4.
  252. // 3.3. If the largest column span among all the items without a definite column position is larger
  253. // than the width of the implicit grid, add columns to the end of the implicit grid to accommodate
  254. // that column span.
  255. // NOTE: Done in step 1, 2, and will be done in step 4.
  256. // 4. Position the remaining grid items.
  257. // For each grid item that hasn't been positioned by the previous steps, in order-modified document
  258. // order:
  259. auto auto_placement_cursor_x = 0;
  260. auto auto_placement_cursor_y = 0;
  261. for (size_t i = 0; i < boxes_to_place.size(); i++) {
  262. auto const& child_box = boxes_to_place[i];
  263. // 4.1. For sparse packing:
  264. // FIXME: no distinction made. See #4.2
  265. // 4.1.1. If the item has a definite column position:
  266. if (!is_auto_positioned_column(child_box.computed_values().grid_column_start(), child_box.computed_values().grid_column_end())) {
  267. int column_start = child_box.computed_values().grid_column_start().raw_value();
  268. int column_end = child_box.computed_values().grid_column_end().raw_value();
  269. // https://drafts.csswg.org/css-grid/#line-placement
  270. // 8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
  271. // https://drafts.csswg.org/css-grid/#grid-placement-slot
  272. // FIXME: <custom-ident>
  273. // First attempt to match the grid area’s edge to a named grid area: if there is a grid line whose
  274. // line name is <custom-ident>-start (for grid-*-start) / <custom-ident>-end (for grid-*-end),
  275. // contributes the first such line to the grid item’s placement.
  276. // Note: Named grid areas automatically generate implicitly-assigned line names of this form, so
  277. // specifying grid-row-start: foo will choose the start edge of that named grid area (unless another
  278. // line named foo-start was explicitly specified before it).
  279. // Otherwise, treat this as if the integer 1 had been specified along with the <custom-ident>.
  280. // https://drafts.csswg.org/css-grid/#grid-placement-int
  281. // [ <integer [−∞,−1]> | <integer [1,∞]> ] && <custom-ident>?
  282. // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
  283. // instead counts in reverse, starting from the end edge of the explicit grid.
  284. if (column_end < 0)
  285. column_end = occupation_grid.column_count() + column_end + 2;
  286. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  287. // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
  288. // of finding this position.
  289. // An <integer> value of zero makes the declaration invalid.
  290. // https://drafts.csswg.org/css-grid/#grid-placement-span-int
  291. // span && [ <integer [1,∞]> || <custom-ident> ]
  292. // Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid
  293. // item’s grid area is N lines from its opposite edge in the corresponding direction. For example,
  294. // grid-column-end: span 2 indicates the second grid line in the endward direction from the
  295. // grid-column-start line.
  296. int column_span = 1;
  297. auto row_span = child_box.computed_values().grid_row_start().is_span() ? child_box.computed_values().grid_row_start().raw_value() : 1;
  298. if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_span())
  299. column_span = child_box.computed_values().grid_column_end().raw_value();
  300. if (child_box.computed_values().grid_column_end().is_position() && child_box.computed_values().grid_column_start().is_span()) {
  301. column_span = child_box.computed_values().grid_column_start().raw_value();
  302. column_start = column_end - column_span;
  303. // FIXME: Remove me once have implemented spans overflowing into negative indexes, e.g., grid-column: span 2 / 1
  304. if (column_start < 0)
  305. column_start = 1;
  306. }
  307. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  308. // lines with that name exist, all implicit grid lines on the side of the explicit grid
  309. // corresponding to the search direction are assumed to have that name for the purpose of counting
  310. // this span.
  311. // https://drafts.csswg.org/css-grid/#grid-placement-auto
  312. // auto
  313. // The property contributes nothing to the grid item’s placement, indicating auto-placement or a
  314. // default span of one. (See § 8 Placing Grid Items, above.)
  315. // https://drafts.csswg.org/css-grid/#grid-placement-errors
  316. // 8.3.1. Grid Placement Conflict Handling
  317. // If the placement for a grid item contains two lines, and the start line is further end-ward than
  318. // the end line, swap the two lines. If the start line is equal to the end line, remove the end
  319. // line.
  320. if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_position()) {
  321. if (column_start > column_end)
  322. swap(column_start, column_end);
  323. if (column_start != column_end)
  324. column_span = column_end - column_start;
  325. }
  326. // If the placement contains two spans, remove the one contributed by the end grid-placement
  327. // property.
  328. if (child_box.computed_values().grid_column_start().is_span() && child_box.computed_values().grid_column_end().is_span())
  329. column_span = child_box.computed_values().grid_column_start().raw_value();
  330. // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.
  331. column_start -= 1;
  332. // 4.1.1.1. Set the column position of the cursor to the grid item's column-start line. If this is
  333. // less than the previous column position of the cursor, increment the row position by 1.
  334. if (column_start < auto_placement_cursor_x)
  335. auto_placement_cursor_y++;
  336. auto_placement_cursor_x = column_start;
  337. occupation_grid.maybe_add_column(auto_placement_cursor_x + column_span);
  338. occupation_grid.maybe_add_row(auto_placement_cursor_y + row_span);
  339. // 4.1.1.2. Increment the cursor's row position until a value is found where the grid item does not
  340. // overlap any occupied grid cells (creating new rows in the implicit grid as necessary).
  341. while (true) {
  342. if (!occupation_grid.is_occupied(column_start, auto_placement_cursor_y)) {
  343. break;
  344. }
  345. auto_placement_cursor_y++;
  346. occupation_grid.maybe_add_row(auto_placement_cursor_y + row_span);
  347. }
  348. // 4.1.1.3. Set the item's row-start line to the cursor's row position, and set the item's row-end
  349. // line according to its span from that position.
  350. occupation_grid.set_occupied(column_start, column_start + column_span, auto_placement_cursor_y, auto_placement_cursor_y + row_span);
  351. positioned_boxes.append({ child_box, auto_placement_cursor_y, row_span, column_start, column_span });
  352. }
  353. // 4.1.2. If the item has an automatic grid position in both axes:
  354. else {
  355. // 4.1.2.1. Increment the column position of the auto-placement cursor until either this item's grid
  356. // area does not overlap any occupied grid cells, or the cursor's column position, plus the item's
  357. // column span, overflow the number of columns in the implicit grid, as determined earlier in this
  358. // algorithm.
  359. auto column_start = 0;
  360. auto column_span = child_box.computed_values().grid_column_start().is_span() ? child_box.computed_values().grid_column_start().raw_value() : 1;
  361. auto row_start = 0;
  362. auto row_span = child_box.computed_values().grid_row_start().is_span() ? child_box.computed_values().grid_row_start().raw_value() : 1;
  363. auto found_unoccupied_area = false;
  364. for (int row_index = auto_placement_cursor_y; row_index < occupation_grid.row_count(); row_index++) {
  365. for (int column_index = auto_placement_cursor_x; column_index < occupation_grid.column_count(); column_index++) {
  366. if (column_span + column_index <= occupation_grid.column_count()) {
  367. auto found_all_available = true;
  368. for (int span_index = 0; span_index < column_span; span_index++) {
  369. if (occupation_grid.is_occupied(column_index + span_index, row_index))
  370. found_all_available = false;
  371. }
  372. if (found_all_available) {
  373. found_unoccupied_area = true;
  374. column_start = column_index;
  375. row_start = row_index;
  376. goto finish;
  377. }
  378. }
  379. auto_placement_cursor_x = 0;
  380. }
  381. auto_placement_cursor_x = 0;
  382. auto_placement_cursor_y++;
  383. }
  384. finish:
  385. // 4.1.2.2. If a non-overlapping position was found in the previous step, set the item's row-start
  386. // and column-start lines to the cursor's position. Otherwise, increment the auto-placement cursor's
  387. // row position (creating new rows in the implicit grid as necessary), set its column position to the
  388. // start-most column line in the implicit grid, and return to the previous step.
  389. if (!found_unoccupied_area) {
  390. row_start = occupation_grid.row_count();
  391. occupation_grid.maybe_add_row(occupation_grid.row_count() + 1);
  392. }
  393. occupation_grid.set_occupied(column_start, column_start + column_span, row_start, row_start + row_span);
  394. positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
  395. }
  396. boxes_to_place.remove(i);
  397. i--;
  398. // FIXME: 4.2. For dense packing:
  399. }
  400. auto& box_state = m_state.get_mutable(box);
  401. for (auto& positioned_box : positioned_boxes) {
  402. auto& child_box_state = m_state.get_mutable(positioned_box.box);
  403. if (child_box_state.content_height() > positioned_box.computed_height)
  404. positioned_box.computed_height = child_box_state.content_height();
  405. if (auto independent_formatting_context = layout_inside(positioned_box.box, LayoutMode::Normal, available_space))
  406. independent_formatting_context->parent_context_did_dimension_child_root_box();
  407. if (child_box_state.content_height() > positioned_box.computed_height)
  408. positioned_box.computed_height = child_box_state.content_height();
  409. }
  410. // https://drafts.csswg.org/css-grid/#overview-sizing
  411. // 2.3. Sizing the Grid
  412. // Once the grid items have been placed, the sizes of the grid tracks (rows and columns) are
  413. // calculated, accounting for the sizes of their contents and/or available space as specified in
  414. // the grid definition.
  415. // https://drafts.csswg.org/css-grid/#layout-algorithm
  416. // 12. Grid Sizing
  417. // This section defines the grid sizing algorithm, which determines the size of all grid tracks and,
  418. // by extension, the entire grid.
  419. // Each track has specified minimum and maximum sizing functions (which may be the same). Each
  420. // sizing function is either:
  421. // - A fixed sizing function (<length> or resolvable <percentage>).
  422. // - An intrinsic sizing function (min-content, max-content, auto, fit-content()).
  423. // - A flexible sizing function (<flex>).
  424. // The grid sizing algorithm defines how to resolve these sizing constraints into used track sizes.
  425. struct GridTrack {
  426. CSS::GridTrackSize min_track_sizing_function;
  427. CSS::GridTrackSize max_track_sizing_function;
  428. float base_size { 0 };
  429. float growth_limit { 0 };
  430. };
  431. Vector<GridTrack> grid_rows;
  432. Vector<GridTrack> grid_columns;
  433. for (auto& column_size : box.computed_values().grid_template_columns())
  434. grid_columns.append({ column_size, column_size });
  435. for (auto& row_size : box.computed_values().grid_template_rows())
  436. grid_rows.append({ row_size, row_size });
  437. for (int column_index = grid_columns.size(); column_index < occupation_grid.column_count(); column_index++)
  438. grid_columns.append({ CSS::GridTrackSize::make_auto(), CSS::GridTrackSize::make_auto() });
  439. for (int row_index = grid_rows.size(); row_index < occupation_grid.row_count(); row_index++)
  440. grid_rows.append({ CSS::GridTrackSize::make_auto(), CSS::GridTrackSize::make_auto() });
  441. // https://drafts.csswg.org/css-grid/#algo-overview
  442. // 12.1. Grid Sizing Algorithm
  443. // FIXME: Deals with subgrids, min-content, and justify-content.. not implemented yet
  444. // https://drafts.csswg.org/css-grid/#algo-track-sizing
  445. // 12.3. Track Sizing Algorithm
  446. // The remainder of this section is the track sizing algorithm, which calculates from the min and
  447. // max track sizing functions the used track size. Each track has a base size, a <length> which
  448. // grows throughout the algorithm and which will eventually be the track’s final size, and a growth
  449. // limit, a <length> which provides a desired maximum size for the base size. There are 5 steps:
  450. // 1. Initialize Track Sizes
  451. // 2. Resolve Intrinsic Track Sizes
  452. // 3. Maximize Tracks
  453. // 4. Expand Flexible Tracks
  454. // 5. [[#algo-stretch|Expand Stretched auto Tracks]]
  455. // https://drafts.csswg.org/css-grid/#algo-init
  456. // 12.4. Initialize Track Sizes
  457. // Initialize each track’s base size and growth limit.
  458. for (auto& grid_column : grid_columns) {
  459. // For each track, if the track’s min track sizing function is:
  460. switch (grid_column.min_track_sizing_function.type()) {
  461. // - A fixed sizing function
  462. // Resolve to an absolute length and use that size as the track’s initial base size.
  463. // Indefinite lengths cannot occur, as they’re treated as auto.
  464. case CSS::GridTrackSize::Type::Length:
  465. if (!grid_column.min_track_sizing_function.length().is_auto())
  466. grid_column.base_size = grid_column.min_track_sizing_function.length().to_px(box);
  467. break;
  468. case CSS::GridTrackSize::Type::Percentage:
  469. grid_column.base_size = grid_column.min_track_sizing_function.percentage().as_fraction() * box_state.content_width();
  470. break;
  471. // - An intrinsic sizing function
  472. // Use an initial base size of zero.
  473. case CSS::GridTrackSize::Type::FlexibleLength:
  474. break;
  475. default:
  476. VERIFY_NOT_REACHED();
  477. }
  478. // For each track, if the track’s max track sizing function is:
  479. switch (grid_column.max_track_sizing_function.type()) {
  480. // - A fixed sizing function
  481. // Resolve to an absolute length and use that size as the track’s initial growth limit.
  482. case CSS::GridTrackSize::Type::Length:
  483. if (!grid_column.max_track_sizing_function.length().is_auto())
  484. grid_column.growth_limit = grid_column.max_track_sizing_function.length().to_px(box);
  485. else
  486. // - An intrinsic sizing function
  487. // Use an initial growth limit of infinity.
  488. grid_column.growth_limit = -1;
  489. break;
  490. case CSS::GridTrackSize::Type::Percentage:
  491. grid_column.growth_limit = grid_column.max_track_sizing_function.percentage().as_fraction() * box_state.content_width();
  492. break;
  493. // - A flexible sizing function
  494. // Use an initial growth limit of infinity.
  495. case CSS::GridTrackSize::Type::FlexibleLength:
  496. grid_column.growth_limit = -1;
  497. break;
  498. default:
  499. VERIFY_NOT_REACHED();
  500. }
  501. }
  502. // Initialize each track’s base size and growth limit.
  503. for (auto& grid_row : grid_rows) {
  504. // For each track, if the track’s min track sizing function is:
  505. switch (grid_row.min_track_sizing_function.type()) {
  506. // - A fixed sizing function
  507. // Resolve to an absolute length and use that size as the track’s initial base size.
  508. // Indefinite lengths cannot occur, as they’re treated as auto.
  509. case CSS::GridTrackSize::Type::Length:
  510. if (!grid_row.min_track_sizing_function.length().is_auto())
  511. grid_row.base_size = grid_row.min_track_sizing_function.length().to_px(box);
  512. break;
  513. case CSS::GridTrackSize::Type::Percentage:
  514. grid_row.base_size = grid_row.min_track_sizing_function.percentage().as_fraction() * box_state.content_height();
  515. break;
  516. // - An intrinsic sizing function
  517. // Use an initial base size of zero.
  518. case CSS::GridTrackSize::Type::FlexibleLength:
  519. break;
  520. default:
  521. VERIFY_NOT_REACHED();
  522. }
  523. // For each track, if the track’s max track sizing function is:
  524. switch (grid_row.max_track_sizing_function.type()) {
  525. // - A fixed sizing function
  526. // Resolve to an absolute length and use that size as the track’s initial growth limit.
  527. case CSS::GridTrackSize::Type::Length:
  528. if (!grid_row.max_track_sizing_function.length().is_auto())
  529. grid_row.growth_limit = grid_row.max_track_sizing_function.length().to_px(box);
  530. else
  531. // - An intrinsic sizing function
  532. // Use an initial growth limit of infinity.
  533. grid_row.growth_limit = -1;
  534. break;
  535. case CSS::GridTrackSize::Type::Percentage:
  536. grid_row.growth_limit = grid_row.max_track_sizing_function.percentage().as_fraction() * box_state.content_height();
  537. break;
  538. // - A flexible sizing function
  539. // Use an initial growth limit of infinity.
  540. case CSS::GridTrackSize::Type::FlexibleLength:
  541. grid_row.growth_limit = -1;
  542. break;
  543. default:
  544. VERIFY_NOT_REACHED();
  545. }
  546. }
  547. // FIXME: In all cases, if the growth limit is less than the base size, increase the growth limit to match
  548. // the base size.
  549. // https://drafts.csswg.org/css-grid/#algo-content
  550. // 12.5. Resolve Intrinsic Track Sizes
  551. // This step resolves intrinsic track sizing functions to absolute lengths. First it resolves those
  552. // sizes based on items that are contained wholly within a single track. Then it gradually adds in
  553. // the space requirements of items that span multiple tracks, evenly distributing the extra space
  554. // across those tracks insofar as possible.
  555. // FIXME: 1. Shim baseline-aligned items so their intrinsic size contributions reflect their baseline
  556. // alignment. For the items in each baseline-sharing group, add a “shim” (effectively, additional
  557. // margin) on the start/end side (for first/last-baseline alignment) of each item so that, when
  558. // start/end-aligned together their baselines align as specified.
  559. // Consider these “shims” as part of the items’ intrinsic size contribution for the purpose of track
  560. // sizing, below. If an item uses multiple intrinsic size contributions, it can have different shims
  561. // for each one.
  562. // 2. Size tracks to fit non-spanning items: For each track with an intrinsic track sizing function and
  563. // not a flexible sizing function, consider the items in it with a span of 1:
  564. int index = 0;
  565. for (auto& grid_column : grid_columns) {
  566. if (!grid_column.min_track_sizing_function.is_intrinsic_track_sizing()) {
  567. ++index;
  568. continue;
  569. }
  570. Vector<Box const&> boxes_of_column;
  571. for (auto& positioned_box : positioned_boxes) {
  572. if (positioned_box.column == index && positioned_box.column_span == 1)
  573. boxes_of_column.append(positioned_box.box);
  574. }
  575. // - For min-content minimums:
  576. // If the track has a min-content min track sizing function, set its base size to the maximum of the
  577. // items’ min-content contributions, floored at zero.
  578. // FIXME: Not implemented yet min-content.
  579. // - For max-content minimums:
  580. // If the track has a max-content min track sizing function, set its base size to the maximum of the
  581. // items’ max-content contributions, floored at zero.
  582. // FIXME: Not implemented yet max-content.
  583. // - For auto minimums:
  584. // If the track has an auto min track sizing function and the grid container is being sized under a
  585. // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
  586. // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
  587. // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
  588. // limited by the max track sizing function (which could be the argument to a fit-content() track
  589. // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
  590. // below).
  591. // FIXME: Not implemented yet min-/max-content.
  592. // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
  593. // at zero. The minimum contribution of an item is the smallest outer size it can have.
  594. // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
  595. // containing block in the relevant axis, its minimum contribution is the outer size that would
  596. // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
  597. // contribution is its min-content contribution. Because the minimum contribution often depends on
  598. // the size of the item’s content, it is considered a type of intrinsic size contribution.
  599. // For items with a specified minimum size of auto (the initial value), the minimum contribution is
  600. // usually equivalent to the min-content contribution—but can differ in some cases, see § 6.6
  601. // Automatic Minimum Size of Grid Items. Also, minimum contribution ≤ min-content contribution ≤
  602. // max-content contribution.
  603. float grid_column_width = 0;
  604. for (auto& box_of_column : boxes_of_column)
  605. grid_column_width = max(grid_column_width, calculate_min_content_width(box_of_column));
  606. grid_column.base_size = grid_column_width;
  607. // - For min-content maximums:
  608. // If the track has a min-content max track sizing function, set its growth limit to the maximum of
  609. // the items’ min-content contributions.
  610. // FIXME: Not implemented yet min-content maximums.
  611. // - For max-content maximums:
  612. // If the track has a max-content max track sizing function, set its growth limit to the maximum of
  613. // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
  614. // limit by the fit-content() argument.
  615. // FIXME: Not implemented yet max-content maximums.
  616. // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
  617. // to match the base size.
  618. if (grid_column.growth_limit != -1 && grid_column.growth_limit < grid_column.base_size)
  619. grid_column.growth_limit = grid_column.base_size;
  620. ++index;
  621. }
  622. index = 0;
  623. for (auto& grid_row : grid_rows) {
  624. if (!grid_row.min_track_sizing_function.is_intrinsic_track_sizing()) {
  625. ++index;
  626. continue;
  627. }
  628. Vector<PositionedBox&> positioned_boxes_of_row;
  629. for (auto& positioned_box : positioned_boxes) {
  630. if (positioned_box.row == index && positioned_box.row_span == 1)
  631. positioned_boxes_of_row.append(positioned_box);
  632. }
  633. // - For min-content minimums:
  634. // If the track has a min-content min track sizing function, set its base size to the maximum of the
  635. // items’ min-content contributions, floored at zero.
  636. // FIXME: Not implemented yet min-content.
  637. // - For max-content minimums:
  638. // If the track has a max-content min track sizing function, set its base size to the maximum of the
  639. // items’ max-content contributions, floored at zero.
  640. // FIXME: Not implemented yet max-content.
  641. // - For auto minimums:
  642. // If the track has an auto min track sizing function and the grid container is being sized under a
  643. // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
  644. // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
  645. // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
  646. // limited by the max track sizing function (which could be the argument to a fit-content() track
  647. // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
  648. // below).
  649. // FIXME: Not implemented yet min-/max-content.
  650. // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
  651. // at zero. The minimum contribution of an item is the smallest outer size it can have.
  652. // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
  653. // containing block in the relevant axis, its minimum contribution is the outer size that would
  654. // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
  655. // contribution is its min-content contribution. Because the minimum contribution often depends on
  656. // the size of the item’s content, it is considered a type of intrinsic size contribution.
  657. // For items with a specified minimum size of auto (the initial value), the minimum contribution is
  658. // usually equivalent to the min-content contribution—but can differ in some cases, see § 6.6
  659. // Automatic Minimum Size of Grid Items. Also, minimum contribution ≤ min-content contribution ≤
  660. // max-content contribution.
  661. float grid_row_height = 0;
  662. for (auto& positioned_box : positioned_boxes_of_row)
  663. grid_row_height = max(grid_row_height, positioned_box.computed_height);
  664. grid_row.base_size = grid_row_height;
  665. // - For min-content maximums:
  666. // If the track has a min-content max track sizing function, set its growth limit to the maximum of
  667. // the items’ min-content contributions.
  668. // FIXME: Not implemented yet min-content maximums.
  669. // - For max-content maximums:
  670. // If the track has a max-content max track sizing function, set its growth limit to the maximum of
  671. // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
  672. // limit by the fit-content() argument.
  673. // FIXME: Not implemented yet max-content maximums.
  674. // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
  675. // to match the base size.
  676. if (grid_row.growth_limit != -1 && grid_row.growth_limit < grid_row.base_size)
  677. grid_row.growth_limit = grid_row.base_size;
  678. ++index;
  679. }
  680. // 3. Increase sizes to accommodate spanning items crossing content-sized tracks: Next, consider the
  681. // items with a span of 2 that do not span a track with a flexible sizing function.
  682. // FIXME: Content-sized tracks not implemented (min-content, etc.)
  683. // 3.1. For intrinsic minimums: First distribute extra space to base sizes of tracks with an intrinsic
  684. // min track sizing function, to accommodate these items’ minimum contributions.
  685. // If the grid container is being sized under a min- or max-content constraint, use the items’
  686. // limited min-content contributions in place of their minimum contributions here. (For an item
  687. // spanning multiple tracks, the upper limit used to calculate its limited min-/max-content
  688. // contribution is the sum of the fixed max track sizing functions of any tracks it spans, and is
  689. // applied if it only spans such tracks.)
  690. // 3.2. For content-based minimums: Next continue to distribute extra space to the base sizes of tracks
  691. // with a min track sizing function of min-content or max-content, to accommodate these items'
  692. // min-content contributions.
  693. // 3.3. For max-content minimums: Next, if the grid container is being sized under a max-content
  694. // constraint, continue to distribute extra space to the base sizes of tracks with a min track
  695. // sizing function of auto or max-content, to accommodate these items' limited max-content
  696. // contributions.
  697. // In all cases, continue to distribute extra space to the base sizes of tracks with a min track
  698. // sizing function of max-content, to accommodate these items' max-content contributions.
  699. // 3.4. If at this point any track’s growth limit is now less than its base size, increase its growth
  700. // limit to match its base size.
  701. // 3.5. For intrinsic maximums: Next distribute extra space to the growth limits of tracks with intrinsic
  702. // max track sizing function, to accommodate these items' min-content contributions. Mark any tracks
  703. // whose growth limit changed from infinite to finite in this step as infinitely growable for the
  704. // next step.
  705. // 3.6. For max-content maximums: Lastly continue to distribute extra space to the growth limits of
  706. // tracks with a max track sizing function of max-content, to accommodate these items' max-content
  707. // contributions. However, limit the growth of any fit-content() tracks by their fit-content()
  708. // argument.
  709. // Repeat incrementally for items with greater spans until all items have been considered.
  710. // FIXME: 4. Increase sizes to accommodate spanning items crossing flexible tracks: Next, repeat the previous
  711. // step instead considering (together, rather than grouped by span size) all items that do span a
  712. // track with a flexible sizing function while distributing space only to flexible tracks (i.e.
  713. // treating all other tracks as having a fixed sizing function)
  714. // if the sum of the flexible sizing functions of all flexible tracks spanned by the item is greater
  715. // than or equal to one, distributing space to such tracks according to the ratios of their flexible
  716. // sizing functions rather than distributing space equally; and if the sum is less than one,
  717. // distributing that proportion of space according to the ratios of their flexible sizing functions
  718. // and the rest equally
  719. // FIXME: 5. If any track still has an infinite growth limit (because, for example, it had no items placed in
  720. // it or it is a flexible track), set its growth limit to its base size.
  721. // https://drafts.csswg.org/css-grid/#extra-space
  722. // 12.5.1. Distributing Extra Space Across Spanned Tracks
  723. // 1. Maintain separately for each affected track a planned increase, initially set to 0. (This
  724. // prevents the size increases from becoming order-dependent.)
  725. // 2. For each accommodated item, considering only tracks the item spans:
  726. // 2.1. Find the space to distribute: Subtract the affected size of every spanned track (not just the
  727. // affected tracks) from the item’s size contribution, flooring it at zero. (For infinite growth
  728. // limits, substitute the track’s base size.) This remaining size contribution is the space to
  729. // distribute.
  730. // space = max(0, size contribution - ∑track-sizes)
  731. // 2.2. Distribute space up to limits:
  732. // Find the item-incurred increase for each affected track by: distributing the space equally among
  733. // these tracks, freezing a track’s item-incurred increase as its affected size + item-incurred
  734. // increase reaches its limit (and continuing to grow the unfrozen tracks as needed).
  735. // For base sizes, the limit is its growth limit. For growth limits, the limit is infinity if it is
  736. // marked as infinitely growable, and equal to the growth limit otherwise.
  737. // If the affected size was a growth limit and the track is not marked infinitely growable, then each
  738. // item-incurred increase will be zero.
  739. // 2.3. Distribute space beyond limits:
  740. // If extra space remains at this point, unfreeze and continue to distribute space to the
  741. // item-incurred increase of…
  742. // - when accommodating minimum contributions or accommodating min-content contributions: any affected
  743. // track that happens to also have an intrinsic max track sizing function; if there are no such
  744. // tracks, then all affected tracks.
  745. // - when accommodating max-content contributions: any affected track that happens to also have a
  746. // max-content max track sizing function; if there are no such tracks, then all affected tracks.
  747. // - when handling any intrinsic growth limit: all affected tracks.
  748. // For this purpose, the max track sizing function of a fit-content() track is treated as
  749. // max-content until it reaches the limit specified as the fit-content() argument, after which it is
  750. // treated as having a fixed sizing function of that argument.
  751. // This step prioritizes the distribution of space for accommodating size contributions beyond the
  752. // tracks' current growth limits based on the types of their max track sizing functions.
  753. // 2.4. For each affected track, if the track’s item-incurred increase is larger than the track’s planned
  754. // increase set the track’s planned increase to that value.
  755. // 3. Update the tracks' affected sizes by adding in the planned increase, so that the next round of
  756. // space distribution will account for the increase. (If the affected size is an infinite growth
  757. // limit, set it to the track’s base size plus the planned increase.)
  758. // https://drafts.csswg.org/css-grid/#algo-grow-tracks
  759. // 12.6. Maximize Tracks
  760. // If the free space is positive, distribute it equally to the base sizes of all tracks, freezing
  761. // tracks as they reach their growth limits (and continuing to grow the unfrozen tracks as needed).
  762. // For the purpose of this step: if sizing the grid container under a max-content constraint, the
  763. // free space is infinite; if sizing under a min-content constraint, the free space is zero.
  764. // If this would cause the grid to be larger than the grid container’s inner size as limited by its
  765. // max-width/height, then redo this step, treating the available grid space as equal to the grid
  766. // container’s inner size when it’s sized to its max-width/height.
  767. // FIXME: Do later as at the moment all growth limits are equal to base sizes.
  768. // https://drafts.csswg.org/css-grid/#algo-flex-tracks
  769. // 12.7. Expand Flexible Tracks
  770. // This step sizes flexible tracks using the largest value it can assign to an fr without exceeding
  771. // the available space.
  772. // First, find the grid’s used flex fraction:
  773. auto column_flex_factor_sum = 0;
  774. for (auto& grid_column : grid_columns) {
  775. if (grid_column.min_track_sizing_function.is_flexible_length())
  776. column_flex_factor_sum++;
  777. }
  778. // See 12.7.1.
  779. // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  780. // than 1, set it to 1 instead.
  781. if (column_flex_factor_sum < 1)
  782. column_flex_factor_sum = 1;
  783. // See 12.7.1.
  784. float sized_column_widths = 0;
  785. for (auto& grid_column : grid_columns) {
  786. if (!grid_column.min_track_sizing_function.is_flexible_length())
  787. sized_column_widths += grid_column.base_size;
  788. }
  789. // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  790. double free_horizontal_space = box_state.content_width() - sized_column_widths;
  791. // If the free space is zero or if sizing the grid container under a min-content constraint:
  792. // The used flex fraction is zero.
  793. // FIXME: Add min-content constraint check.
  794. // Otherwise, if the free space is a definite length:
  795. // The used flex fraction is the result of finding the size of an fr using all of the grid tracks
  796. // and a space to fill of the available grid space.
  797. if (free_horizontal_space > 0) {
  798. for (auto& grid_column : grid_columns) {
  799. if (grid_column.min_track_sizing_function.is_flexible_length()) {
  800. // See 12.7.1.
  801. // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  802. auto hypothetical_fr_size = static_cast<double>(1.0 / column_flex_factor_sum) * free_horizontal_space;
  803. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  804. // greater than the track’s base size, set its base size to that product.
  805. grid_column.base_size = max(grid_column.base_size, hypothetical_fr_size);
  806. }
  807. }
  808. }
  809. // First, find the grid’s used flex fraction:
  810. auto row_flex_factor_sum = 0;
  811. for (auto& grid_row : grid_rows) {
  812. if (grid_row.min_track_sizing_function.is_flexible_length())
  813. row_flex_factor_sum++;
  814. }
  815. // See 12.7.1.
  816. // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  817. // than 1, set it to 1 instead.
  818. if (row_flex_factor_sum < 1)
  819. row_flex_factor_sum = 1;
  820. // See 12.7.1.
  821. float sized_row_heights = 0;
  822. for (auto& grid_row : grid_rows) {
  823. if (!grid_row.min_track_sizing_function.is_flexible_length())
  824. sized_row_heights += grid_row.base_size;
  825. }
  826. // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  827. double free_vertical_space = box_state.content_height() - sized_row_heights;
  828. // If the free space is zero or if sizing the grid container under a min-content constraint:
  829. // The used flex fraction is zero.
  830. // FIXME: Add min-content constraint check.
  831. // Otherwise, if the free space is a definite length:
  832. // The used flex fraction is the result of finding the size of an fr using all of the grid tracks
  833. // and a space to fill of the available grid space.
  834. if (free_vertical_space > 0) {
  835. for (auto& grid_row : grid_rows) {
  836. if (grid_row.min_track_sizing_function.is_flexible_length()) {
  837. // See 12.7.1.
  838. // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  839. auto hypothetical_fr_size = static_cast<double>(1.0 / row_flex_factor_sum) * free_vertical_space;
  840. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  841. // greater than the track’s base size, set its base size to that product.
  842. grid_row.base_size = max(grid_row.base_size, hypothetical_fr_size);
  843. }
  844. }
  845. }
  846. // Otherwise, if the free space is an indefinite length:
  847. // FIXME: No tracks will have indefinite length as per current implementation.
  848. // The used flex fraction is the maximum of:
  849. // For each flexible track, if the flexible track’s flex factor is greater than one, the result of
  850. // dividing the track’s base size by its flex factor; otherwise, the track’s base size.
  851. // For each grid item that crosses a flexible track, the result of finding the size of an fr using
  852. // all the grid tracks that the item crosses and a space to fill of the item’s max-content
  853. // contribution.
  854. // If using this flex fraction would cause the grid to be smaller than the grid container’s
  855. // min-width/height (or larger than the grid container’s max-width/height), then redo this step,
  856. // treating the free space as definite and the available grid space as equal to the grid container’s
  857. // inner size when it’s sized to its min-width/height (max-width/height).
  858. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  859. // greater than the track’s base size, set its base size to that product.
  860. // https://drafts.csswg.org/css-grid/#algo-find-fr-size
  861. // 12.7.1. Find the Size of an fr
  862. // This algorithm finds the largest size that an fr unit can be without exceeding the target size.
  863. // It must be called with a set of grid tracks and some quantity of space to fill.
  864. // 1. Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  865. // 2. Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  866. // than 1, set it to 1 instead.
  867. // 3. Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  868. // FIXME: 4. If the product of the hypothetical fr size and a flexible track’s flex factor is less than the
  869. // track’s base size, restart this algorithm treating all such tracks as inflexible.
  870. // 5. Return the hypothetical fr size.
  871. // https://drafts.csswg.org/css-grid/#algo-stretch
  872. // 12.8. Stretch auto Tracks
  873. // When the content-distribution property of the grid container is normal or stretch in this axis,
  874. // this step expands tracks that have an auto max track sizing function by dividing any remaining
  875. // positive, definite free space equally amongst them. If the free space is indefinite, but the grid
  876. // container has a definite min-width/height, use that size to calculate the free space for this
  877. // step instead.
  878. float used_horizontal_space = 0;
  879. for (auto& grid_column : grid_columns) {
  880. if (!(grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto()))
  881. used_horizontal_space += grid_column.base_size;
  882. }
  883. float remaining_horizontal_space = box_state.content_width() - used_horizontal_space;
  884. auto count_of_auto_max_column_tracks = 0;
  885. for (auto& grid_column : grid_columns) {
  886. if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
  887. count_of_auto_max_column_tracks++;
  888. }
  889. for (auto& grid_column : grid_columns) {
  890. if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
  891. grid_column.base_size = max(grid_column.base_size, remaining_horizontal_space / count_of_auto_max_column_tracks);
  892. }
  893. float used_vertical_space = 0;
  894. for (auto& grid_row : grid_rows) {
  895. if (!(grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto()))
  896. used_vertical_space += grid_row.base_size;
  897. }
  898. float remaining_vertical_space = box_state.content_height() - used_vertical_space;
  899. auto count_of_auto_max_row_tracks = 0;
  900. for (auto& grid_row : grid_rows) {
  901. if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
  902. count_of_auto_max_row_tracks++;
  903. }
  904. for (auto& grid_row : grid_rows) {
  905. if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
  906. grid_row.base_size = max(grid_row.base_size, remaining_vertical_space / count_of_auto_max_row_tracks);
  907. }
  908. auto layout_box = [&](int row_start, int row_end, int column_start, int column_end, Box const& child_box) -> void {
  909. auto& child_box_state = m_state.get_mutable(child_box);
  910. float x_start = 0;
  911. float x_end = 0;
  912. float y_start = 0;
  913. float y_end = 0;
  914. for (int i = 0; i < column_start; i++)
  915. x_start += grid_columns[i].base_size;
  916. for (int i = 0; i < column_end; i++)
  917. x_end += grid_columns[i].base_size;
  918. for (int i = 0; i < row_start; i++)
  919. y_start += grid_rows[i].base_size;
  920. for (int i = 0; i < row_end; i++)
  921. y_end += grid_rows[i].base_size;
  922. child_box_state.set_content_width(x_end - x_start);
  923. child_box_state.set_content_height(y_end - y_start);
  924. child_box_state.offset = { x_start, y_start };
  925. };
  926. for (auto& positioned_box : positioned_boxes) {
  927. auto resolved_span = positioned_box.row + positioned_box.row_span > static_cast<int>(grid_rows.size()) ? static_cast<int>(grid_rows.size()) - positioned_box.row : positioned_box.row_span;
  928. layout_box(positioned_box.row, positioned_box.row + resolved_span, positioned_box.column, positioned_box.column + positioned_box.column_span, positioned_box.box);
  929. }
  930. float total_y = 0;
  931. for (auto& grid_row : grid_rows)
  932. total_y += grid_row.base_size;
  933. m_automatic_content_height = total_y;
  934. }
  935. float GridFormattingContext::automatic_content_height() const
  936. {
  937. return m_automatic_content_height;
  938. }
  939. bool GridFormattingContext::is_auto_positioned_row(CSS::GridTrackPlacement const& grid_row_start, CSS::GridTrackPlacement const& grid_row_end) const
  940. {
  941. return is_auto_positioned_track(grid_row_start, grid_row_end);
  942. }
  943. bool GridFormattingContext::is_auto_positioned_column(CSS::GridTrackPlacement const& grid_column_start, CSS::GridTrackPlacement const& grid_column_end) const
  944. {
  945. return is_auto_positioned_track(grid_column_start, grid_column_end);
  946. }
  947. bool GridFormattingContext::is_auto_positioned_track(CSS::GridTrackPlacement const& grid_track_start, CSS::GridTrackPlacement const& grid_track_end) const
  948. {
  949. return grid_track_start.is_auto_positioned() && grid_track_end.is_auto_positioned();
  950. }
  951. OccupationGrid::OccupationGrid(int column_count, int row_count)
  952. {
  953. Vector<bool> occupation_grid_row;
  954. for (int column_index = 0; column_index < max(column_count, 1); column_index++)
  955. occupation_grid_row.append(false);
  956. for (int row_index = 0; row_index < max(row_count, 1); row_index++)
  957. m_occupation_grid.append(occupation_grid_row);
  958. }
  959. void OccupationGrid::maybe_add_column(int needed_number_of_columns)
  960. {
  961. if (needed_number_of_columns <= column_count())
  962. return;
  963. for (auto& occupation_grid_row : m_occupation_grid)
  964. for (int idx = 0; idx < (needed_number_of_columns + 1) - column_count(); idx++)
  965. occupation_grid_row.append(false);
  966. }
  967. void OccupationGrid::maybe_add_row(int needed_number_of_rows)
  968. {
  969. if (needed_number_of_rows <= row_count())
  970. return;
  971. Vector<bool> new_occupation_grid_row;
  972. for (int idx = 0; idx < column_count(); idx++)
  973. new_occupation_grid_row.append(false);
  974. for (int idx = 0; idx < needed_number_of_rows - row_count(); idx++)
  975. m_occupation_grid.append(new_occupation_grid_row);
  976. }
  977. void OccupationGrid::set_occupied(int column_start, int column_end, int row_start, int row_end)
  978. {
  979. for (int row_index = 0; row_index < row_count(); row_index++) {
  980. if (row_index >= row_start && row_index < row_end) {
  981. for (int column_index = 0; column_index < column_count(); column_index++) {
  982. if (column_index >= column_start && column_index < column_end)
  983. set_occupied(column_index, row_index);
  984. }
  985. }
  986. }
  987. }
  988. void OccupationGrid::set_occupied(int column_index, int row_index)
  989. {
  990. m_occupation_grid[row_index][column_index] = true;
  991. }
  992. bool OccupationGrid::is_occupied(int column_index, int row_index)
  993. {
  994. return m_occupation_grid[row_index][column_index];
  995. }
  996. }