GridFormattingContext.cpp 91 KB

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