GridFormattingContext.cpp 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  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).to_px() / 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 (size_t 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 (size_t row_index = auto_placement_cursor_y; row_index < m_occupation_grid.row_count(); row_index++) {
  447. for (size_t 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_from_definition(AvailableSpace const& available_space, Vector<CSS::ExplicitGridTrack> const& tracks_definition, Vector<TemporaryTrack>& tracks)
  478. {
  479. auto track_count = get_count_of_tracks(tracks_definition, available_space);
  480. for (auto const& track_definition : tracks_definition) {
  481. auto repeat_count = (track_definition.is_repeat() && track_definition.repeat().is_default()) ? track_definition.repeat().repeat_count() : 1;
  482. if (track_definition.is_repeat()) {
  483. if (track_definition.repeat().is_auto_fill() || track_definition.repeat().is_auto_fit())
  484. repeat_count = track_count;
  485. }
  486. for (auto _ = 0; _ < repeat_count; _++) {
  487. switch (track_definition.type()) {
  488. case CSS::ExplicitGridTrack::Type::MinMax:
  489. tracks.append(TemporaryTrack(track_definition.minmax().min_grid_size(), track_definition.minmax().max_grid_size()));
  490. break;
  491. case CSS::ExplicitGridTrack::Type::Repeat:
  492. for (auto& explicit_grid_track : track_definition.repeat().grid_track_size_list().track_list()) {
  493. auto track_sizing_function = explicit_grid_track;
  494. if (track_sizing_function.is_minmax())
  495. tracks.append(TemporaryTrack(track_sizing_function.minmax().min_grid_size(), track_sizing_function.minmax().max_grid_size()));
  496. else
  497. tracks.append(TemporaryTrack(track_sizing_function.grid_size()));
  498. }
  499. break;
  500. case CSS::ExplicitGridTrack::Type::Default:
  501. tracks.append(TemporaryTrack(track_definition.grid_size()));
  502. break;
  503. default:
  504. VERIFY_NOT_REACHED();
  505. }
  506. }
  507. }
  508. }
  509. void GridFormattingContext::initialize_grid_tracks_for_columns_and_rows(AvailableSpace const& available_space)
  510. {
  511. initialize_grid_tracks_from_definition(available_space, grid_container().computed_values().grid_template_columns().track_list(), m_grid_columns);
  512. initialize_grid_tracks_from_definition(available_space, grid_container().computed_values().grid_template_rows().track_list(), m_grid_rows);
  513. for (size_t column_index = m_grid_columns.size(); column_index < m_occupation_grid.column_count(); column_index++)
  514. m_grid_columns.append(TemporaryTrack());
  515. for (size_t row_index = m_grid_rows.size(); row_index < m_occupation_grid.row_count(); row_index++)
  516. m_grid_rows.append(TemporaryTrack());
  517. }
  518. void GridFormattingContext::initialize_gap_tracks(AvailableSpace const& available_space)
  519. {
  520. // https://www.w3.org/TR/css-grid-2/#gutters
  521. // 11.1. Gutters: the row-gap, column-gap, and gap properties
  522. // For the purpose of track sizing, each gutter is treated as an extra, empty, fixed-size track of
  523. // the specified size, which is spanned by any grid items that span across its corresponding grid
  524. // line.
  525. if (!grid_container().computed_values().column_gap().is_auto()) {
  526. for (size_t column_index = 1; column_index < (m_occupation_grid.column_count() * 2) - 1; column_index += 2) {
  527. auto column_gap_width = grid_container().computed_values().column_gap().to_px(grid_container(), available_space.width.to_px());
  528. m_grid_columns.insert(column_index, TemporaryTrack(column_gap_width, true));
  529. }
  530. }
  531. if (!grid_container().computed_values().row_gap().is_auto()) {
  532. for (size_t row_index = 1; row_index < (m_occupation_grid.row_count() * 2) - 1; row_index += 2) {
  533. auto column_gap_height = grid_container().computed_values().row_gap().to_px(grid_container(), available_space.height.to_px());
  534. m_grid_rows.insert(row_index, TemporaryTrack(column_gap_height, true));
  535. }
  536. }
  537. }
  538. void GridFormattingContext::initialize_track_sizes(AvailableSize const& available_size, Vector<TemporaryTrack>& tracks)
  539. {
  540. // https://www.w3.org/TR/css-grid-2/#algo-init
  541. // 12.4. Initialize Track Sizes
  542. // Initialize each track’s base size and growth limit.
  543. // For each track, if the track’s min track sizing function is:
  544. for (auto& track : tracks) {
  545. if (track.is_gap)
  546. continue;
  547. switch (track.min_track_sizing_function.type()) {
  548. // - A fixed sizing function
  549. // Resolve to an absolute length and use that size as the track’s initial base size.
  550. case CSS::GridSize::Type::Length: {
  551. if (!track.min_track_sizing_function.length().is_auto())
  552. track.base_size = track.min_track_sizing_function.length().to_px(grid_container());
  553. break;
  554. }
  555. case CSS::GridSize::Type::Percentage: {
  556. if (available_size.is_definite())
  557. track.base_size = track.min_track_sizing_function.percentage().as_fraction() * available_size.to_px().value();
  558. break;
  559. }
  560. // - An intrinsic sizing function
  561. // Use an initial base size of zero.
  562. case CSS::GridSize::Type::FlexibleLength:
  563. case CSS::GridSize::Type::MaxContent:
  564. case CSS::GridSize::Type::MinContent: {
  565. track.base_size = 0;
  566. break;
  567. }
  568. default:
  569. VERIFY_NOT_REACHED();
  570. }
  571. // For each track, if the track’s max track sizing function is:
  572. switch (track.max_track_sizing_function.type()) {
  573. // - A fixed sizing function
  574. // Resolve to an absolute length and use that size as the track’s initial growth limit.
  575. case CSS::GridSize::Type::Length: {
  576. if (!track.max_track_sizing_function.length().is_auto())
  577. track.growth_limit = track.max_track_sizing_function.length().to_px(grid_container());
  578. else
  579. // - An intrinsic sizing function
  580. // Use an initial growth limit of infinity.
  581. track.growth_limit = INFINITY;
  582. break;
  583. }
  584. case CSS::GridSize::Type::Percentage: {
  585. if (available_size.is_definite())
  586. track.growth_limit = track.max_track_sizing_function.percentage().as_fraction() * available_size.to_px().value();
  587. break;
  588. }
  589. // - A flexible sizing function
  590. // Use an initial growth limit of infinity.
  591. case CSS::GridSize::Type::FlexibleLength: {
  592. track.growth_limit = INFINITY;
  593. break;
  594. }
  595. // - An intrinsic sizing function
  596. // Use an initial growth limit of infinity.
  597. case CSS::GridSize::Type::MaxContent:
  598. case CSS::GridSize::Type::MinContent: {
  599. track.growth_limit = INFINITY;
  600. break;
  601. }
  602. default:
  603. VERIFY_NOT_REACHED();
  604. }
  605. // In all cases, if the growth limit is less than the base size, increase the growth limit to match
  606. // the base size.
  607. if (track.growth_limit < track.base_size)
  608. track.growth_limit = track.base_size;
  609. }
  610. }
  611. void GridFormattingContext::resolve_intrinsic_track_sizes(GridDimension const dimension, AvailableSize const& available_size, Vector<TemporaryTrack>& tracks)
  612. {
  613. // https://www.w3.org/TR/css-grid-2/#algo-content
  614. // 12.5. Resolve Intrinsic Track Sizes
  615. // This step resolves intrinsic track sizing functions to absolute lengths. First it resolves those
  616. // sizes based on items that are contained wholly within a single track. Then it gradually adds in
  617. // the space requirements of items that span multiple tracks, evenly distributing the extra space
  618. // across those tracks insofar as possible.
  619. // FIXME: 1. Shim baseline-aligned items so their intrinsic size contributions reflect their baseline alignment.
  620. auto calculate_item_min_content_contribution = [&](GridItem const& item) {
  621. if (dimension == GridDimension::Column) {
  622. return calculate_min_content_width(item.box());
  623. } else {
  624. return content_based_minimum_height(item);
  625. }
  626. };
  627. size_t index = 0;
  628. for (auto& track : tracks) {
  629. if (track.is_gap) {
  630. ++index;
  631. continue;
  632. }
  633. Vector<GridItem&> grid_items_of_track;
  634. for (auto& grid_item : m_grid_items) {
  635. if (dimension == GridDimension::Column) {
  636. if (grid_item.gap_adjusted_column(grid_container()) == index && grid_item.raw_column_span() == 1) {
  637. grid_items_of_track.append(grid_item);
  638. track.border_left = max(track.border_left, grid_item.box().computed_values().border_left().width);
  639. track.border_right = max(track.border_right, grid_item.box().computed_values().border_right().width);
  640. }
  641. } else {
  642. if (grid_item.gap_adjusted_row(grid_container()) == index && grid_item.raw_row_span() == 1) {
  643. grid_items_of_track.append(grid_item);
  644. track.border_top = max(track.border_top, grid_item.box().computed_values().border_top().width);
  645. track.border_bottom = max(track.border_bottom, grid_item.box().computed_values().border_bottom().width);
  646. }
  647. }
  648. }
  649. if (!track.min_track_sizing_function.is_intrinsic_track_sizing() && !track.max_track_sizing_function.is_intrinsic_track_sizing()) {
  650. ++index;
  651. continue;
  652. }
  653. switch (track.min_track_sizing_function.type()) {
  654. // - For min-content minimums:
  655. // If the track has a min-content min track sizing function, set its base size to the maximum of the
  656. // items’ min-content contributions, floored at zero.
  657. case CSS::GridSize::Type::MinContent: {
  658. CSSPixels base_size = 0;
  659. for (auto& item : grid_items_of_track) {
  660. base_size = max(base_size, calculate_item_min_content_contribution(item));
  661. }
  662. track.base_size = base_size;
  663. } break;
  664. // - For max-content minimums:
  665. // If the track has a max-content min track sizing function, set its base size to the maximum of the
  666. // items’ max-content contributions, floored at zero.
  667. case CSS::GridSize::Type::MaxContent: {
  668. CSSPixels base_size = 0;
  669. for (auto& item : grid_items_of_track) {
  670. base_size = max(base_size, calculate_item_min_content_contribution(item));
  671. }
  672. track.base_size = base_size;
  673. } break;
  674. // - For auto minimums:
  675. // If the track has an auto min track sizing function and the grid container is being sized under a
  676. // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
  677. // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
  678. // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
  679. // limited by the max track sizing function (which could be the argument to a fit-content() track
  680. // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
  681. // below).
  682. // FIXME: Container min/max-content
  683. case CSS::GridSize::Type::Length:
  684. // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
  685. // at zero. The minimum contribution of an item is the smallest outer size it can have.
  686. // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
  687. // containing block in the relevant axis, its minimum contribution is the outer size that would
  688. // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
  689. // contribution is its min-content contribution. Because the minimum contribution often depends on
  690. // the size of the item’s content, it is considered a type of intrinsic size contribution.
  691. case CSS::GridSize::Type::Percentage:
  692. case CSS::GridSize::Type::FlexibleLength: {
  693. CSSPixels base_size = 0;
  694. for (auto& item : grid_items_of_track) {
  695. base_size = max(base_size, calculate_item_min_content_contribution(item));
  696. }
  697. track.base_size = base_size;
  698. } break;
  699. default:
  700. VERIFY_NOT_REACHED();
  701. }
  702. switch (track.max_track_sizing_function.type()) {
  703. // - For min-content maximums:
  704. // If the track has a min-content max track sizing function, set its growth limit to the maximum of
  705. // the items’ min-content contributions.
  706. case CSS::GridSize::Type::MinContent: {
  707. CSSPixels growth_limit = 0;
  708. for (auto& item : grid_items_of_track) {
  709. growth_limit = max(growth_limit, calculate_item_min_content_contribution(item));
  710. }
  711. track.growth_limit = growth_limit;
  712. } break;
  713. // - For max-content maximums:
  714. // If the track has a max-content max track sizing function, set its growth limit to the maximum of
  715. // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
  716. // limit by the fit-content() argument.
  717. case CSS::GridSize::Type::MaxContent: {
  718. CSSPixels growth_limit = 0;
  719. for (auto& item : grid_items_of_track) {
  720. growth_limit = max(growth_limit, calculate_item_min_content_contribution(item));
  721. }
  722. track.growth_limit = growth_limit;
  723. } break;
  724. case CSS::GridSize::Type::Length:
  725. case CSS::GridSize::Type::Percentage:
  726. case CSS::GridSize::Type::FlexibleLength:
  727. break;
  728. default:
  729. VERIFY_NOT_REACHED();
  730. }
  731. // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
  732. // to match the base size.
  733. if (track.growth_limit < track.base_size)
  734. track.growth_limit = track.base_size;
  735. ++index;
  736. }
  737. // https://www.w3.org/TR/css-grid-2/#auto-repeat
  738. // The auto-fit keyword behaves the same as auto-fill, except that after grid item placement any
  739. // empty repeated tracks are collapsed. An empty track is one with no in-flow grid items placed into
  740. // or spanning across it. (This can result in all tracks being collapsed, if they’re all empty.)
  741. if (dimension == GridDimension::Column // FIXME: Handle for columns
  742. && grid_container().computed_values().grid_template_columns().track_list().size() == 1
  743. && grid_container().computed_values().grid_template_columns().track_list().first().is_repeat()
  744. && grid_container().computed_values().grid_template_columns().track_list().first().repeat().is_auto_fit()) {
  745. for (size_t idx = 0; idx < m_grid_columns.size(); idx++) {
  746. auto column_to_check = grid_container().computed_values().column_gap().is_auto() ? idx : idx / 2;
  747. if (m_occupation_grid.is_occupied(column_to_check, 0))
  748. continue;
  749. if (!grid_container().computed_values().column_gap().is_auto() && idx % 2 != 0)
  750. continue;
  751. // A collapsed track is treated as having a fixed track sizing function of 0px
  752. m_grid_columns[idx].base_size = 0;
  753. m_grid_columns[idx].growth_limit = 0;
  754. // FIXME: And the gutters on either side of it—including any space allotted through distributed
  755. // alignment—collapse.
  756. }
  757. }
  758. // 3. Increase sizes to accommodate spanning items crossing content-sized tracks: Next, consider the
  759. // items with a span of 2 that do not span a track with a flexible sizing function.
  760. // FIXME: Content-sized tracks not implemented (min-content, etc.)
  761. // 3.1. For intrinsic minimums: First increase the base size of tracks with an intrinsic min track sizing
  762. // function by distributing extra space as needed to accommodate these items’ minimum contributions.
  763. // If the grid container is being sized under a min- or max-content constraint, use the items’
  764. // limited min-content contributions in place of their minimum contributions here. (For an item
  765. // spanning multiple tracks, the upper limit used to calculate its limited min-/max-content
  766. // contribution is the sum of the fixed max track sizing functions of any tracks it spans, and is
  767. // applied if it only spans such tracks.)
  768. // 3.2. For content-based minimums: Next continue to increase the base size of tracks with a min track
  769. // sizing function of min-content or max-content by distributing extra space as needed to account
  770. // for these items' min-content contributions.
  771. // 3.3. For max-content minimums: Next, if the grid container is being sized under a max-content
  772. // constraint, continue to increase the base size of tracks with a min track sizing function of auto
  773. // or max-content by distributing extra space as needed to account for these items' limited
  774. // max-content contributions.
  775. // In all cases, continue to increase the base size of tracks with a min track sizing function of
  776. // max-content by distributing extra space as needed to account for these items' max-content
  777. // contributions.
  778. // 3.4. If at this point any track’s growth limit is now less than its base size, increase its growth
  779. // limit to match its base size.
  780. // 3.5. For intrinsic maximums: Next increase the growth limit of tracks with an intrinsic max track
  781. // sizing function by distributing extra space as needed to account for these items' min-content
  782. // contributions. Mark any tracks whose growth limit changed from infinite to finite in this step as
  783. // infinitely growable for the next step.
  784. // 3.6. For max-content maximums: Lastly continue to increase the growth limit of tracks with a max track
  785. // sizing function of max-content by distributing extra space as needed to account for these items'
  786. // max-content contributions. However, limit the growth of any fit-content() tracks by their
  787. // fit-content() argument.
  788. // Repeat incrementally for items with greater spans until all items have been considered.
  789. // FIXME: 4. Increase sizes to accommodate spanning items crossing flexible tracks: Next, repeat the previous
  790. // step instead considering (together, rather than grouped by span size) all items that do span a
  791. // track with a flexible sizing function while
  792. // - distributing space only to flexible tracks (i.e. treating all other tracks as having a fixed
  793. // sizing function)
  794. // - if the sum of the flexible sizing functions of all flexible tracks spanned by the item is greater
  795. // than zero, distributing space to such tracks according to the ratios of their flexible sizing
  796. // functions rather than distributing space equally
  797. // FIXME: 5. If any track still has an infinite growth limit (because, for example, it had no items placed in
  798. // it or it is a flexible track), set its growth limit to its base size.
  799. for (auto& track : tracks) {
  800. if (track.growth_limit == INFINITY) {
  801. track.growth_limit = track.base_size;
  802. }
  803. }
  804. // https://www.w3.org/TR/css-grid-2/#extra-space
  805. // 12.5.1. Distributing Extra Space Across Spanned Tracks
  806. // To distribute extra space by increasing the affected sizes of a set of tracks as required by a
  807. // set of intrinsic size contributions,
  808. CSSPixels sum_of_track_sizes = 0;
  809. for (auto& it : tracks)
  810. sum_of_track_sizes += it.base_size;
  811. // 1. Maintain separately for each affected base size or growth limit a planned increase, initially
  812. // set to 0. (This prevents the size increases from becoming order-dependent.)
  813. // 2. For each considered item,
  814. // 2.1. Find the space to distribute: Subtract the corresponding size (base size or growth limit) of
  815. // every spanned track from the item’s size contribution to find the item’s remaining size
  816. // contribution. (For infinite growth limits, substitute the track’s base size.) This is the space
  817. // to distribute. Floor it at zero.
  818. // For base sizes, the limit is its growth limit. For growth limits, the limit is infinity if it is
  819. // marked as infinitely growable, and equal to the growth limit otherwise. If the affected size was
  820. // a growth limit and the track is not marked infinitely growable, then each item-incurred increase
  821. // will be zero.
  822. // extra-space = max(0, size-contribution - ∑track-sizes)
  823. for (auto& track : tracks) {
  824. if (track.is_gap)
  825. continue;
  826. track.space_to_distribute = max(CSSPixels(0), track.growth_limit - track.base_size);
  827. }
  828. auto remaining_free_space = available_size.is_definite() ? available_size.to_px() - sum_of_track_sizes : 0;
  829. // 2.2. Distribute space up to limits: Find the item-incurred increase for each spanned track with an
  830. // affected size by: distributing the space equally among such tracks, freezing a track’s
  831. // item-incurred increase as its affected size + item-incurred increase reaches its limit (and
  832. // continuing to grow the unfrozen tracks as needed).
  833. auto count_of_unfrozen_tracks = 0;
  834. for (auto& track : tracks) {
  835. if (track.space_to_distribute > 0)
  836. count_of_unfrozen_tracks++;
  837. }
  838. while (remaining_free_space > 0) {
  839. if (count_of_unfrozen_tracks == 0)
  840. break;
  841. auto free_space_to_distribute_per_track = remaining_free_space / count_of_unfrozen_tracks;
  842. for (auto& track : tracks) {
  843. if (track.space_to_distribute == 0)
  844. continue;
  845. // 2.4. For each affected track, if the track’s item-incurred increase is larger than the track’s planned
  846. // increase set the track’s planned increase to that value.
  847. if (track.space_to_distribute <= free_space_to_distribute_per_track) {
  848. track.planned_increase += track.space_to_distribute;
  849. remaining_free_space -= track.space_to_distribute;
  850. track.space_to_distribute = 0;
  851. } else {
  852. track.space_to_distribute -= free_space_to_distribute_per_track;
  853. track.planned_increase += free_space_to_distribute_per_track;
  854. remaining_free_space -= free_space_to_distribute_per_track;
  855. }
  856. }
  857. count_of_unfrozen_tracks = 0;
  858. for (auto& track : tracks) {
  859. if (track.space_to_distribute > 0)
  860. count_of_unfrozen_tracks++;
  861. }
  862. if (remaining_free_space == 0)
  863. break;
  864. }
  865. // 2.3. Distribute space beyond limits: If space remains after all tracks are frozen, unfreeze and
  866. // continue to distribute space to the item-incurred increase of…
  867. // - when accommodating minimum contributions or accommodating min-content contributions: any affected
  868. // track that happens to also have an intrinsic max track sizing function; if there are no such
  869. // tracks, then all affected tracks.
  870. // - when accommodating max-content contributions: any affected track that happens to also have a
  871. // max-content max track sizing function; if there are no such tracks, then all affected tracks.
  872. // - when handling any intrinsic growth limit: all affected tracks.
  873. // For this purpose, the max track sizing function of a fit-content() track is treated as
  874. // max-content until it reaches the limit specified as the fit-content() argument, after which it is
  875. // treated as having a fixed sizing function of that argument.
  876. // This step prioritizes the distribution of space for accommodating space required by the
  877. // tracks’ min track sizing functions beyond their current growth limits based on the types of their
  878. // max track sizing functions.
  879. // 3. Update the tracks' affected sizes by adding in the planned increase so that the next round of
  880. // space distribution will account for the increase. (If the affected size is an infinite growth
  881. // limit, set it to the track’s base size plus the planned increase.)
  882. for (auto& track : tracks)
  883. track.base_size += track.planned_increase;
  884. }
  885. void GridFormattingContext::maximize_tracks(AvailableSize const& available_size, Vector<TemporaryTrack>& tracks)
  886. {
  887. // https://www.w3.org/TR/css-grid-2/#algo-grow-tracks
  888. // 12.6. Maximize Tracks
  889. auto get_free_space_px = [&]() -> CSSPixels {
  890. // For the purpose of this step: if sizing the grid container under a max-content constraint, the
  891. // free space is infinite; if sizing under a min-content constraint, the free space is zero.
  892. auto free_space = get_free_space(available_size, tracks);
  893. if (free_space.is_max_content()) {
  894. return INFINITY;
  895. } else if (free_space.is_min_content()) {
  896. return 0;
  897. } else {
  898. return free_space.to_px();
  899. }
  900. };
  901. auto free_space_px = get_free_space_px();
  902. // If the free space is positive, distribute it equally to the base sizes of all tracks, freezing
  903. // tracks as they reach their growth limits (and continuing to grow the unfrozen tracks as needed).
  904. while (free_space_px > 0) {
  905. auto free_space_to_distribute_per_track = free_space_px / (tracks.size() - count_of_gap_tracks(tracks));
  906. for (auto& track : tracks) {
  907. if (track.is_gap)
  908. continue;
  909. VERIFY(track.growth_limit != INFINITY);
  910. track.base_size = min(track.growth_limit, track.base_size + free_space_to_distribute_per_track);
  911. }
  912. if (get_free_space_px() == free_space_px)
  913. break;
  914. free_space_px = get_free_space_px();
  915. }
  916. // FIXME: If this would cause the grid to be larger than the grid container’s inner size as limited by its
  917. // max-width/height, then redo this step, treating the available grid space as equal to the grid
  918. // container’s inner size when it’s sized to its max-width/height.
  919. }
  920. void GridFormattingContext::expand_flexible_tracks(AvailableSize const& available_size, Vector<TemporaryTrack>& tracks)
  921. {
  922. // https://drafts.csswg.org/css-grid/#algo-flex-tracks
  923. // 12.7. Expand Flexible Tracks
  924. // This step sizes flexible tracks using the largest value it can assign to an fr without exceeding
  925. // the available space.
  926. auto find_the_size_of_an_fr = [&]() -> CSSPixels {
  927. // https://www.w3.org/TR/css-grid-2/#algo-find-fr-size
  928. VERIFY(available_size.is_definite());
  929. // 1. Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  930. auto leftover_space = available_size.to_px();
  931. for (auto& track : tracks) {
  932. if (!track.max_track_sizing_function.is_flexible_length()) {
  933. leftover_space -= track.base_size;
  934. }
  935. }
  936. // 2. Let flex factor sum be the sum of the flex factors of the flexible tracks.
  937. // If this value is less than 1, set it to 1 instead.
  938. auto flex_factor_sum = 0;
  939. for (auto& track : tracks) {
  940. if (track.max_track_sizing_function.is_flexible_length())
  941. flex_factor_sum++;
  942. }
  943. if (flex_factor_sum < 1)
  944. flex_factor_sum = 1;
  945. // 3. Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  946. auto hypothetical_fr_size = leftover_space / flex_factor_sum;
  947. // FIXME: 4. If the product of the hypothetical fr size and a flexible track’s flex factor is less than the track’s
  948. // base size, restart this algorithm treating all such tracks as inflexible.
  949. // 5. Return the hypothetical fr size.
  950. return hypothetical_fr_size;
  951. };
  952. // First, find the grid’s used flex fraction:
  953. auto flex_fraction = [&]() {
  954. auto free_space = get_free_space(available_size, tracks);
  955. // If the free space is zero or if sizing the grid container under a min-content constraint:
  956. if (free_space.to_px() == 0 || available_size.is_min_content()) {
  957. // The used flex fraction is zero.
  958. return CSSPixels(0);
  959. // Otherwise, if the free space is a definite length:
  960. } else if (free_space.is_definite()) {
  961. // The used flex fraction is the result of finding the size of an fr using all of the grid tracks and a space
  962. // to fill of the available grid space.
  963. return find_the_size_of_an_fr();
  964. } else {
  965. // FIXME
  966. return CSSPixels(0);
  967. }
  968. }();
  969. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is greater than
  970. // the track’s base size, set its base size to that product.
  971. for (auto& track : tracks) {
  972. if (track.max_track_sizing_function.flexible_length() * flex_fraction > track.base_size) {
  973. track.base_size = track.max_track_sizing_function.flexible_length() * flex_fraction;
  974. }
  975. }
  976. }
  977. void GridFormattingContext::stretch_auto_tracks(AvailableSize const& available_size, Vector<TemporaryTrack>& tracks)
  978. {
  979. // https://drafts.csswg.org/css-grid/#algo-stretch
  980. // 12.8. Stretch auto Tracks
  981. // When the content-distribution property of the grid container is normal or stretch in this axis,
  982. // this step expands tracks that have an auto max track sizing function by dividing any remaining
  983. // positive, definite free space equally amongst them. If the free space is indefinite, but the grid
  984. // container has a definite min-width/height, use that size to calculate the free space for this
  985. // step instead.
  986. CSSPixels used_space = 0;
  987. for (auto& track : tracks) {
  988. if (!(track.max_track_sizing_function.is_length() && track.max_track_sizing_function.length().is_auto()))
  989. used_space += track.base_size;
  990. }
  991. CSSPixels remaining_space = available_size.is_definite() ? available_size.to_px() - used_space : 0;
  992. auto count_of_auto_max_sizing_tracks = 0;
  993. for (auto& track : tracks) {
  994. if (track.max_track_sizing_function.is_length() && track.max_track_sizing_function.length().is_auto())
  995. count_of_auto_max_sizing_tracks++;
  996. }
  997. for (auto& track : tracks) {
  998. if (track.max_track_sizing_function.is_length() && track.max_track_sizing_function.length().is_auto())
  999. track.base_size = max(track.base_size, remaining_space / count_of_auto_max_sizing_tracks);
  1000. }
  1001. }
  1002. void GridFormattingContext::run_track_sizing(GridDimension const dimension, AvailableSpace const& available_space, Vector<TemporaryTrack>& tracks)
  1003. {
  1004. auto track_available_size = dimension == GridDimension::Column ? available_space.width : available_space.height;
  1005. initialize_track_sizes(track_available_size, tracks);
  1006. resolve_intrinsic_track_sizes(dimension, track_available_size, tracks);
  1007. maximize_tracks(track_available_size, tracks);
  1008. expand_flexible_tracks(track_available_size, tracks);
  1009. stretch_auto_tracks(track_available_size, tracks);
  1010. // If calculating the layout of a grid item in this step depends on the available space in the block
  1011. // axis, assume the available space that it would have if any row with a definite max track sizing
  1012. // function had that size and all other rows were infinite. If both the grid container and all
  1013. // tracks have definite sizes, also apply align-content to find the final effective size of any gaps
  1014. // spanned by such items; otherwise ignore the effects of track alignment in this estimation.
  1015. }
  1016. void GridFormattingContext::build_valid_grid_areas()
  1017. {
  1018. Vector<GridArea> found_grid_areas;
  1019. auto get_index_of_found_grid_area = [&](String needle) -> int {
  1020. for (size_t x = 0; x < found_grid_areas.size(); x++) {
  1021. if (found_grid_areas[x].name == needle)
  1022. return static_cast<int>(x);
  1023. }
  1024. return -1;
  1025. };
  1026. // https://www.w3.org/TR/css-grid-2/#grid-template-areas-property
  1027. // If a named grid area spans multiple grid cells, but those cells do not form a single
  1028. // filled-in rectangle, the declaration is invalid.
  1029. for (size_t y = 0; y < grid_container().computed_values().grid_template_areas().size(); y++) {
  1030. for (size_t x = 0; x < grid_container().computed_values().grid_template_areas()[y].size(); x++) {
  1031. auto grid_area_idx = get_index_of_found_grid_area(grid_container().computed_values().grid_template_areas()[y][x]);
  1032. if (grid_area_idx == -1) {
  1033. found_grid_areas.append({ grid_container().computed_values().grid_template_areas()[y][x], y, y + 1, x, x + 1 });
  1034. } else {
  1035. auto& grid_area = found_grid_areas[grid_area_idx];
  1036. if (grid_area.row_start == y) {
  1037. if (grid_area.column_end == x)
  1038. grid_area.column_end = grid_area.column_end + 1;
  1039. else
  1040. return;
  1041. } else {
  1042. if (grid_area.row_end == y) {
  1043. if (grid_area.column_start != x)
  1044. return;
  1045. grid_area.row_end = grid_area.row_end + 1;
  1046. } else if (grid_area.row_end == y + 1) {
  1047. if (grid_area.column_end < x || grid_area.column_end > x + 1)
  1048. return;
  1049. } else {
  1050. return;
  1051. }
  1052. }
  1053. }
  1054. }
  1055. }
  1056. for (auto const& checked_grid_area : found_grid_areas)
  1057. m_valid_grid_areas.append(checked_grid_area);
  1058. }
  1059. int GridFormattingContext::find_valid_grid_area(String const& needle)
  1060. {
  1061. for (size_t x = 0; x < m_valid_grid_areas.size(); x++) {
  1062. if (m_valid_grid_areas[x].name == needle)
  1063. return static_cast<int>(x);
  1064. }
  1065. return -1;
  1066. }
  1067. void GridFormattingContext::place_grid_items(AvailableSpace const& available_space)
  1068. {
  1069. auto grid_template_columns = grid_container().computed_values().grid_template_columns();
  1070. auto grid_template_rows = grid_container().computed_values().grid_template_rows();
  1071. auto column_count = get_count_of_tracks(grid_template_columns.track_list(), available_space);
  1072. auto row_count = get_count_of_tracks(grid_template_rows.track_list(), available_space);
  1073. // https://drafts.csswg.org/css-grid/#overview-placement
  1074. // 2.2. Placing Items
  1075. // The contents of the grid container are organized into individual grid items (analogous to
  1076. // flex items), which are then assigned to predefined areas in the grid. They can be explicitly
  1077. // placed using coordinates through the grid-placement properties or implicitly placed into
  1078. // empty areas using auto-placement.
  1079. grid_container().for_each_child_of_type<Box>([&](Box& child_box) {
  1080. if (can_skip_is_anonymous_text_run(child_box))
  1081. return IterationDecision::Continue;
  1082. m_boxes_to_place.append(child_box);
  1083. return IterationDecision::Continue;
  1084. });
  1085. m_occupation_grid = OccupationGrid(column_count, row_count);
  1086. build_valid_grid_areas();
  1087. // https://drafts.csswg.org/css-grid/#auto-placement-algo
  1088. // 8.5. Grid Item Placement Algorithm
  1089. // FIXME: 0. Generate anonymous grid items
  1090. // 1. Position anything that's not auto-positioned.
  1091. for (size_t i = 0; i < m_boxes_to_place.size(); i++) {
  1092. auto const& child_box = m_boxes_to_place[i];
  1093. if (is_auto_positioned_row(child_box->computed_values().grid_row_start(), child_box->computed_values().grid_row_end())
  1094. || is_auto_positioned_column(child_box->computed_values().grid_column_start(), child_box->computed_values().grid_column_end()))
  1095. continue;
  1096. place_item_with_row_and_column_position(child_box);
  1097. m_boxes_to_place.remove(i);
  1098. i--;
  1099. }
  1100. // 2. Process the items locked to a given row.
  1101. // FIXME: Do "dense" packing
  1102. for (size_t i = 0; i < m_boxes_to_place.size(); i++) {
  1103. auto const& child_box = m_boxes_to_place[i];
  1104. if (is_auto_positioned_row(child_box->computed_values().grid_row_start(), child_box->computed_values().grid_row_end()))
  1105. continue;
  1106. place_item_with_row_position(child_box);
  1107. m_boxes_to_place.remove(i);
  1108. i--;
  1109. }
  1110. // 3. Determine the columns in the implicit grid.
  1111. // NOTE: "implicit grid" here is the same as the m_occupation_grid
  1112. // 3.1. Start with the columns from the explicit grid.
  1113. // NOTE: Done in step 1.
  1114. // 3.2. Among all the items with a definite column position (explicitly positioned items, items
  1115. // positioned in the previous step, and items not yet positioned but with a definite column) add
  1116. // columns to the beginning and end of the implicit grid as necessary to accommodate those items.
  1117. // NOTE: "Explicitly positioned items" and "items positioned in the previous step" done in step 1
  1118. // and 2, respectively. Adding columns for "items not yet positioned but with a definite column"
  1119. // will be done in step 4.
  1120. // 4. Position the remaining grid items.
  1121. // For each grid item that hasn't been positioned by the previous steps, in order-modified document
  1122. // order:
  1123. auto auto_placement_cursor_x = 0;
  1124. auto auto_placement_cursor_y = 0;
  1125. for (size_t i = 0; i < m_boxes_to_place.size(); i++) {
  1126. auto const& child_box = m_boxes_to_place[i];
  1127. // 4.1. For sparse packing:
  1128. // FIXME: no distinction made. See #4.2
  1129. // 4.1.1. If the item has a definite column position:
  1130. if (!is_auto_positioned_column(child_box->computed_values().grid_column_start(), child_box->computed_values().grid_column_end()))
  1131. place_item_with_column_position(child_box, auto_placement_cursor_x, auto_placement_cursor_y);
  1132. // 4.1.2. If the item has an automatic grid position in both axes:
  1133. else
  1134. place_item_with_no_declared_position(child_box, auto_placement_cursor_x, auto_placement_cursor_y);
  1135. m_boxes_to_place.remove(i);
  1136. i--;
  1137. // FIXME: 4.2. For dense packing:
  1138. }
  1139. }
  1140. void GridFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const& available_space)
  1141. {
  1142. place_grid_items(available_space);
  1143. // https://drafts.csswg.org/css-grid/#overview-sizing
  1144. // 2.3. Sizing the Grid
  1145. // Once the grid items have been placed, the sizes of the grid tracks (rows and columns) are
  1146. // calculated, accounting for the sizes of their contents and/or available space as specified in
  1147. // the grid definition.
  1148. // https://www.w3.org/TR/css-grid-2/#layout-algorithm
  1149. // 12. Grid Sizing
  1150. // This section defines the grid sizing algorithm, which determines the size of all grid tracks and,
  1151. // by extension, the entire grid.
  1152. // Each track has specified minimum and maximum sizing functions (which may be the same). Each
  1153. // sizing function is either:
  1154. // - A fixed sizing function (<length> or resolvable <percentage>).
  1155. // - An intrinsic sizing function (min-content, max-content, auto, fit-content()).
  1156. // - A flexible sizing function (<flex>).
  1157. // The grid sizing algorithm defines how to resolve these sizing constraints into used track sizes.
  1158. initialize_grid_tracks_for_columns_and_rows(available_space);
  1159. initialize_gap_tracks(available_space);
  1160. // https://www.w3.org/TR/css-grid-2/#algo-overview
  1161. // 12.1. Grid Sizing Algorithm
  1162. // 1. First, the track sizing algorithm is used to resolve the sizes of the grid columns.
  1163. // In this process, any grid item which is subgridded in the grid container’s inline axis is treated
  1164. // as empty and its grid items (the grandchildren) are treated as direct children of the grid
  1165. // container (their grandparent). This introspection is recursive.
  1166. // Items which are subgridded only in the block axis, and whose grid container size in the inline
  1167. // axis depends on the size of its contents are also introspected: since the size of the item in
  1168. // this dimension can be dependent on the sizing of its subgridded tracks in the other, the size
  1169. // contribution of any such item to this grid’s column sizing (see Resolve Intrinsic Track Sizes) is
  1170. // taken under the provision of having determined its track sizing only up to the same point in the
  1171. // Grid Sizing Algorithm as this itself. E.g. for the first pass through this step, the item will
  1172. // have its tracks sized only through this first step; if a second pass of this step is triggered
  1173. // then the item will have completed a first pass through steps 1-3 as well as the second pass of
  1174. // this step prior to returning its size for consideration in this grid’s column sizing. Again, this
  1175. // introspection is recursive.
  1176. // https://www.w3.org/TR/css-grid-2/#algo-track-sizing
  1177. // 12.3. Track Sizing Algorithm
  1178. // The remainder of this section is the track sizing algorithm, which calculates from the min and
  1179. // max track sizing functions the used track size. Each track has a base size, a <length> which
  1180. // grows throughout the algorithm and which will eventually be the track’s final size, and a growth
  1181. // limit, a <length> which provides a desired maximum size for the base size. There are 5 steps:
  1182. // 1. Initialize Track Sizes
  1183. // 2. Resolve Intrinsic Track Sizes
  1184. // 3. Maximize Tracks
  1185. // 4. Expand Flexible Tracks
  1186. // 5. Expand Stretched auto Tracks
  1187. run_track_sizing(GridDimension::Column, available_space, m_grid_columns);
  1188. // https://www.w3.org/TR/css-grid-2/#algo-overview
  1189. // 12.1. Grid Sizing Algorithm
  1190. // 2. Next, the track sizing algorithm resolves the sizes of the grid rows.
  1191. // In this process, any grid item which is subgridded in the grid container’s block axis is treated
  1192. // as empty and its grid items (the grandchildren) are treated as direct children of the grid
  1193. // container (their grandparent). This introspection is recursive.
  1194. // As with sizing columns, items which are subgridded only in the inline axis, and whose grid
  1195. // container size in the block axis depends on the size of its contents are also introspected. (As
  1196. // with sizing columns, the size contribution to this grid’s row sizing is taken under the provision
  1197. // of having determined its track sizing only up to this corresponding point in the algorithm; and
  1198. // again, this introspection is recursive.)
  1199. // To find the inline-axis available space for any items whose block-axis size contributions require
  1200. // it, use the grid column sizes calculated in the previous step. If the grid container’s inline
  1201. // size is definite, also apply justify-content to account for the effective column gap sizes.
  1202. // https://www.w3.org/TR/css-grid-2/#algo-track-sizing
  1203. // 12.3. Track Sizing Algorithm
  1204. // The remainder of this section is the track sizing algorithm, which calculates from the min and
  1205. // max track sizing functions the used track size. Each track has a base size, a <length> which
  1206. // grows throughout the algorithm and which will eventually be the track’s final size, and a growth
  1207. // limit, a <length> which provides a desired maximum size for the base size. There are 5 steps:
  1208. // 1. Initialize Track Sizes
  1209. // 2. Resolve Intrinsic Track Sizes
  1210. // 3. Maximize Tracks
  1211. // 4. Expand Flexible Tracks
  1212. // 5. Expand Stretched auto Tracks
  1213. run_track_sizing(GridDimension::Row, available_space, m_grid_rows);
  1214. // https://www.w3.org/TR/css-grid-2/#algo-overview
  1215. // 12.1. Grid Sizing Algorithm
  1216. // 3. Then, if the min-content contribution of any grid item has changed based on the row sizes and
  1217. // alignment calculated in step 2, re-resolve the sizes of the grid columns with the new min-content
  1218. // and max-content contributions (once only).
  1219. // To find the block-axis available space for any items whose inline-axis size contributions require
  1220. // it, use the grid row sizes calculated in the previous step. If the grid container’s block size is
  1221. // definite, also apply align-content to account for the effective row gap sizes
  1222. // 4. Next, if the min-content contribution of any grid item has changed based on the column sizes and
  1223. // alignment calculated in step 3, re-resolve the sizes of the grid rows with the new min-content
  1224. // and max-content contributions (once only).
  1225. // To find the inline-axis available space for any items whose block-axis size contributions require
  1226. // it, use the grid column sizes calculated in the previous step. If the grid container’s inline
  1227. // size is definite, also apply justify-content to account for the effective column gap sizes.
  1228. // 5. Finally, the grid container is sized using the resulting size of the grid as its content size,
  1229. // and the tracks are aligned within the grid container according to the align-content and
  1230. // justify-content properties.
  1231. // Once the size of each grid area is thus established, the grid items are laid out into their
  1232. // respective containing blocks. The grid area’s width and height are considered definite for this
  1233. // purpose.
  1234. auto layout_box = [&](int row_start, int row_end, int column_start, int column_end, Box const& child_box) -> void {
  1235. if (column_start < 0 || row_start < 0)
  1236. return;
  1237. auto& child_box_state = m_state.get_mutable(child_box);
  1238. CSSPixels x_start = 0;
  1239. CSSPixels x_end = 0;
  1240. CSSPixels y_start = 0;
  1241. CSSPixels y_end = 0;
  1242. for (int i = 0; i < column_start; i++)
  1243. x_start += m_grid_columns[i].base_size;
  1244. for (int i = 0; i < column_end; i++)
  1245. x_end += m_grid_columns[i].base_size;
  1246. for (int i = 0; i < row_start; i++)
  1247. y_start += m_grid_rows[i].full_vertical_size();
  1248. for (int i = 0; i < row_end; i++) {
  1249. if (i >= row_start)
  1250. y_end += m_grid_rows[i].base_size;
  1251. else
  1252. y_end += m_grid_rows[i].full_vertical_size();
  1253. }
  1254. 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));
  1255. child_box_state.set_content_height(y_end - y_start);
  1256. child_box_state.offset = { x_start + m_grid_columns[column_start].border_left, y_start + m_grid_rows[row_start].border_top };
  1257. child_box_state.border_left = child_box.computed_values().border_left().width;
  1258. child_box_state.border_right = child_box.computed_values().border_right().width;
  1259. child_box_state.border_top = child_box.computed_values().border_top().width;
  1260. child_box_state.border_bottom = child_box.computed_values().border_bottom().width;
  1261. auto available_space_for_children = AvailableSpace(AvailableSize::make_definite(child_box_state.content_width()), AvailableSize::make_definite(child_box_state.content_height()));
  1262. if (auto independent_formatting_context = layout_inside(child_box, LayoutMode::Normal, available_space_for_children))
  1263. independent_formatting_context->parent_context_did_dimension_child_root_box();
  1264. };
  1265. for (auto& grid_item : m_grid_items) {
  1266. auto resolved_row_span = box.computed_values().row_gap().is_auto() ? grid_item.raw_row_span() : grid_item.raw_row_span() * 2;
  1267. if (!box.computed_values().row_gap().is_auto() && grid_item.gap_adjusted_row(box) == 0)
  1268. resolved_row_span -= 1;
  1269. if (grid_item.gap_adjusted_row(box) + resolved_row_span > m_grid_rows.size())
  1270. resolved_row_span = m_grid_rows.size() - grid_item.gap_adjusted_row(box);
  1271. auto resolved_column_span = box.computed_values().column_gap().is_auto() ? grid_item.raw_column_span() : grid_item.raw_column_span() * 2;
  1272. if (!box.computed_values().column_gap().is_auto() && grid_item.gap_adjusted_column(box) == 0)
  1273. resolved_column_span -= 1;
  1274. if (grid_item.gap_adjusted_column(box) + resolved_column_span > m_grid_columns.size())
  1275. resolved_column_span = m_grid_columns.size() - grid_item.gap_adjusted_column(box);
  1276. layout_box(
  1277. grid_item.gap_adjusted_row(box),
  1278. grid_item.gap_adjusted_row(box) + resolved_row_span,
  1279. grid_item.gap_adjusted_column(box),
  1280. grid_item.gap_adjusted_column(box) + resolved_column_span,
  1281. grid_item.box());
  1282. }
  1283. CSSPixels total_y = 0;
  1284. for (auto& grid_row : m_grid_rows)
  1285. total_y += grid_row.full_vertical_size();
  1286. m_automatic_content_height = total_y;
  1287. }
  1288. CSSPixels GridFormattingContext::automatic_content_width() const
  1289. {
  1290. return greatest_child_width(context_box());
  1291. }
  1292. CSSPixels GridFormattingContext::automatic_content_height() const
  1293. {
  1294. return m_automatic_content_height;
  1295. }
  1296. bool GridFormattingContext::is_auto_positioned_row(CSS::GridTrackPlacement const& grid_row_start, CSS::GridTrackPlacement const& grid_row_end) const
  1297. {
  1298. return is_auto_positioned_track(grid_row_start, grid_row_end);
  1299. }
  1300. bool GridFormattingContext::is_auto_positioned_column(CSS::GridTrackPlacement const& grid_column_start, CSS::GridTrackPlacement const& grid_column_end) const
  1301. {
  1302. return is_auto_positioned_track(grid_column_start, grid_column_end);
  1303. }
  1304. bool GridFormattingContext::is_auto_positioned_track(CSS::GridTrackPlacement const& grid_track_start, CSS::GridTrackPlacement const& grid_track_end) const
  1305. {
  1306. return grid_track_start.is_auto_positioned() && grid_track_end.is_auto_positioned();
  1307. }
  1308. AvailableSize GridFormattingContext::get_free_space(AvailableSize const& available_size, Vector<TemporaryTrack> const& tracks) const
  1309. {
  1310. // https://www.w3.org/TR/css-grid-2/#algo-terms
  1311. // free space: Equal to the available grid space minus the sum of the base sizes of all the grid
  1312. // tracks (including gutters), floored at zero. If available grid space is indefinite, the free
  1313. // space is indefinite as well.
  1314. if (available_size.is_definite()) {
  1315. CSSPixels sum_base_sizes = 0;
  1316. for (auto& track : tracks)
  1317. sum_base_sizes += track.base_size;
  1318. return AvailableSize::make_definite(max(CSSPixels(0), available_size.to_px() - sum_base_sizes));
  1319. }
  1320. return available_size;
  1321. }
  1322. int GridFormattingContext::get_line_index_by_line_name(String const& needle, CSS::GridTrackSizeList grid_track_size_list)
  1323. {
  1324. if (grid_track_size_list.track_list().size() == 0)
  1325. return -1;
  1326. auto repeated_tracks_count = 0;
  1327. for (size_t x = 0; x < grid_track_size_list.track_list().size(); x++) {
  1328. if (grid_track_size_list.track_list()[x].is_repeat()) {
  1329. // FIXME: Calculate amount of columns/rows if auto-fill/fit
  1330. if (!grid_track_size_list.track_list()[x].repeat().is_default())
  1331. return -1;
  1332. auto repeat = grid_track_size_list.track_list()[x].repeat().grid_track_size_list();
  1333. for (size_t y = 0; y < repeat.track_list().size(); y++) {
  1334. for (size_t z = 0; z < repeat.line_names()[y].size(); z++) {
  1335. if (repeat.line_names()[y][z] == needle)
  1336. return x + repeated_tracks_count;
  1337. repeated_tracks_count++;
  1338. }
  1339. }
  1340. } else {
  1341. for (size_t y = 0; y < grid_track_size_list.line_names()[x].size(); y++) {
  1342. if (grid_track_size_list.line_names()[x][y] == needle)
  1343. return x + repeated_tracks_count;
  1344. }
  1345. }
  1346. }
  1347. for (size_t y = 0; y < grid_track_size_list.line_names()[grid_track_size_list.track_list().size()].size(); y++) {
  1348. if (grid_track_size_list.line_names()[grid_track_size_list.track_list().size()][y] == needle)
  1349. return grid_track_size_list.track_list().size() + repeated_tracks_count;
  1350. }
  1351. return -1;
  1352. }
  1353. OccupationGrid::OccupationGrid(size_t column_count, size_t row_count)
  1354. {
  1355. Vector<bool> occupation_grid_row;
  1356. for (size_t column_index = 0; column_index < max(column_count, 1); column_index++)
  1357. occupation_grid_row.append(false);
  1358. for (size_t row_index = 0; row_index < max(row_count, 1); row_index++)
  1359. m_occupation_grid.append(occupation_grid_row);
  1360. }
  1361. OccupationGrid::OccupationGrid()
  1362. {
  1363. }
  1364. void OccupationGrid::maybe_add_column(size_t needed_number_of_columns)
  1365. {
  1366. if (needed_number_of_columns <= column_count())
  1367. return;
  1368. auto column_count_before_modification = column_count();
  1369. for (auto& occupation_grid_row : m_occupation_grid)
  1370. for (size_t idx = 0; idx < needed_number_of_columns - column_count_before_modification; idx++)
  1371. occupation_grid_row.append(false);
  1372. }
  1373. void OccupationGrid::maybe_add_row(size_t needed_number_of_rows)
  1374. {
  1375. if (needed_number_of_rows <= row_count())
  1376. return;
  1377. Vector<bool> new_occupation_grid_row;
  1378. for (size_t idx = 0; idx < column_count(); idx++)
  1379. new_occupation_grid_row.append(false);
  1380. for (size_t idx = 0; idx < needed_number_of_rows - row_count(); idx++)
  1381. m_occupation_grid.append(new_occupation_grid_row);
  1382. }
  1383. void OccupationGrid::set_occupied(size_t column_start, size_t column_end, size_t row_start, size_t row_end)
  1384. {
  1385. for (size_t row_index = 0; row_index < row_count(); row_index++) {
  1386. if (row_index >= row_start && row_index < row_end) {
  1387. for (size_t column_index = 0; column_index < column_count(); column_index++) {
  1388. if (column_index >= column_start && column_index < column_end)
  1389. set_occupied(column_index, row_index);
  1390. }
  1391. }
  1392. }
  1393. }
  1394. void OccupationGrid::set_occupied(size_t column_index, size_t row_index)
  1395. {
  1396. m_occupation_grid[row_index][column_index] = true;
  1397. }
  1398. bool OccupationGrid::is_occupied(size_t column_index, size_t row_index)
  1399. {
  1400. return m_occupation_grid[row_index][column_index];
  1401. }
  1402. size_t GridItem::gap_adjusted_row(Box const& grid_box) const
  1403. {
  1404. return grid_box.computed_values().row_gap().is_auto() ? m_row : m_row * 2;
  1405. }
  1406. size_t GridItem::gap_adjusted_column(Box const& grid_box) const
  1407. {
  1408. return grid_box.computed_values().column_gap().is_auto() ? m_column : m_column * 2;
  1409. }
  1410. // https://www.w3.org/TR/css-grid-2/#min-size-auto
  1411. CSSPixels GridFormattingContext::content_based_minimum_height(GridItem const& item)
  1412. {
  1413. // The content-based minimum size for a grid item in a given dimension is its specified size suggestion if it exists
  1414. if (!item.box().computed_values().height().is_auto()) {
  1415. if (item.box().computed_values().height().is_length())
  1416. return item.box().computed_values().height().length().to_px(item.box());
  1417. }
  1418. // FIXME: otherwise its transferred size suggestion if that exists
  1419. // else its content size suggestion
  1420. return calculate_min_content_height(item.box(), AvailableSize::make_definite(m_grid_columns[item.gap_adjusted_column(grid_container())].base_size));
  1421. }
  1422. }