GridFormattingContext.cpp 83 KB

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