GridFormattingContext.cpp 84 KB

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