TableFormattingContext.cpp 62 KB

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