TableFormattingContext.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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/Layout/BlockFormattingContext.h>
  11. #include <LibWeb/Layout/Box.h>
  12. #include <LibWeb/Layout/InlineFormattingContext.h>
  13. #include <LibWeb/Layout/TableFormattingContext.h>
  14. struct GridPosition {
  15. size_t x;
  16. size_t y;
  17. };
  18. inline bool operator==(GridPosition const& a, GridPosition const& b)
  19. {
  20. return a.x == b.x && a.y == b.y;
  21. }
  22. namespace AK {
  23. template<>
  24. struct Traits<GridPosition> : public GenericTraits<GridPosition> {
  25. static unsigned hash(GridPosition const& key)
  26. {
  27. return pair_int_hash(key.x, key.y);
  28. }
  29. };
  30. }
  31. namespace Web::Layout {
  32. TableFormattingContext::TableFormattingContext(LayoutState& state, Box const& root, FormattingContext* parent)
  33. : FormattingContext(Type::Table, state, root, parent)
  34. {
  35. }
  36. TableFormattingContext::~TableFormattingContext() = default;
  37. static inline bool is_table_row_group(Box const& box)
  38. {
  39. auto const& display = box.display();
  40. return display.is_table_row_group() || display.is_table_header_group() || display.is_table_footer_group();
  41. }
  42. static inline bool is_table_row(Box const& box)
  43. {
  44. return box.display().is_table_row();
  45. }
  46. template<typename Matcher, typename Callback>
  47. static void for_each_child_box_matching(Box const& parent, Matcher matcher, Callback callback)
  48. {
  49. parent.for_each_child_of_type<Box>([&](Box const& child_box) {
  50. if (matcher(child_box))
  51. callback(child_box);
  52. });
  53. }
  54. CSSPixels TableFormattingContext::run_caption_layout(LayoutMode layout_mode, CSS::CaptionSide phase)
  55. {
  56. CSSPixels caption_height = 0;
  57. for (auto* child = table_box().first_child(); child; child = child->next_sibling()) {
  58. if (!child->display().is_table_caption() || child->computed_values().caption_side() != phase) {
  59. continue;
  60. }
  61. // The caption boxes are principal block-level boxes that retain their own content, padding, margin, and border areas,
  62. // and are rendered as normal block boxes inside the table wrapper box, as described in https://www.w3.org/TR/CSS22/tables.html#model
  63. auto caption_context = make<BlockFormattingContext>(m_state, *verify_cast<BlockContainer>(child), this);
  64. caption_context->run(table_box(), layout_mode, *m_available_space);
  65. VERIFY(child->is_box());
  66. auto const& child_box = static_cast<Box const&>(*child);
  67. // FIXME: Since caption only has inline children, BlockFormattingContext doesn't resolve the vertical metrics.
  68. // We need to do it manually here.
  69. caption_context->resolve_vertical_box_model_metrics(child_box);
  70. auto const& caption_state = m_state.get(child_box);
  71. if (phase == CSS::CaptionSide::Top) {
  72. m_state.get_mutable(table_box()).set_content_y(caption_state.margin_box_height());
  73. } else {
  74. m_state.get_mutable(child_box).set_content_y(
  75. m_state.get(table_box()).margin_box_height() + caption_state.margin_box_top());
  76. }
  77. caption_height += caption_state.margin_box_height();
  78. }
  79. return caption_height;
  80. }
  81. void TableFormattingContext::calculate_row_column_grid(Box const& box)
  82. {
  83. // Implements https://html.spec.whatwg.org/multipage/tables.html#forming-a-table
  84. HashMap<GridPosition, bool> grid;
  85. size_t x_width = 0, y_height = 0;
  86. size_t x_current = 0, y_current = 0;
  87. // Implements https://html.spec.whatwg.org/multipage/tables.html#algorithm-for-processing-rows
  88. auto process_row = [&](auto& row) {
  89. if (y_height == y_current)
  90. y_height++;
  91. x_current = 0;
  92. for (auto* child = row.first_child(); child; child = child->next_sibling()) {
  93. if (child->display().is_table_cell()) {
  94. // 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.
  95. while (x_current < x_width && grid.contains(GridPosition { x_current, y_current }))
  96. x_current++;
  97. Box const* box = static_cast<Box const*>(child);
  98. if (x_current == x_width)
  99. x_width++;
  100. size_t colspan = 1, rowspan = 1;
  101. if (box->dom_node() && is<HTML::HTMLTableCellElement>(*box->dom_node())) {
  102. auto const& node = static_cast<HTML::HTMLTableCellElement const&>(*box->dom_node());
  103. colspan = node.col_span();
  104. rowspan = node.row_span();
  105. }
  106. if (x_width < x_current + colspan)
  107. x_width = x_current + colspan;
  108. if (y_height < y_current + rowspan)
  109. y_height = y_current + rowspan;
  110. for (size_t y = y_current; y < y_current + rowspan; y++)
  111. for (size_t x = x_current; x < x_current + colspan; x++)
  112. grid.set(GridPosition { x, y }, true);
  113. m_cells.append(Cell { *box, x_current, y_current, colspan, rowspan });
  114. x_current += colspan;
  115. }
  116. }
  117. m_rows.append(Row { row });
  118. y_current++;
  119. };
  120. for_each_child_box_matching(box, is_table_row_group, [&](auto& row_group_box) {
  121. for_each_child_box_matching(row_group_box, is_table_row, [&](auto& row_box) {
  122. process_row(row_box);
  123. return IterationDecision::Continue;
  124. });
  125. });
  126. for_each_child_box_matching(box, is_table_row, [&](auto& row_box) {
  127. process_row(row_box);
  128. return IterationDecision::Continue;
  129. });
  130. m_columns.resize(x_width);
  131. for (auto& cell : m_cells) {
  132. // Clip spans to the end of the table.
  133. cell.row_span = min(cell.row_span, m_rows.size() - cell.row_index);
  134. cell.column_span = min(cell.column_span, m_columns.size() - cell.column_index);
  135. }
  136. }
  137. void TableFormattingContext::compute_cell_measures(AvailableSpace const& available_space)
  138. {
  139. auto const& containing_block = m_state.get(*table_wrapper().containing_block());
  140. for (auto& cell : m_cells) {
  141. auto const& computed_values = cell.box->computed_values();
  142. CSSPixels padding_top = computed_values.padding().top().to_px(cell.box, containing_block.content_height());
  143. CSSPixels padding_bottom = computed_values.padding().bottom().to_px(cell.box, containing_block.content_height());
  144. CSSPixels padding_left = computed_values.padding().left().to_px(cell.box, containing_block.content_width());
  145. CSSPixels padding_right = computed_values.padding().right().to_px(cell.box, containing_block.content_width());
  146. auto const& cell_state = m_state.get(cell.box);
  147. auto is_collapse = cell.box->computed_values().border_collapse() == CSS::BorderCollapse::Collapse;
  148. CSSPixels border_top = is_collapse ? cell_state.border_top : computed_values.border_top().width;
  149. CSSPixels border_bottom = is_collapse ? cell_state.border_bottom : computed_values.border_bottom().width;
  150. CSSPixels border_left = is_collapse ? cell_state.border_left : computed_values.border_left().width;
  151. CSSPixels border_right = is_collapse ? cell_state.border_right : computed_values.border_right().width;
  152. auto height = computed_values.height().to_px(cell.box, containing_block.content_height());
  153. auto width = computed_values.width().to_px(cell.box, containing_block.content_width());
  154. auto min_content_height = calculate_min_content_height(cell.box, available_space.width);
  155. auto max_content_height = calculate_max_content_height(cell.box, available_space.width);
  156. auto min_content_width = calculate_min_content_width(cell.box);
  157. auto max_content_width = calculate_max_content_width(cell.box);
  158. CSSPixels min_height = min_content_height;
  159. CSSPixels min_width = min_content_width;
  160. if (!computed_values.min_height().is_auto())
  161. min_height = max(min_height, computed_values.min_height().to_px(cell.box, containing_block.content_height()));
  162. if (!computed_values.min_width().is_auto())
  163. min_width = max(min_width, computed_values.min_width().to_px(cell.box, containing_block.content_width()));
  164. CSSPixels max_height = computed_values.height().is_auto() ? max_content_height : height;
  165. CSSPixels max_width = computed_values.width().is_auto() ? max_content_width : width;
  166. if (!should_treat_max_height_as_none(cell.box, available_space.height))
  167. max_height = min(max_height, computed_values.max_height().to_px(cell.box, containing_block.content_height()));
  168. if (!should_treat_max_width_as_none(cell.box, available_space.width))
  169. max_width = min(max_width, computed_values.max_width().to_px(cell.box, containing_block.content_width()));
  170. if (computed_values.height().is_percentage()) {
  171. m_rows[cell.row_index].type = SizeType::Percent;
  172. m_rows[cell.row_index].percentage_height = max(m_rows[cell.row_index].percentage_height, computed_values.height().percentage().value());
  173. } else {
  174. m_rows[cell.row_index].type = SizeType::Pixel;
  175. }
  176. if (computed_values.width().is_percentage()) {
  177. m_columns[cell.column_index].type = SizeType::Percent;
  178. m_columns[cell.column_index].percentage_width = max(m_columns[cell.column_index].percentage_width, computed_values.width().percentage().value());
  179. } else {
  180. m_columns[cell.column_index].type = SizeType::Pixel;
  181. }
  182. auto cell_intrinsic_height_offsets = padding_top + padding_bottom + border_top + border_bottom;
  183. cell.min_height = min_height + cell_intrinsic_height_offsets;
  184. cell.max_height = max(max(height, min_height), max_height) + cell_intrinsic_height_offsets;
  185. auto cell_intrinsic_width_offsets = padding_left + padding_right + border_left + border_right;
  186. cell.min_width = min_width + cell_intrinsic_width_offsets;
  187. cell.max_width = max(max(width, min_width), max_width) + cell_intrinsic_width_offsets;
  188. }
  189. }
  190. template<class RowOrColumn>
  191. void TableFormattingContext::compute_table_measures()
  192. {
  193. auto& rows_or_columns = table_rows_or_columns<RowOrColumn>();
  194. for (auto& cell : m_cells) {
  195. if (cell_span<RowOrColumn>(cell) == 1) {
  196. auto rc_index = cell_index<RowOrColumn>(cell);
  197. rows_or_columns[rc_index].min_size = max(rows_or_columns[rc_index].min_size, cell_min_size<RowOrColumn>(cell));
  198. rows_or_columns[rc_index].max_size = max(rows_or_columns[rc_index].max_size, cell_max_size<RowOrColumn>(cell));
  199. }
  200. }
  201. size_t max_cell_span = 1;
  202. for (auto& cell : m_cells) {
  203. max_cell_span = max(max_cell_span, cell_span<RowOrColumn>(cell));
  204. }
  205. for (size_t current_span = 2; current_span <= max_cell_span; current_span++) {
  206. // https://www.w3.org/TR/css-tables-3/#min-content-width-of-a-column-based-on-cells-of-span-up-to-n-n--1
  207. Vector<Vector<CSSPixels>> cell_min_contributions_by_rc_index;
  208. cell_min_contributions_by_rc_index.resize(rows_or_columns.size());
  209. // https://www.w3.org/TR/css-tables-3/#max-content-width-of-a-column-based-on-cells-of-span-up-to-n-n--1
  210. Vector<Vector<CSSPixels>> cell_max_contributions_by_rc_index;
  211. cell_max_contributions_by_rc_index.resize(rows_or_columns.size());
  212. for (auto& cell : m_cells) {
  213. auto cell_span_value = cell_span<RowOrColumn>(cell);
  214. if (cell_span_value == current_span) {
  215. // 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.
  216. CSSPixels baseline_max_content_size = 0;
  217. auto cell_start_rc_index = cell_index<RowOrColumn>(cell);
  218. auto cell_end_rc_index = cell_start_rc_index + cell_span_value;
  219. for (auto rc_index = cell_start_rc_index; rc_index < cell_end_rc_index; rc_index++) {
  220. baseline_max_content_size += rows_or_columns[rc_index].max_size;
  221. }
  222. // 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.
  223. auto baseline_border_spacing = border_spacing<RowOrColumn>() * (cell_span_value - 1);
  224. // Add contribution from all rows / columns, since we've weighted the gap to the desired spanned size by the the
  225. // 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.
  226. for (auto rc_index = cell_start_rc_index; rc_index < cell_end_rc_index; rc_index++) {
  227. // The contribution of the cell is the sum of:
  228. // the min-content size of the column based on cells of span up to N-1
  229. auto cell_min_contribution = rows_or_columns[rc_index].min_size;
  230. // and the product of:
  231. // - 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
  232. // - the outer min-content size of the cell minus the baseline max-content size and baseline border spacing, or 0 if this is negative
  233. cell_min_contribution += (rows_or_columns[rc_index].max_size / baseline_max_content_size)
  234. * max(CSSPixels(0), cell_min_size<RowOrColumn>(cell) - baseline_max_content_size - baseline_border_spacing);
  235. // The contribution of the cell is the sum of:
  236. // the max-content size of the column based on cells of span up to N-1
  237. auto cell_max_contribution = rows_or_columns[rc_index].max_size;
  238. // and the product of:
  239. // - 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
  240. // - 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
  241. cell_max_contribution += (rows_or_columns[rc_index].max_size / baseline_max_content_size)
  242. * max(CSSPixels(0), cell_max_size<RowOrColumn>(cell) - baseline_max_content_size - baseline_border_spacing);
  243. cell_min_contributions_by_rc_index[rc_index].append(cell_min_contribution);
  244. cell_max_contributions_by_rc_index[rc_index].append(cell_max_contribution);
  245. }
  246. }
  247. }
  248. for (size_t rc_index = 0; rc_index < rows_or_columns.size(); rc_index++) {
  249. // min-content size of a row / column based on cells of span up to N (N > 1) is
  250. // the largest of the min-content size of the row / column based on cells of span up to N-1 and
  251. // the contributions of the cells in the row / column whose rowSpan / colSpan is N
  252. for (auto min_contribution : cell_min_contributions_by_rc_index[rc_index])
  253. rows_or_columns[rc_index].min_size = max(rows_or_columns[rc_index].min_size, min_contribution);
  254. // max-content size of a row / column based on cells of span up to N (N > 1) is
  255. // the largest of the max-content size based on cells of span up to N-1 and the contributions of
  256. // the cells in the row / column whose rowSpan / colSpan is N
  257. for (auto max_contribution : cell_max_contributions_by_rc_index[rc_index])
  258. rows_or_columns[rc_index].max_size = max(rows_or_columns[rc_index].max_size, max_contribution);
  259. }
  260. }
  261. }
  262. CSSPixels TableFormattingContext::compute_capmin()
  263. {
  264. // The caption width minimum (CAPMIN) is the largest of the table captions min-content contribution:
  265. // https://drafts.csswg.org/css-tables-3/#computing-the-table-width
  266. CSSPixels capmin = 0;
  267. for (auto* child = table_box().first_child(); child; child = child->next_sibling()) {
  268. if (!child->display().is_table_caption()) {
  269. continue;
  270. }
  271. VERIFY(child->is_box());
  272. capmin = max(calculate_min_content_width(static_cast<Box const&>(*child)), capmin);
  273. }
  274. return capmin;
  275. }
  276. void TableFormattingContext::compute_table_width()
  277. {
  278. // https://drafts.csswg.org/css-tables-3/#computing-the-table-width
  279. auto& table_box_state = m_state.get_mutable(table_box());
  280. auto& computed_values = table_box().computed_values();
  281. CSSPixels width_of_table_containing_block = m_state.get(*table_box().containing_block()).content_width();
  282. // Percentages on 'width' and 'height' on the table are relative to the table wrapper box's containing block,
  283. // not the table wrapper box itself.
  284. CSSPixels width_of_table_wrapper_containing_block = m_state.get(*table_wrapper().containing_block()).content_width();
  285. // The row/column-grid width minimum (GRIDMIN) width is the sum of the min-content width
  286. // of all the columns plus cell spacing or borders.
  287. CSSPixels grid_min = 0.0f;
  288. for (auto& column : m_columns) {
  289. grid_min += column.min_size;
  290. }
  291. // The row/column-grid width maximum (GRIDMAX) width is the sum of the max-content width
  292. // of all the columns plus cell spacing or borders.
  293. CSSPixels grid_max = 0.0f;
  294. for (auto& column : m_columns) {
  295. grid_max += column.max_size;
  296. }
  297. // The used min-width of a table is the greater of the resolved min-width, CAPMIN, and GRIDMIN.
  298. auto used_min_width = max(grid_min, compute_capmin());
  299. if (!computed_values.min_width().is_auto()) {
  300. used_min_width = max(used_min_width, computed_values.min_width().to_px(table_box(), width_of_table_wrapper_containing_block));
  301. }
  302. CSSPixels used_width;
  303. if (computed_values.width().is_auto()) {
  304. // If the table-root has 'width: auto', the used width is the greater of
  305. // min(GRIDMAX, the table’s containing block width), the used min-width of the table.
  306. used_width = max(min(grid_max, width_of_table_containing_block), used_min_width);
  307. } else {
  308. // If the table-root’s width property has a computed value (resolving to
  309. // resolved-table-width) other than auto, the used width is the greater
  310. // of resolved-table-width, and the used min-width of the table.
  311. CSSPixels resolved_table_width = computed_values.width().to_px(table_box(), width_of_table_wrapper_containing_block);
  312. used_width = max(resolved_table_width, used_min_width);
  313. if (!should_treat_max_width_as_none(table_box(), m_available_space->width))
  314. used_width = min(used_width, computed_values.max_width().to_px(table_box(), width_of_table_wrapper_containing_block));
  315. }
  316. // Compute undistributable space due to border spacing: https://www.w3.org/TR/css-tables-3/#computing-undistributable-space.
  317. auto undistributable_space = (m_columns.size() + 1) * border_spacing_horizontal();
  318. table_box_state.set_content_width(used_width - table_box_state.border_left - table_box_state.border_right + undistributable_space);
  319. }
  320. void TableFormattingContext::distribute_width_to_columns()
  321. {
  322. // Implements https://www.w3.org/TR/css-tables-3/#width-distribution-algorithm
  323. // The total horizontal border spacing is defined for each table:
  324. // - 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
  325. // - Otherwise, 0
  326. auto total_horizontal_border_spacing = m_columns.is_empty() ? 0 : (m_columns.size() + 1) * border_spacing_horizontal();
  327. // The assignable table width is the used width of the table minus the total horizontal border spacing (if any).
  328. // This is the width that we will be able to allocate to the columns.
  329. CSSPixels available_width = m_state.get(table_box()).content_width() - total_horizontal_border_spacing;
  330. auto columns_total_used_width = [&]() {
  331. CSSPixels total_used_width = 0;
  332. for (auto& column : m_columns) {
  333. total_used_width += column.used_width;
  334. }
  335. return total_used_width;
  336. };
  337. auto column_preferred_width = [&](Column& column) {
  338. switch (column.type) {
  339. case SizeType::Percent: {
  340. return max(column.min_size, column.percentage_width / 100 * available_width);
  341. }
  342. case SizeType::Pixel:
  343. case SizeType::Auto: {
  344. return column.max_size;
  345. }
  346. default: {
  347. VERIFY_NOT_REACHED();
  348. }
  349. }
  350. };
  351. auto expand_columns_to_fill_available_width = [&](SizeType column_type) {
  352. CSSPixels remaining_available_width = available_width;
  353. CSSPixels total_preferred_width_increment = 0;
  354. for (auto& column : m_columns) {
  355. remaining_available_width -= column.used_width;
  356. if (column.type == column_type) {
  357. total_preferred_width_increment += column_preferred_width(column) - column.min_size;
  358. }
  359. }
  360. if (total_preferred_width_increment == 0)
  361. return;
  362. for (auto& column : m_columns) {
  363. if (column.type == column_type) {
  364. CSSPixels preferred_width_increment = column_preferred_width(column) - column.min_size;
  365. column.used_width += preferred_width_increment * remaining_available_width / total_preferred_width_increment;
  366. }
  367. }
  368. };
  369. auto shrink_columns_to_fit_available_width = [&](SizeType column_type) {
  370. for (auto& column : m_columns) {
  371. if (column.type == column_type)
  372. column.used_width = column.min_size;
  373. }
  374. expand_columns_to_fill_available_width(column_type);
  375. };
  376. for (auto& column : m_columns) {
  377. column.used_width = column.min_size;
  378. }
  379. for (auto& column : m_columns) {
  380. if (column.type == SizeType::Percent) {
  381. column.used_width = max(column.min_size, column.percentage_width / 100 * available_width);
  382. }
  383. }
  384. if (columns_total_used_width() > available_width) {
  385. shrink_columns_to_fit_available_width(SizeType::Percent);
  386. return;
  387. }
  388. for (auto& column : m_columns) {
  389. if (column.type == SizeType::Pixel) {
  390. column.used_width = column.max_size;
  391. }
  392. }
  393. if (columns_total_used_width() > available_width) {
  394. shrink_columns_to_fit_available_width(SizeType::Pixel);
  395. return;
  396. }
  397. if (columns_total_used_width() < available_width) {
  398. expand_columns_to_fill_available_width(SizeType::Auto);
  399. }
  400. if (columns_total_used_width() < available_width) {
  401. expand_columns_to_fill_available_width(SizeType::Pixel);
  402. }
  403. if (columns_total_used_width() < available_width) {
  404. expand_columns_to_fill_available_width(SizeType::Percent);
  405. }
  406. if (columns_total_used_width() < available_width) {
  407. // NOTE: if all columns got their max width and there is still width to distribute left
  408. // it should be assigned to columns proportionally to their max width
  409. CSSPixels grid_max = 0.0f;
  410. for (auto& column : m_columns) {
  411. grid_max += column.max_size;
  412. }
  413. auto width_to_distribute = available_width - columns_total_used_width();
  414. if (grid_max == 0) {
  415. // If total max width of columns is zero then divide distributable width equally among them
  416. auto column_width = width_to_distribute / m_columns.size();
  417. for (auto& column : m_columns) {
  418. column.used_width = column_width;
  419. }
  420. } else {
  421. // Distribute width to columns proportionally to their max width
  422. for (auto& column : m_columns) {
  423. column.used_width += width_to_distribute * column.max_size / grid_max;
  424. }
  425. }
  426. }
  427. }
  428. void TableFormattingContext::determine_intrisic_size_of_table_container(AvailableSpace const& available_space)
  429. {
  430. auto& table_box_state = m_state.get_mutable(table_box());
  431. if (available_space.width.is_min_content()) {
  432. // The min-content width of a table is the width required to fit all of its columns min-content widths and its undistributable spaces.
  433. CSSPixels grid_min = 0.0f;
  434. for (auto& column : m_columns)
  435. grid_min += column.min_size;
  436. table_box_state.set_content_width(grid_min);
  437. }
  438. if (available_space.width.is_max_content()) {
  439. // The max-content width of a table is the width required to fit all of its columns max-content widths and its undistributable spaces.
  440. CSSPixels grid_max = 0.0f;
  441. for (auto& column : m_columns)
  442. grid_max += column.max_size;
  443. table_box_state.set_content_width(grid_max);
  444. }
  445. }
  446. void TableFormattingContext::compute_table_height(LayoutMode layout_mode)
  447. {
  448. // First pass of row height calculation:
  449. for (auto& row : m_rows) {
  450. auto row_computed_height = row.box->computed_values().height();
  451. if (row_computed_height.is_length()) {
  452. auto height_of_containing_block = m_state.get(*row.box->containing_block()).content_height();
  453. auto row_used_height = row_computed_height.to_px(row.box, height_of_containing_block);
  454. row.base_height = max(row.base_height, row_used_height);
  455. }
  456. }
  457. // First pass of cells layout:
  458. for (auto& cell : m_cells) {
  459. auto& row = m_rows[cell.row_index];
  460. auto& cell_state = m_state.get_mutable(cell.box);
  461. CSSPixels span_width = 0;
  462. for (size_t i = 0; i < cell.column_span; ++i)
  463. span_width += m_columns[cell.column_index + i].used_width;
  464. auto width_of_containing_block = m_state.get(*cell.box->containing_block()).content_width();
  465. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  466. auto height_of_containing_block = m_state.get(*cell.box->containing_block()).content_height();
  467. auto height_of_containing_block_as_length = CSS::Length::make_px(height_of_containing_block);
  468. cell_state.padding_top = cell.box->computed_values().padding().top().to_px(cell.box, width_of_containing_block);
  469. cell_state.padding_bottom = cell.box->computed_values().padding().bottom().to_px(cell.box, width_of_containing_block);
  470. cell_state.padding_left = cell.box->computed_values().padding().left().to_px(cell.box, width_of_containing_block);
  471. cell_state.padding_right = cell.box->computed_values().padding().right().to_px(cell.box, width_of_containing_block);
  472. if (cell.box->computed_values().border_collapse() == CSS::BorderCollapse::Separate) {
  473. cell_state.border_top = cell.box->computed_values().border_top().width;
  474. cell_state.border_bottom = cell.box->computed_values().border_bottom().width;
  475. cell_state.border_left = cell.box->computed_values().border_left().width;
  476. cell_state.border_right = cell.box->computed_values().border_right().width;
  477. }
  478. auto cell_computed_height = cell.box->computed_values().height();
  479. if (cell_computed_height.is_length()) {
  480. auto cell_used_height = cell_computed_height.to_px(cell.box, height_of_containing_block);
  481. cell_state.set_content_height(cell_used_height - cell_state.border_box_top() - cell_state.border_box_bottom());
  482. row.base_height = max(row.base_height, cell_used_height);
  483. }
  484. // Compute cell width as specified by https://www.w3.org/TR/css-tables-3/#bounding-box-assignment:
  485. // 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:
  486. // - the widths/heights of all spanned visible columns/rows
  487. // - the horizontal/vertical border-spacing times the amount of spanned visible columns/rows minus one
  488. // FIXME: Account for visibility.
  489. cell_state.set_content_width(span_width - cell_state.border_box_left() - cell_state.border_box_right() + (cell.column_span - 1) * border_spacing_horizontal());
  490. if (auto independent_formatting_context = layout_inside(cell.box, layout_mode, cell_state.available_inner_space_or_constraints_from(*m_available_space))) {
  491. cell_state.set_content_height(independent_formatting_context->automatic_content_height());
  492. independent_formatting_context->parent_context_did_dimension_child_root_box();
  493. }
  494. cell.baseline = box_baseline(cell.box);
  495. // Implements https://www.w3.org/TR/css-tables-3/#computing-the-table-height
  496. // The minimum height of a row is the maximum of:
  497. // - the computed height (if definite, percentages being considered 0px) of its corresponding table-row (if nay)
  498. // - the computed height of each cell spanning the current row exclusively (if definite, percentages being treated as 0px), and
  499. // - the minimum height (ROWMIN) required by the cells spanning the row.
  500. // Note that we've already applied the first rule at the top of the method.
  501. if (cell.row_span == 1) {
  502. row.base_height = max(row.base_height, cell_state.border_box_height());
  503. }
  504. row.base_height = max(row.base_height, m_rows[cell.row_index].min_size);
  505. row.baseline = max(row.baseline, cell.baseline);
  506. }
  507. CSSPixels sum_rows_height = 0;
  508. for (auto& row : m_rows) {
  509. sum_rows_height += row.base_height;
  510. }
  511. m_table_height = sum_rows_height;
  512. if (!table_box().computed_values().height().is_auto()) {
  513. // If the table has a height property with a value other than auto, it is treated as a minimum height for the
  514. // table grid, and will eventually be distributed to the height of the rows if their collective minimum height
  515. // ends up smaller than this number.
  516. CSSPixels height_of_table_containing_block = m_state.get(*table_wrapper().containing_block()).content_height();
  517. auto specified_table_height = table_box().computed_values().height().to_px(table_box(), height_of_table_containing_block);
  518. if (m_table_height < specified_table_height) {
  519. m_table_height = specified_table_height;
  520. }
  521. }
  522. for (auto& row : m_rows) {
  523. // Reference size is the largest of
  524. // - its initial base height and
  525. // - its new base height (the one evaluated during the second layout pass, where percentages used in
  526. // rowgroups/rows/cells' specified heights were resolved according to the table height, instead of
  527. // being ignored as 0px).
  528. // Assign reference size to base size. Later reference size might change to largee value during
  529. // second pass of rows layout.
  530. row.reference_height = row.base_height;
  531. }
  532. // Second pass of rows height calculation:
  533. // At this point percentage row height can be resolved because final table height is calculated.
  534. for (auto& row : m_rows) {
  535. auto row_computed_height = row.box->computed_values().height();
  536. if (row_computed_height.is_percentage()) {
  537. auto row_used_height = row_computed_height.to_px(row.box, m_table_height);
  538. row.reference_height = max(row.reference_height, row_used_height);
  539. } else {
  540. continue;
  541. }
  542. }
  543. // Second pass cells layout:
  544. // At this point percantage cell height can be resolved because final table heigh is calculated.
  545. for (auto& cell : m_cells) {
  546. auto& row = m_rows[cell.row_index];
  547. auto& cell_state = m_state.get_mutable(cell.box);
  548. CSSPixels span_width = 0;
  549. for (size_t i = 0; i < cell.column_span; ++i)
  550. span_width += m_columns[cell.column_index + i].used_width;
  551. auto cell_computed_height = cell.box->computed_values().height();
  552. if (cell_computed_height.is_percentage()) {
  553. auto cell_used_height = cell_computed_height.to_px(cell.box, m_table_height);
  554. cell_state.set_content_height(cell_used_height - cell_state.border_box_top() - cell_state.border_box_bottom());
  555. row.reference_height = max(row.reference_height, cell_used_height);
  556. } else {
  557. continue;
  558. }
  559. cell_state.set_content_width(span_width - cell_state.border_box_left() - cell_state.border_box_right() + (cell.column_span - 1) * border_spacing_horizontal());
  560. if (auto independent_formatting_context = layout_inside(cell.box, layout_mode, cell_state.available_inner_space_or_constraints_from(*m_available_space))) {
  561. independent_formatting_context->parent_context_did_dimension_child_root_box();
  562. }
  563. cell.baseline = box_baseline(cell.box);
  564. row.reference_height = max(row.reference_height, cell_state.border_box_height());
  565. row.baseline = max(row.baseline, cell.baseline);
  566. }
  567. }
  568. void TableFormattingContext::distribute_height_to_rows()
  569. {
  570. CSSPixels sum_reference_height = 0;
  571. for (auto& row : m_rows) {
  572. sum_reference_height += row.reference_height;
  573. }
  574. if (sum_reference_height == 0)
  575. return;
  576. Vector<Row&> rows_with_auto_height;
  577. for (auto& row : m_rows) {
  578. if (row.box->computed_values().height().is_auto()) {
  579. rows_with_auto_height.append(row);
  580. }
  581. }
  582. if (m_table_height <= sum_reference_height) {
  583. // If the table height is equal or smaller than sum of reference sizes, the final height assigned to each row
  584. // will be the weighted mean of the base and the reference size that yields the correct total height.
  585. for (auto& row : m_rows) {
  586. auto weight = row.reference_height / sum_reference_height;
  587. auto final_height = m_table_height * weight;
  588. row.final_height = final_height;
  589. }
  590. } else if (rows_with_auto_height.size() > 0) {
  591. // Else, if the table owns any “auto-height” row (a row whose size is only determined by its content size and
  592. // none of the specified heights), each non-auto-height row receives its reference height and auto-height rows
  593. // receive their reference size plus some increment which is equal to the height missing to amount to the
  594. // specified table height divided by the amount of such rows.
  595. for (auto& row : m_rows) {
  596. row.final_height = row.reference_height;
  597. }
  598. auto auto_height_rows_increment = (m_table_height - sum_reference_height) / rows_with_auto_height.size();
  599. for (auto& row : rows_with_auto_height) {
  600. row.final_height += auto_height_rows_increment;
  601. }
  602. } else {
  603. // Else, all rows receive their reference size plus some increment which is equal to the height missing to
  604. // amount to the specified table height divided by the amount of rows.
  605. auto increment = (m_table_height - sum_reference_height) / m_rows.size();
  606. for (auto& row : m_rows) {
  607. row.final_height = row.reference_height + increment;
  608. }
  609. }
  610. }
  611. void TableFormattingContext::position_row_boxes(CSSPixels& total_content_height)
  612. {
  613. auto const& table_state = m_state.get(table_box());
  614. CSSPixels row_top_offset = table_state.offset.y() + table_state.padding_top + border_spacing_vertical();
  615. CSSPixels row_left_offset = table_state.border_left + table_state.padding_left + border_spacing_horizontal();
  616. for (size_t y = 0; y < m_rows.size(); y++) {
  617. auto& row = m_rows[y];
  618. auto& row_state = m_state.get_mutable(row.box);
  619. CSSPixels row_width = 0.0f;
  620. for (auto& column : m_columns) {
  621. row_width += column.used_width;
  622. }
  623. row_state.set_content_height(row.final_height);
  624. row_state.set_content_width(row_width);
  625. row_state.set_content_x(row_left_offset);
  626. row_state.set_content_y(row_top_offset);
  627. row_top_offset += row_state.content_height();
  628. }
  629. CSSPixels row_group_top_offset = table_state.border_top + table_state.padding_top;
  630. CSSPixels row_group_left_offset = table_state.border_left + table_state.padding_left;
  631. for_each_child_box_matching(table_box(), is_table_row_group, [&](auto& row_group_box) {
  632. CSSPixels row_group_height = 0.0f;
  633. CSSPixels row_group_width = 0.0f;
  634. auto& row_group_box_state = m_state.get_mutable(row_group_box);
  635. row_group_box_state.set_content_x(row_group_left_offset);
  636. row_group_box_state.set_content_y(row_group_top_offset);
  637. for_each_child_box_matching(row_group_box, is_table_row, [&](auto& row) {
  638. auto const& row_state = m_state.get(row);
  639. row_group_height += row_state.border_box_height();
  640. row_group_width = max(row_group_width, row_state.border_box_width());
  641. });
  642. row_group_box_state.set_content_height(row_group_height);
  643. row_group_box_state.set_content_width(row_group_width);
  644. row_group_top_offset += row_group_height;
  645. });
  646. total_content_height = max(row_top_offset, row_group_top_offset) - table_state.offset.y() - table_state.padding_top;
  647. // The height of a table is the sum of the row heights plus any cell spacing or borders.
  648. // Note that we've already added one vertical border-spacing to row_top_offset, so it's sufficient to multiply it by row count here.
  649. total_content_height += m_rows.size() * border_spacing_vertical();
  650. }
  651. void TableFormattingContext::position_cell_boxes()
  652. {
  653. CSSPixels left_column_offset = 0;
  654. for (auto& column : m_columns) {
  655. column.left_offset = left_column_offset;
  656. left_column_offset += column.used_width;
  657. }
  658. for (auto& cell : m_cells) {
  659. auto& cell_state = m_state.get_mutable(cell.box);
  660. auto& row_state = m_state.get(m_rows[cell.row_index].box);
  661. CSSPixels const cell_border_box_height = cell_state.content_height() + cell_state.border_box_top() + cell_state.border_box_bottom();
  662. CSSPixels const row_content_height = compute_row_content_height(cell);
  663. auto const& vertical_align = cell.box->computed_values().vertical_align();
  664. if (vertical_align.has<CSS::VerticalAlign>()) {
  665. switch (vertical_align.get<CSS::VerticalAlign>()) {
  666. case CSS::VerticalAlign::Middle: {
  667. cell_state.padding_top += (row_content_height - cell_border_box_height) / 2;
  668. cell_state.padding_bottom += (row_content_height - cell_border_box_height) / 2;
  669. break;
  670. }
  671. // FIXME: implement actual 'top' and 'bottom' support instead of fall back to 'baseline'
  672. case CSS::VerticalAlign::Top:
  673. case CSS::VerticalAlign::Bottom:
  674. case CSS::VerticalAlign::Baseline: {
  675. cell_state.padding_top += m_rows[cell.row_index].baseline - cell.baseline;
  676. cell_state.padding_bottom += row_content_height - cell_border_box_height;
  677. break;
  678. }
  679. default:
  680. VERIFY_NOT_REACHED();
  681. }
  682. }
  683. // Compute cell position as specified by https://www.w3.org/TR/css-tables-3/#bounding-box-assignment:
  684. // left/top location is the sum of:
  685. // - for top: the height reserved for top captions (including margins), if any
  686. // - the padding-left/padding-top and border-left-width/border-top-width of the table
  687. // FIXME: Account for visibility.
  688. cell_state.offset = row_state.offset.translated(
  689. cell_state.border_box_left() + m_columns[cell.column_index].left_offset + cell.column_index * border_spacing_horizontal(),
  690. cell_state.border_box_top() + cell.row_index * border_spacing_vertical());
  691. }
  692. }
  693. static const CSS::BorderData& winning_border_style(const CSS::BorderData& a, const CSS::BorderData& b)
  694. {
  695. // Implements steps 1, 2 and 3 of border conflict resolution algorithm.
  696. static HashMap<CSS::LineStyle, unsigned> const line_style_score = {
  697. { CSS::LineStyle::Inset, 0 },
  698. { CSS::LineStyle::Groove, 1 },
  699. { CSS::LineStyle::Outset, 2 },
  700. { CSS::LineStyle::Ridge, 3 },
  701. { CSS::LineStyle::Dotted, 4 },
  702. { CSS::LineStyle::Dashed, 5 },
  703. { CSS::LineStyle::Solid, 6 },
  704. { CSS::LineStyle::Double, 7 },
  705. };
  706. if (a.line_style == CSS::LineStyle::Hidden) {
  707. return a;
  708. }
  709. if (b.line_style == CSS::LineStyle::Hidden) {
  710. return b;
  711. }
  712. if (a.line_style == CSS::LineStyle::None) {
  713. return b;
  714. }
  715. if (b.line_style == CSS::LineStyle::None) {
  716. return a;
  717. }
  718. if (a.width > b.width) {
  719. return a;
  720. } else if (a.width < b.width) {
  721. return b;
  722. }
  723. if (*line_style_score.get(a.line_style) > *line_style_score.get(b.line_style)) {
  724. return a;
  725. } else if (*line_style_score.get(a.line_style) < *line_style_score.get(b.line_style)) {
  726. return b;
  727. }
  728. return a;
  729. }
  730. void TableFormattingContext::border_conflict_resolution()
  731. {
  732. // Partially implements border conflict resolution, as described in
  733. // https://www.w3.org/TR/CSS22/tables.html#border-conflict-resolution
  734. BorderConflictFinder finder(this);
  735. for (auto& cell : m_cells) {
  736. if (cell.box->computed_values().border_collapse() == CSS::BorderCollapse::Separate) {
  737. continue;
  738. }
  739. // Execute steps 1, 2 and 3 of the algorithm for each edge.
  740. Painting::BordersData override_borders_data;
  741. auto const& cell_style = cell.box->computed_values();
  742. auto& cell_state = m_state.get_mutable(cell.box);
  743. for (auto const conflicting_element : finder.conflicting_elements(cell, ConflictingEdge::Left)) {
  744. auto const& other_style = conflicting_element->computed_values();
  745. override_borders_data.left = winning_border_style(cell_style.border_left(), other_style.border_left());
  746. cell_state.border_left = override_borders_data.left.width;
  747. }
  748. for (auto const conflicting_element : finder.conflicting_elements(cell, ConflictingEdge::Right)) {
  749. auto const& other_style = conflicting_element->computed_values();
  750. override_borders_data.right = winning_border_style(cell_style.border_right(), other_style.border_right());
  751. cell_state.border_right = override_borders_data.right.width;
  752. }
  753. for (auto const conflicting_element : finder.conflicting_elements(cell, ConflictingEdge::Top)) {
  754. auto const& other_style = conflicting_element->computed_values();
  755. override_borders_data.top = winning_border_style(cell_style.border_top(), other_style.border_top());
  756. cell_state.border_top = override_borders_data.top.width;
  757. }
  758. for (auto const conflicting_element : finder.conflicting_elements(cell, ConflictingEdge::Bottom)) {
  759. auto const& other_style = conflicting_element->computed_values();
  760. override_borders_data.bottom = winning_border_style(cell_style.border_bottom(), other_style.border_bottom());
  761. cell_state.border_bottom = override_borders_data.bottom.width;
  762. }
  763. // FIXME: 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
  764. // row group, column, column group and, lastly, table. When two elements of the same type conflict, then the one
  765. // further to the left (if the table's 'direction' is 'ltr'; right, if it is 'rtl') and further to the top wins.
  766. cell_state.set_override_borders_data(override_borders_data);
  767. }
  768. }
  769. CSSPixels TableFormattingContext::compute_row_content_height(Cell const& cell) const
  770. {
  771. auto& row_state = m_state.get(m_rows[cell.row_index].box);
  772. if (cell.row_span == 1) {
  773. return row_state.content_height();
  774. }
  775. // The height of a cell is the sum of all spanned rows, as described in
  776. // https://www.w3.org/TR/css-tables-3/#bounding-box-assignment
  777. // When the row span is greater than 1, the borders of inner rows within the span have to be
  778. // included in the content height of the spanning cell. First top and final bottom borders are
  779. // excluded to be consistent with the handling of row span 1 case above, which uses the content
  780. // height (no top and bottom borders) of the row.
  781. CSSPixels span_height = 0;
  782. for (size_t i = 0; i < cell.row_span; ++i) {
  783. auto const& row_state = m_state.get(m_rows[cell.row_index + i].box);
  784. if (i == 0) {
  785. span_height += row_state.content_height() + row_state.border_box_bottom();
  786. } else if (i == cell.row_span - 1) {
  787. span_height += row_state.border_box_top() + row_state.content_height();
  788. } else {
  789. span_height += row_state.border_box_height();
  790. }
  791. }
  792. // Compute cell height as specified by https://www.w3.org/TR/css-tables-3/#bounding-box-assignment:
  793. // width/height is the sum of:
  794. // - the widths/heights of all spanned visible columns/rows
  795. // - the horizontal/vertical border-spacing times the amount of spanned visible columns/rows minus one
  796. // FIXME: Account for visibility.
  797. span_height += (cell.row_span - 1) * border_spacing_vertical();
  798. return span_height;
  799. }
  800. TableFormattingContext::BorderConflictFinder::BorderConflictFinder(TableFormattingContext const* context)
  801. : m_context(context)
  802. {
  803. collect_conflicting_col_elements();
  804. }
  805. void TableFormattingContext::BorderConflictFinder::collect_conflicting_col_elements()
  806. {
  807. m_col_elements_by_index.resize(m_context->m_columns.size());
  808. for (auto* child = m_context->table_box().first_child(); child; child = child->next_sibling()) {
  809. if (!child->display().is_table_column_group()) {
  810. continue;
  811. }
  812. size_t column_index = 0;
  813. for (auto* child_of_column_group = child->first_child(); child_of_column_group; child_of_column_group = child_of_column_group->next_sibling()) {
  814. VERIFY(child_of_column_group->display().is_table_column());
  815. auto const& col_node = static_cast<HTML::HTMLTableColElement const&>(*child_of_column_group->dom_node());
  816. unsigned span = col_node.attribute(HTML::AttributeNames::span).to_uint().value_or(1);
  817. for (size_t i = column_index; i < column_index + span; ++i) {
  818. m_col_elements_by_index[i] = child_of_column_group;
  819. }
  820. column_index += span;
  821. }
  822. }
  823. }
  824. Vector<Node const*> TableFormattingContext::BorderConflictFinder::conflicting_elements(Cell const& cell, TableFormattingContext::ConflictingEdge edge) const
  825. {
  826. // FIXME: Conflicting elements can be cells, rows, row groups, columns, column groups, and the table itself,
  827. // but we only consider 'col' elements in a 'colgroup', the table and the cell itself for now.
  828. Vector<Node const*> result = { cell.box };
  829. auto is_top = cell.row_index == 0;
  830. auto is_left = cell.column_index == 0;
  831. auto is_right = cell.column_index == m_context->m_columns.size() - 1;
  832. auto is_bottom = cell.row_index == m_context->m_rows.size() - 1;
  833. auto conflicts_top_or_bottom = (is_top && edge == ConflictingEdge::Top) || (is_bottom && edge == ConflictingEdge::Bottom);
  834. if (conflicts_top_or_bottom || (is_left && edge == ConflictingEdge::Left) || (is_right && edge == ConflictingEdge::Right)) {
  835. result.append(&m_context->table_box());
  836. }
  837. if (m_col_elements_by_index[cell.column_index] && (conflicts_top_or_bottom || edge == ConflictingEdge::Left || edge == ConflictingEdge::Right)) {
  838. result.append(m_col_elements_by_index[cell.column_index]);
  839. }
  840. return result;
  841. }
  842. void TableFormattingContext::run(Box const& box, LayoutMode layout_mode, AvailableSpace const& available_space)
  843. {
  844. m_available_space = available_space;
  845. auto total_captions_height = run_caption_layout(layout_mode, CSS::CaptionSide::Top);
  846. CSSPixels total_content_height = 0;
  847. // Determine the number of rows/columns the table requires.
  848. calculate_row_column_grid(box);
  849. border_conflict_resolution();
  850. // Compute the minimum width of each column.
  851. compute_cell_measures(available_space);
  852. compute_table_measures<Column>();
  853. // https://www.w3.org/TR/css-tables-3/#row-layout
  854. // Since during row layout the specified heights of cells in the row were ignored and cells that were spanning more than one rows
  855. // have not been sized correctly, their height will need to be eventually distributed to the set of rows they spanned. This is done
  856. // by running the same algorithm as the column measurement, with the span=1 value being initialized (for min-content) with the largest
  857. // of the resulting height of the previous row layout, the height specified on the corresponding table-row (if any), and the largest
  858. // height specified on cells that span this row only (the algorithm starts by considering cells of span 2 on top of that assignment).
  859. compute_table_measures<Row>();
  860. if (available_space.width.is_intrinsic_sizing_constraint() && !available_space.height.is_intrinsic_sizing_constraint()) {
  861. determine_intrisic_size_of_table_container(available_space);
  862. return;
  863. }
  864. // Compute the width of the table.
  865. compute_table_width();
  866. // Distribute the width of the table among columns.
  867. distribute_width_to_columns();
  868. compute_table_height(layout_mode);
  869. distribute_height_to_rows();
  870. position_row_boxes(total_content_height);
  871. position_cell_boxes();
  872. m_state.get_mutable(table_box()).set_content_height(total_content_height);
  873. total_captions_height += run_caption_layout(layout_mode, CSS::CaptionSide::Bottom);
  874. // Table captions are positioned between the table margins and its borders (outside the grid box borders) as described in
  875. // https://www.w3.org/TR/css-tables-3/#bounding-box-assignment
  876. // A visual representation of this model can be found at https://www.w3.org/TR/css-tables-3/images/table_container.png
  877. m_state.get_mutable(table_box()).margin_bottom += total_captions_height;
  878. // FIXME: This is a total hack, we should respect the 'height' property.
  879. m_automatic_content_height = total_content_height;
  880. }
  881. CSSPixels TableFormattingContext::automatic_content_width() const
  882. {
  883. return greatest_child_width(context_box());
  884. }
  885. CSSPixels TableFormattingContext::automatic_content_height() const
  886. {
  887. return m_automatic_content_height;
  888. }
  889. template<>
  890. size_t TableFormattingContext::cell_span<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  891. {
  892. return cell.row_span;
  893. }
  894. template<>
  895. size_t TableFormattingContext::cell_span<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  896. {
  897. return cell.column_span;
  898. }
  899. template<>
  900. size_t TableFormattingContext::cell_index<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  901. {
  902. return cell.row_index;
  903. }
  904. template<>
  905. size_t TableFormattingContext::cell_index<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  906. {
  907. return cell.column_index;
  908. }
  909. template<>
  910. CSSPixels TableFormattingContext::cell_min_size<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  911. {
  912. return cell.min_height;
  913. }
  914. template<>
  915. CSSPixels TableFormattingContext::cell_min_size<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  916. {
  917. return cell.min_width;
  918. }
  919. template<>
  920. CSSPixels TableFormattingContext::cell_max_size<TableFormattingContext::Row>(TableFormattingContext::Cell const& cell)
  921. {
  922. return cell.max_height;
  923. }
  924. template<>
  925. CSSPixels TableFormattingContext::cell_max_size<TableFormattingContext::Column>(TableFormattingContext::Cell const& cell)
  926. {
  927. return cell.max_width;
  928. }
  929. template<>
  930. CSSPixels TableFormattingContext::border_spacing<TableFormattingContext::Row>()
  931. {
  932. return border_spacing_vertical();
  933. }
  934. template<>
  935. CSSPixels TableFormattingContext::border_spacing<TableFormattingContext::Column>()
  936. {
  937. return border_spacing_horizontal();
  938. }
  939. CSSPixels TableFormattingContext::border_spacing_horizontal() const
  940. {
  941. auto const& computed_values = table_box().computed_values();
  942. // 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):
  943. // https://www.w3.org/TR/css-tables-3/#collapsed-style-overrides
  944. if (computed_values.border_collapse() == CSS::BorderCollapse::Collapse)
  945. return 0;
  946. return computed_values.border_spacing_horizontal().to_px(table_box());
  947. }
  948. CSSPixels TableFormattingContext::border_spacing_vertical() const
  949. {
  950. auto const& computed_values = table_box().computed_values();
  951. // 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):
  952. // https://www.w3.org/TR/css-tables-3/#collapsed-style-overrides
  953. if (computed_values.border_collapse() == CSS::BorderCollapse::Collapse)
  954. return 0;
  955. return computed_values.border_spacing_vertical().to_px(table_box());
  956. }
  957. template<>
  958. Vector<TableFormattingContext::Row>& TableFormattingContext::table_rows_or_columns()
  959. {
  960. return m_rows;
  961. }
  962. template<>
  963. Vector<TableFormattingContext::Column>& TableFormattingContext::table_rows_or_columns()
  964. {
  965. return m_columns;
  966. }
  967. }