TableFormattingContext.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  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()
  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_width = calculate_min_content_width(cell.box);
  207. auto max_content_width = calculate_max_content_width(cell.box);
  208. auto min_content_height = calculate_min_content_height(cell.box, max_content_width);
  209. auto max_content_height = calculate_max_content_height(cell.box, min_content_width);
  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. auto width_of_table_containing_block = m_available_space->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. if (width_of_table_containing_block.is_definite())
  533. used_width = max(min(grid_max, width_of_table_containing_block.to_px_or_zero()), used_min_width);
  534. else
  535. used_width = max(grid_max, used_min_width);
  536. // https://www.w3.org/TR/CSS22/tables.html#auto-table-layout
  537. // A percentage value for a column width is relative to the table width. If the table has 'width: auto',
  538. // a percentage represents a constraint on the column's width, which a UA should try to satisfy.
  539. CSSPixels adjusted_used_width = 0;
  540. for (auto& cell : m_cells) {
  541. auto const& cell_width = cell.box->computed_values().width();
  542. if (cell_width.is_percentage()) {
  543. adjusted_used_width = ceil(100 / cell_width.percentage().value() * cell.outer_max_width.to_double());
  544. if (width_of_table_containing_block.is_definite())
  545. used_width = min(max(used_width, adjusted_used_width), width_of_table_containing_block.to_px_or_zero());
  546. else
  547. used_width = max(used_width, adjusted_used_width);
  548. }
  549. }
  550. } else {
  551. // If the table-root’s width property has a computed value (resolving to
  552. // resolved-table-width) other than auto, the used width is the greater
  553. // of resolved-table-width, and the used min-width of the table.
  554. CSSPixels resolved_table_width = computed_values.width().to_px(table_box(), width_of_table_wrapper_containing_block);
  555. // Since used_width is content width, we need to subtract the border spacing from the specified width for a consistent comparison.
  556. used_width = max(resolved_table_width - table_box_state.border_box_left() - table_box_state.border_box_right(), used_min_width);
  557. if (!should_treat_max_width_as_none(table_box(), m_available_space->width))
  558. used_width = min(used_width, computed_values.max_width().to_px(table_box(), width_of_table_wrapper_containing_block));
  559. }
  560. table_box_state.set_content_width(used_width);
  561. }
  562. CSSPixels TableFormattingContext::compute_columns_total_used_width() const
  563. {
  564. CSSPixels total_used_width = 0;
  565. for (auto& column : m_columns) {
  566. total_used_width += column.used_width;
  567. }
  568. return total_used_width;
  569. }
  570. static CSSPixels compute_columns_total_candidate_width(Vector<CSSPixels> const& candidate_widths)
  571. {
  572. CSSPixels total_candidate_width = 0;
  573. for (auto width : candidate_widths) {
  574. total_candidate_width += width;
  575. }
  576. return total_candidate_width;
  577. }
  578. void TableFormattingContext::commit_candidate_column_widths(Vector<CSSPixels> const& candidate_widths)
  579. {
  580. VERIFY(candidate_widths.size() == m_columns.size());
  581. for (size_t i = 0; i < m_columns.size(); ++i) {
  582. m_columns[i].used_width = candidate_widths[i];
  583. }
  584. }
  585. void TableFormattingContext::assign_columns_width_linear_combination(Vector<CSSPixels> const& candidate_widths, CSSPixels available_width)
  586. {
  587. auto columns_total_candidate_width = compute_columns_total_candidate_width(candidate_widths);
  588. auto columns_total_used_width = compute_columns_total_used_width();
  589. if (columns_total_candidate_width == columns_total_used_width) {
  590. return;
  591. }
  592. auto candidate_weight = (available_width - columns_total_used_width) / static_cast<double>(columns_total_candidate_width - columns_total_used_width);
  593. for (size_t i = 0; i < m_columns.size(); ++i) {
  594. auto& column = m_columns[i];
  595. column.used_width = candidate_weight * candidate_widths[i] + (1 - candidate_weight) * column.used_width;
  596. }
  597. }
  598. template<class ColumnFilter, class BaseWidthGetter>
  599. bool TableFormattingContext::distribute_excess_width_proportionally_to_base_width(CSSPixels excess_width, ColumnFilter column_filter, BaseWidthGetter base_width_getter)
  600. {
  601. bool found_matching_columns = false;
  602. CSSPixels total_base_width = 0;
  603. for (auto const& column : m_columns) {
  604. if (column_filter(column)) {
  605. total_base_width += base_width_getter(column);
  606. found_matching_columns = true;
  607. }
  608. }
  609. if (!found_matching_columns) {
  610. return false;
  611. }
  612. VERIFY(total_base_width > 0);
  613. for (auto& column : m_columns) {
  614. if (column_filter(column)) {
  615. column.used_width += excess_width * base_width_getter(column) / static_cast<double>(total_base_width);
  616. }
  617. }
  618. return true;
  619. }
  620. template<class ColumnFilter>
  621. bool TableFormattingContext::distribute_excess_width_equally(CSSPixels excess_width, ColumnFilter column_filter)
  622. {
  623. size_t matching_column_count = 0;
  624. for (auto const& column : m_columns) {
  625. if (column_filter(column)) {
  626. ++matching_column_count;
  627. }
  628. }
  629. if (matching_column_count == 0) {
  630. return false;
  631. }
  632. for (auto& column : m_columns) {
  633. if (column_filter(column)) {
  634. column.used_width += excess_width / matching_column_count;
  635. }
  636. }
  637. return matching_column_count;
  638. }
  639. template<class ColumnFilter>
  640. bool TableFormattingContext::distribute_excess_width_by_intrinsic_percentage(CSSPixels excess_width, ColumnFilter column_filter)
  641. {
  642. bool found_matching_columns = false;
  643. double total_percentage_width = 0;
  644. for (auto const& column : m_columns) {
  645. if (column_filter(column)) {
  646. found_matching_columns = true;
  647. total_percentage_width += column.intrinsic_percentage;
  648. }
  649. }
  650. if (!found_matching_columns) {
  651. return false;
  652. }
  653. for (auto& column : m_columns) {
  654. if (column_filter(column)) {
  655. column.used_width += excess_width * column.intrinsic_percentage / total_percentage_width;
  656. }
  657. }
  658. return true;
  659. }
  660. void TableFormattingContext::distribute_width_to_columns()
  661. {
  662. // Implements https://www.w3.org/TR/css-tables-3/#width-distribution-algorithm
  663. // The total horizontal border spacing is defined for each table:
  664. // - 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
  665. // - Otherwise, 0
  666. auto total_horizontal_border_spacing = m_columns.is_empty() ? 0 : (m_columns.size() + 1) * border_spacing_horizontal();
  667. // The assignable table width is the used width of the table minus the total horizontal border spacing (if any).
  668. // This is the width that we will be able to allocate to the columns.
  669. const CSSPixels available_width = m_state.get(table_box()).content_width() - total_horizontal_border_spacing;
  670. Vector<CSSPixels> candidate_widths;
  671. candidate_widths.resize(m_columns.size());
  672. // 1. The min-content sizing-guess is the set of column width assignments where each column is assigned its min-content width.
  673. for (size_t i = 0; i < m_columns.size(); ++i) {
  674. auto& column = m_columns[i];
  675. // In fixed mode, the min-content width of percent-columns and auto-columns is considered to be zero:
  676. // https://www.w3.org/TR/css-tables-3/#width-distribution-in-fixed-mode
  677. if (use_fixed_mode_layout() && !column.is_constrained) {
  678. continue;
  679. }
  680. column.used_width = column.min_size;
  681. candidate_widths[i] = column.min_size;
  682. }
  683. // 2. The min-content-percentage sizing-guess is the set of column width assignments where:
  684. // - each percent-column is assigned the larger of:
  685. // - its intrinsic percentage width times the assignable width and
  686. // - its min-content width.
  687. // - all other columns are assigned their min-content width.
  688. for (size_t i = 0; i < m_columns.size(); ++i) {
  689. auto& column = m_columns[i];
  690. if (column.has_intrinsic_percentage) {
  691. candidate_widths[i] = max(column.min_size, column.intrinsic_percentage / 100 * available_width);
  692. }
  693. }
  694. // 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
  695. // linear combination (with weights adding to 1) of the two consecutive sizing-guesses whose width sums bound the available width.
  696. if (available_width < compute_columns_total_candidate_width(candidate_widths)) {
  697. assign_columns_width_linear_combination(candidate_widths, available_width);
  698. return;
  699. } else {
  700. commit_candidate_column_widths(candidate_widths);
  701. }
  702. // 3. The min-content-specified sizing-guess is the set of column width assignments where:
  703. // - each percent-column is assigned the larger of:
  704. // - its intrinsic percentage width times the assignable width and
  705. // - its min-content width
  706. // - any other column that is constrained is assigned its max-content width
  707. // - all other columns are assigned their min-content width.
  708. for (size_t i = 0; i < m_columns.size(); ++i) {
  709. auto& column = m_columns[i];
  710. if (column.is_constrained) {
  711. candidate_widths[i] = column.max_size;
  712. }
  713. }
  714. if (available_width < compute_columns_total_candidate_width(candidate_widths)) {
  715. assign_columns_width_linear_combination(candidate_widths, available_width);
  716. return;
  717. } else {
  718. commit_candidate_column_widths(candidate_widths);
  719. }
  720. // 4. The max-content sizing-guess is the set of column width assignments where:
  721. // - each percent-column is assigned the larger of:
  722. // - its intrinsic percentage width times the assignable width and
  723. // - its min-content width
  724. // - all other columns are assigned their max-content width.
  725. for (size_t i = 0; i < m_columns.size(); ++i) {
  726. auto& column = m_columns[i];
  727. if (!column.has_intrinsic_percentage) {
  728. candidate_widths[i] = column.max_size;
  729. }
  730. }
  731. if (available_width < compute_columns_total_candidate_width(candidate_widths)) {
  732. assign_columns_width_linear_combination(candidate_widths, available_width);
  733. return;
  734. } else {
  735. commit_candidate_column_widths(candidate_widths);
  736. }
  737. // Otherwise, the used widths of the columns are the result of starting from the max-content sizing-guess and distributing
  738. // the excess width to the columns of the table according to the rules for distributing excess width to columns (for used width).
  739. distribute_excess_width_to_columns(available_width);
  740. }
  741. void TableFormattingContext::distribute_excess_width_to_columns(CSSPixels available_width)
  742. {
  743. // Implements https://www.w3.org/TR/css-tables-3/#distributing-width-to-columns
  744. auto columns_total_used_width = compute_columns_total_used_width();
  745. if (columns_total_used_width >= available_width) {
  746. return;
  747. }
  748. auto excess_width = available_width - columns_total_used_width;
  749. if (excess_width == 0) {
  750. return;
  751. }
  752. if (use_fixed_mode_layout()) {
  753. distribute_excess_width_to_columns_fixed_mode(excess_width);
  754. return;
  755. }
  756. // 1. If there are non-constrained columns that have originating cells with intrinsic percentage width of 0% and with nonzero
  757. // max-content width (aka the columns allowed to grow by this rule), the distributed widths of the columns allowed to grow
  758. // by this rule are increased in proportion to max-content width so the total increase adds to the excess width.
  759. if (distribute_excess_width_proportionally_to_base_width(
  760. excess_width,
  761. [](auto const& column) {
  762. return !column.is_constrained && column.has_originating_cells && column.intrinsic_percentage == 0 && column.max_size > 0;
  763. },
  764. [](auto const& column) { return column.max_size; })) {
  765. excess_width = available_width - compute_columns_total_used_width();
  766. }
  767. if (excess_width == 0) {
  768. return;
  769. }
  770. // 2. Otherwise, if there are non-constrained columns that have originating cells with intrinsic percentage width of 0% (aka the columns
  771. // allowed to grow by this rule, which thanks to the previous rule must have zero max-content width), the distributed widths of the
  772. // columns allowed to grow by this rule are increased by equal amounts so the total increase adds to the excess width.
  773. if (distribute_excess_width_equally(excess_width,
  774. [](auto const& column) {
  775. return !column.is_constrained && column.has_originating_cells && column.intrinsic_percentage == 0;
  776. })) {
  777. excess_width = available_width - compute_columns_total_used_width();
  778. }
  779. if (excess_width == 0) {
  780. return;
  781. }
  782. // 3. Otherwise, if there are constrained columns with intrinsic percentage width of 0% and with nonzero max-content width
  783. // (aka the columns allowed to grow by this rule, which, due to other rules, must have originating cells), the distributed widths of the
  784. // columns allowed to grow by this rule are increased in proportion to max-content width so the total increase adds to the excess width.
  785. if (distribute_excess_width_proportionally_to_base_width(
  786. excess_width,
  787. [](auto const& column) {
  788. return column.is_constrained && column.intrinsic_percentage == 0 && column.max_size > 0;
  789. },
  790. [](auto const& column) { return column.max_size; })) {
  791. excess_width = available_width - compute_columns_total_used_width();
  792. }
  793. if (excess_width == 0) {
  794. return;
  795. }
  796. // 4. Otherwise, if there are columns with intrinsic percentage width greater than 0% (aka the columns allowed to grow by this rule,
  797. // which, due to other rules, must have originating cells), the distributed widths of the columns allowed to grow by this rule are
  798. // increased in proportion to intrinsic percentage width so the total increase adds to the excess width.
  799. if (distribute_excess_width_by_intrinsic_percentage(excess_width, [](auto const& column) {
  800. return column.intrinsic_percentage > 0;
  801. })) {
  802. excess_width = available_width - compute_columns_total_used_width();
  803. }
  804. if (excess_width == 0) {
  805. return;
  806. }
  807. // 5. Otherwise, if there is any such column, the distributed widths of all columns that have originating cells are increased by equal amounts
  808. // so the total increase adds to the excess width.
  809. if (distribute_excess_width_equally(
  810. excess_width,
  811. [](auto const& column) { return column.has_originating_cells; })) {
  812. excess_width = available_width - compute_columns_total_used_width();
  813. }
  814. if (excess_width == 0) {
  815. return;
  816. }
  817. // 6. Otherwise, the distributed widths of all columns are increased by equal amounts so the total increase adds to the excess width.
  818. distribute_excess_width_equally(excess_width, [](auto const&) { return true; });
  819. }
  820. void TableFormattingContext::distribute_excess_width_to_columns_fixed_mode(CSSPixels excess_width)
  821. {
  822. // Implements the fixed mode for https://www.w3.org/TR/css-tables-3/#distributing-width-to-columns.
  823. // If there are any columns with no width specified, the excess width is distributed in equally to such columns
  824. if (distribute_excess_width_equally(excess_width, [](auto const& column) { return !column.is_constrained && !column.has_intrinsic_percentage; })) {
  825. return;
  826. }
  827. // 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
  828. if (distribute_excess_width_proportionally_to_base_width(
  829. excess_width, [](auto const& column) { return column.used_width > 0; }, [](auto const& column) { return column.used_width; })) {
  830. return;
  831. }
  832. // 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
  833. if (distribute_excess_width_by_intrinsic_percentage(excess_width, [](auto const& column) { return column.intrinsic_percentage > 0; })) {
  834. return;
  835. }
  836. // otherwise, the excess width is distributed equally to the zero-sized columns
  837. distribute_excess_width_equally(excess_width, [](auto const& column) { return column.used_width == 0; });
  838. }
  839. void TableFormattingContext::compute_table_height(LayoutMode layout_mode)
  840. {
  841. // First pass of row height calculation:
  842. for (auto& row : m_rows) {
  843. auto row_computed_height = row.box->computed_values().height();
  844. if (row_computed_height.is_length()) {
  845. auto height_of_containing_block = m_state.get(*row.box->containing_block()).content_height();
  846. auto row_used_height = row_computed_height.to_px(row.box, height_of_containing_block);
  847. row.base_height = max(row.base_height, row_used_height);
  848. }
  849. }
  850. // First pass of cells layout:
  851. for (auto& cell : m_cells) {
  852. auto& row = m_rows[cell.row_index];
  853. auto& cell_state = m_state.get_mutable(cell.box);
  854. CSSPixels span_width = 0;
  855. for (size_t i = 0; i < cell.column_span; ++i)
  856. span_width += m_columns[cell.column_index + i].used_width;
  857. auto width_of_containing_block = m_state.get(*cell.box->containing_block()).content_width();
  858. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  859. auto height_of_containing_block = m_state.get(*cell.box->containing_block()).content_height();
  860. auto height_of_containing_block_as_length = CSS::Length::make_px(height_of_containing_block);
  861. cell_state.padding_top = cell.box->computed_values().padding().top().to_px(cell.box, width_of_containing_block);
  862. cell_state.padding_bottom = cell.box->computed_values().padding().bottom().to_px(cell.box, width_of_containing_block);
  863. cell_state.padding_left = cell.box->computed_values().padding().left().to_px(cell.box, width_of_containing_block);
  864. cell_state.padding_right = cell.box->computed_values().padding().right().to_px(cell.box, width_of_containing_block);
  865. if (cell.box->computed_values().border_collapse() == CSS::BorderCollapse::Separate) {
  866. cell_state.border_top = cell.box->computed_values().border_top().width;
  867. cell_state.border_bottom = cell.box->computed_values().border_bottom().width;
  868. cell_state.border_left = cell.box->computed_values().border_left().width;
  869. cell_state.border_right = cell.box->computed_values().border_right().width;
  870. }
  871. auto cell_computed_height = cell.box->computed_values().height();
  872. if (cell_computed_height.is_length()) {
  873. auto cell_used_height = cell_computed_height.to_px(cell.box, height_of_containing_block);
  874. cell_state.set_content_height(cell_used_height - cell_state.border_box_top() - cell_state.border_box_bottom());
  875. row.base_height = max(row.base_height, cell_used_height);
  876. }
  877. // Compute cell width as specified by https://www.w3.org/TR/css-tables-3/#bounding-box-assignment:
  878. // 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:
  879. // - the widths/heights of all spanned visible columns/rows
  880. // - the horizontal/vertical border-spacing times the amount of spanned visible columns/rows minus one
  881. // FIXME: Account for visibility.
  882. cell_state.set_content_width(span_width - cell_state.border_box_left() - cell_state.border_box_right() + (cell.column_span - 1) * border_spacing_horizontal());
  883. if (auto independent_formatting_context = layout_inside(cell.box, layout_mode, cell_state.available_inner_space_or_constraints_from(*m_available_space))) {
  884. cell_state.set_content_height(independent_formatting_context->automatic_content_height());
  885. independent_formatting_context->parent_context_did_dimension_child_root_box();
  886. }
  887. cell.baseline = box_baseline(cell.box);
  888. // Implements https://www.w3.org/TR/css-tables-3/#computing-the-table-height
  889. // The minimum height of a row is the maximum of:
  890. // - the computed height (if definite, percentages being considered 0px) of its corresponding table-row (if nay)
  891. // - the computed height of each cell spanning the current row exclusively (if definite, percentages being treated as 0px), and
  892. // - the minimum height (ROWMIN) required by the cells spanning the row.
  893. // Note that we've already applied the first rule at the top of the method.
  894. if (cell.row_span == 1) {
  895. row.base_height = max(row.base_height, cell_state.border_box_height());
  896. }
  897. row.base_height = max(row.base_height, m_rows[cell.row_index].min_size);
  898. row.baseline = max(row.baseline, cell.baseline);
  899. }
  900. CSSPixels sum_rows_height = 0;
  901. for (auto& row : m_rows) {
  902. sum_rows_height += row.base_height;
  903. }
  904. m_table_height = sum_rows_height;
  905. if (!table_box().computed_values().height().is_auto()) {
  906. // If the table has a height property with a value other than auto, it is treated as a minimum height for the
  907. // table grid, and will eventually be distributed to the height of the rows if their collective minimum height
  908. // ends up smaller than this number.
  909. CSSPixels height_of_table_containing_block = m_state.get(*table_wrapper().containing_block()).content_height();
  910. auto specified_table_height = table_box().computed_values().height().to_px(table_box(), height_of_table_containing_block);
  911. auto const& table_state = m_state.get(table_box());
  912. m_table_height = max(m_table_height, specified_table_height - table_state.border_box_top() - table_state.border_box_bottom());
  913. }
  914. for (auto& row : m_rows) {
  915. // Reference size is the largest of
  916. // - its initial base height and
  917. // - its new base height (the one evaluated during the second layout pass, where percentages used in
  918. // rowgroups/rows/cells' specified heights were resolved according to the table height, instead of
  919. // being ignored as 0px).
  920. // Assign reference size to base size. Later, the reference size might change to a larger value during
  921. // the second pass of rows layout.
  922. row.reference_height = row.base_height;
  923. }
  924. // Second pass of rows height calculation:
  925. // At this point, percentage row height can be resolved because the final table height is calculated.
  926. for (auto& row : m_rows) {
  927. auto row_computed_height = row.box->computed_values().height();
  928. if (row_computed_height.is_percentage()) {
  929. auto row_used_height = row_computed_height.to_px(row.box, m_table_height);
  930. row.reference_height = max(row.reference_height, row_used_height);
  931. } else {
  932. continue;
  933. }
  934. }
  935. // Second pass cells layout:
  936. // At this point, percentage cell height can be resolved because the final table height is calculated.
  937. for (auto& cell : m_cells) {
  938. auto& row = m_rows[cell.row_index];
  939. auto& cell_state = m_state.get_mutable(cell.box);
  940. CSSPixels span_width = 0;
  941. for (size_t i = 0; i < cell.column_span; ++i)
  942. span_width += m_columns[cell.column_index + i].used_width;
  943. auto cell_computed_height = cell.box->computed_values().height();
  944. if (cell_computed_height.is_percentage()) {
  945. auto cell_used_height = cell_computed_height.to_px(cell.box, m_table_height);
  946. cell_state.set_content_height(cell_used_height - cell_state.border_box_top() - cell_state.border_box_bottom());
  947. row.reference_height = max(row.reference_height, cell_used_height);
  948. } else {
  949. continue;
  950. }
  951. cell_state.set_content_width(span_width - cell_state.border_box_left() - cell_state.border_box_right() + (cell.column_span - 1) * border_spacing_horizontal());
  952. if (auto independent_formatting_context = layout_inside(cell.box, layout_mode, cell_state.available_inner_space_or_constraints_from(*m_available_space))) {
  953. independent_formatting_context->parent_context_did_dimension_child_root_box();
  954. }
  955. cell.baseline = box_baseline(cell.box);
  956. row.reference_height = max(row.reference_height, cell_state.border_box_height());
  957. row.baseline = max(row.baseline, cell.baseline);
  958. }
  959. }
  960. void TableFormattingContext::distribute_height_to_rows()
  961. {
  962. CSSPixels sum_reference_height = 0;
  963. for (auto& row : m_rows) {
  964. sum_reference_height += row.reference_height;
  965. }
  966. if (sum_reference_height == 0)
  967. return;
  968. Vector<Row&> rows_with_auto_height;
  969. for (auto& row : m_rows) {
  970. if (row.box->computed_values().height().is_auto()) {
  971. rows_with_auto_height.append(row);
  972. }
  973. }
  974. if (m_table_height <= sum_reference_height) {
  975. // If the table height is equal or smaller than sum of reference sizes, the final height assigned to each row
  976. // will be the weighted mean of the base and the reference size that yields the correct total height.
  977. for (auto& row : m_rows) {
  978. auto weight = row.reference_height / static_cast<double>(sum_reference_height);
  979. auto final_height = m_table_height * weight;
  980. row.final_height = final_height;
  981. }
  982. } else if (rows_with_auto_height.size() > 0) {
  983. // Else, if the table owns any “auto-height” row (a row whose size is only determined by its content size and
  984. // none of the specified heights), each non-auto-height row receives its reference height and auto-height rows
  985. // receive their reference size plus some increment which is equal to the height missing to amount to the
  986. // specified table height divided by the amount of such rows.
  987. for (auto& row : m_rows) {
  988. row.final_height = row.reference_height;
  989. }
  990. auto auto_height_rows_increment = (m_table_height - sum_reference_height) / rows_with_auto_height.size();
  991. for (auto& row : rows_with_auto_height) {
  992. row.final_height += auto_height_rows_increment;
  993. }
  994. } else {
  995. // Else, all rows receive their reference size plus some increment which is equal to the height missing to
  996. // amount to the specified table height divided by the amount of rows.
  997. auto increment = (m_table_height - sum_reference_height) / m_rows.size();
  998. for (auto& row : m_rows) {
  999. row.final_height = row.reference_height + increment;
  1000. }
  1001. }
  1002. // Add undistributable space due to border spacing: https://www.w3.org/TR/css-tables-3/#computing-undistributable-space.
  1003. m_table_height += (m_rows.size() + 1) * border_spacing_vertical();
  1004. }
  1005. void TableFormattingContext::position_row_boxes()
  1006. {
  1007. auto const& table_state = m_state.get(table_box());
  1008. CSSPixels row_top_offset = table_state.offset.y() + table_state.padding_top + border_spacing_vertical();
  1009. CSSPixels row_left_offset = table_state.border_left + table_state.padding_left + border_spacing_horizontal();
  1010. for (size_t y = 0; y < m_rows.size(); y++) {
  1011. auto& row = m_rows[y];
  1012. auto& row_state = m_state.get_mutable(row.box);
  1013. CSSPixels row_width = 0.0f;
  1014. for (auto& column : m_columns) {
  1015. row_width += column.used_width;
  1016. }
  1017. row_state.set_content_height(row.final_height);
  1018. row_state.set_content_width(row_width);
  1019. row_state.set_content_x(row_left_offset);
  1020. row_state.set_content_y(row_top_offset);
  1021. row_top_offset += row_state.content_height() + border_spacing_vertical();
  1022. }
  1023. CSSPixels row_group_top_offset = table_state.border_top + table_state.padding_top;
  1024. CSSPixels row_group_left_offset = table_state.border_left + table_state.padding_left;
  1025. for_each_child_box_matching(table_box(), is_table_row_group, [&](auto& row_group_box) {
  1026. CSSPixels row_group_height = 0.0f;
  1027. CSSPixels row_group_width = 0.0f;
  1028. auto& row_group_box_state = m_state.get_mutable(row_group_box);
  1029. row_group_box_state.set_content_x(row_group_left_offset);
  1030. row_group_box_state.set_content_y(row_group_top_offset);
  1031. for_each_child_box_matching(row_group_box, is_table_row, [&](auto& row) {
  1032. auto const& row_state = m_state.get(row);
  1033. row_group_height += row_state.border_box_height();
  1034. row_group_width = max(row_group_width, row_state.border_box_width());
  1035. });
  1036. row_group_box_state.set_content_height(row_group_height);
  1037. row_group_box_state.set_content_width(row_group_width);
  1038. row_group_top_offset += row_group_height;
  1039. });
  1040. auto total_content_height = max(row_top_offset, row_group_top_offset) - table_state.offset.y() - table_state.padding_top;
  1041. m_table_height = max(total_content_height, m_table_height);
  1042. }
  1043. void TableFormattingContext::position_cell_boxes()
  1044. {
  1045. CSSPixels left_column_offset = 0;
  1046. for (auto& column : m_columns) {
  1047. column.left_offset = left_column_offset;
  1048. left_column_offset += column.used_width;
  1049. }
  1050. for (auto& cell : m_cells) {
  1051. auto& cell_state = m_state.get_mutable(cell.box);
  1052. auto& row_state = m_state.get(m_rows[cell.row_index].box);
  1053. CSSPixels const cell_border_box_height = cell_state.content_height() + cell_state.border_box_top() + cell_state.border_box_bottom();
  1054. CSSPixels const row_content_height = compute_row_content_height(cell);
  1055. auto const& vertical_align = cell.box->computed_values().vertical_align();
  1056. // The following image shows various alignment lines of a row:
  1057. // https://www.w3.org/TR/css-tables-3/images/cell-align-explainer.png
  1058. if (vertical_align.has<CSS::VerticalAlign>()) {
  1059. auto height_diff = row_content_height - cell_border_box_height;
  1060. switch (vertical_align.get<CSS::VerticalAlign>()) {
  1061. case CSS::VerticalAlign::Middle: {
  1062. cell_state.padding_top += height_diff / 2;
  1063. cell_state.padding_bottom += height_diff / 2;
  1064. break;
  1065. }
  1066. case CSS::VerticalAlign::Top: {
  1067. cell_state.padding_bottom += height_diff;
  1068. break;
  1069. }
  1070. case CSS::VerticalAlign::Bottom: {
  1071. cell_state.padding_top += height_diff;
  1072. break;
  1073. }
  1074. case CSS::VerticalAlign::Baseline: {
  1075. cell_state.padding_top += m_rows[cell.row_index].baseline - cell.baseline;
  1076. cell_state.padding_bottom += height_diff;
  1077. break;
  1078. }
  1079. default:
  1080. VERIFY_NOT_REACHED();
  1081. }
  1082. }
  1083. // Compute cell position as specified by https://www.w3.org/TR/css-tables-3/#bounding-box-assignment:
  1084. // left/top location is the sum of:
  1085. // - for top: the height reserved for top captions (including margins), if any
  1086. // - the padding-left/padding-top and border-left-width/border-top-width of the table
  1087. // FIXME: Account for visibility.
  1088. cell_state.offset = row_state.offset.translated(
  1089. cell_state.border_box_left() + m_columns[cell.column_index].left_offset + cell.column_index * border_spacing_horizontal(),
  1090. cell_state.border_box_top());
  1091. }
  1092. }
  1093. bool TableFormattingContext::use_fixed_mode_layout() const
  1094. {
  1095. // Implements https://www.w3.org/TR/css-tables-3/#in-fixed-mode.
  1096. // 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
  1097. // specified width of the table root is either a <length-percentage>, min-content or fit-content. When the specified width is not one of
  1098. // 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.
  1099. auto const& width = table_box().computed_values().width();
  1100. return table_box().computed_values().table_layout() == CSS::TableLayout::Fixed && (width.is_length() || width.is_percentage() || width.is_min_content() || width.is_fit_content());
  1101. }
  1102. bool TableFormattingContext::border_is_less_specific(const CSS::BorderData& a, const CSS::BorderData& b)
  1103. {
  1104. // Implements criteria for steps 1, 2 and 3 of border conflict resolution algorithm, as described in
  1105. // https://www.w3.org/TR/CSS22/tables.html#border-conflict-resolution.
  1106. static HashMap<CSS::LineStyle, unsigned> const line_style_score = {
  1107. { CSS::LineStyle::Inset, 0 },
  1108. { CSS::LineStyle::Groove, 1 },
  1109. { CSS::LineStyle::Outset, 2 },
  1110. { CSS::LineStyle::Ridge, 3 },
  1111. { CSS::LineStyle::Dotted, 4 },
  1112. { CSS::LineStyle::Dashed, 5 },
  1113. { CSS::LineStyle::Solid, 6 },
  1114. { CSS::LineStyle::Double, 7 },
  1115. };
  1116. // 1. Borders with the 'border-style' of 'hidden' take precedence over all other conflicting borders. Any border with this
  1117. // value suppresses all borders at this location.
  1118. if (a.line_style == CSS::LineStyle::Hidden) {
  1119. return false;
  1120. }
  1121. if (b.line_style == CSS::LineStyle::Hidden) {
  1122. return true;
  1123. }
  1124. // 2. Borders with a style of 'none' have the lowest priority. Only if the border properties of all the elements meeting
  1125. // at this edge are 'none' will the border be omitted (but note that 'none' is the default value for the border style.)
  1126. if (a.line_style == CSS::LineStyle::None) {
  1127. return true;
  1128. }
  1129. if (b.line_style == CSS::LineStyle::None) {
  1130. return false;
  1131. }
  1132. // 3. If none of the styles are 'hidden' and at least one of them is not 'none', then narrow borders are discarded in favor
  1133. // of wider ones. If several have the same 'border-width' then styles are preferred in this order: 'double', 'solid',
  1134. // 'dashed', 'dotted', 'ridge', 'outset', 'groove', and the lowest: 'inset'.
  1135. if (a.width > b.width) {
  1136. return false;
  1137. } else if (a.width < b.width) {
  1138. return true;
  1139. }
  1140. if (*line_style_score.get(a.line_style) > *line_style_score.get(b.line_style)) {
  1141. return false;
  1142. } else if (*line_style_score.get(a.line_style) < *line_style_score.get(b.line_style)) {
  1143. return true;
  1144. }
  1145. return false;
  1146. }
  1147. const CSS::BorderData& TableFormattingContext::border_data_conflicting_edge(TableFormattingContext::ConflictingEdge const& conflicting_edge)
  1148. {
  1149. auto const& style = conflicting_edge.element->computed_values();
  1150. switch (conflicting_edge.side) {
  1151. case ConflictingSide::Top: {
  1152. return style.border_top();
  1153. }
  1154. case ConflictingSide::Bottom: {
  1155. return style.border_bottom();
  1156. }
  1157. case ConflictingSide::Left: {
  1158. return style.border_left();
  1159. }
  1160. case ConflictingSide::Right: {
  1161. return style.border_right();
  1162. }
  1163. default: {
  1164. VERIFY_NOT_REACHED();
  1165. }
  1166. }
  1167. }
  1168. const Painting::PaintableBox::BorderDataWithElementKind TableFormattingContext::border_data_with_element_kind_from_conflicting_edge(ConflictingEdge const& conflicting_edge)
  1169. {
  1170. auto const& border_data = border_data_conflicting_edge(conflicting_edge);
  1171. return { .border_data = border_data, .element_kind = conflicting_edge.element_kind };
  1172. }
  1173. TableFormattingContext::ConflictingEdge const& TableFormattingContext::winning_conflicting_edge(TableFormattingContext::ConflictingEdge const& a, TableFormattingContext::ConflictingEdge const& b)
  1174. {
  1175. auto a_border_data = border_data_conflicting_edge(a);
  1176. auto b_border_data = border_data_conflicting_edge(b);
  1177. // First check if step 4 of border conflict resolution applies, as described in https://www.w3.org/TR/CSS22/tables.html#border-conflict-resolution.
  1178. if (a_border_data.line_style == b_border_data.line_style && a_border_data.width == b_border_data.width) {
  1179. // 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
  1180. // row group, column, column group and, lastly, table. When two elements of the same type conflict, then the one
  1181. // further to the left (if the table's 'direction' is 'ltr'; right, if it is 'rtl') and further to the top wins.
  1182. if (static_cast<unsigned>(a.element_kind) < static_cast<unsigned>(b.element_kind)) {
  1183. return a;
  1184. } else if (static_cast<unsigned>(a.element_kind) > static_cast<unsigned>(b.element_kind)) {
  1185. return b;
  1186. }
  1187. // Here the element kind is the same, thus the coordinates are either both set or not set.
  1188. VERIFY(a.column.has_value() == b.column.has_value());
  1189. VERIFY(a.row.has_value() == b.row.has_value());
  1190. if (a.column.has_value()) {
  1191. if (a.column.value() < b.column.value()) {
  1192. return a;
  1193. } else if (a.column.value() > b.column.value()) {
  1194. return b;
  1195. }
  1196. }
  1197. if (a.row.has_value()) {
  1198. if (a.row.value() < b.row.value()) {
  1199. return a;
  1200. } else if (a.row.value() > b.row.value()) {
  1201. return b;
  1202. }
  1203. }
  1204. return a;
  1205. }
  1206. // Apply steps 1, 2 and 3 of the border conflict resolution algorithm.
  1207. return border_is_less_specific(a_border_data, b_border_data) ? b : a;
  1208. }
  1209. void TableFormattingContext::border_conflict_resolution()
  1210. {
  1211. // Implements border conflict resolution, as described in https://www.w3.org/TR/CSS22/tables.html#border-conflict-resolution.
  1212. BorderConflictFinder finder(this);
  1213. for (auto& cell : m_cells) {
  1214. auto& cell_state = m_state.get_mutable(cell.box);
  1215. cell_state.set_table_cell_coordinates(
  1216. Painting::PaintableBox::TableCellCoordinates {
  1217. .row_index = cell.row_index,
  1218. .column_index = cell.column_index,
  1219. .row_span = cell.row_span,
  1220. .column_span = cell.column_span });
  1221. if (cell.box->computed_values().border_collapse() == CSS::BorderCollapse::Separate) {
  1222. continue;
  1223. }
  1224. Painting::PaintableBox::BordersDataWithElementKind override_borders_data;
  1225. ConflictingEdge winning_edge_left {
  1226. .element = cell.box,
  1227. .element_kind = Painting::PaintableBox::ConflictingElementKind::Cell,
  1228. .side = ConflictingSide::Left,
  1229. .row = cell.row_index,
  1230. .column = cell.column_index,
  1231. };
  1232. for (auto const& conflicting_edge : finder.conflicting_edges(cell, ConflictingSide::Left)) {
  1233. winning_edge_left = winning_conflicting_edge(winning_edge_left, conflicting_edge);
  1234. }
  1235. override_borders_data.left = border_data_with_element_kind_from_conflicting_edge(winning_edge_left);
  1236. cell_state.border_left = override_borders_data.left.border_data.width;
  1237. ConflictingEdge winning_edge_right {
  1238. .element = cell.box,
  1239. .element_kind = Painting::PaintableBox::ConflictingElementKind::Cell,
  1240. .side = ConflictingSide::Right,
  1241. .row = cell.row_index,
  1242. .column = cell.column_index,
  1243. };
  1244. for (auto const& conflicting_edge : finder.conflicting_edges(cell, ConflictingSide::Right)) {
  1245. winning_edge_right = winning_conflicting_edge(winning_edge_right, conflicting_edge);
  1246. }
  1247. override_borders_data.right = border_data_with_element_kind_from_conflicting_edge(winning_edge_right);
  1248. cell_state.border_right = override_borders_data.right.border_data.width;
  1249. ConflictingEdge winning_edge_top {
  1250. .element = cell.box,
  1251. .element_kind = Painting::PaintableBox::ConflictingElementKind::Cell,
  1252. .side = ConflictingSide::Top,
  1253. .row = cell.row_index,
  1254. .column = cell.column_index,
  1255. };
  1256. for (auto const& conflicting_edge : finder.conflicting_edges(cell, ConflictingSide::Top)) {
  1257. winning_edge_top = winning_conflicting_edge(winning_edge_top, conflicting_edge);
  1258. }
  1259. override_borders_data.top = border_data_with_element_kind_from_conflicting_edge(winning_edge_top);
  1260. cell_state.border_top = override_borders_data.top.border_data.width;
  1261. ConflictingEdge winning_edge_bottom {
  1262. .element = cell.box,
  1263. .element_kind = Painting::PaintableBox::ConflictingElementKind::Cell,
  1264. .side = ConflictingSide::Bottom,
  1265. .row = cell.row_index,
  1266. .column = cell.column_index,
  1267. };
  1268. for (auto const& conflicting_edge : finder.conflicting_edges(cell, ConflictingSide::Bottom)) {
  1269. winning_edge_bottom = winning_conflicting_edge(winning_edge_bottom, conflicting_edge);
  1270. }
  1271. override_borders_data.bottom = border_data_with_element_kind_from_conflicting_edge(winning_edge_bottom);
  1272. cell_state.border_bottom = override_borders_data.bottom.border_data.width;
  1273. cell_state.set_override_borders_data(override_borders_data);
  1274. }
  1275. }
  1276. CSSPixels TableFormattingContext::compute_row_content_height(Cell const& cell) const
  1277. {
  1278. auto& row_state = m_state.get(m_rows[cell.row_index].box);
  1279. if (cell.row_span == 1) {
  1280. return row_state.content_height();
  1281. }
  1282. // The height of a cell is the sum of all spanned rows, as described in
  1283. // https://www.w3.org/TR/css-tables-3/#bounding-box-assignment
  1284. // When the row span is greater than 1, the borders of inner rows within the span have to be
  1285. // included in the content height of the spanning cell. First top and final bottom borders are
  1286. // excluded to be consistent with the handling of row span 1 case above, which uses the content
  1287. // height (no top and bottom borders) of the row.
  1288. CSSPixels span_height = 0;
  1289. for (size_t i = 0; i < cell.row_span; ++i) {
  1290. auto const& row_state = m_state.get(m_rows[cell.row_index + i].box);
  1291. if (i == 0) {
  1292. span_height += row_state.content_height() + row_state.border_box_bottom();
  1293. } else if (i == cell.row_span - 1) {
  1294. span_height += row_state.border_box_top() + row_state.content_height();
  1295. } else {
  1296. span_height += row_state.border_box_height();
  1297. }
  1298. }
  1299. // Compute cell height as specified by https://www.w3.org/TR/css-tables-3/#bounding-box-assignment:
  1300. // width/height is the sum of:
  1301. // - the widths/heights of all spanned visible columns/rows
  1302. // - the horizontal/vertical border-spacing times the amount of spanned visible columns/rows minus one
  1303. // FIXME: Account for visibility.
  1304. span_height += (cell.row_span - 1) * border_spacing_vertical();
  1305. return span_height;
  1306. }
  1307. TableFormattingContext::BorderConflictFinder::BorderConflictFinder(TableFormattingContext const* context)
  1308. : m_context(context)
  1309. {
  1310. collect_conflicting_col_elements();
  1311. collect_conflicting_row_group_elements();
  1312. }
  1313. void TableFormattingContext::BorderConflictFinder::collect_conflicting_col_elements()
  1314. {
  1315. m_col_elements_by_index.resize(m_context->m_columns.size());
  1316. for (auto* child = m_context->table_box().first_child(); child; child = child->next_sibling()) {
  1317. if (!child->display().is_table_column_group()) {
  1318. continue;
  1319. }
  1320. size_t column_index = 0;
  1321. for (auto* child_of_column_group = child->first_child(); child_of_column_group; child_of_column_group = child_of_column_group->next_sibling()) {
  1322. VERIFY(child_of_column_group->display().is_table_column());
  1323. auto const& col_node = static_cast<HTML::HTMLTableColElement const&>(*child_of_column_group->dom_node());
  1324. unsigned span = col_node.attribute(HTML::AttributeNames::span).to_uint().value_or(1);
  1325. for (size_t i = column_index; i < column_index + span; ++i) {
  1326. m_col_elements_by_index[i] = child_of_column_group;
  1327. }
  1328. column_index += span;
  1329. }
  1330. }
  1331. }
  1332. void TableFormattingContext::BorderConflictFinder::collect_conflicting_row_group_elements()
  1333. {
  1334. m_row_group_elements_by_index.resize(m_context->m_rows.size());
  1335. size_t current_row_index = 0;
  1336. for_each_child_box_matching(m_context->table_box(), is_table_row_group, [&](auto& row_group_box) {
  1337. auto start_row_index = current_row_index;
  1338. size_t row_count = 0;
  1339. for_each_child_box_matching(row_group_box, is_table_row, [&](auto&) {
  1340. ++row_count;
  1341. });
  1342. for_each_child_box_matching(row_group_box, is_table_row, [&](auto&) {
  1343. m_row_group_elements_by_index[current_row_index] = RowGroupInfo {
  1344. .row_group = &row_group_box, .start_index = start_row_index, .row_count = row_count
  1345. };
  1346. ++current_row_index;
  1347. return IterationDecision::Continue;
  1348. });
  1349. });
  1350. }
  1351. void TableFormattingContext::BorderConflictFinder::collect_cell_conflicting_edges(Vector<ConflictingEdge>& result, Cell const& cell, TableFormattingContext::ConflictingSide edge) const
  1352. {
  1353. // Right edge of the cell to the left.
  1354. if (cell.column_index >= cell.column_span && edge == ConflictingSide::Left) {
  1355. auto left_cell_column_index = cell.column_index - cell.column_span;
  1356. auto maybe_cell_to_left = m_context->m_cells_by_coordinate[cell.row_index][left_cell_column_index];
  1357. if (maybe_cell_to_left.has_value()) {
  1358. result.append({ maybe_cell_to_left->box, Painting::PaintableBox::ConflictingElementKind::Cell, ConflictingSide::Right, cell.row_index, left_cell_column_index });
  1359. }
  1360. }
  1361. // Left edge of the cell to the right.
  1362. if (cell.column_index + cell.column_span < m_context->m_cells_by_coordinate[cell.row_index].size() && edge == ConflictingSide::Right) {
  1363. auto right_cell_column_index = cell.column_index + cell.column_span;
  1364. auto maybe_cell_to_right = m_context->m_cells_by_coordinate[cell.row_index][right_cell_column_index];
  1365. if (maybe_cell_to_right.has_value()) {
  1366. result.append({ maybe_cell_to_right->box, Painting::PaintableBox::ConflictingElementKind::Cell, ConflictingSide::Left, cell.row_index, right_cell_column_index });
  1367. }
  1368. }
  1369. // Bottom edge of the cell above.
  1370. if (cell.row_index >= cell.row_span && edge == ConflictingSide::Top) {
  1371. auto above_cell_row_index = cell.row_index - cell.row_span;
  1372. auto maybe_cell_above = m_context->m_cells_by_coordinate[above_cell_row_index][cell.column_index];
  1373. if (maybe_cell_above.has_value()) {
  1374. result.append({ maybe_cell_above->box, Painting::PaintableBox::ConflictingElementKind::Cell, ConflictingSide::Bottom, above_cell_row_index, cell.column_index });
  1375. }
  1376. }
  1377. // Top edge of the cell below.
  1378. if (cell.row_index + cell.row_span < m_context->m_cells_by_coordinate.size() && edge == ConflictingSide::Bottom) {
  1379. auto below_cell_row_index = cell.row_index + cell.row_span;
  1380. auto maybe_cell_below = m_context->m_cells_by_coordinate[below_cell_row_index][cell.column_index];
  1381. if (maybe_cell_below.has_value()) {
  1382. result.append({ maybe_cell_below->box, Painting::PaintableBox::ConflictingElementKind::Cell, ConflictingSide::Top, below_cell_row_index, cell.column_index });
  1383. }
  1384. }
  1385. }
  1386. void TableFormattingContext::BorderConflictFinder::collect_row_conflicting_edges(Vector<ConflictingEdge>& result, Cell const& cell, TableFormattingContext::ConflictingSide edge) const
  1387. {
  1388. // Top edge of the row.
  1389. if (edge == ConflictingSide::Top) {
  1390. result.append({ m_context->m_rows[cell.row_index].box, Painting::PaintableBox::ConflictingElementKind::Row, ConflictingSide::Top, cell.row_index, {} });
  1391. }
  1392. // Bottom edge of the row.
  1393. if (edge == ConflictingSide::Bottom) {
  1394. result.append({ m_context->m_rows[cell.row_index].box, Painting::PaintableBox::ConflictingElementKind::Row, ConflictingSide::Bottom, cell.row_index, {} });
  1395. }
  1396. // Bottom edge of the row above.
  1397. if (cell.row_index >= cell.row_span && edge == ConflictingSide::Top) {
  1398. auto above_row_index = cell.row_index - cell.row_span;
  1399. result.append({ m_context->m_rows[above_row_index].box, Painting::PaintableBox::ConflictingElementKind::Row, ConflictingSide::Bottom, above_row_index, {} });
  1400. }
  1401. // Top edge of the row below.
  1402. if (cell.row_index + cell.row_span < m_context->m_rows.size() && edge == ConflictingSide::Bottom) {
  1403. auto below_row_index = cell.row_index + cell.row_span;
  1404. result.append({ m_context->m_rows[below_row_index].box, Painting::PaintableBox::ConflictingElementKind::Row, ConflictingSide::Top, below_row_index, {} });
  1405. }
  1406. }
  1407. void TableFormattingContext::BorderConflictFinder::collect_row_group_conflicting_edges(Vector<ConflictingEdge>& result, Cell const& cell, TableFormattingContext::ConflictingSide edge) const
  1408. {
  1409. auto const& maybe_row_group = m_row_group_elements_by_index[cell.row_index];
  1410. // Top edge of the row group.
  1411. if (maybe_row_group.has_value() && cell.row_index == maybe_row_group->start_index && edge == ConflictingSide::Top) {
  1412. result.append({ maybe_row_group->row_group, Painting::PaintableBox::ConflictingElementKind::RowGroup, ConflictingSide::Top, maybe_row_group->start_index, {} });
  1413. }
  1414. // Bottom edge of the row group above.
  1415. if (cell.row_index >= cell.row_span) {
  1416. auto const& maybe_row_group_above = m_row_group_elements_by_index[cell.row_index - cell.row_span];
  1417. 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) {
  1418. result.append({ maybe_row_group_above->row_group, Painting::PaintableBox::ConflictingElementKind::RowGroup, ConflictingSide::Bottom, maybe_row_group_above->start_index, {} });
  1419. }
  1420. }
  1421. // Bottom edge of the row group.
  1422. if (maybe_row_group.has_value() && cell.row_index == maybe_row_group->start_index + maybe_row_group->row_count - 1 && edge == ConflictingSide::Bottom) {
  1423. result.append({ maybe_row_group->row_group, Painting::PaintableBox::ConflictingElementKind::RowGroup, ConflictingSide::Bottom, maybe_row_group->start_index, {} });
  1424. }
  1425. // Top edge of the row group below.
  1426. if (cell.row_index + cell.row_span < m_row_group_elements_by_index.size()) {
  1427. auto const& maybe_row_group_below = m_row_group_elements_by_index[cell.row_index + cell.row_span];
  1428. if (maybe_row_group_below.has_value() && cell.row_index + cell.row_span == maybe_row_group_below->start_index && edge == ConflictingSide::Bottom) {
  1429. result.append({ maybe_row_group_below->row_group, Painting::PaintableBox::ConflictingElementKind::RowGroup, ConflictingSide::Top, maybe_row_group_below->start_index, {} });
  1430. }
  1431. }
  1432. }
  1433. void TableFormattingContext::BorderConflictFinder::collect_column_group_conflicting_edges(Vector<ConflictingEdge>& result, Cell const& cell, TableFormattingContext::ConflictingSide edge) const
  1434. {
  1435. // Left edge of the column group.
  1436. if (m_col_elements_by_index[cell.column_index] && edge == ConflictingSide::Left) {
  1437. result.append({ m_col_elements_by_index[cell.column_index], Painting::PaintableBox::ConflictingElementKind::ColumnGroup, ConflictingSide::Left, {}, cell.column_index });
  1438. }
  1439. // Right edge of the column group to the left.
  1440. if (cell.column_index >= cell.column_span && m_col_elements_by_index[cell.column_index - cell.column_span] && edge == ConflictingSide::Left) {
  1441. auto left_column_index = cell.column_index - cell.column_span;
  1442. result.append({ m_col_elements_by_index[left_column_index], Painting::PaintableBox::ConflictingElementKind::ColumnGroup, ConflictingSide::Right, {}, left_column_index });
  1443. }
  1444. // Right edge of the column group.
  1445. if (m_col_elements_by_index[cell.column_index] && edge == ConflictingSide::Right) {
  1446. result.append({ m_col_elements_by_index[cell.column_index], Painting::PaintableBox::ConflictingElementKind::ColumnGroup, ConflictingSide::Right, {}, cell.column_index });
  1447. }
  1448. // Left edge of the column group to the right.
  1449. 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) {
  1450. auto right_column_index = cell.column_index + cell.column_span;
  1451. result.append({ m_col_elements_by_index[right_column_index], Painting::PaintableBox::ConflictingElementKind::ColumnGroup, ConflictingSide::Left, {}, right_column_index });
  1452. }
  1453. }
  1454. void TableFormattingContext::BorderConflictFinder::collect_table_box_conflicting_edges(Vector<ConflictingEdge>& result, Cell const& cell, TableFormattingContext::ConflictingSide edge) const
  1455. {
  1456. // Top edge from column group or table. Left and right edges of the column group are handled in collect_column_group_conflicting_edges.
  1457. if (cell.row_index == 0 && edge == ConflictingSide::Top) {
  1458. if (m_col_elements_by_index[cell.column_index]) {
  1459. result.append({ m_col_elements_by_index[cell.column_index], Painting::PaintableBox::ConflictingElementKind::ColumnGroup, ConflictingSide::Top, {}, cell.column_index });
  1460. }
  1461. result.append({ &m_context->table_box(), Painting::PaintableBox::ConflictingElementKind::Table, ConflictingSide::Top, {}, {} });
  1462. }
  1463. // Bottom edge from column group or table. Left and right edges of the column group are handled in collect_column_group_conflicting_edges.
  1464. if (cell.row_index + cell.row_span == m_context->m_rows.size() && edge == ConflictingSide::Bottom) {
  1465. if (m_col_elements_by_index[cell.column_index]) {
  1466. result.append({ m_col_elements_by_index[cell.column_index], Painting::PaintableBox::ConflictingElementKind::ColumnGroup, ConflictingSide::Bottom, {}, cell.column_index });
  1467. }
  1468. result.append({ &m_context->table_box(), Painting::PaintableBox::ConflictingElementKind::Table, ConflictingSide::Bottom, {}, {} });
  1469. }
  1470. // Left edge from row group or table. Top and bottom edges of the row group are handled in collect_row_group_conflicting_edges.
  1471. if (cell.column_index == 0 && edge == ConflictingSide::Left) {
  1472. result.append({ m_context->m_rows[cell.row_index].box, Painting::PaintableBox::ConflictingElementKind::Row, ConflictingSide::Left, cell.row_index, {} });
  1473. if (m_row_group_elements_by_index[cell.row_index].has_value()) {
  1474. result.append({ m_row_group_elements_by_index[cell.row_index]->row_group, Painting::PaintableBox::ConflictingElementKind::RowGroup, ConflictingSide::Left, cell.row_index, {} });
  1475. }
  1476. result.append({ &m_context->table_box(), Painting::PaintableBox::ConflictingElementKind::Table, ConflictingSide::Left, {}, {} });
  1477. }
  1478. // Right edge from row group or table. Top and bottom edges of the row group are handled in collect_row_group_conflicting_edges.
  1479. if (cell.column_index + cell.column_span == m_context->m_columns.size() && edge == ConflictingSide::Right) {
  1480. result.append({ m_context->m_rows[cell.row_index].box, Painting::PaintableBox::ConflictingElementKind::Row, ConflictingSide::Right, cell.row_index, {} });
  1481. if (m_row_group_elements_by_index[cell.row_index].has_value()) {
  1482. result.append({ m_row_group_elements_by_index[cell.row_index]->row_group, Painting::PaintableBox::ConflictingElementKind::RowGroup, ConflictingSide::Right, cell.row_index, {} });
  1483. }
  1484. result.append({ &m_context->table_box(), Painting::PaintableBox::ConflictingElementKind::Table, ConflictingSide::Right, {}, {} });
  1485. }
  1486. }
  1487. Vector<TableFormattingContext::ConflictingEdge> TableFormattingContext::BorderConflictFinder::conflicting_edges(
  1488. Cell const& cell, TableFormattingContext::ConflictingSide edge) const
  1489. {
  1490. Vector<ConflictingEdge> result = {};
  1491. collect_cell_conflicting_edges(result, cell, edge);
  1492. collect_row_conflicting_edges(result, cell, edge);
  1493. collect_row_group_conflicting_edges(result, cell, edge);
  1494. collect_column_group_conflicting_edges(result, cell, edge);
  1495. collect_table_box_conflicting_edges(result, cell, edge);
  1496. return result;
  1497. }
  1498. void TableFormattingContext::run(Box const& box, LayoutMode layout_mode, AvailableSpace const& available_space)
  1499. {
  1500. m_available_space = available_space;
  1501. auto total_captions_height = run_caption_layout(layout_mode, CSS::CaptionSide::Top);
  1502. // Determine the number of rows/columns the table requires.
  1503. calculate_row_column_grid(box);
  1504. border_conflict_resolution();
  1505. // Compute the minimum width of each column.
  1506. compute_cell_measures();
  1507. compute_outer_content_sizes();
  1508. compute_table_measures<Column>();
  1509. // https://www.w3.org/TR/css-tables-3/#row-layout
  1510. // Since during row layout the specified heights of cells in the row were ignored and cells that were spanning more than one rows
  1511. // have not been sized correctly, their height will need to be eventually distributed to the set of rows they spanned. This is done
  1512. // by running the same algorithm as the column measurement, with the span=1 value being initialized (for min-content) with the largest
  1513. // of the resulting height of the previous row layout, the height specified on the corresponding table-row (if any), and the largest
  1514. // height specified on cells that span this row only (the algorithm starts by considering cells of span 2 on top of that assignment).
  1515. compute_table_measures<Row>();
  1516. // Compute the width of the table.
  1517. compute_table_width();
  1518. if (available_space.width.is_intrinsic_sizing_constraint() && !available_space.height.is_intrinsic_sizing_constraint()) {
  1519. return;
  1520. }
  1521. // Distribute the width of the table among columns.
  1522. distribute_width_to_columns();
  1523. compute_table_height(layout_mode);
  1524. distribute_height_to_rows();
  1525. position_row_boxes();
  1526. position_cell_boxes();
  1527. m_state.get_mutable(table_box()).set_content_height(m_table_height);
  1528. total_captions_height += run_caption_layout(layout_mode, CSS::CaptionSide::Bottom);
  1529. // Table captions are positioned between the table margins and its borders (outside the grid box borders) as described in
  1530. // https://www.w3.org/TR/css-tables-3/#bounding-box-assignment
  1531. // A visual representation of this model can be found at https://www.w3.org/TR/css-tables-3/images/table_container.png
  1532. m_state.get_mutable(table_box()).margin_bottom += total_captions_height;
  1533. m_automatic_content_height = m_table_height;
  1534. }
  1535. CSSPixels TableFormattingContext::automatic_content_width() const
  1536. {
  1537. return greatest_child_width(context_box());
  1538. }
  1539. CSSPixels TableFormattingContext::automatic_content_height() const
  1540. {
  1541. return m_automatic_content_height;
  1542. }
  1543. template<>
  1544. size_t TableFormattingContext::cell_span<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  1545. {
  1546. return cell.row_span;
  1547. }
  1548. template<>
  1549. size_t TableFormattingContext::cell_span<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  1550. {
  1551. return cell.column_span;
  1552. }
  1553. template<>
  1554. size_t TableFormattingContext::cell_index<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  1555. {
  1556. return cell.row_index;
  1557. }
  1558. template<>
  1559. size_t TableFormattingContext::cell_index<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  1560. {
  1561. return cell.column_index;
  1562. }
  1563. template<>
  1564. CSSPixels TableFormattingContext::cell_min_size<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  1565. {
  1566. return cell.outer_min_height;
  1567. }
  1568. template<>
  1569. CSSPixels TableFormattingContext::cell_min_size<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  1570. {
  1571. return cell.outer_min_width;
  1572. }
  1573. template<>
  1574. CSSPixels TableFormattingContext::cell_max_size<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  1575. {
  1576. return cell.outer_max_height;
  1577. }
  1578. template<>
  1579. CSSPixels TableFormattingContext::cell_max_size<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  1580. {
  1581. return cell.outer_max_width;
  1582. }
  1583. template<>
  1584. double TableFormattingContext::cell_percentage_contribution<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  1585. {
  1586. // Definition of percentage contribution: https://www.w3.org/TR/css-tables-3/#percentage-contribution
  1587. auto const& computed_values = cell.box->computed_values();
  1588. auto max_height_percentage = computed_values.max_height().is_percentage() ? computed_values.max_height().percentage().value() : static_cast<double>(INFINITY);
  1589. auto height_percentage = computed_values.height().is_percentage() ? computed_values.height().percentage().value() : 0;
  1590. return min(height_percentage, max_height_percentage);
  1591. }
  1592. template<>
  1593. double TableFormattingContext::cell_percentage_contribution<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  1594. {
  1595. // Definition of percentage contribution: https://www.w3.org/TR/css-tables-3/#percentage-contribution
  1596. auto const& computed_values = cell.box->computed_values();
  1597. auto max_width_percentage = computed_values.max_width().is_percentage() ? computed_values.max_width().percentage().value() : static_cast<double>(INFINITY);
  1598. auto width_percentage = computed_values.width().is_percentage() ? computed_values.width().percentage().value() : 0;
  1599. return min(width_percentage, max_width_percentage);
  1600. }
  1601. template<>
  1602. bool TableFormattingContext::cell_has_intrinsic_percentage<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  1603. {
  1604. return cell.box->computed_values().height().is_percentage();
  1605. }
  1606. template<>
  1607. bool TableFormattingContext::cell_has_intrinsic_percentage<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  1608. {
  1609. return cell.box->computed_values().width().is_percentage();
  1610. }
  1611. template<>
  1612. void TableFormattingContext::initialize_intrinsic_percentages_from_rows_or_columns<TableFormattingContext::Row>()
  1613. {
  1614. for (auto& row : m_rows) {
  1615. auto const& computed_values = row.box->computed_values();
  1616. // Definition of percentage contribution: https://www.w3.org/TR/css-tables-3/#percentage-contribution
  1617. auto max_height_percentage = computed_values.max_height().is_percentage() ? computed_values.max_height().percentage().value() : static_cast<double>(INFINITY);
  1618. auto height_percentage = computed_values.height().is_percentage() ? computed_values.height().percentage().value() : 0;
  1619. row.has_intrinsic_percentage = computed_values.max_height().is_percentage() || computed_values.height().is_percentage();
  1620. row.intrinsic_percentage = min(height_percentage, max_height_percentage);
  1621. }
  1622. }
  1623. template<>
  1624. void TableFormattingContext::initialize_intrinsic_percentages_from_rows_or_columns<TableFormattingContext::Column>()
  1625. {
  1626. size_t column_index = 0;
  1627. for_each_child_box_matching(table_box(), is_table_column_group, [&](auto& column_group_box) {
  1628. for_each_child_box_matching(column_group_box, is_table_column, [&](auto& column_box) {
  1629. auto const& computed_values = column_box.computed_values();
  1630. // Definition of percentage contribution: https://www.w3.org/TR/css-tables-3/#percentage-contribution
  1631. auto max_width_percentage = computed_values.max_width().is_percentage() ? computed_values.max_width().percentage().value() : static_cast<double>(INFINITY);
  1632. auto width_percentage = computed_values.width().is_percentage() ? computed_values.width().percentage().value() : 0;
  1633. m_columns[column_index].has_intrinsic_percentage = computed_values.max_width().is_percentage() || computed_values.width().is_percentage();
  1634. m_columns[column_index].intrinsic_percentage = min(width_percentage, max_width_percentage);
  1635. auto const& col_node = static_cast<HTML::HTMLTableColElement const&>(*column_box.dom_node());
  1636. unsigned span = col_node.attribute(HTML::AttributeNames::span).to_uint().value_or(1);
  1637. column_index += span;
  1638. });
  1639. });
  1640. }
  1641. template<class RowOrColumn>
  1642. void TableFormattingContext::initialize_intrinsic_percentages_from_cells()
  1643. {
  1644. auto& rows_or_columns = table_rows_or_columns<RowOrColumn>();
  1645. for (auto& cell : m_cells) {
  1646. auto cell_span_value = cell_span<RowOrColumn>(cell);
  1647. auto cell_start_rc_index = cell_index<RowOrColumn>(cell);
  1648. auto cell_end_rc_index = cell_start_rc_index + cell_span_value;
  1649. if (cell_has_intrinsic_percentage<RowOrColumn>(cell)) {
  1650. for (auto rc_index = cell_start_rc_index; rc_index < cell_end_rc_index; rc_index++) {
  1651. rows_or_columns[rc_index].has_intrinsic_percentage = true;
  1652. }
  1653. if (cell_span_value != 1) {
  1654. continue;
  1655. }
  1656. } else {
  1657. continue;
  1658. }
  1659. size_t rc_index = cell_index<RowOrColumn>(cell);
  1660. rows_or_columns[rc_index].has_intrinsic_percentage = true;
  1661. rows_or_columns[rc_index].intrinsic_percentage = max(cell_percentage_contribution<RowOrColumn>(cell), rows_or_columns[rc_index].intrinsic_percentage);
  1662. }
  1663. }
  1664. template<>
  1665. CSSPixels TableFormattingContext::border_spacing<TableFormattingContext::Row>()
  1666. {
  1667. return border_spacing_vertical();
  1668. }
  1669. template<>
  1670. CSSPixels TableFormattingContext::border_spacing<TableFormattingContext::Column>()
  1671. {
  1672. return border_spacing_horizontal();
  1673. }
  1674. CSSPixels TableFormattingContext::border_spacing_horizontal() const
  1675. {
  1676. auto const& computed_values = table_box().computed_values();
  1677. // 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):
  1678. // https://www.w3.org/TR/css-tables-3/#collapsed-style-overrides
  1679. if (computed_values.border_collapse() == CSS::BorderCollapse::Collapse)
  1680. return 0;
  1681. return computed_values.border_spacing_horizontal().to_px(table_box());
  1682. }
  1683. CSSPixels TableFormattingContext::border_spacing_vertical() const
  1684. {
  1685. auto const& computed_values = table_box().computed_values();
  1686. // 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):
  1687. // https://www.w3.org/TR/css-tables-3/#collapsed-style-overrides
  1688. if (computed_values.border_collapse() == CSS::BorderCollapse::Collapse)
  1689. return 0;
  1690. return computed_values.border_spacing_vertical().to_px(table_box());
  1691. }
  1692. template<>
  1693. Vector<TableFormattingContext::Row>& TableFormattingContext::table_rows_or_columns()
  1694. {
  1695. return m_rows;
  1696. }
  1697. template<>
  1698. Vector<TableFormattingContext::Column>& TableFormattingContext::table_rows_or_columns()
  1699. {
  1700. return m_columns;
  1701. }
  1702. }