GridFormattingContext.cpp 89 KB

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