GridFormattingContext.cpp 95 KB

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