TableFormattingContext.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Node.h>
  7. #include <LibWeb/HTML/BrowsingContext.h>
  8. #include <LibWeb/HTML/HTMLTableCellElement.h>
  9. #include <LibWeb/HTML/HTMLTableColElement.h>
  10. #include <LibWeb/HTML/HTMLTableRowElement.h>
  11. #include <LibWeb/Layout/BlockFormattingContext.h>
  12. #include <LibWeb/Layout/Box.h>
  13. #include <LibWeb/Layout/InlineFormattingContext.h>
  14. #include <LibWeb/Layout/TableFormattingContext.h>
  15. struct GridPosition {
  16. size_t x;
  17. size_t y;
  18. };
  19. inline bool operator==(GridPosition const& a, GridPosition const& b)
  20. {
  21. return a.x == b.x && a.y == b.y;
  22. }
  23. namespace AK {
  24. template<>
  25. struct Traits<GridPosition> : public GenericTraits<GridPosition> {
  26. static unsigned hash(GridPosition const& key)
  27. {
  28. return pair_int_hash(key.x, key.y);
  29. }
  30. };
  31. }
  32. namespace Web::Layout {
  33. TableFormattingContext::TableFormattingContext(LayoutState& state, Box const& root, FormattingContext* parent)
  34. : FormattingContext(Type::Table, state, root, parent)
  35. {
  36. }
  37. TableFormattingContext::~TableFormattingContext() = default;
  38. static inline bool is_table_row_group(Box const& box)
  39. {
  40. auto const& display = box.display();
  41. return display.is_table_row_group() || display.is_table_header_group() || display.is_table_footer_group();
  42. }
  43. static inline bool is_table_row(Box const& box)
  44. {
  45. return box.display().is_table_row();
  46. }
  47. static inline bool is_table_column_group(Box const& box)
  48. {
  49. return box.display().is_table_column_group();
  50. }
  51. static inline bool is_table_column(Box const& box)
  52. {
  53. return box.display().is_table_column();
  54. }
  55. template<typename Matcher, typename Callback>
  56. static void for_each_child_box_matching(Box const& parent, Matcher matcher, Callback callback)
  57. {
  58. parent.for_each_child_of_type<Box>([&](Box const& child_box) {
  59. if (matcher(child_box))
  60. callback(child_box);
  61. });
  62. }
  63. CSSPixels TableFormattingContext::run_caption_layout(LayoutMode layout_mode, CSS::CaptionSide phase)
  64. {
  65. CSSPixels caption_height = 0;
  66. for (auto* child = table_box().first_child(); child; child = child->next_sibling()) {
  67. if (!child->display().is_table_caption() || child->computed_values().caption_side() != phase) {
  68. continue;
  69. }
  70. // The caption boxes are principal block-level boxes that retain their own content, padding, margin, and border areas,
  71. // and are rendered as normal block boxes inside the table wrapper box, as described in https://www.w3.org/TR/CSS22/tables.html#model
  72. auto caption_context = make<BlockFormattingContext>(m_state, *verify_cast<BlockContainer>(child), this);
  73. caption_context->run(table_box(), layout_mode, *m_available_space);
  74. VERIFY(child->is_box());
  75. auto const& child_box = static_cast<Box const&>(*child);
  76. // FIXME: Since caption only has inline children, BlockFormattingContext doesn't resolve the vertical metrics.
  77. // We need to do it manually here.
  78. caption_context->resolve_vertical_box_model_metrics(child_box);
  79. auto const& caption_state = m_state.get(child_box);
  80. if (phase == CSS::CaptionSide::Top) {
  81. m_state.get_mutable(table_box()).set_content_y(caption_state.content_height() + caption_state.margin_box_bottom());
  82. } else {
  83. m_state.get_mutable(child_box).set_content_y(
  84. m_state.get(table_box()).margin_box_height() + caption_state.margin_box_top());
  85. }
  86. caption_height += caption_state.margin_box_height();
  87. }
  88. return caption_height;
  89. }
  90. void TableFormattingContext::calculate_row_column_grid(Box const& box)
  91. {
  92. // Implements https://html.spec.whatwg.org/multipage/tables.html#forming-a-table
  93. HashMap<GridPosition, bool> grid;
  94. size_t x_width = 0, y_height = 0;
  95. size_t x_current = 0, y_current = 0;
  96. size_t max_cell_x = 0, max_cell_y = 0;
  97. // Implements https://html.spec.whatwg.org/multipage/tables.html#algorithm-for-processing-rows
  98. auto process_row = [&](auto& row) {
  99. if (y_height == y_current)
  100. y_height++;
  101. x_current = 0;
  102. for (auto* child = row.first_child(); child; child = child->next_sibling()) {
  103. if (child->display().is_table_cell()) {
  104. // Cells: While x_current is less than x_width and the slot with coordinate (x_current, y_current) already has a cell assigned to it, increase x_current by 1.
  105. while (x_current < x_width && grid.contains(GridPosition { x_current, y_current }))
  106. x_current++;
  107. Box const* box = static_cast<Box const*>(child);
  108. if (x_current == x_width)
  109. x_width++;
  110. size_t colspan = 1, rowspan = 1;
  111. if (box->dom_node() && is<HTML::HTMLTableCellElement>(*box->dom_node())) {
  112. auto const& node = static_cast<HTML::HTMLTableCellElement const&>(*box->dom_node());
  113. colspan = node.col_span();
  114. rowspan = node.row_span();
  115. }
  116. if (x_width < x_current + colspan)
  117. x_width = x_current + colspan;
  118. if (y_height < y_current + rowspan)
  119. y_height = y_current + rowspan;
  120. for (size_t y = y_current; y < y_current + rowspan; y++)
  121. for (size_t x = x_current; x < x_current + colspan; x++)
  122. grid.set(GridPosition { x, y }, true);
  123. m_cells.append(Cell { *box, x_current, y_current, colspan, rowspan });
  124. max_cell_x = max(x_current, max_cell_x);
  125. max_cell_y = max(y_current, max_cell_y);
  126. x_current += colspan;
  127. }
  128. }
  129. m_rows.append(Row { row });
  130. y_current++;
  131. };
  132. for_each_child_box_matching(box, is_table_row_group, [&](auto& row_group_box) {
  133. for_each_child_box_matching(row_group_box, is_table_row, [&](auto& row_box) {
  134. process_row(row_box);
  135. return IterationDecision::Continue;
  136. });
  137. });
  138. for_each_child_box_matching(box, is_table_row, [&](auto& row_box) {
  139. process_row(row_box);
  140. return IterationDecision::Continue;
  141. });
  142. m_columns.resize(x_width);
  143. for (auto& cell : m_cells) {
  144. // Clip spans to the end of the table.
  145. cell.row_span = min(cell.row_span, m_rows.size() - cell.row_index);
  146. cell.column_span = min(cell.column_span, m_columns.size() - cell.column_index);
  147. }
  148. m_cells_by_coordinate.resize(max_cell_y + 1);
  149. for (auto& position_to_cell_row : m_cells_by_coordinate) {
  150. position_to_cell_row.resize(max_cell_x + 1);
  151. }
  152. for (auto const& cell : m_cells) {
  153. m_cells_by_coordinate[cell.row_index][cell.column_index] = cell;
  154. m_columns[cell.column_index].has_originating_cells = true;
  155. }
  156. }
  157. void TableFormattingContext::compute_constrainedness()
  158. {
  159. // Definition of constrainedness: https://www.w3.org/TR/css-tables-3/#constrainedness
  160. size_t column_index = 0;
  161. for_each_child_box_matching(table_box(), is_table_column_group, [&](auto& column_group_box) {
  162. for_each_child_box_matching(column_group_box, is_table_column, [&](auto& column_box) {
  163. auto const& computed_values = column_box.computed_values();
  164. if (!computed_values.width().is_auto() && !computed_values.width().is_percentage()) {
  165. m_columns[column_index].is_constrained = true;
  166. }
  167. auto const& col_node = static_cast<HTML::HTMLTableColElement const&>(*column_box.dom_node());
  168. unsigned span = col_node.attribute(HTML::AttributeNames::span).to_uint().value_or(1);
  169. column_index += span;
  170. });
  171. });
  172. for (auto& row : m_rows) {
  173. auto const& computed_values = row.box->computed_values();
  174. if (!computed_values.height().is_auto() && !computed_values.height().is_percentage()) {
  175. row.is_constrained = true;
  176. }
  177. }
  178. for (auto& cell : m_cells) {
  179. auto const& computed_values = cell.box->computed_values();
  180. if (!computed_values.width().is_auto() && !computed_values.width().is_percentage()) {
  181. m_columns[cell.column_index].is_constrained = true;
  182. }
  183. if (!computed_values.height().is_auto() && !computed_values.height().is_percentage()) {
  184. m_rows[cell.row_index].is_constrained = true;
  185. }
  186. }
  187. }
  188. void TableFormattingContext::compute_cell_measures(AvailableSpace const& available_space)
  189. {
  190. // Implements https://www.w3.org/TR/css-tables-3/#computing-cell-measures.
  191. auto const& containing_block = m_state.get(*table_wrapper().containing_block());
  192. compute_constrainedness();
  193. for (auto& cell : m_cells) {
  194. auto const& computed_values = cell.box->computed_values();
  195. CSSPixels padding_top = computed_values.padding().top().to_px(cell.box, containing_block.content_height());
  196. CSSPixels padding_bottom = computed_values.padding().bottom().to_px(cell.box, containing_block.content_height());
  197. CSSPixels padding_left = computed_values.padding().left().to_px(cell.box, containing_block.content_width());
  198. CSSPixels padding_right = computed_values.padding().right().to_px(cell.box, containing_block.content_width());
  199. auto const& cell_state = m_state.get(cell.box);
  200. auto use_collapsing_borders_model = cell_state.override_borders_data().has_value();
  201. // Implement the collapsing border model https://www.w3.org/TR/CSS22/tables.html#collapsing-borders.
  202. CSSPixels border_top = use_collapsing_borders_model ? round(cell_state.border_top / 2) : computed_values.border_top().width;
  203. CSSPixels border_bottom = use_collapsing_borders_model ? round(cell_state.border_bottom / 2) : computed_values.border_bottom().width;
  204. CSSPixels border_left = use_collapsing_borders_model ? round(cell_state.border_left / 2) : computed_values.border_left().width;
  205. CSSPixels border_right = use_collapsing_borders_model ? round(cell_state.border_right / 2) : computed_values.border_right().width;
  206. auto min_content_height = calculate_min_content_height(cell.box, available_space.width);
  207. auto max_content_height = calculate_max_content_height(cell.box, available_space.width);
  208. auto min_content_width = calculate_min_content_width(cell.box);
  209. auto max_content_width = calculate_max_content_width(cell.box);
  210. // The outer min-content height of a table-cell is max(min-height, min-content height) adjusted by the cell intrinsic offsets.
  211. auto min_height = computed_values.min_height().to_px(cell.box, containing_block.content_height());
  212. auto cell_intrinsic_height_offsets = padding_top + padding_bottom + border_top + border_bottom;
  213. cell.outer_min_height = max(min_height, min_content_height) + cell_intrinsic_height_offsets;
  214. // The outer min-content width of a table-cell is max(min-width, min-content width) adjusted by the cell intrinsic offsets.
  215. auto min_width = computed_values.min_width().to_px(cell.box, containing_block.content_width());
  216. auto cell_intrinsic_width_offsets = padding_left + padding_right + border_left + border_right;
  217. // For fixed mode, according to https://www.w3.org/TR/css-tables-3/#computing-column-measures:
  218. // The min-content and max-content width of cells is considered zero unless they are directly specified as a length-percentage,
  219. // in which case they are resolved based on the table width (if it is definite, otherwise use 0).
  220. auto width_is_specified_length_or_percentage = computed_values.width().is_length() || computed_values.width().is_percentage();
  221. if (!use_fixed_mode_layout() || width_is_specified_length_or_percentage) {
  222. cell.outer_min_width = max(min_width, min_content_width) + cell_intrinsic_width_offsets;
  223. }
  224. // The tables specification isn't explicit on how to use the height and max-height CSS properties in the outer max-content formulas.
  225. // However, during this early phase we don't have enough information to resolve percentage sizes yet and the formulas for outer sizes
  226. // in the specification give enough clues to pick defaults in a way that makes sense.
  227. auto height = computed_values.height().is_length() ? computed_values.height().to_px(cell.box, containing_block.content_height()) : 0;
  228. auto max_height = computed_values.max_height().is_length() ? computed_values.max_height().to_px(cell.box, containing_block.content_height()) : INFINITY;
  229. if (m_rows[cell.row_index].is_constrained) {
  230. // The outer max-content height of a table-cell in a constrained row is
  231. // max(min-height, height, min-content height, min(max-height, height)) adjusted by the cell intrinsic offsets.
  232. // NB: min(max-height, height) doesn't have any effect here, we can simplify the expression to max(min-height, height, min-content height).
  233. cell.outer_max_height = max(min_height, max(height, min_content_height)) + cell_intrinsic_height_offsets;
  234. } else {
  235. // The outer max-content height of a table-cell in a non-constrained row is
  236. // max(min-height, height, min-content height, min(max-height, max-content height)) adjusted by the cell intrinsic offsets.
  237. cell.outer_max_height = max(min_height, max(height, max(min_content_height, min(max_height, max_content_height)))) + cell_intrinsic_height_offsets;
  238. }
  239. // See the explanation for height and max_height above.
  240. auto width = computed_values.width().is_length() ? computed_values.width().to_px(cell.box, containing_block.content_width()) : 0;
  241. auto max_width = computed_values.max_width().is_length() ? computed_values.max_width().to_px(cell.box, containing_block.content_width()) : INFINITY;
  242. if (use_fixed_mode_layout() && !width_is_specified_length_or_percentage) {
  243. continue;
  244. }
  245. if (m_columns[cell.column_index].is_constrained) {
  246. // The outer max-content width of a table-cell in a constrained column is
  247. // max(min-width, width, min-content width, min(max-width, width)) adjusted by the cell intrinsic offsets.
  248. // NB: min(max-width, width) doesn't have any effect here, we can simplify the expression to max(min-width, width, min-content width).
  249. cell.outer_max_width = max(min_width, max(width, min_content_width)) + cell_intrinsic_width_offsets;
  250. } else {
  251. // The outer max-content width of a table-cell in a non-constrained column is
  252. // max(min-width, width, min-content width, min(max-width, max-content width)) adjusted by the cell intrinsic offsets.
  253. cell.outer_max_width = max(min_width, max(width, max(min_content_width, min(max_width, max_content_width)))) + cell_intrinsic_width_offsets;
  254. }
  255. }
  256. }
  257. void TableFormattingContext::compute_outer_content_sizes()
  258. {
  259. auto const& containing_block = m_state.get(*table_wrapper().containing_block());
  260. size_t column_index = 0;
  261. for_each_child_box_matching(table_box(), is_table_column_group, [&](auto& column_group_box) {
  262. for_each_child_box_matching(column_group_box, is_table_column, [&](auto& column_box) {
  263. auto const& computed_values = column_box.computed_values();
  264. auto min_width = computed_values.min_width().to_px(column_box, containing_block.content_width());
  265. auto max_width = computed_values.max_width().is_length() ? computed_values.max_width().to_px(column_box, containing_block.content_width()) : INFINITY;
  266. auto width = computed_values.width().to_px(column_box, containing_block.content_width());
  267. // The outer min-content width of a table-column or table-column-group is max(min-width, width).
  268. m_columns[column_index].min_size = max(min_width, width);
  269. // The outer max-content width of a table-column or table-column-group is max(min-width, min(max-width, width)).
  270. m_columns[column_index].max_size = max(min_width, min(max_width, width));
  271. auto const& col_node = static_cast<HTML::HTMLTableColElement const&>(*column_box.dom_node());
  272. unsigned span = col_node.attribute(HTML::AttributeNames::span).to_uint().value_or(1);
  273. column_index += span;
  274. });
  275. });
  276. for (auto& row : m_rows) {
  277. auto const& computed_values = row.box->computed_values();
  278. auto min_height = computed_values.min_height().to_px(row.box, containing_block.content_height());
  279. auto max_height = computed_values.max_height().is_length() ? computed_values.max_height().to_px(row.box, containing_block.content_height()) : INFINITY;
  280. auto height = computed_values.height().to_px(row.box, containing_block.content_height());
  281. // The outer min-content height of a table-row or table-row-group is max(min-height, height).
  282. row.min_size = max(min_height, height);
  283. // The outer max-content height of a table-row or table-row-group is max(min-height, min(max-height, height)).
  284. row.max_size = max(min_height, min(max_height, height));
  285. }
  286. }
  287. template<>
  288. void TableFormattingContext::initialize_table_measures<TableFormattingContext::Row>()
  289. {
  290. auto const& containing_block = m_state.get(*table_wrapper().containing_block());
  291. for (auto& cell : m_cells) {
  292. auto const& computed_values = cell.box->computed_values();
  293. if (cell.row_span == 1) {
  294. auto specified_height = computed_values.height().to_px(cell.box, containing_block.content_height());
  295. // https://www.w3.org/TR/css-tables-3/#row-layout makes specified cell height part of the initialization formula for row table measures:
  296. // This is done by running the same algorithm as the column measurement, with the span=1 value being initialized (for min-content) with
  297. // the largest of the resulting height of the previous row layout, the height specified on the corresponding table-row (if any), and
  298. // the largest height specified on cells that span this row only (the algorithm starts by considering cells of span 2 on top of that assignment).
  299. m_rows[cell.row_index].min_size = max(m_rows[cell.row_index].min_size, max(cell.outer_min_height, specified_height));
  300. m_rows[cell.row_index].max_size = max(m_rows[cell.row_index].max_size, cell.outer_max_height);
  301. }
  302. }
  303. }
  304. template<>
  305. void TableFormattingContext::initialize_table_measures<TableFormattingContext::Column>()
  306. {
  307. // Implement the following parts of the specification, accounting for fixed layout mode:
  308. // https://www.w3.org/TR/css-tables-3/#min-content-width-of-a-column-based-on-cells-of-span-up-to-1
  309. // https://www.w3.org/TR/css-tables-3/#max-content-width-of-a-column-based-on-cells-of-span-up-to-1
  310. for (auto& cell : m_cells) {
  311. if (cell.column_span == 1 && (cell.row_index == 0 || !use_fixed_mode_layout())) {
  312. m_columns[cell.column_index].min_size = max(m_columns[cell.column_index].min_size, cell.outer_min_width);
  313. m_columns[cell.column_index].max_size = max(m_columns[cell.column_index].max_size, cell.outer_max_width);
  314. }
  315. }
  316. }
  317. template<class RowOrColumn>
  318. void TableFormattingContext::compute_intrinsic_percentage(size_t max_cell_span)
  319. {
  320. auto& rows_or_columns = table_rows_or_columns<RowOrColumn>();
  321. // https://www.w3.org/TR/css-tables-3/#intrinsic-percentage-width-of-a-column-based-on-cells-of-span-up-to-1
  322. initialize_intrinsic_percentages_from_rows_or_columns<RowOrColumn>();
  323. initialize_intrinsic_percentages_from_cells<RowOrColumn>();
  324. // Stores intermediate values for intrinsic percentage based on cells of span up to N for the iterative algorithm, to store them back at the end of the step.
  325. Vector<double> intrinsic_percentage_contribution_by_index;
  326. intrinsic_percentage_contribution_by_index.resize(rows_or_columns.size());
  327. for (size_t rc_index = 0; rc_index < rows_or_columns.size(); ++rc_index) {
  328. intrinsic_percentage_contribution_by_index[rc_index] = rows_or_columns[rc_index].intrinsic_percentage;
  329. }
  330. for (size_t current_span = 2; current_span <= max_cell_span; current_span++) {
  331. // https://www.w3.org/TR/css-tables-3/#intrinsic-percentage-width-of-a-column-based-on-cells-of-span-up-to-n-n--1
  332. for (auto& cell : m_cells) {
  333. auto cell_span_value = cell_span<RowOrColumn>(cell);
  334. if (cell_span_value != current_span) {
  335. continue;
  336. }
  337. auto cell_start_rc_index = cell_index<RowOrColumn>(cell);
  338. auto cell_end_rc_index = cell_start_rc_index + cell_span_value;
  339. // 1. Start with the percentage contribution of the cell.
  340. CSSPixels cell_contribution = cell_percentage_contribution<RowOrColumn>(cell);
  341. // 2. Subtract the intrinsic percentage width of the column based on cells of span up to N-1 of all columns
  342. // that the cell spans. If this gives a negative result, change it to 0%.
  343. for (auto rc_index = cell_start_rc_index; rc_index < cell_end_rc_index; rc_index++) {
  344. cell_contribution -= rows_or_columns[rc_index].intrinsic_percentage;
  345. cell_contribution = max(cell_contribution, 0);
  346. }
  347. // Compute the sum of the non-spanning max-content sizes of all rows / columns spanned by the cell that have an intrinsic percentage
  348. // size of the row / column based on cells of span up to N-1 equal to 0%, to be used in step 3 of the cell contribution algorithm.
  349. CSSPixels width_sum_of_columns_with_zero_intrinsic_percentage = 0;
  350. size_t number_of_columns_with_zero_intrinsic_percentage = 0;
  351. for (auto rc_index = cell_start_rc_index; rc_index < cell_end_rc_index; rc_index++) {
  352. if (rows_or_columns[rc_index].intrinsic_percentage == 0) {
  353. width_sum_of_columns_with_zero_intrinsic_percentage += rows_or_columns[rc_index].max_size;
  354. ++number_of_columns_with_zero_intrinsic_percentage;
  355. }
  356. }
  357. for (size_t rc_index = cell_start_rc_index; rc_index < cell_end_rc_index; rc_index++) {
  358. // If the intrinsic percentage width of a column based on cells of span up to N-1 is greater than 0%, then the intrinsic percentage width of
  359. // the column based on cells of span up to N is the same as the intrinsic percentage width of the column based on cells of span up to N-1.
  360. if (rows_or_columns[rc_index].intrinsic_percentage > 0) {
  361. continue;
  362. }
  363. // Otherwise, it is the largest of the contributions of the cells in the column whose colSpan is N,
  364. // where the contribution of a cell is the result of taking the following steps:
  365. // 1. Start with the percentage contribution of the cell.
  366. // 2. Subtract the intrinsic percentage width of the column based on cells of span up to N-1 of all columns
  367. // that the cell spans. If this gives a negative result, change it to 0%.
  368. // 3. Multiply by the ratio of the column’s non-spanning max-content width to the sum of the non-spanning max-content widths of all
  369. // columns spanned by the cell that have an intrinsic percentage width of the column based on cells of span up to N-1 equal to 0%.
  370. CSSPixels ajusted_cell_contribution;
  371. if (width_sum_of_columns_with_zero_intrinsic_percentage != 0) {
  372. ajusted_cell_contribution = cell_contribution * rows_or_columns[rc_index].max_size / static_cast<double>(width_sum_of_columns_with_zero_intrinsic_percentage);
  373. } else {
  374. // However, if this ratio is undefined because the denominator is zero, instead use the 1 divided by the number of columns
  375. // spanned by the cell that have an intrinsic percentage width of the column based on cells of span up to N-1 equal to zero.
  376. ajusted_cell_contribution = cell_contribution * 1 / number_of_columns_with_zero_intrinsic_percentage;
  377. }
  378. intrinsic_percentage_contribution_by_index[rc_index] = max(static_cast<double>(ajusted_cell_contribution), intrinsic_percentage_contribution_by_index[rc_index]);
  379. }
  380. }
  381. for (size_t rc_index = 0; rc_index < rows_or_columns.size(); ++rc_index) {
  382. rows_or_columns[rc_index].intrinsic_percentage = intrinsic_percentage_contribution_by_index[rc_index];
  383. }
  384. }
  385. // Clamp total intrinsic percentage to 100%: https://www.w3.org/TR/css-tables-3/#intrinsic-percentage-width-of-a-column
  386. double total_intrinsic_percentage = 0;
  387. for (auto& rows_or_column : rows_or_columns) {
  388. rows_or_column.intrinsic_percentage = max(min(100 - total_intrinsic_percentage, rows_or_column.intrinsic_percentage), 0);
  389. total_intrinsic_percentage += rows_or_column.intrinsic_percentage;
  390. }
  391. }
  392. template<class RowOrColumn>
  393. void TableFormattingContext::compute_table_measures()
  394. {
  395. initialize_table_measures<RowOrColumn>();
  396. auto& rows_or_columns = table_rows_or_columns<RowOrColumn>();
  397. size_t max_cell_span = 1;
  398. for (auto& cell : m_cells) {
  399. max_cell_span = max(max_cell_span, cell_span<RowOrColumn>(cell));
  400. }
  401. // Since the intrinsic percentage specification uses non-spanning max-content size for the iterative algorithm,
  402. // run it before we compute the spanning max-content size with its own iterative algorithm for span up to N.
  403. compute_intrinsic_percentage<RowOrColumn>(max_cell_span);
  404. for (size_t current_span = 2; current_span <= max_cell_span; current_span++) {
  405. // https://www.w3.org/TR/css-tables-3/#min-content-width-of-a-column-based-on-cells-of-span-up-to-n-n--1
  406. Vector<Vector<CSSPixels>> cell_min_contributions_by_rc_index;
  407. cell_min_contributions_by_rc_index.resize(rows_or_columns.size());
  408. // https://www.w3.org/TR/css-tables-3/#max-content-width-of-a-column-based-on-cells-of-span-up-to-n-n--1
  409. Vector<Vector<CSSPixels>> cell_max_contributions_by_rc_index;
  410. cell_max_contributions_by_rc_index.resize(rows_or_columns.size());
  411. for (auto& cell : m_cells) {
  412. auto cell_span_value = cell_span<RowOrColumn>(cell);
  413. if (cell_span_value == current_span) {
  414. // Define the baseline max-content size as the sum of the max-content sizes based on cells of span up to N-1 of all columns that the cell spans.
  415. auto cell_start_rc_index = cell_index<RowOrColumn>(cell);
  416. auto cell_end_rc_index = cell_start_rc_index + cell_span_value;
  417. CSSPixels baseline_max_content_size = 0;
  418. for (auto rc_index = cell_start_rc_index; rc_index < cell_end_rc_index; rc_index++) {
  419. baseline_max_content_size += rows_or_columns[rc_index].max_size;
  420. }
  421. CSSPixels baseline_min_content_size = 0;
  422. for (auto rc_index = cell_start_rc_index; rc_index < cell_end_rc_index; rc_index++) {
  423. baseline_min_content_size += rows_or_columns[rc_index].min_size;
  424. }
  425. // Define the baseline border spacing as the sum of the horizontal border-spacing for any columns spanned by the cell, other than the one in which the cell originates.
  426. auto baseline_border_spacing = border_spacing<RowOrColumn>() * (cell_span_value - 1);
  427. // Add contribution from all rows / columns, since we've weighted the gap to the desired spanned size by the the
  428. // ratio of the max-content size based on cells of span up to N-1 of the row / column to the baseline max-content width.
  429. for (auto rc_index = cell_start_rc_index; rc_index < cell_end_rc_index; rc_index++) {
  430. // The contribution of the cell is the sum of:
  431. // the min-content size of the column based on cells of span up to N-1
  432. auto cell_min_contribution = rows_or_columns[rc_index].min_size;
  433. // the product of:
  434. // - the ratio of:
  435. // - the max-content size of the row / column based on cells of span up to N-1 of the row / column minus the
  436. // min-content size of the row / column based on cells of span up to N-1 of the row / column, to
  437. // - the baseline max-content size minus the baseline min-content size
  438. // or zero if this ratio is undefined, and
  439. // - the outer min-content size of the cell minus the baseline min-content size and the baseline border spacing, clamped
  440. // to be at least 0 and at most the difference between the baseline max-content size and the baseline min-content size
  441. auto normalized_max_min_diff = baseline_max_content_size != baseline_min_content_size
  442. ? (rows_or_columns[rc_index].max_size - rows_or_columns[rc_index].min_size) / static_cast<double>(baseline_max_content_size - baseline_min_content_size)
  443. : 0;
  444. auto clamped_diff_to_baseline_min = min(
  445. max(cell_min_size<RowOrColumn>(cell) - baseline_min_content_size - baseline_border_spacing, 0),
  446. baseline_max_content_size - baseline_min_content_size);
  447. cell_min_contribution += normalized_max_min_diff * clamped_diff_to_baseline_min;
  448. // the product of:
  449. // - the ratio of the max-content size based on cells of span up to N-1 of the column to the baseline max-content size
  450. // - the outer min-content size of the cell minus the baseline max-content size and baseline border spacing, or 0 if this is negative
  451. if (baseline_max_content_size != 0) {
  452. cell_min_contribution += (rows_or_columns[rc_index].max_size / static_cast<double>(baseline_max_content_size))
  453. * max(CSSPixels(0), cell_min_size<RowOrColumn>(cell) - baseline_max_content_size - baseline_border_spacing);
  454. }
  455. // The contribution of the cell is the sum of:
  456. // the max-content size of the column based on cells of span up to N-1
  457. auto cell_max_contribution = rows_or_columns[rc_index].max_size;
  458. // and the product of:
  459. // - the ratio of the max-content size based on cells of span up to N-1 of the column to the baseline max-content size
  460. // - the outer max-content size of the cell minus the baseline max-content size and the baseline border spacing, or 0 if this is negative
  461. if (baseline_max_content_size != 0) {
  462. cell_max_contribution += (rows_or_columns[rc_index].max_size / static_cast<double>(baseline_max_content_size))
  463. * max(CSSPixels(0), cell_max_size<RowOrColumn>(cell) - baseline_max_content_size - baseline_border_spacing);
  464. }
  465. cell_min_contributions_by_rc_index[rc_index].append(cell_min_contribution);
  466. cell_max_contributions_by_rc_index[rc_index].append(cell_max_contribution);
  467. }
  468. }
  469. }
  470. for (size_t rc_index = 0; rc_index < rows_or_columns.size(); rc_index++) {
  471. // min-content size of a row / column based on cells of span up to N (N > 1) is
  472. // the largest of the min-content size of the row / column based on cells of span up to N-1 and
  473. // the contributions of the cells in the row / column whose rowSpan / colSpan is N
  474. for (auto min_contribution : cell_min_contributions_by_rc_index[rc_index])
  475. rows_or_columns[rc_index].min_size = max(rows_or_columns[rc_index].min_size, min_contribution);
  476. // max-content size of a row / column based on cells of span up to N (N > 1) is
  477. // the largest of the max-content size based on cells of span up to N-1 and the contributions of
  478. // the cells in the row / column whose rowSpan / colSpan is N
  479. for (auto max_contribution : cell_max_contributions_by_rc_index[rc_index])
  480. rows_or_columns[rc_index].max_size = max(rows_or_columns[rc_index].max_size, max_contribution);
  481. }
  482. }
  483. }
  484. CSSPixels TableFormattingContext::compute_capmin()
  485. {
  486. // The caption width minimum (CAPMIN) is the largest of the table captions min-content contribution:
  487. // https://drafts.csswg.org/css-tables-3/#computing-the-table-width
  488. CSSPixels capmin = 0;
  489. for (auto* child = table_box().first_child(); child; child = child->next_sibling()) {
  490. if (!child->display().is_table_caption()) {
  491. continue;
  492. }
  493. VERIFY(child->is_box());
  494. capmin = max(calculate_min_content_width(static_cast<Box const&>(*child)), capmin);
  495. }
  496. return capmin;
  497. }
  498. void TableFormattingContext::compute_table_width()
  499. {
  500. // https://drafts.csswg.org/css-tables-3/#computing-the-table-width
  501. auto& table_box_state = m_state.get_mutable(table_box());
  502. auto& computed_values = table_box().computed_values();
  503. CSSPixels width_of_table_containing_block = m_state.get(*table_box().containing_block()).content_width();
  504. // Percentages on 'width' and 'height' on the table are relative to the table wrapper box's containing block,
  505. // not the table wrapper box itself.
  506. CSSPixels width_of_table_wrapper_containing_block = m_state.get(*table_wrapper().containing_block()).content_width();
  507. // Compute undistributable space due to border spacing: https://www.w3.org/TR/css-tables-3/#computing-undistributable-space.
  508. auto undistributable_space = (m_columns.size() + 1) * border_spacing_horizontal();
  509. // The row/column-grid width minimum (GRIDMIN) width is the sum of the min-content width
  510. // of all the columns plus cell spacing or borders.
  511. CSSPixels grid_min = 0.0f;
  512. for (auto& column : m_columns) {
  513. grid_min += column.min_size;
  514. }
  515. grid_min += undistributable_space;
  516. // The row/column-grid width maximum (GRIDMAX) width is the sum of the max-content width
  517. // of all the columns plus cell spacing or borders.
  518. CSSPixels grid_max = 0.0f;
  519. for (auto& column : m_columns) {
  520. grid_max += column.max_size;
  521. }
  522. grid_max += undistributable_space;
  523. // The used min-width of a table is the greater of the resolved min-width, CAPMIN, and GRIDMIN.
  524. auto used_min_width = max(grid_min, compute_capmin());
  525. if (!computed_values.min_width().is_auto()) {
  526. used_min_width = max(used_min_width, computed_values.min_width().to_px(table_box(), width_of_table_wrapper_containing_block));
  527. }
  528. CSSPixels used_width;
  529. if (computed_values.width().is_auto()) {
  530. // If the table-root has 'width: auto', the used width is the greater of
  531. // min(GRIDMAX, the table’s containing block width), the used min-width of the table.
  532. used_width = max(min(grid_max, width_of_table_containing_block), used_min_width);
  533. // https://www.w3.org/TR/CSS22/tables.html#auto-table-layout
  534. // A percentage value for a column width is relative to the table width. If the table has 'width: auto',
  535. // a percentage represents a constraint on the column's width, which a UA should try to satisfy.
  536. CSSPixels adjusted_used_width = 0;
  537. for (auto& cell : m_cells) {
  538. auto const& cell_width = cell.box->computed_values().width();
  539. if (cell_width.is_percentage()) {
  540. adjusted_used_width = ceil(100 / cell_width.percentage().value() * cell.outer_max_width.to_double());
  541. used_width = min(max(used_width, adjusted_used_width), width_of_table_containing_block);
  542. }
  543. }
  544. } else {
  545. // If the table-root’s width property has a computed value (resolving to
  546. // resolved-table-width) other than auto, the used width is the greater
  547. // of resolved-table-width, and the used min-width of the table.
  548. CSSPixels resolved_table_width = computed_values.width().to_px(table_box(), width_of_table_wrapper_containing_block);
  549. // Since used_width is content width, we need to subtract the border spacing from the specified width for a consistent comparison.
  550. used_width = max(resolved_table_width - table_box_state.border_box_left() - table_box_state.border_box_right(), used_min_width);
  551. if (!should_treat_max_width_as_none(table_box(), m_available_space->width))
  552. used_width = min(used_width, computed_values.max_width().to_px(table_box(), width_of_table_wrapper_containing_block));
  553. }
  554. table_box_state.set_content_width(used_width);
  555. }
  556. CSSPixels TableFormattingContext::compute_columns_total_used_width() const
  557. {
  558. CSSPixels total_used_width = 0;
  559. for (auto& column : m_columns) {
  560. total_used_width += column.used_width;
  561. }
  562. return total_used_width;
  563. }
  564. static CSSPixels compute_columns_total_candidate_width(Vector<CSSPixels> const& candidate_widths)
  565. {
  566. CSSPixels total_candidate_width = 0;
  567. for (auto width : candidate_widths) {
  568. total_candidate_width += width;
  569. }
  570. return total_candidate_width;
  571. }
  572. void TableFormattingContext::commit_candidate_column_widths(Vector<CSSPixels> const& candidate_widths)
  573. {
  574. VERIFY(candidate_widths.size() == m_columns.size());
  575. for (size_t i = 0; i < m_columns.size(); ++i) {
  576. m_columns[i].used_width = candidate_widths[i];
  577. }
  578. }
  579. void TableFormattingContext::assign_columns_width_linear_combination(Vector<CSSPixels> const& candidate_widths, CSSPixels available_width)
  580. {
  581. auto columns_total_candidate_width = compute_columns_total_candidate_width(candidate_widths);
  582. auto columns_total_used_width = compute_columns_total_used_width();
  583. if (columns_total_candidate_width == columns_total_used_width) {
  584. return;
  585. }
  586. auto candidate_weight = (available_width - columns_total_used_width) / static_cast<double>(columns_total_candidate_width - columns_total_used_width);
  587. for (size_t i = 0; i < m_columns.size(); ++i) {
  588. auto& column = m_columns[i];
  589. column.used_width = candidate_weight * candidate_widths[i] + (1 - candidate_weight) * column.used_width;
  590. }
  591. }
  592. template<class ColumnFilter, class BaseWidthGetter>
  593. bool TableFormattingContext::distribute_excess_width_proportionally_to_base_width(CSSPixels excess_width, ColumnFilter column_filter, BaseWidthGetter base_width_getter)
  594. {
  595. bool found_matching_columns = false;
  596. CSSPixels total_base_width = 0;
  597. for (auto const& column : m_columns) {
  598. if (column_filter(column)) {
  599. total_base_width += base_width_getter(column);
  600. found_matching_columns = true;
  601. }
  602. }
  603. if (!found_matching_columns) {
  604. return false;
  605. }
  606. VERIFY(total_base_width > 0);
  607. for (auto& column : m_columns) {
  608. if (column_filter(column)) {
  609. column.used_width += excess_width * base_width_getter(column) / static_cast<double>(total_base_width);
  610. }
  611. }
  612. return true;
  613. }
  614. template<class ColumnFilter>
  615. bool TableFormattingContext::distribute_excess_width_equally(CSSPixels excess_width, ColumnFilter column_filter)
  616. {
  617. size_t matching_column_count = 0;
  618. for (auto const& column : m_columns) {
  619. if (column_filter(column)) {
  620. ++matching_column_count;
  621. }
  622. }
  623. if (matching_column_count == 0) {
  624. return false;
  625. }
  626. for (auto& column : m_columns) {
  627. if (column_filter(column)) {
  628. column.used_width += excess_width / matching_column_count;
  629. }
  630. }
  631. return matching_column_count;
  632. }
  633. template<class ColumnFilter>
  634. bool TableFormattingContext::distribute_excess_width_by_intrinsic_percentage(CSSPixels excess_width, ColumnFilter column_filter)
  635. {
  636. bool found_matching_columns = false;
  637. double total_percentage_width = 0;
  638. for (auto const& column : m_columns) {
  639. if (column_filter(column)) {
  640. found_matching_columns = true;
  641. total_percentage_width += column.intrinsic_percentage;
  642. }
  643. }
  644. if (!found_matching_columns) {
  645. return false;
  646. }
  647. for (auto& column : m_columns) {
  648. if (column_filter(column)) {
  649. column.used_width += excess_width * column.intrinsic_percentage / total_percentage_width;
  650. }
  651. }
  652. return true;
  653. }
  654. void TableFormattingContext::distribute_width_to_columns()
  655. {
  656. // Implements https://www.w3.org/TR/css-tables-3/#width-distribution-algorithm
  657. // The total horizontal border spacing is defined for each table:
  658. // - For tables laid out in separated-borders mode containing at least one column, the horizontal component of the computed value of the border-spacing property times one plus the number of columns in the table
  659. // - Otherwise, 0
  660. auto total_horizontal_border_spacing = m_columns.is_empty() ? 0 : (m_columns.size() + 1) * border_spacing_horizontal();
  661. // The assignable table width is the used width of the table minus the total horizontal border spacing (if any).
  662. // This is the width that we will be able to allocate to the columns.
  663. const CSSPixels available_width = m_state.get(table_box()).content_width() - total_horizontal_border_spacing;
  664. Vector<CSSPixels> candidate_widths;
  665. candidate_widths.resize(m_columns.size());
  666. // 1. The min-content sizing-guess is the set of column width assignments where each column is assigned its min-content width.
  667. for (size_t i = 0; i < m_columns.size(); ++i) {
  668. auto& column = m_columns[i];
  669. // In fixed mode, the min-content width of percent-columns and auto-columns is considered to be zero:
  670. // https://www.w3.org/TR/css-tables-3/#width-distribution-in-fixed-mode
  671. if (use_fixed_mode_layout() && !column.is_constrained) {
  672. continue;
  673. }
  674. column.used_width = column.min_size;
  675. candidate_widths[i] = column.min_size;
  676. }
  677. // 2. The min-content-percentage sizing-guess is the set of column width assignments where:
  678. // - each percent-column is assigned the larger of:
  679. // - its intrinsic percentage width times the assignable width and
  680. // - its min-content width.
  681. // - all other columns are assigned their min-content width.
  682. for (size_t i = 0; i < m_columns.size(); ++i) {
  683. auto& column = m_columns[i];
  684. if (column.has_intrinsic_percentage) {
  685. candidate_widths[i] = max(column.min_size, column.intrinsic_percentage / 100 * available_width);
  686. }
  687. }
  688. // If the assignable table width is less than or equal to the max-content sizing-guess, the used widths of the columns must be the
  689. // linear combination (with weights adding to 1) of the two consecutive sizing-guesses whose width sums bound the available width.
  690. if (available_width < compute_columns_total_candidate_width(candidate_widths)) {
  691. assign_columns_width_linear_combination(candidate_widths, available_width);
  692. return;
  693. } else {
  694. commit_candidate_column_widths(candidate_widths);
  695. }
  696. // 3. The min-content-specified sizing-guess is the set of column width assignments where:
  697. // - each percent-column is assigned the larger of:
  698. // - its intrinsic percentage width times the assignable width and
  699. // - its min-content width
  700. // - any other column that is constrained is assigned its max-content width
  701. // - all other columns are assigned their min-content width.
  702. for (size_t i = 0; i < m_columns.size(); ++i) {
  703. auto& column = m_columns[i];
  704. if (column.is_constrained) {
  705. candidate_widths[i] = column.max_size;
  706. }
  707. }
  708. if (available_width < compute_columns_total_candidate_width(candidate_widths)) {
  709. assign_columns_width_linear_combination(candidate_widths, available_width);
  710. return;
  711. } else {
  712. commit_candidate_column_widths(candidate_widths);
  713. }
  714. // 4. The max-content sizing-guess is the set of column width assignments where:
  715. // - each percent-column is assigned the larger of:
  716. // - its intrinsic percentage width times the assignable width and
  717. // - its min-content width
  718. // - all other columns are assigned their max-content width.
  719. for (size_t i = 0; i < m_columns.size(); ++i) {
  720. auto& column = m_columns[i];
  721. if (!column.has_intrinsic_percentage) {
  722. candidate_widths[i] = column.max_size;
  723. }
  724. }
  725. if (available_width < compute_columns_total_candidate_width(candidate_widths)) {
  726. assign_columns_width_linear_combination(candidate_widths, available_width);
  727. return;
  728. } else {
  729. commit_candidate_column_widths(candidate_widths);
  730. }
  731. // Otherwise, the used widths of the columns are the result of starting from the max-content sizing-guess and distributing
  732. // the excess width to the columns of the table according to the rules for distributing excess width to columns (for used width).
  733. distribute_excess_width_to_columns(available_width);
  734. }
  735. void TableFormattingContext::distribute_excess_width_to_columns(CSSPixels available_width)
  736. {
  737. // Implements https://www.w3.org/TR/css-tables-3/#distributing-width-to-columns
  738. auto columns_total_used_width = compute_columns_total_used_width();
  739. if (columns_total_used_width >= available_width) {
  740. return;
  741. }
  742. auto excess_width = available_width - columns_total_used_width;
  743. if (excess_width == 0) {
  744. return;
  745. }
  746. if (use_fixed_mode_layout()) {
  747. distribute_excess_width_to_columns_fixed_mode(excess_width);
  748. return;
  749. }
  750. // 1. If there are non-constrained columns that have originating cells with intrinsic percentage width of 0% and with nonzero
  751. // max-content width (aka the columns allowed to grow by this rule), the distributed widths of the columns allowed to grow
  752. // by this rule are increased in proportion to max-content width so the total increase adds to the excess width.
  753. if (distribute_excess_width_proportionally_to_base_width(
  754. excess_width,
  755. [](auto const& column) {
  756. return !column.is_constrained && column.has_originating_cells && column.intrinsic_percentage == 0 && column.max_size > 0;
  757. },
  758. [](auto const& column) { return column.max_size; })) {
  759. excess_width = available_width - compute_columns_total_used_width();
  760. }
  761. if (excess_width == 0) {
  762. return;
  763. }
  764. // 2. Otherwise, if there are non-constrained columns that have originating cells with intrinsic percentage width of 0% (aka the columns
  765. // allowed to grow by this rule, which thanks to the previous rule must have zero max-content width), the distributed widths of the
  766. // columns allowed to grow by this rule are increased by equal amounts so the total increase adds to the excess width.
  767. if (distribute_excess_width_equally(excess_width,
  768. [](auto const& column) {
  769. return !column.is_constrained && column.has_originating_cells && column.intrinsic_percentage == 0;
  770. })) {
  771. excess_width = available_width - compute_columns_total_used_width();
  772. }
  773. if (excess_width == 0) {
  774. return;
  775. }
  776. // 3. Otherwise, if there are constrained columns with intrinsic percentage width of 0% and with nonzero max-content width
  777. // (aka the columns allowed to grow by this rule, which, due to other rules, must have originating cells), the distributed widths of the
  778. // columns allowed to grow by this rule are increased in proportion to max-content width so the total increase adds to the excess width.
  779. if (distribute_excess_width_proportionally_to_base_width(
  780. excess_width,
  781. [](auto const& column) {
  782. return column.is_constrained && column.intrinsic_percentage == 0 && column.max_size > 0;
  783. },
  784. [](auto const& column) { return column.max_size; })) {
  785. excess_width = available_width - compute_columns_total_used_width();
  786. }
  787. if (excess_width == 0) {
  788. return;
  789. }
  790. // 4. Otherwise, if there are columns with intrinsic percentage width greater than 0% (aka the columns allowed to grow by this rule,
  791. // which, due to other rules, must have originating cells), the distributed widths of the columns allowed to grow by this rule are
  792. // increased in proportion to intrinsic percentage width so the total increase adds to the excess width.
  793. if (distribute_excess_width_by_intrinsic_percentage(excess_width, [](auto const& column) {
  794. return column.intrinsic_percentage > 0;
  795. })) {
  796. excess_width = available_width - compute_columns_total_used_width();
  797. }
  798. if (excess_width == 0) {
  799. return;
  800. }
  801. // 5. Otherwise, if there is any such column, the distributed widths of all columns that have originating cells are increased by equal amounts
  802. // so the total increase adds to the excess width.
  803. if (distribute_excess_width_equally(
  804. excess_width,
  805. [](auto const& column) { return column.has_originating_cells; })) {
  806. excess_width = available_width - compute_columns_total_used_width();
  807. }
  808. if (excess_width == 0) {
  809. return;
  810. }
  811. // 6. Otherwise, the distributed widths of all columns are increased by equal amounts so the total increase adds to the excess width.
  812. distribute_excess_width_equally(excess_width, [](auto const&) { return true; });
  813. }
  814. void TableFormattingContext::distribute_excess_width_to_columns_fixed_mode(CSSPixels excess_width)
  815. {
  816. // Implements the fixed mode for https://www.w3.org/TR/css-tables-3/#distributing-width-to-columns.
  817. // If there are any columns with no width specified, the excess width is distributed in equally to such columns
  818. if (distribute_excess_width_equally(excess_width, [](auto const& column) { return !column.is_constrained && !column.has_intrinsic_percentage; })) {
  819. return;
  820. }
  821. // otherwise, if there are columns with non-zero length widths from the base assignment, the excess width is distributed proportionally to width among those columns
  822. if (distribute_excess_width_proportionally_to_base_width(
  823. excess_width, [](auto const& column) { return column.used_width > 0; }, [](auto const& column) { return column.used_width; })) {
  824. return;
  825. }
  826. // otherwise, if there are columns with non-zero percentage widths from the base assignment, the excess width is distributed proportionally to percentage width among those columns
  827. if (distribute_excess_width_by_intrinsic_percentage(excess_width, [](auto const& column) { return column.intrinsic_percentage > 0; })) {
  828. return;
  829. }
  830. // otherwise, the excess width is distributed equally to the zero-sized columns
  831. distribute_excess_width_equally(excess_width, [](auto const& column) { return column.used_width == 0; });
  832. }
  833. void TableFormattingContext::compute_table_height(LayoutMode layout_mode)
  834. {
  835. // First pass of row height calculation:
  836. for (auto& row : m_rows) {
  837. auto row_computed_height = row.box->computed_values().height();
  838. if (row_computed_height.is_length()) {
  839. auto height_of_containing_block = m_state.get(*row.box->containing_block()).content_height();
  840. auto row_used_height = row_computed_height.to_px(row.box, height_of_containing_block);
  841. row.base_height = max(row.base_height, row_used_height);
  842. }
  843. }
  844. // First pass of cells layout:
  845. for (auto& cell : m_cells) {
  846. auto& row = m_rows[cell.row_index];
  847. auto& cell_state = m_state.get_mutable(cell.box);
  848. CSSPixels span_width = 0;
  849. for (size_t i = 0; i < cell.column_span; ++i)
  850. span_width += m_columns[cell.column_index + i].used_width;
  851. auto width_of_containing_block = m_state.get(*cell.box->containing_block()).content_width();
  852. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  853. auto height_of_containing_block = m_state.get(*cell.box->containing_block()).content_height();
  854. auto height_of_containing_block_as_length = CSS::Length::make_px(height_of_containing_block);
  855. cell_state.padding_top = cell.box->computed_values().padding().top().to_px(cell.box, width_of_containing_block);
  856. cell_state.padding_bottom = cell.box->computed_values().padding().bottom().to_px(cell.box, width_of_containing_block);
  857. cell_state.padding_left = cell.box->computed_values().padding().left().to_px(cell.box, width_of_containing_block);
  858. cell_state.padding_right = cell.box->computed_values().padding().right().to_px(cell.box, width_of_containing_block);
  859. if (cell.box->computed_values().border_collapse() == CSS::BorderCollapse::Separate) {
  860. cell_state.border_top = cell.box->computed_values().border_top().width;
  861. cell_state.border_bottom = cell.box->computed_values().border_bottom().width;
  862. cell_state.border_left = cell.box->computed_values().border_left().width;
  863. cell_state.border_right = cell.box->computed_values().border_right().width;
  864. }
  865. auto cell_computed_height = cell.box->computed_values().height();
  866. if (cell_computed_height.is_length()) {
  867. auto cell_used_height = cell_computed_height.to_px(cell.box, height_of_containing_block);
  868. cell_state.set_content_height(cell_used_height - cell_state.border_box_top() - cell_state.border_box_bottom());
  869. row.base_height = max(row.base_height, cell_used_height);
  870. }
  871. // Compute cell width as specified by https://www.w3.org/TR/css-tables-3/#bounding-box-assignment:
  872. // The position of any table-cell, table-track, or table-track-group box within the table is defined as the rectangle whose width/height is the sum of:
  873. // - the widths/heights of all spanned visible columns/rows
  874. // - the horizontal/vertical border-spacing times the amount of spanned visible columns/rows minus one
  875. // FIXME: Account for visibility.
  876. cell_state.set_content_width(span_width - cell_state.border_box_left() - cell_state.border_box_right() + (cell.column_span - 1) * border_spacing_horizontal());
  877. if (auto independent_formatting_context = layout_inside(cell.box, layout_mode, cell_state.available_inner_space_or_constraints_from(*m_available_space))) {
  878. cell_state.set_content_height(independent_formatting_context->automatic_content_height());
  879. independent_formatting_context->parent_context_did_dimension_child_root_box();
  880. }
  881. cell.baseline = box_baseline(cell.box);
  882. // Implements https://www.w3.org/TR/css-tables-3/#computing-the-table-height
  883. // The minimum height of a row is the maximum of:
  884. // - the computed height (if definite, percentages being considered 0px) of its corresponding table-row (if nay)
  885. // - the computed height of each cell spanning the current row exclusively (if definite, percentages being treated as 0px), and
  886. // - the minimum height (ROWMIN) required by the cells spanning the row.
  887. // Note that we've already applied the first rule at the top of the method.
  888. if (cell.row_span == 1) {
  889. row.base_height = max(row.base_height, cell_state.border_box_height());
  890. }
  891. row.base_height = max(row.base_height, m_rows[cell.row_index].min_size);
  892. row.baseline = max(row.baseline, cell.baseline);
  893. }
  894. CSSPixels sum_rows_height = 0;
  895. for (auto& row : m_rows) {
  896. sum_rows_height += row.base_height;
  897. }
  898. m_table_height = sum_rows_height;
  899. if (!table_box().computed_values().height().is_auto()) {
  900. // If the table has a height property with a value other than auto, it is treated as a minimum height for the
  901. // table grid, and will eventually be distributed to the height of the rows if their collective minimum height
  902. // ends up smaller than this number.
  903. CSSPixels height_of_table_containing_block = m_state.get(*table_wrapper().containing_block()).content_height();
  904. auto specified_table_height = table_box().computed_values().height().to_px(table_box(), height_of_table_containing_block);
  905. auto const& table_state = m_state.get(table_box());
  906. m_table_height = max(m_table_height, specified_table_height - table_state.border_box_top() - table_state.border_box_bottom());
  907. }
  908. for (auto& row : m_rows) {
  909. // Reference size is the largest of
  910. // - its initial base height and
  911. // - its new base height (the one evaluated during the second layout pass, where percentages used in
  912. // rowgroups/rows/cells' specified heights were resolved according to the table height, instead of
  913. // being ignored as 0px).
  914. // Assign reference size to base size. Later, the reference size might change to a larger value during
  915. // the second pass of rows layout.
  916. row.reference_height = row.base_height;
  917. }
  918. // Second pass of rows height calculation:
  919. // At this point, percentage row height can be resolved because the final table height is calculated.
  920. for (auto& row : m_rows) {
  921. auto row_computed_height = row.box->computed_values().height();
  922. if (row_computed_height.is_percentage()) {
  923. auto row_used_height = row_computed_height.to_px(row.box, m_table_height);
  924. row.reference_height = max(row.reference_height, row_used_height);
  925. } else {
  926. continue;
  927. }
  928. }
  929. // Second pass cells layout:
  930. // At this point, percentage cell height can be resolved because the final table height is calculated.
  931. for (auto& cell : m_cells) {
  932. auto& row = m_rows[cell.row_index];
  933. auto& cell_state = m_state.get_mutable(cell.box);
  934. CSSPixels span_width = 0;
  935. for (size_t i = 0; i < cell.column_span; ++i)
  936. span_width += m_columns[cell.column_index + i].used_width;
  937. auto cell_computed_height = cell.box->computed_values().height();
  938. if (cell_computed_height.is_percentage()) {
  939. auto cell_used_height = cell_computed_height.to_px(cell.box, m_table_height);
  940. cell_state.set_content_height(cell_used_height - cell_state.border_box_top() - cell_state.border_box_bottom());
  941. row.reference_height = max(row.reference_height, cell_used_height);
  942. } else {
  943. continue;
  944. }
  945. cell_state.set_content_width(span_width - cell_state.border_box_left() - cell_state.border_box_right() + (cell.column_span - 1) * border_spacing_horizontal());
  946. if (auto independent_formatting_context = layout_inside(cell.box, layout_mode, cell_state.available_inner_space_or_constraints_from(*m_available_space))) {
  947. independent_formatting_context->parent_context_did_dimension_child_root_box();
  948. }
  949. cell.baseline = box_baseline(cell.box);
  950. row.reference_height = max(row.reference_height, cell_state.border_box_height());
  951. row.baseline = max(row.baseline, cell.baseline);
  952. }
  953. }
  954. void TableFormattingContext::distribute_height_to_rows()
  955. {
  956. CSSPixels sum_reference_height = 0;
  957. for (auto& row : m_rows) {
  958. sum_reference_height += row.reference_height;
  959. }
  960. if (sum_reference_height == 0)
  961. return;
  962. Vector<Row&> rows_with_auto_height;
  963. for (auto& row : m_rows) {
  964. if (row.box->computed_values().height().is_auto()) {
  965. rows_with_auto_height.append(row);
  966. }
  967. }
  968. if (m_table_height <= sum_reference_height) {
  969. // If the table height is equal or smaller than sum of reference sizes, the final height assigned to each row
  970. // will be the weighted mean of the base and the reference size that yields the correct total height.
  971. for (auto& row : m_rows) {
  972. auto weight = row.reference_height / static_cast<double>(sum_reference_height);
  973. auto final_height = m_table_height * weight;
  974. row.final_height = final_height;
  975. }
  976. } else if (rows_with_auto_height.size() > 0) {
  977. // Else, if the table owns any “auto-height” row (a row whose size is only determined by its content size and
  978. // none of the specified heights), each non-auto-height row receives its reference height and auto-height rows
  979. // receive their reference size plus some increment which is equal to the height missing to amount to the
  980. // specified table height divided by the amount of such rows.
  981. for (auto& row : m_rows) {
  982. row.final_height = row.reference_height;
  983. }
  984. auto auto_height_rows_increment = (m_table_height - sum_reference_height) / rows_with_auto_height.size();
  985. for (auto& row : rows_with_auto_height) {
  986. row.final_height += auto_height_rows_increment;
  987. }
  988. } else {
  989. // Else, all rows receive their reference size plus some increment which is equal to the height missing to
  990. // amount to the specified table height divided by the amount of rows.
  991. auto increment = (m_table_height - sum_reference_height) / m_rows.size();
  992. for (auto& row : m_rows) {
  993. row.final_height = row.reference_height + increment;
  994. }
  995. }
  996. // Add undistributable space due to border spacing: https://www.w3.org/TR/css-tables-3/#computing-undistributable-space.
  997. m_table_height += (m_rows.size() + 1) * border_spacing_vertical();
  998. }
  999. void TableFormattingContext::position_row_boxes()
  1000. {
  1001. auto const& table_state = m_state.get(table_box());
  1002. CSSPixels row_top_offset = table_state.offset.y() + table_state.padding_top + border_spacing_vertical();
  1003. CSSPixels row_left_offset = table_state.border_left + table_state.padding_left + border_spacing_horizontal();
  1004. for (size_t y = 0; y < m_rows.size(); y++) {
  1005. auto& row = m_rows[y];
  1006. auto& row_state = m_state.get_mutable(row.box);
  1007. CSSPixels row_width = 0.0f;
  1008. for (auto& column : m_columns) {
  1009. row_width += column.used_width;
  1010. }
  1011. row_state.set_content_height(row.final_height);
  1012. row_state.set_content_width(row_width);
  1013. row_state.set_content_x(row_left_offset);
  1014. row_state.set_content_y(row_top_offset);
  1015. row_top_offset += row_state.content_height() + border_spacing_vertical();
  1016. }
  1017. CSSPixels row_group_top_offset = table_state.border_top + table_state.padding_top;
  1018. CSSPixels row_group_left_offset = table_state.border_left + table_state.padding_left;
  1019. for_each_child_box_matching(table_box(), is_table_row_group, [&](auto& row_group_box) {
  1020. CSSPixels row_group_height = 0.0f;
  1021. CSSPixels row_group_width = 0.0f;
  1022. auto& row_group_box_state = m_state.get_mutable(row_group_box);
  1023. row_group_box_state.set_content_x(row_group_left_offset);
  1024. row_group_box_state.set_content_y(row_group_top_offset);
  1025. for_each_child_box_matching(row_group_box, is_table_row, [&](auto& row) {
  1026. auto const& row_state = m_state.get(row);
  1027. row_group_height += row_state.border_box_height();
  1028. row_group_width = max(row_group_width, row_state.border_box_width());
  1029. });
  1030. row_group_box_state.set_content_height(row_group_height);
  1031. row_group_box_state.set_content_width(row_group_width);
  1032. row_group_top_offset += row_group_height;
  1033. });
  1034. auto total_content_height = max(row_top_offset, row_group_top_offset) - table_state.offset.y() - table_state.padding_top;
  1035. m_table_height = max(total_content_height, m_table_height);
  1036. }
  1037. void TableFormattingContext::position_cell_boxes()
  1038. {
  1039. CSSPixels left_column_offset = 0;
  1040. for (auto& column : m_columns) {
  1041. column.left_offset = left_column_offset;
  1042. left_column_offset += column.used_width;
  1043. }
  1044. for (auto& cell : m_cells) {
  1045. auto& cell_state = m_state.get_mutable(cell.box);
  1046. auto& row_state = m_state.get(m_rows[cell.row_index].box);
  1047. CSSPixels const cell_border_box_height = cell_state.content_height() + cell_state.border_box_top() + cell_state.border_box_bottom();
  1048. CSSPixels const row_content_height = compute_row_content_height(cell);
  1049. auto const& vertical_align = cell.box->computed_values().vertical_align();
  1050. // The following image shows various alignment lines of a row:
  1051. // https://www.w3.org/TR/css-tables-3/images/cell-align-explainer.png
  1052. if (vertical_align.has<CSS::VerticalAlign>()) {
  1053. auto height_diff = row_content_height - cell_border_box_height;
  1054. switch (vertical_align.get<CSS::VerticalAlign>()) {
  1055. case CSS::VerticalAlign::Middle: {
  1056. cell_state.padding_top += height_diff / 2;
  1057. cell_state.padding_bottom += height_diff / 2;
  1058. break;
  1059. }
  1060. case CSS::VerticalAlign::Top: {
  1061. cell_state.padding_bottom += height_diff;
  1062. break;
  1063. }
  1064. case CSS::VerticalAlign::Bottom: {
  1065. cell_state.padding_top += height_diff;
  1066. break;
  1067. }
  1068. case CSS::VerticalAlign::Baseline: {
  1069. cell_state.padding_top += m_rows[cell.row_index].baseline - cell.baseline;
  1070. cell_state.padding_bottom += height_diff;
  1071. break;
  1072. }
  1073. default:
  1074. VERIFY_NOT_REACHED();
  1075. }
  1076. }
  1077. // Compute cell position as specified by https://www.w3.org/TR/css-tables-3/#bounding-box-assignment:
  1078. // left/top location is the sum of:
  1079. // - for top: the height reserved for top captions (including margins), if any
  1080. // - the padding-left/padding-top and border-left-width/border-top-width of the table
  1081. // FIXME: Account for visibility.
  1082. cell_state.offset = row_state.offset.translated(
  1083. cell_state.border_box_left() + m_columns[cell.column_index].left_offset + cell.column_index * border_spacing_horizontal(),
  1084. cell_state.border_box_top());
  1085. }
  1086. }
  1087. bool TableFormattingContext::use_fixed_mode_layout() const
  1088. {
  1089. // Implements https://www.w3.org/TR/css-tables-3/#in-fixed-mode.
  1090. // A table-root is said to be laid out in fixed mode whenever the computed value of the table-layout property is equal to fixed, and the
  1091. // specified width of the table root is either a <length-percentage>, min-content or fit-content. When the specified width is not one of
  1092. // those values, or if the computed value of the table-layout property is auto, then the table-root is said to be laid out in auto mode.
  1093. auto const& width = table_box().computed_values().width();
  1094. return table_box().computed_values().table_layout() == CSS::TableLayout::Fixed && (width.is_length() || width.is_percentage() || width.is_min_content() || width.is_fit_content());
  1095. }
  1096. bool TableFormattingContext::border_is_less_specific(const CSS::BorderData& a, const CSS::BorderData& b)
  1097. {
  1098. // Implements criteria for steps 1, 2 and 3 of border conflict resolution algorithm, as described in
  1099. // https://www.w3.org/TR/CSS22/tables.html#border-conflict-resolution.
  1100. static HashMap<CSS::LineStyle, unsigned> const line_style_score = {
  1101. { CSS::LineStyle::Inset, 0 },
  1102. { CSS::LineStyle::Groove, 1 },
  1103. { CSS::LineStyle::Outset, 2 },
  1104. { CSS::LineStyle::Ridge, 3 },
  1105. { CSS::LineStyle::Dotted, 4 },
  1106. { CSS::LineStyle::Dashed, 5 },
  1107. { CSS::LineStyle::Solid, 6 },
  1108. { CSS::LineStyle::Double, 7 },
  1109. };
  1110. // 1. Borders with the 'border-style' of 'hidden' take precedence over all other conflicting borders. Any border with this
  1111. // value suppresses all borders at this location.
  1112. if (a.line_style == CSS::LineStyle::Hidden) {
  1113. return false;
  1114. }
  1115. if (b.line_style == CSS::LineStyle::Hidden) {
  1116. return true;
  1117. }
  1118. // 2. Borders with a style of 'none' have the lowest priority. Only if the border properties of all the elements meeting
  1119. // at this edge are 'none' will the border be omitted (but note that 'none' is the default value for the border style.)
  1120. if (a.line_style == CSS::LineStyle::None) {
  1121. return true;
  1122. }
  1123. if (b.line_style == CSS::LineStyle::None) {
  1124. return false;
  1125. }
  1126. // 3. If none of the styles are 'hidden' and at least one of them is not 'none', then narrow borders are discarded in favor
  1127. // of wider ones. If several have the same 'border-width' then styles are preferred in this order: 'double', 'solid',
  1128. // 'dashed', 'dotted', 'ridge', 'outset', 'groove', and the lowest: 'inset'.
  1129. if (a.width > b.width) {
  1130. return false;
  1131. } else if (a.width < b.width) {
  1132. return true;
  1133. }
  1134. if (*line_style_score.get(a.line_style) > *line_style_score.get(b.line_style)) {
  1135. return false;
  1136. } else if (*line_style_score.get(a.line_style) < *line_style_score.get(b.line_style)) {
  1137. return true;
  1138. }
  1139. return false;
  1140. }
  1141. const CSS::BorderData& TableFormattingContext::border_data_conflicting_edge(TableFormattingContext::ConflictingEdge const& conflicting_edge)
  1142. {
  1143. auto const& style = conflicting_edge.element->computed_values();
  1144. switch (conflicting_edge.side) {
  1145. case ConflictingSide::Top: {
  1146. return style.border_top();
  1147. }
  1148. case ConflictingSide::Bottom: {
  1149. return style.border_bottom();
  1150. }
  1151. case ConflictingSide::Left: {
  1152. return style.border_left();
  1153. }
  1154. case ConflictingSide::Right: {
  1155. return style.border_right();
  1156. }
  1157. default: {
  1158. VERIFY_NOT_REACHED();
  1159. }
  1160. }
  1161. }
  1162. const Painting::PaintableBox::BorderDataWithElementKind TableFormattingContext::border_data_with_element_kind_from_conflicting_edge(ConflictingEdge const& conflicting_edge)
  1163. {
  1164. auto const& border_data = border_data_conflicting_edge(conflicting_edge);
  1165. return { .border_data = border_data, .element_kind = conflicting_edge.element_kind };
  1166. }
  1167. TableFormattingContext::ConflictingEdge const& TableFormattingContext::winning_conflicting_edge(TableFormattingContext::ConflictingEdge const& a, TableFormattingContext::ConflictingEdge const& b)
  1168. {
  1169. auto a_border_data = border_data_conflicting_edge(a);
  1170. auto b_border_data = border_data_conflicting_edge(b);
  1171. // First check if step 4 of border conflict resolution applies, as described in https://www.w3.org/TR/CSS22/tables.html#border-conflict-resolution.
  1172. if (a_border_data.line_style == b_border_data.line_style && a_border_data.width == b_border_data.width) {
  1173. // 4. If border styles differ only in color, then a style set on a cell wins over one on a row, which wins over a
  1174. // row group, column, column group and, lastly, table. When two elements of the same type conflict, then the one
  1175. // further to the left (if the table's 'direction' is 'ltr'; right, if it is 'rtl') and further to the top wins.
  1176. if (static_cast<unsigned>(a.element_kind) < static_cast<unsigned>(b.element_kind)) {
  1177. return a;
  1178. } else if (static_cast<unsigned>(a.element_kind) > static_cast<unsigned>(b.element_kind)) {
  1179. return b;
  1180. }
  1181. // Here the element kind is the same, thus the coordinates are either both set or not set.
  1182. VERIFY(a.column.has_value() == b.column.has_value());
  1183. VERIFY(a.row.has_value() == b.row.has_value());
  1184. if (a.column.has_value()) {
  1185. if (a.column.value() < b.column.value()) {
  1186. return a;
  1187. } else if (a.column.value() > b.column.value()) {
  1188. return b;
  1189. }
  1190. }
  1191. if (a.row.has_value()) {
  1192. if (a.row.value() < b.row.value()) {
  1193. return a;
  1194. } else if (a.row.value() > b.row.value()) {
  1195. return b;
  1196. }
  1197. }
  1198. return a;
  1199. }
  1200. // Apply steps 1, 2 and 3 of the border conflict resolution algorithm.
  1201. return border_is_less_specific(a_border_data, b_border_data) ? b : a;
  1202. }
  1203. void TableFormattingContext::border_conflict_resolution()
  1204. {
  1205. // Implements border conflict resolution, as described in https://www.w3.org/TR/CSS22/tables.html#border-conflict-resolution.
  1206. BorderConflictFinder finder(this);
  1207. for (auto& cell : m_cells) {
  1208. auto& cell_state = m_state.get_mutable(cell.box);
  1209. cell_state.set_table_cell_coordinates(
  1210. Painting::PaintableBox::TableCellCoordinates {
  1211. .row_index = cell.row_index,
  1212. .column_index = cell.column_index,
  1213. .row_span = cell.row_span,
  1214. .column_span = cell.column_span });
  1215. if (cell.box->computed_values().border_collapse() == CSS::BorderCollapse::Separate) {
  1216. continue;
  1217. }
  1218. Painting::PaintableBox::BordersDataWithElementKind override_borders_data;
  1219. ConflictingEdge winning_edge_left {
  1220. .element = cell.box,
  1221. .element_kind = Painting::PaintableBox::ConflictingElementKind::Cell,
  1222. .side = ConflictingSide::Left,
  1223. .row = cell.row_index,
  1224. .column = cell.column_index,
  1225. };
  1226. for (auto const& conflicting_edge : finder.conflicting_edges(cell, ConflictingSide::Left)) {
  1227. winning_edge_left = winning_conflicting_edge(winning_edge_left, conflicting_edge);
  1228. }
  1229. override_borders_data.left = border_data_with_element_kind_from_conflicting_edge(winning_edge_left);
  1230. cell_state.border_left = override_borders_data.left.border_data.width;
  1231. ConflictingEdge winning_edge_right {
  1232. .element = cell.box,
  1233. .element_kind = Painting::PaintableBox::ConflictingElementKind::Cell,
  1234. .side = ConflictingSide::Right,
  1235. .row = cell.row_index,
  1236. .column = cell.column_index,
  1237. };
  1238. for (auto const& conflicting_edge : finder.conflicting_edges(cell, ConflictingSide::Right)) {
  1239. winning_edge_right = winning_conflicting_edge(winning_edge_right, conflicting_edge);
  1240. }
  1241. override_borders_data.right = border_data_with_element_kind_from_conflicting_edge(winning_edge_right);
  1242. cell_state.border_right = override_borders_data.right.border_data.width;
  1243. ConflictingEdge winning_edge_top {
  1244. .element = cell.box,
  1245. .element_kind = Painting::PaintableBox::ConflictingElementKind::Cell,
  1246. .side = ConflictingSide::Top,
  1247. .row = cell.row_index,
  1248. .column = cell.column_index,
  1249. };
  1250. for (auto const& conflicting_edge : finder.conflicting_edges(cell, ConflictingSide::Top)) {
  1251. winning_edge_top = winning_conflicting_edge(winning_edge_top, conflicting_edge);
  1252. }
  1253. override_borders_data.top = border_data_with_element_kind_from_conflicting_edge(winning_edge_top);
  1254. cell_state.border_top = override_borders_data.top.border_data.width;
  1255. ConflictingEdge winning_edge_bottom {
  1256. .element = cell.box,
  1257. .element_kind = Painting::PaintableBox::ConflictingElementKind::Cell,
  1258. .side = ConflictingSide::Bottom,
  1259. .row = cell.row_index,
  1260. .column = cell.column_index,
  1261. };
  1262. for (auto const& conflicting_edge : finder.conflicting_edges(cell, ConflictingSide::Bottom)) {
  1263. winning_edge_bottom = winning_conflicting_edge(winning_edge_bottom, conflicting_edge);
  1264. }
  1265. override_borders_data.bottom = border_data_with_element_kind_from_conflicting_edge(winning_edge_bottom);
  1266. cell_state.border_bottom = override_borders_data.bottom.border_data.width;
  1267. cell_state.set_override_borders_data(override_borders_data);
  1268. }
  1269. }
  1270. CSSPixels TableFormattingContext::compute_row_content_height(Cell const& cell) const
  1271. {
  1272. auto& row_state = m_state.get(m_rows[cell.row_index].box);
  1273. if (cell.row_span == 1) {
  1274. return row_state.content_height();
  1275. }
  1276. // The height of a cell is the sum of all spanned rows, as described in
  1277. // https://www.w3.org/TR/css-tables-3/#bounding-box-assignment
  1278. // When the row span is greater than 1, the borders of inner rows within the span have to be
  1279. // included in the content height of the spanning cell. First top and final bottom borders are
  1280. // excluded to be consistent with the handling of row span 1 case above, which uses the content
  1281. // height (no top and bottom borders) of the row.
  1282. CSSPixels span_height = 0;
  1283. for (size_t i = 0; i < cell.row_span; ++i) {
  1284. auto const& row_state = m_state.get(m_rows[cell.row_index + i].box);
  1285. if (i == 0) {
  1286. span_height += row_state.content_height() + row_state.border_box_bottom();
  1287. } else if (i == cell.row_span - 1) {
  1288. span_height += row_state.border_box_top() + row_state.content_height();
  1289. } else {
  1290. span_height += row_state.border_box_height();
  1291. }
  1292. }
  1293. // Compute cell height as specified by https://www.w3.org/TR/css-tables-3/#bounding-box-assignment:
  1294. // width/height is the sum of:
  1295. // - the widths/heights of all spanned visible columns/rows
  1296. // - the horizontal/vertical border-spacing times the amount of spanned visible columns/rows minus one
  1297. // FIXME: Account for visibility.
  1298. span_height += (cell.row_span - 1) * border_spacing_vertical();
  1299. return span_height;
  1300. }
  1301. TableFormattingContext::BorderConflictFinder::BorderConflictFinder(TableFormattingContext const* context)
  1302. : m_context(context)
  1303. {
  1304. collect_conflicting_col_elements();
  1305. collect_conflicting_row_group_elements();
  1306. }
  1307. void TableFormattingContext::BorderConflictFinder::collect_conflicting_col_elements()
  1308. {
  1309. m_col_elements_by_index.resize(m_context->m_columns.size());
  1310. for (auto* child = m_context->table_box().first_child(); child; child = child->next_sibling()) {
  1311. if (!child->display().is_table_column_group()) {
  1312. continue;
  1313. }
  1314. size_t column_index = 0;
  1315. for (auto* child_of_column_group = child->first_child(); child_of_column_group; child_of_column_group = child_of_column_group->next_sibling()) {
  1316. VERIFY(child_of_column_group->display().is_table_column());
  1317. auto const& col_node = static_cast<HTML::HTMLTableColElement const&>(*child_of_column_group->dom_node());
  1318. unsigned span = col_node.attribute(HTML::AttributeNames::span).to_uint().value_or(1);
  1319. for (size_t i = column_index; i < column_index + span; ++i) {
  1320. m_col_elements_by_index[i] = child_of_column_group;
  1321. }
  1322. column_index += span;
  1323. }
  1324. }
  1325. }
  1326. void TableFormattingContext::BorderConflictFinder::collect_conflicting_row_group_elements()
  1327. {
  1328. m_row_group_elements_by_index.resize(m_context->m_rows.size());
  1329. size_t current_row_index = 0;
  1330. for_each_child_box_matching(m_context->table_box(), is_table_row_group, [&](auto& row_group_box) {
  1331. auto start_row_index = current_row_index;
  1332. size_t row_count = 0;
  1333. for_each_child_box_matching(row_group_box, is_table_row, [&](auto&) {
  1334. ++row_count;
  1335. });
  1336. for_each_child_box_matching(row_group_box, is_table_row, [&](auto&) {
  1337. m_row_group_elements_by_index[current_row_index] = RowGroupInfo {
  1338. .row_group = &row_group_box, .start_index = start_row_index, .row_count = row_count
  1339. };
  1340. ++current_row_index;
  1341. return IterationDecision::Continue;
  1342. });
  1343. });
  1344. }
  1345. void TableFormattingContext::BorderConflictFinder::collect_cell_conflicting_edges(Vector<ConflictingEdge>& result, Cell const& cell, TableFormattingContext::ConflictingSide edge) const
  1346. {
  1347. // Right edge of the cell to the left.
  1348. if (cell.column_index >= cell.column_span && edge == ConflictingSide::Left) {
  1349. auto left_cell_column_index = cell.column_index - cell.column_span;
  1350. auto maybe_cell_to_left = m_context->m_cells_by_coordinate[cell.row_index][left_cell_column_index];
  1351. if (maybe_cell_to_left.has_value()) {
  1352. result.append({ maybe_cell_to_left->box, Painting::PaintableBox::ConflictingElementKind::Cell, ConflictingSide::Right, cell.row_index, left_cell_column_index });
  1353. }
  1354. }
  1355. // Left edge of the cell to the right.
  1356. if (cell.column_index + cell.column_span < m_context->m_cells_by_coordinate[cell.row_index].size() && edge == ConflictingSide::Right) {
  1357. auto right_cell_column_index = cell.column_index + cell.column_span;
  1358. auto maybe_cell_to_right = m_context->m_cells_by_coordinate[cell.row_index][right_cell_column_index];
  1359. if (maybe_cell_to_right.has_value()) {
  1360. result.append({ maybe_cell_to_right->box, Painting::PaintableBox::ConflictingElementKind::Cell, ConflictingSide::Left, cell.row_index, right_cell_column_index });
  1361. }
  1362. }
  1363. // Bottom edge of the cell above.
  1364. if (cell.row_index >= cell.row_span && edge == ConflictingSide::Top) {
  1365. auto above_cell_row_index = cell.row_index - cell.row_span;
  1366. auto maybe_cell_above = m_context->m_cells_by_coordinate[above_cell_row_index][cell.column_index];
  1367. if (maybe_cell_above.has_value()) {
  1368. result.append({ maybe_cell_above->box, Painting::PaintableBox::ConflictingElementKind::Cell, ConflictingSide::Bottom, above_cell_row_index, cell.column_index });
  1369. }
  1370. }
  1371. // Top edge of the cell below.
  1372. if (cell.row_index + cell.row_span < m_context->m_cells_by_coordinate.size() && edge == ConflictingSide::Bottom) {
  1373. auto below_cell_row_index = cell.row_index + cell.row_span;
  1374. auto maybe_cell_below = m_context->m_cells_by_coordinate[below_cell_row_index][cell.column_index];
  1375. if (maybe_cell_below.has_value()) {
  1376. result.append({ maybe_cell_below->box, Painting::PaintableBox::ConflictingElementKind::Cell, ConflictingSide::Top, below_cell_row_index, cell.column_index });
  1377. }
  1378. }
  1379. }
  1380. void TableFormattingContext::BorderConflictFinder::collect_row_conflicting_edges(Vector<ConflictingEdge>& result, Cell const& cell, TableFormattingContext::ConflictingSide edge) const
  1381. {
  1382. // Top edge of the row.
  1383. if (edge == ConflictingSide::Top) {
  1384. result.append({ m_context->m_rows[cell.row_index].box, Painting::PaintableBox::ConflictingElementKind::Row, ConflictingSide::Top, cell.row_index, {} });
  1385. }
  1386. // Bottom edge of the row.
  1387. if (edge == ConflictingSide::Bottom) {
  1388. result.append({ m_context->m_rows[cell.row_index].box, Painting::PaintableBox::ConflictingElementKind::Row, ConflictingSide::Bottom, cell.row_index, {} });
  1389. }
  1390. // Bottom edge of the row above.
  1391. if (cell.row_index >= cell.row_span && edge == ConflictingSide::Top) {
  1392. auto above_row_index = cell.row_index - cell.row_span;
  1393. result.append({ m_context->m_rows[above_row_index].box, Painting::PaintableBox::ConflictingElementKind::Row, ConflictingSide::Bottom, above_row_index, {} });
  1394. }
  1395. // Top edge of the row below.
  1396. if (cell.row_index + cell.row_span < m_context->m_rows.size() && edge == ConflictingSide::Bottom) {
  1397. auto below_row_index = cell.row_index + cell.row_span;
  1398. result.append({ m_context->m_rows[below_row_index].box, Painting::PaintableBox::ConflictingElementKind::Row, ConflictingSide::Top, below_row_index, {} });
  1399. }
  1400. }
  1401. void TableFormattingContext::BorderConflictFinder::collect_row_group_conflicting_edges(Vector<ConflictingEdge>& result, Cell const& cell, TableFormattingContext::ConflictingSide edge) const
  1402. {
  1403. auto const& maybe_row_group = m_row_group_elements_by_index[cell.row_index];
  1404. // Top edge of the row group.
  1405. if (maybe_row_group.has_value() && cell.row_index == maybe_row_group->start_index && edge == ConflictingSide::Top) {
  1406. result.append({ maybe_row_group->row_group, Painting::PaintableBox::ConflictingElementKind::RowGroup, ConflictingSide::Top, maybe_row_group->start_index, {} });
  1407. }
  1408. // Bottom edge of the row group above.
  1409. if (cell.row_index >= cell.row_span) {
  1410. auto const& maybe_row_group_above = m_row_group_elements_by_index[cell.row_index - cell.row_span];
  1411. if (maybe_row_group_above.has_value() && cell.row_index == maybe_row_group_above->start_index + maybe_row_group_above->row_count && edge == ConflictingSide::Top) {
  1412. result.append({ maybe_row_group_above->row_group, Painting::PaintableBox::ConflictingElementKind::RowGroup, ConflictingSide::Bottom, maybe_row_group_above->start_index, {} });
  1413. }
  1414. }
  1415. // Bottom edge of the row group.
  1416. if (maybe_row_group.has_value() && cell.row_index == maybe_row_group->start_index + maybe_row_group->row_count - 1 && edge == ConflictingSide::Bottom) {
  1417. result.append({ maybe_row_group->row_group, Painting::PaintableBox::ConflictingElementKind::RowGroup, ConflictingSide::Bottom, maybe_row_group->start_index, {} });
  1418. }
  1419. // Top edge of the row group below.
  1420. if (cell.row_index + cell.row_span < m_row_group_elements_by_index.size()) {
  1421. auto const& maybe_row_group_below = m_row_group_elements_by_index[cell.row_index + cell.row_span];
  1422. if (maybe_row_group_below.has_value() && cell.row_index + cell.row_span == maybe_row_group_below->start_index && edge == ConflictingSide::Bottom) {
  1423. result.append({ maybe_row_group_below->row_group, Painting::PaintableBox::ConflictingElementKind::RowGroup, ConflictingSide::Top, maybe_row_group_below->start_index, {} });
  1424. }
  1425. }
  1426. }
  1427. void TableFormattingContext::BorderConflictFinder::collect_column_group_conflicting_edges(Vector<ConflictingEdge>& result, Cell const& cell, TableFormattingContext::ConflictingSide edge) const
  1428. {
  1429. // Left edge of the column group.
  1430. if (m_col_elements_by_index[cell.column_index] && edge == ConflictingSide::Left) {
  1431. result.append({ m_col_elements_by_index[cell.column_index], Painting::PaintableBox::ConflictingElementKind::ColumnGroup, ConflictingSide::Left, {}, cell.column_index });
  1432. }
  1433. // Right edge of the column group to the left.
  1434. if (cell.column_index >= cell.column_span && m_col_elements_by_index[cell.column_index - cell.column_span] && edge == ConflictingSide::Left) {
  1435. auto left_column_index = cell.column_index - cell.column_span;
  1436. result.append({ m_col_elements_by_index[left_column_index], Painting::PaintableBox::ConflictingElementKind::ColumnGroup, ConflictingSide::Right, {}, left_column_index });
  1437. }
  1438. // Right edge of the column group.
  1439. if (m_col_elements_by_index[cell.column_index] && edge == ConflictingSide::Right) {
  1440. result.append({ m_col_elements_by_index[cell.column_index], Painting::PaintableBox::ConflictingElementKind::ColumnGroup, ConflictingSide::Right, {}, cell.column_index });
  1441. }
  1442. // Left edge of the column group to the right.
  1443. if (cell.column_index + cell.column_span < m_col_elements_by_index.size() && m_col_elements_by_index[cell.column_index + cell.column_span] && edge == ConflictingSide::Right) {
  1444. auto right_column_index = cell.column_index + cell.column_span;
  1445. result.append({ m_col_elements_by_index[right_column_index], Painting::PaintableBox::ConflictingElementKind::ColumnGroup, ConflictingSide::Left, {}, right_column_index });
  1446. }
  1447. }
  1448. void TableFormattingContext::BorderConflictFinder::collect_table_box_conflicting_edges(Vector<ConflictingEdge>& result, Cell const& cell, TableFormattingContext::ConflictingSide edge) const
  1449. {
  1450. // Top edge from column group or table. Left and right edges of the column group are handled in collect_column_group_conflicting_edges.
  1451. if (cell.row_index == 0 && edge == ConflictingSide::Top) {
  1452. if (m_col_elements_by_index[cell.column_index]) {
  1453. result.append({ m_col_elements_by_index[cell.column_index], Painting::PaintableBox::ConflictingElementKind::ColumnGroup, ConflictingSide::Top, {}, cell.column_index });
  1454. }
  1455. result.append({ &m_context->table_box(), Painting::PaintableBox::ConflictingElementKind::Table, ConflictingSide::Top, {}, {} });
  1456. }
  1457. // Bottom edge from column group or table. Left and right edges of the column group are handled in collect_column_group_conflicting_edges.
  1458. if (cell.row_index + cell.row_span == m_context->m_rows.size() && edge == ConflictingSide::Bottom) {
  1459. if (m_col_elements_by_index[cell.column_index]) {
  1460. result.append({ m_col_elements_by_index[cell.column_index], Painting::PaintableBox::ConflictingElementKind::ColumnGroup, ConflictingSide::Bottom, {}, cell.column_index });
  1461. }
  1462. result.append({ &m_context->table_box(), Painting::PaintableBox::ConflictingElementKind::Table, ConflictingSide::Bottom, {}, {} });
  1463. }
  1464. // Left edge from row group or table. Top and bottom edges of the row group are handled in collect_row_group_conflicting_edges.
  1465. if (cell.column_index == 0 && edge == ConflictingSide::Left) {
  1466. result.append({ m_context->m_rows[cell.row_index].box, Painting::PaintableBox::ConflictingElementKind::Row, ConflictingSide::Left, cell.row_index, {} });
  1467. if (m_row_group_elements_by_index[cell.row_index].has_value()) {
  1468. result.append({ m_row_group_elements_by_index[cell.row_index]->row_group, Painting::PaintableBox::ConflictingElementKind::RowGroup, ConflictingSide::Left, cell.row_index, {} });
  1469. }
  1470. result.append({ &m_context->table_box(), Painting::PaintableBox::ConflictingElementKind::Table, ConflictingSide::Left, {}, {} });
  1471. }
  1472. // Right edge from row group or table. Top and bottom edges of the row group are handled in collect_row_group_conflicting_edges.
  1473. if (cell.column_index + cell.column_span == m_context->m_columns.size() && edge == ConflictingSide::Right) {
  1474. result.append({ m_context->m_rows[cell.row_index].box, Painting::PaintableBox::ConflictingElementKind::Row, ConflictingSide::Right, cell.row_index, {} });
  1475. if (m_row_group_elements_by_index[cell.row_index].has_value()) {
  1476. result.append({ m_row_group_elements_by_index[cell.row_index]->row_group, Painting::PaintableBox::ConflictingElementKind::RowGroup, ConflictingSide::Right, cell.row_index, {} });
  1477. }
  1478. result.append({ &m_context->table_box(), Painting::PaintableBox::ConflictingElementKind::Table, ConflictingSide::Right, {}, {} });
  1479. }
  1480. }
  1481. Vector<TableFormattingContext::ConflictingEdge> TableFormattingContext::BorderConflictFinder::conflicting_edges(
  1482. Cell const& cell, TableFormattingContext::ConflictingSide edge) const
  1483. {
  1484. Vector<ConflictingEdge> result = {};
  1485. collect_cell_conflicting_edges(result, cell, edge);
  1486. collect_row_conflicting_edges(result, cell, edge);
  1487. collect_row_group_conflicting_edges(result, cell, edge);
  1488. collect_column_group_conflicting_edges(result, cell, edge);
  1489. collect_table_box_conflicting_edges(result, cell, edge);
  1490. return result;
  1491. }
  1492. void TableFormattingContext::run(Box const& box, LayoutMode layout_mode, AvailableSpace const& available_space)
  1493. {
  1494. m_available_space = available_space;
  1495. auto total_captions_height = run_caption_layout(layout_mode, CSS::CaptionSide::Top);
  1496. // Determine the number of rows/columns the table requires.
  1497. calculate_row_column_grid(box);
  1498. border_conflict_resolution();
  1499. // Compute the minimum width of each column.
  1500. compute_cell_measures(available_space);
  1501. compute_outer_content_sizes();
  1502. compute_table_measures<Column>();
  1503. // https://www.w3.org/TR/css-tables-3/#row-layout
  1504. // Since during row layout the specified heights of cells in the row were ignored and cells that were spanning more than one rows
  1505. // have not been sized correctly, their height will need to be eventually distributed to the set of rows they spanned. This is done
  1506. // by running the same algorithm as the column measurement, with the span=1 value being initialized (for min-content) with the largest
  1507. // of the resulting height of the previous row layout, the height specified on the corresponding table-row (if any), and the largest
  1508. // height specified on cells that span this row only (the algorithm starts by considering cells of span 2 on top of that assignment).
  1509. compute_table_measures<Row>();
  1510. // Compute the width of the table.
  1511. compute_table_width();
  1512. if (available_space.width.is_intrinsic_sizing_constraint() && !available_space.height.is_intrinsic_sizing_constraint()) {
  1513. return;
  1514. }
  1515. // Distribute the width of the table among columns.
  1516. distribute_width_to_columns();
  1517. compute_table_height(layout_mode);
  1518. distribute_height_to_rows();
  1519. position_row_boxes();
  1520. position_cell_boxes();
  1521. m_state.get_mutable(table_box()).set_content_height(m_table_height);
  1522. total_captions_height += run_caption_layout(layout_mode, CSS::CaptionSide::Bottom);
  1523. // Table captions are positioned between the table margins and its borders (outside the grid box borders) as described in
  1524. // https://www.w3.org/TR/css-tables-3/#bounding-box-assignment
  1525. // A visual representation of this model can be found at https://www.w3.org/TR/css-tables-3/images/table_container.png
  1526. m_state.get_mutable(table_box()).margin_bottom += total_captions_height;
  1527. m_automatic_content_height = m_table_height;
  1528. }
  1529. CSSPixels TableFormattingContext::automatic_content_width() const
  1530. {
  1531. return greatest_child_width(context_box());
  1532. }
  1533. CSSPixels TableFormattingContext::automatic_content_height() const
  1534. {
  1535. return m_automatic_content_height;
  1536. }
  1537. template<>
  1538. size_t TableFormattingContext::cell_span<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  1539. {
  1540. return cell.row_span;
  1541. }
  1542. template<>
  1543. size_t TableFormattingContext::cell_span<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  1544. {
  1545. return cell.column_span;
  1546. }
  1547. template<>
  1548. size_t TableFormattingContext::cell_index<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  1549. {
  1550. return cell.row_index;
  1551. }
  1552. template<>
  1553. size_t TableFormattingContext::cell_index<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  1554. {
  1555. return cell.column_index;
  1556. }
  1557. template<>
  1558. CSSPixels TableFormattingContext::cell_min_size<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  1559. {
  1560. return cell.outer_min_height;
  1561. }
  1562. template<>
  1563. CSSPixels TableFormattingContext::cell_min_size<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  1564. {
  1565. return cell.outer_min_width;
  1566. }
  1567. template<>
  1568. CSSPixels TableFormattingContext::cell_max_size<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  1569. {
  1570. return cell.outer_max_height;
  1571. }
  1572. template<>
  1573. CSSPixels TableFormattingContext::cell_max_size<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  1574. {
  1575. return cell.outer_max_width;
  1576. }
  1577. template<>
  1578. double TableFormattingContext::cell_percentage_contribution<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  1579. {
  1580. // Definition of percentage contribution: https://www.w3.org/TR/css-tables-3/#percentage-contribution
  1581. auto const& computed_values = cell.box->computed_values();
  1582. auto max_height_percentage = computed_values.max_height().is_percentage() ? computed_values.max_height().percentage().value() : static_cast<double>(INFINITY);
  1583. auto height_percentage = computed_values.height().is_percentage() ? computed_values.height().percentage().value() : 0;
  1584. return min(height_percentage, max_height_percentage);
  1585. }
  1586. template<>
  1587. double TableFormattingContext::cell_percentage_contribution<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  1588. {
  1589. // Definition of percentage contribution: https://www.w3.org/TR/css-tables-3/#percentage-contribution
  1590. auto const& computed_values = cell.box->computed_values();
  1591. auto max_width_percentage = computed_values.max_width().is_percentage() ? computed_values.max_width().percentage().value() : static_cast<double>(INFINITY);
  1592. auto width_percentage = computed_values.width().is_percentage() ? computed_values.width().percentage().value() : 0;
  1593. return min(width_percentage, max_width_percentage);
  1594. }
  1595. template<>
  1596. bool TableFormattingContext::cell_has_intrinsic_percentage<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  1597. {
  1598. return cell.box->computed_values().height().is_percentage();
  1599. }
  1600. template<>
  1601. bool TableFormattingContext::cell_has_intrinsic_percentage<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  1602. {
  1603. return cell.box->computed_values().width().is_percentage();
  1604. }
  1605. template<>
  1606. void TableFormattingContext::initialize_intrinsic_percentages_from_rows_or_columns<TableFormattingContext::Row>()
  1607. {
  1608. for (auto& row : m_rows) {
  1609. auto const& computed_values = row.box->computed_values();
  1610. // Definition of percentage contribution: https://www.w3.org/TR/css-tables-3/#percentage-contribution
  1611. auto max_height_percentage = computed_values.max_height().is_percentage() ? computed_values.max_height().percentage().value() : static_cast<double>(INFINITY);
  1612. auto height_percentage = computed_values.height().is_percentage() ? computed_values.height().percentage().value() : 0;
  1613. row.has_intrinsic_percentage = computed_values.max_height().is_percentage() || computed_values.height().is_percentage();
  1614. row.intrinsic_percentage = min(height_percentage, max_height_percentage);
  1615. }
  1616. }
  1617. template<>
  1618. void TableFormattingContext::initialize_intrinsic_percentages_from_rows_or_columns<TableFormattingContext::Column>()
  1619. {
  1620. size_t column_index = 0;
  1621. for_each_child_box_matching(table_box(), is_table_column_group, [&](auto& column_group_box) {
  1622. for_each_child_box_matching(column_group_box, is_table_column, [&](auto& column_box) {
  1623. auto const& computed_values = column_box.computed_values();
  1624. // Definition of percentage contribution: https://www.w3.org/TR/css-tables-3/#percentage-contribution
  1625. auto max_width_percentage = computed_values.max_width().is_percentage() ? computed_values.max_width().percentage().value() : static_cast<double>(INFINITY);
  1626. auto width_percentage = computed_values.width().is_percentage() ? computed_values.width().percentage().value() : 0;
  1627. m_columns[column_index].has_intrinsic_percentage = computed_values.max_width().is_percentage() || computed_values.width().is_percentage();
  1628. m_columns[column_index].intrinsic_percentage = min(width_percentage, max_width_percentage);
  1629. auto const& col_node = static_cast<HTML::HTMLTableColElement const&>(*column_box.dom_node());
  1630. unsigned span = col_node.attribute(HTML::AttributeNames::span).to_uint().value_or(1);
  1631. column_index += span;
  1632. });
  1633. });
  1634. }
  1635. template<class RowOrColumn>
  1636. void TableFormattingContext::initialize_intrinsic_percentages_from_cells()
  1637. {
  1638. auto& rows_or_columns = table_rows_or_columns<RowOrColumn>();
  1639. for (auto& cell : m_cells) {
  1640. auto cell_span_value = cell_span<RowOrColumn>(cell);
  1641. auto cell_start_rc_index = cell_index<RowOrColumn>(cell);
  1642. auto cell_end_rc_index = cell_start_rc_index + cell_span_value;
  1643. if (cell_has_intrinsic_percentage<RowOrColumn>(cell)) {
  1644. for (auto rc_index = cell_start_rc_index; rc_index < cell_end_rc_index; rc_index++) {
  1645. rows_or_columns[rc_index].has_intrinsic_percentage = true;
  1646. }
  1647. if (cell_span_value != 1) {
  1648. continue;
  1649. }
  1650. } else {
  1651. continue;
  1652. }
  1653. size_t rc_index = cell_index<RowOrColumn>(cell);
  1654. rows_or_columns[rc_index].has_intrinsic_percentage = true;
  1655. rows_or_columns[rc_index].intrinsic_percentage = max(cell_percentage_contribution<RowOrColumn>(cell), rows_or_columns[rc_index].intrinsic_percentage);
  1656. }
  1657. }
  1658. template<>
  1659. CSSPixels TableFormattingContext::border_spacing<TableFormattingContext::Row>()
  1660. {
  1661. return border_spacing_vertical();
  1662. }
  1663. template<>
  1664. CSSPixels TableFormattingContext::border_spacing<TableFormattingContext::Column>()
  1665. {
  1666. return border_spacing_horizontal();
  1667. }
  1668. CSSPixels TableFormattingContext::border_spacing_horizontal() const
  1669. {
  1670. auto const& computed_values = table_box().computed_values();
  1671. // When a table is laid out in collapsed-borders mode, the border-spacing of the table-root is ignored (as if it was set to 0px):
  1672. // https://www.w3.org/TR/css-tables-3/#collapsed-style-overrides
  1673. if (computed_values.border_collapse() == CSS::BorderCollapse::Collapse)
  1674. return 0;
  1675. return computed_values.border_spacing_horizontal().to_px(table_box());
  1676. }
  1677. CSSPixels TableFormattingContext::border_spacing_vertical() const
  1678. {
  1679. auto const& computed_values = table_box().computed_values();
  1680. // When a table is laid out in collapsed-borders mode, the border-spacing of the table-root is ignored (as if it was set to 0px):
  1681. // https://www.w3.org/TR/css-tables-3/#collapsed-style-overrides
  1682. if (computed_values.border_collapse() == CSS::BorderCollapse::Collapse)
  1683. return 0;
  1684. return computed_values.border_spacing_vertical().to_px(table_box());
  1685. }
  1686. template<>
  1687. Vector<TableFormattingContext::Row>& TableFormattingContext::table_rows_or_columns()
  1688. {
  1689. return m_rows;
  1690. }
  1691. template<>
  1692. Vector<TableFormattingContext::Column>& TableFormattingContext::table_rows_or_columns()
  1693. {
  1694. return m_columns;
  1695. }
  1696. }