GridFormattingContext.cpp 105 KB

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