Skip to content

Truly independent web browser

LibWeb: Lay out the columns of right-to-left tables from the right

sideshowbarker119a38e43308
diff --git a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
index 8c981edb9e7..1182d1fd10d 100644
--- a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
@@ -1342,6 +1342,25 @@ impl<'pass> TableFormattingContext<'pass> {
ranges
}
+ /// Whether the columns of the table run from right to left: "Column boxes are placed next to each other in the
+ /// order they occur. The first column box may be either on the left or on the right, depending on the value of
+ /// the 'direction' property of the table."
+ /// https://www.w3.org/TR/CSS22/tables.html#table-layout
+ /// The columns are counted in the table's column order, from its first column, so only where they are placed
+ /// do they count from the left (see `physical_columns`).
+ fn is_right_to_left(&self) -> bool {
+ self.style(self.run.box_).direction() == direction::RTL
+ }
+
+ /// The columns `columns` of the table's column order, as physical columns counted from the left.
+ fn physical_columns(&self, columns: Range<usize>) -> Range<usize> {
+ if !self.is_right_to_left() {
+ return columns;
+ }
+ let column_count = self.columns.len();
+ column_count - columns.end..column_count - columns.start
+ }
+
/// The box whose style gives the width specified for the columns of a range, which is also the one that makes
/// them constrained and gives them a percentage contribution.
fn column_sizing_box(&self, range: ColumnTrackRange) -> Node {
@@ -1492,18 +1511,20 @@ impl<'pass> TableFormattingContext<'pass> {
next_source_order += 1;
source_order
};
- // Cells, column by column so that on ties the cell further to the left, then further to the
- // top, wins. TableCell spans are already clipped to the table end by TableGrid.
+ // Cells, column by column in the table's column order, so that on ties the cell further to the left (to the
+ // right, in a right-to-left table), then further to the top, wins. TableCell spans are already clipped to the
+ // table end by TableGrid. The border grid counts its columns from the left.
let mut cells = self.cells.clone();
cells.sort_by_key(|cell| (cell.column_index, cell.row_index));
for cell in cells {
let source_order = take_source_order();
+ let columns = self.physical_columns(cell.column_index..cell.column_index + cell.column_span);
if cell.row_span > 1 || cell.column_span > 1 {
grid.hide_segments_inside_span(
cell.row_index,
cell.row_index + cell.row_span,
- cell.column_index,
- cell.column_index + cell.column_span,
+ columns.start,
+ columns.end,
source_order,
);
}
@@ -1512,8 +1533,8 @@ impl<'pass> TableFormattingContext<'pass> {
borders,
cell.row_index,
cell.row_index + cell.row_span,
- cell.column_index,
- cell.column_index + cell.column_span,
+ columns.start,
+ columns.end,
source_order,
);
}
@@ -1559,14 +1580,18 @@ impl<'pass> TableFormattingContext<'pass> {
}
let borders = self.element_borders(range.box_);
let source_order = take_source_order();
+ // A column spanning several columns applies its borders to them in the table's column order, so that where two
+ // of them tie, the one further to the left (to the right, in a right-to-left table) wins.
for column_index in range.start..range.end {
- grid.apply_borders(borders, 0, row_count, column_index, column_index + 1, source_order);
+ let column = self.physical_columns(column_index..column_index + 1).start;
+ grid.apply_borders(borders, 0, row_count, column, column + 1, source_order);
}
}
for (column_group, group_start, group_end) in column_group_ranges {
if group_start < group_end {
let borders = self.element_borders(column_group);
- grid.apply_borders(borders, 0, row_count, group_start, group_end, take_source_order());
+ let columns = self.physical_columns(group_start..group_end);
+ grid.apply_borders(borders, 0, row_count, columns.start, columns.end, take_source_order());
}
}
let table_borders = self.element_borders(self.run.box_);
@@ -1607,8 +1632,8 @@ impl<'pass> TableFormattingContext<'pass> {
continue;
}
let row_end = cell.row_index + cell.row_span;
- let column_end = cell.column_index + cell.column_span;
- let widths = grid.resolve_used_widths_for_cell(cell.row_index, row_end, cell.column_index, column_end);
+ let columns = self.physical_columns(cell.column_index..cell.column_index + cell.column_span);
+ let widths = grid.resolve_used_widths_for_cell(cell.row_index, row_end, columns.start, columns.end);
let cell_box = &mut self.cell_layouts[cell_index].box_cells;
cell_box.border_top = widths.top;
cell_box.border_right = widths.right;
@@ -3703,7 +3728,11 @@ impl<'pass> TableFormattingContext<'pass> {
// - for top: the height reserved for top captions (including margins), if any
// - the padding-left/padding-top and border-left-width/border-top-width of the table
// - the border spacing after each visible column before the cell (in its column's offset)
- let x = row_content_offset.x + cell_box.border_box_left(collapsed) + self.column_offsets[cell.column_index];
+ let x = row_content_offset.x
+ + cell_box.border_box_left(collapsed)
+ + self.column_offsets[self
+ .physical_columns(cell.column_index..cell.column_index + cell.column_span)
+ .start];
let y = row_content_offset.y + cell_box.border_box_top(collapsed);
// A relatively positioned cell is offset from where the table places it, as any in-flow box is.
let insets = abspos_engine::compute_inset_native(
@@ -3788,7 +3817,8 @@ impl<'pass> TableFormattingContext<'pass> {
debug_assert_eq!(placements.len(), self.column_group_and_column_boxes.len());
for (placement_index, (node, group_placement, start, end)) in placements.into_iter().enumerate() {
let inline_size = if (start..end).any(|index| !self.columns[index].is_collapsed) {
- column_offsets[end] - column_offsets[start] - inline_spacing
+ let columns = self.physical_columns(start..end);
+ column_offsets[columns.end] - column_offsets[columns.start] - inline_spacing
} else {
CssPixels::default()
};
@@ -3808,7 +3838,7 @@ impl<'pass> TableFormattingContext<'pass> {
let used = &column_box.cells;
used.set_content_inline_size(inline_size);
used.set_content_block_size(block_size);
- let mut x = inline_offset + column_offsets[start];
+ let mut x = inline_offset + column_offsets[self.physical_columns(start..end).start];
let mut y = block_start;
if let Some((group, group_box)) =
group_placement.map(|group_placement| &self.column_group_and_column_boxes[group_placement])
@@ -3900,13 +3930,19 @@ impl<'pass> TableFormattingContext<'pass> {
distribute_inline_size(&mut self.columns, assignable, fixed);
self.remove_collapsed_columns();
let inline_spacing = self.border_spacing_inline();
- self.column_offsets = grid_line_offsets(self.columns.iter().map(|column| {
+ // The offsets of the columns are physical ones, from the left, as those of the collapsed borders are.
+ let column_inline_size = |column: &Column| {
if column.is_collapsed {
CssPixels::default()
} else {
column.used_inline_size + inline_spacing
}
- }));
+ };
+ self.column_offsets = if self.is_right_to_left() {
+ grid_line_offsets(self.columns.iter().rev().map(column_inline_size))
+ } else {
+ grid_line_offsets(self.columns.iter().map(column_inline_size))
+ };
self.compute_table_block_size(run);
self.distribute_block_size_to_rows();
self.position_row_boxes();
diff --git a/Tests/LibWeb/Ref/expected/table-rtl-column-span-border-tie-ref.html b/Tests/LibWeb/Ref/expected/table-rtl-column-span-border-tie-ref.html
new file mode 100644
index 00000000000..8546cd0c3a5
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/table-rtl-column-span-border-tie-ref.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<style>
+ table { border-collapse: collapse; }
+ td { width: 40px; height: 40px; padding: 0; }
+</style>
+<table><tr><td style="border-left: 6px solid red; border-right: 6px solid red"></td><td style="border-right: 6px solid blue"></td></tr></table>
diff --git a/Tests/LibWeb/Ref/input/table-rtl-column-span-border-tie.html b/Tests/LibWeb/Ref/input/table-rtl-column-span-border-tie.html
new file mode 100644
index 00000000000..62b6a5d2f11
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/table-rtl-column-span-border-tie.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/table-rtl-column-span-border-tie-ref.html">
+<style>
+ table { border-collapse: collapse; }
+ td { width: 40px; height: 40px; padding: 0; }
+ col { border-left: 6px solid red; border-right: 6px solid blue; }
+</style>
+<table dir="rtl"><col span="2"><tr><td></td><td></td></tr></table>
diff --git a/Tests/LibWeb/Text/expected/table/rtl-column-order.txt b/Tests/LibWeb/Text/expected/table/rtl-column-order.txt
new file mode 100644
index 00000000000..2bd62ca8244
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/table/rtl-column-order.txt
@@ -0,0 +1,12 @@
+attribute: width 184; 1@123+61 2@62+61 3@1+61
+inherited: width 184; 1@123+61 2@62+61 3@1+61
+ltr-in-rtl-parent: width 184; 1@1+61 2@62+61 3@123+61
+separate: width 196; 1@132+60 2@68+60 3@4+60
+spans: width 196; 1@68+124 2@4+60 3@132+60 4@68+60
+columns: width 176; 1@72+100 2@38+30 3@4+30; colgroup@4+168 col@72+100 col@4+64
+collapsed-border-widths: width 196; 1@131+63 2@70+61 3@5+65
+missing-cell: width 196; 1@132+60 2@68+60 3@4+60 4@132+60
+percentages: width 200; 1@40+160 2@0+40
+three-percentages: width 300; 1@150+150 2@30+120 3@0+30
+span-originates-on-the-right: width 200; @20+180 @0+20; colgroup@0+200 col@20+180 col@20+0 col@0+20
+column-group: width 150; 1@120+30 2@70+50 3@0+70; colgroup@70+80 col@120+30 col@70+50 colgroup@0+70 col@0+70
diff --git a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/tables/table-001.txt b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/tables/table-001.txt
index 7a8e62c37f7..550b2f9daa4 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/tables/table-001.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/mathml/presentation-markup/tables/table-001.txt
@@ -2,11 +2,10 @@ Harness status: OK
Found 6 tests
-3 Pass
-3 Fail
+6 Pass
Pass Layout of table-001
Pass Layout of table-002
Pass Layout of table-003
-Fail Layout of table-011
-Fail Layout of table-012
-Fail Layout of table-013
\ No newline at end of file
+Pass Layout of table-011
+Pass Layout of table-012
+Pass Layout of table-013
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/table/rtl-column-order.html b/Tests/LibWeb/Text/input/table/rtl-column-order.html
new file mode 100644
index 00000000000..b87d5d7ce30
--- /dev/null
+++ b/Tests/LibWeb/Text/input/table/rtl-column-order.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<style>
+ table { margin-bottom: 6px; }
+ td { width: 60px; height: 20px; padding: 0; }
+ .collapse { border-collapse: collapse; }
+ .collapse td { border: 1px solid; }
+ .separate { border-spacing: 4px; }
+ .zero { border-spacing: 0; }
+ .zero td { height: 10px; }
+</style>
+<table id="attribute" class="collapse" dir="rtl"><tr><td>1</td><td>2</td><td>3</td></tr></table>
+<div dir="rtl"><table id="inherited" class="collapse"><tr><td>1</td><td>2</td><td>3</td></tr></table></div>
+<table id="ltr-in-rtl-parent" class="collapse" dir="ltr"><tr><td>1</td><td>2</td><td>3</td></tr></table>
+<table id="separate" class="separate" dir="rtl"><tr><td>1</td><td>2</td><td>3</td></tr></table>
+<table id="spans" class="separate" dir="rtl"><tr><td colspan="2">1</td><td rowspan="2">2</td></tr><tr><td>3</td><td>4</td></tr></table>
+<table id="columns" class="separate" dir="rtl"><col style="width: 100px"><col span="2" style="width: 30px"><tr><td style="width: auto">1</td><td style="width: auto">2</td><td style="width: auto">3</td></tr></table>
+<table id="collapsed-border-widths" class="collapse" dir="rtl"><tr><td style="border-right-width: 5px">1</td><td>2</td><td style="border-left-width: 9px">3</td></tr></table>
+<table id="missing-cell" class="separate" dir="rtl"><tr><td>1</td><td>2</td><td>3</td></tr><tr><td>4</td></tr></table>
+<table id="percentages" class="zero" dir="rtl" style="width: 200px"><tr><td style="width: 80%">1</td><td style="width: 80%">2</td></tr></table>
+<table id="three-percentages" class="zero" dir="rtl" style="width: 300px"><tr><td style="width: 50%">1</td><td style="width: 40%">2</td><td style="width: 30%">3</td></tr></table>
+<table id="span-originates-on-the-right" class="zero" dir="rtl" style="width: 200px"><col><col><col><tr><td colspan="2" style="width: auto"></td><td style="width: 20px"></td></tr></table>
+<table id="column-group" class="zero" dir="rtl"><colgroup><col style="width: 30px"><col style="width: 50px"></colgroup><col style="width: 70px"><tr><td style="width: auto">1</td><td style="width: auto">2</td><td style="width: auto">3</td></tr></table>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ for (const table of document.querySelectorAll("table")) {
+ const left = table.getBoundingClientRect().left;
+ const cells = [...table.querySelectorAll("td")].map(cell => {
+ const r = cell.getBoundingClientRect();
+ return `${cell.textContent}@${Math.round(r.left - left)}+${Math.round(r.width)}`;
+ });
+ const columns = [...table.querySelectorAll("colgroup, col")].map(column => {
+ const r = column.getBoundingClientRect();
+ return `${column.localName}@${Math.round(r.left - left)}+${Math.round(r.width)}`;
+ });
+ println(`${table.id}: width ${Math.round(table.getBoundingClientRect().width)}; ${cells.join(" ")}${columns.length ? "; " + columns.join(" ") : ""}`);
+ }
+ });
+</script>