GridFormattingContext.cpp 109 KB

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