GridFormattingContext.cpp 62 KB

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