GridFormattingContext.cpp 86 KB

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