GridFormattingContext.cpp 64 KB

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