Skip to content

Truly independent web browser

LibWeb: Lay out a floated table row group as a float

sideshowbarker0c559512d833
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
index 90ec93e1ab6..2f9d300fceb 100644
--- a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
@@ -1925,6 +1925,10 @@ impl crate::layout::tree_plan::table_fixup::TableFixupTree for TreeBuilderHost<'
node_is_out_of_flow(self, node)
}
+ fn is_absolutely_positioned(&self, node: LayoutNode) -> bool {
+ self.style(node).is_some_and(|style| style.is_absolutely_positioned())
+ }
+
fn is_inline_outside(&self, node: LayoutNode) -> bool {
node_is_inline_outside(self, node)
}
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_plan/child_list.rs b/Libraries/LibWeb/Rust/src/layout/tree_plan/child_list.rs
index 516ca35654b..baf2129144e 100644
--- a/Libraries/LibWeb/Rust/src/layout/tree_plan/child_list.rs
+++ b/Libraries/LibWeb/Rust/src/layout/tree_plan/child_list.rs
@@ -53,11 +53,11 @@ impl PlannedBox<'_> {
!parent.has_fact(box_fact::MATH_INSIDE) || self.has_fact(box_fact::ABSOLUTELY_POSITIONED)
}
- fn is_out_of_flow_table_internal_child_of_table_root(&self, parent: &PlannedBox<'_>) -> bool {
+ fn is_absolutely_positioned_table_internal_child_of_table_root(&self, parent: &PlannedBox<'_>) -> bool {
parent.has_fact(box_fact::TABLE_INSIDE)
&& self.has_flag(NodeFlag::HasStyle)
&& !self.has_flag(NodeFlag::Anonymous)
- && self.is_out_of_flow()
+ && self.has_fact(box_fact::ABSOLUTELY_POSITIONED)
&& !self.has_flag(NodeFlag::IsReplacedElement)
&& self.has_fact(box_fact::TABLE_NON_ROOT_BEFORE_BLOCKIFICATION)
}
@@ -90,9 +90,13 @@ impl PlanTree {
self.node(node).is_out_of_flow_child_of(&self.node(parent))
}
- fn is_out_of_flow_table_internal_child_of_table_root(&self, parent: NodeSlotId, child: NodeSlotId) -> bool {
+ fn is_absolutely_positioned_table_internal_child_of_table_root(
+ &self,
+ parent: NodeSlotId,
+ child: NodeSlotId,
+ ) -> bool {
self.node(child)
- .is_out_of_flow_table_internal_child_of_table_root(&self.node(parent))
+ .is_absolutely_positioned_table_internal_child_of_table_root(&self.node(parent))
}
/// Makes an anonymous block container under `parent`, before `before`, that takes its style from `parent`.
@@ -209,7 +213,7 @@ impl PlanTree {
// Table-internal boxes may have been blockified before insertion, but table fixup still needs to see them as
// direct table children instead of grouping them with neighboring table whitespace.
let node_box = self.node(node);
- if node_box.is_out_of_flow_table_internal_child_of_table_root(&new_parent_box) {
+ if node_box.is_absolutely_positioned_table_internal_child_of_table_root(&new_parent_box) {
return new_parent;
}
@@ -269,7 +273,7 @@ impl PlanTree {
let mut child = self.node(new_parent).first_child();
let mut keeps_children = false;
while !child.is_invalid() && !keeps_children {
- keeps_children = self.is_out_of_flow_table_internal_child_of_table_root(new_parent, child);
+ keeps_children = self.is_absolutely_positioned_table_internal_child_of_table_root(new_parent, child);
child = self.node(child).next_sibling();
}
let wrapper = if keeps_children {
@@ -283,7 +287,9 @@ impl PlanTree {
let mut child = self.node(new_parent).first_child();
while !child.is_invalid() {
let next = self.node(child).next_sibling();
- if child != wrapper && !self.is_out_of_flow_table_internal_child_of_table_root(new_parent, child) {
+ if child != wrapper
+ && !self.is_absolutely_positioned_table_internal_child_of_table_root(new_parent, child)
+ {
self.move_child(child, wrapper);
}
child = next;
@@ -860,7 +866,7 @@ mod tests {
}
#[test]
- fn out_of_flow_table_internal_child_of_a_table_stays_out_of_the_wrapper() {
+ fn absolutely_positioned_table_internal_child_of_a_table_stays_out_of_the_wrapper() {
let mut tree = PlanTree::default();
let parent = root(
&mut tree,
@@ -872,7 +878,7 @@ mod tests {
let cell = element(
&mut tree,
NodeKind::BlockContainer,
- block() | box_fact::FLOATING | box_fact::TABLE_NON_ROOT_BEFORE_BLOCKIFICATION,
+ block() | box_fact::ABSOLUTELY_POSITIONED | box_fact::TABLE_NON_ROOT_BEFORE_BLOCKIFICATION,
);
assert_eq!(append(&mut tree, parent, cell), parent);
let second = text(&mut tree);
@@ -887,6 +893,33 @@ mod tests {
assert_eq!(parent_children[2], child);
}
+ #[test]
+ fn floated_table_internal_child_of_a_table_joins_the_wrapper() {
+ let mut tree = PlanTree::default();
+ let parent = root(
+ &mut tree,
+ NodeKind::Box,
+ display(display_outside::BLOCK, display_inside::TABLE),
+ );
+ let first = text(&mut tree);
+ append(&mut tree, parent, first);
+ let row_group = element(
+ &mut tree,
+ NodeKind::BlockContainer,
+ block() | box_fact::FLOATING | box_fact::TABLE_NON_ROOT_BEFORE_BLOCKIFICATION,
+ );
+ append(&mut tree, parent, row_group);
+ let second = text(&mut tree);
+ append(&mut tree, parent, second);
+ let child = element(&mut tree, NodeKind::BlockContainer, block());
+ append(&mut tree, parent, child);
+ let parent_children = children(&tree, parent);
+ assert_eq!(parent_children.len(), 2);
+ assert!(is_wrapper(&tree, parent_children[0]));
+ assert_eq!(children(&tree, parent_children[0]), [first, row_group, second]);
+ assert_eq!(parent_children[1], child);
+ }
+
#[test]
fn floats_under_math_are_in_flow() {
let mut tree = PlanTree::default();
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_plan/mod.rs b/Libraries/LibWeb/Rust/src/layout/tree_plan/mod.rs
index 0e6e652e224..f05329dc8ce 100644
--- a/Libraries/LibWeb/Rust/src/layout/tree_plan/mod.rs
+++ b/Libraries/LibWeb/Rust/src/layout/tree_plan/mod.rs
@@ -269,7 +269,7 @@ impl<'a> PlannedBox<'a> {
/// Whether the box's display, as table fixup reads it, is table-internal or a caption.
pub(crate) fn is_table_non_root_for_table_fixup(&self) -> bool {
- if !self.is_out_of_flow()
+ if !(self.has_fact(box_fact::HAS_STYLE) && self.has_fact(box_fact::ABSOLUTELY_POSITIONED))
|| self.has_flag(NodeFlag::IsReplacedElement)
|| self.has_flag(NodeFlag::Anonymous)
|| self.has_flag(NodeFlag::UsesButtonLayout)
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_plan/table_fixup.rs b/Libraries/LibWeb/Rust/src/layout/tree_plan/table_fixup.rs
index 4e3f39513a2..bb24d19b872 100644
--- a/Libraries/LibWeb/Rust/src/layout/tree_plan/table_fixup.rs
+++ b/Libraries/LibWeb/Rust/src/layout/tree_plan/table_fixup.rs
@@ -49,6 +49,7 @@ pub(crate) trait TableFixupTree {
fn kind(&self, node: NodeSlotId) -> NodeKind;
fn has_flag(&self, node: NodeSlotId, flag: NodeFlag) -> bool;
fn is_out_of_flow(&self, node: NodeSlotId) -> bool;
+ fn is_absolutely_positioned(&self, node: NodeSlotId) -> bool;
fn is_inline_outside(&self, node: NodeSlotId) -> bool;
/// The display of the box `node`, the one its box uses; that of its parent for a text, whose style it carries.
fn display(&self, node: NodeSlotId) -> FfiDisplay;
@@ -113,11 +114,12 @@ pub(crate) fn display_for_table_fixup(tree: &impl TableFixupTree, node: NodeSlot
// For the purposes of these rules, out-of-flow elements are represented as inline elements of zero width and
// height. Their containing blocks are chosen accordingly.
//
- // AD-HOC: Table-internal boxes can be blockified before fixup. Use the pre-transformation display for out-of-flow
- // authored boxes so an out-of-flow table-header-group is still recognized as a proper table child during fixup.
- // An in-flow box blockified as a flex or grid item is a block, though, and not a proper table child.
+ // AD-HOC: Table-internal boxes can be blockified before fixup. Use the pre-transformation display for absolutely
+ // positioned authored boxes so an absolutely positioned table-header-group is still recognized as a proper table
+ // child during fixup. A floated table-internal box is a block float, though, which fixup wraps in an anonymous row
+ // and cell like any other block; so is an in-flow box blockified as a flex or grid item.
// Element-specific display adjustments for replaced elements and buttons take precedence over that display.
- if !tree.is_out_of_flow(node)
+ if !tree.is_absolutely_positioned(node)
|| tree.has_flag(node, NodeFlag::IsReplacedElement)
|| tree.has_flag(node, NodeFlag::Anonymous)
|| tree.has_flag(node, NodeFlag::UsesButtonLayout)
@@ -510,6 +512,10 @@ impl TableFixupTree for PlanTableFixup<'_> {
self.tree.node(node).is_out_of_flow()
}
+ fn is_absolutely_positioned(&self, node: NodeSlotId) -> bool {
+ self.tree.node(node).has_fact(box_fact::ABSOLUTELY_POSITIONED)
+ }
+
fn is_inline_outside(&self, node: NodeSlotId) -> bool {
self.tree.node(node).has_fact(box_fact::INLINE_OUTSIDE)
}
diff --git a/Tests/LibWeb/Text/expected/table/floated-table-internal-boxes.txt b/Tests/LibWeb/Text/expected/table/floated-table-internal-boxes.txt
new file mode 100644
index 00000000000..ac82c1ef042
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/table/floated-table-internal-boxes.txt
@@ -0,0 +1,6 @@
+tbody-float: table 0,0 150x23; contents 0,0 150x20
+all-float: table 0,0 150x23; contents 0,0 150x20
+thead-float: table 0,0 100x46; contents 50,0 50x20, 0,23 100x20
+second-tbody-float: table 0,0 150x46; contents 0,0 100x20, 0,23 150x20
+tr-float: table 0,0 150x23; contents 0,0 150x20
+abspos-tbody: table 0,0 0x0; contents 0,0 150x20
diff --git a/Tests/LibWeb/Text/input/table/floated-table-internal-boxes.html b/Tests/LibWeb/Text/input/table/floated-table-internal-boxes.html
new file mode 100644
index 00000000000..9e7a79700b0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/table/floated-table-internal-boxes.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<style>
+ .box { width: 400px; margin: 0 0 8px; }
+ td { padding: 0; }
+ table { border-spacing: 0; }
+ .content { display: inline-block; width: 150px; height: 20px; }
+ #tbody-float tbody { float: left; width: 100%; }
+ #all-float tbody, #all-float tr, #all-float td { float: left; width: 100%; }
+ #thead-float thead { float: right; }
+ #second-tbody-float tbody:last-child { float: left; }
+ #tr-float > tbody > tr { float: left; }
+ #abspos-tbody tbody { position: absolute; }
+</style>
+<div class="box"><table id="tbody-float"><tbody><tr><td><span class="content"></span></td></tr></tbody></table></div>
+<div class="box"><table id="all-float"><tbody><tr><td><span class="content"></span></td></tr></tbody></table></div>
+<div class="box"><table id="thead-float"><thead><tr><td><span class="content" style="width: 50px"></span></td></tr></thead><tbody><tr><td><span class="content" style="width: 100px"></span></td></tr></tbody></table></div>
+<div class="box"><table id="second-tbody-float"><tbody><tr><td><span class="content" style="width: 100px"></span></td></tr></tbody><tbody><tr><td><span class="content"></span></td></tr></tbody></table></div>
+<div class="box"><table id="tr-float"><tbody><tr><td><span class="content"></span></td></tr></tbody></table></div>
+<div class="box" style="position: relative; height: 20px"><table id="abspos-tbody"><tbody><tr><td><span class="content"></span></td></tr></tbody></table></div>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const rect = (element, origin) => {
+ const r = element.getBoundingClientRect();
+ return `${Math.round(r.left - origin.left)},${Math.round(r.top - origin.top)} ${Math.round(r.width)}x${Math.round(r.height)}`;
+ };
+ for (const table of document.querySelectorAll("table")) {
+ const origin = table.parentElement.getBoundingClientRect();
+ const contents = [...table.querySelectorAll(".content")].map(content => rect(content, origin)).join(", ");
+ println(`${table.id}: table ${rect(table, origin)}; contents ${contents}`);
+ }
+ });
+</script>