GridFormattingContext.cpp 109 KB

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