Skip to content

Truly independent web browser

LibWeb: Keep a replaced element’s inline display through a restyle

sideshowbarkerd2675804cc1c
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index f280db81847..4f1d73a6ae7 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -4061,27 +4061,17 @@ impl LayoutNodeArena {
self.update_layout_style(host_calls, node, LayoutStyle::reset_table_properties);
}
- pub(crate) fn make_display_contents_pseudo_element_box_inline(
- &mut self,
- host_calls: HostCalls<'_>,
- node: NodeSlotId,
- ) {
- self.update_layout_style(host_calls, node, |style, _| {
- style.set_display(crate::layout::FfiDisplay::outside_and_inside(
- crate::css::css_enums::display_outside::INLINE,
- crate::css::css_enums::display_inside::FLOW,
- false,
- ));
- });
- }
-
- fn is_display_contents_pseudo_element_box_holding_its_published_display(&self, node: NodeSlotId) -> bool {
+ /// Gives `node` the display its box uses for the display it publishes, as building the box did: A replaced element
+ /// in a table display is inline, e.g., and a `display: contents` pseudo-element is an inline box.
+ fn rewrite_used_display(&mut self, host_calls: HostCalls<'_>, node: NodeSlotId) {
+ let Some(display) = self.style_view(node).map(|payloads| payloads.display()) else {
+ return;
+ };
let data = self.data(node);
- data.kind.get() == NodeKind::InlineNode
- && super::node_facts::has_flag(data, NodeFlag::IsPseudoElementPrincipalBox)
- && self
- .style_view(node)
- .is_some_and(|payloads| payloads.display().is_contents())
+ let used_display = super::tree_builder::used_display_of_box(data.kind.get(), data.flags.get(), display);
+ if used_display != display {
+ self.update_layout_style(host_calls, node, |style, _| style.set_display(used_display));
+ }
}
pub(crate) fn reinherit_anonymous_descendants(&mut self, host_calls: HostCalls<'_>, node: NodeSlotId) {
@@ -4094,9 +4084,7 @@ impl LayoutNodeArena {
return;
}
self.reinherit_first_letter_box(host_calls, node);
- if self.is_display_contents_pseudo_element_box_holding_its_published_display(node) {
- self.make_display_contents_pseudo_element_box_inline(host_calls, node);
- }
+ self.rewrite_used_display(host_calls, node);
let parent = self.data(node).parent.get();
let parent_is_table_wrapper_of_this_table_box = !parent.is_invalid()
&& self.data(parent).kind.get() == NodeKind::TableWrapper
diff --git a/Tests/LibWeb/Text/expected/layout-tree-update/replaced-element-in-table-display-keeps-its-box-on-restyle.txt b/Tests/LibWeb/Text/expected/layout-tree-update/replaced-element-in-table-display-keeps-its-box-on-restyle.txt
new file mode 100644
index 00000000000..15ebd9faa11
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/layout-tree-update/replaced-element-in-table-display-keeps-its-box-on-restyle.txt
@@ -0,0 +1,5 @@
+input 110x26 -> 110x26
+img 40x20 -> 40x20
+img 40x20 -> 40x20
+img 40x20 -> 40x20
+canvas 40x20 -> 40x20
diff --git a/Tests/LibWeb/Text/input/layout-tree-update/replaced-element-in-table-display-keeps-its-box-on-restyle.html b/Tests/LibWeb/Text/input/layout-tree-update/replaced-element-in-table-display-keeps-its-box-on-restyle.html
new file mode 100644
index 00000000000..57a5863d584
--- /dev/null
+++ b/Tests/LibWeb/Text/input/layout-tree-update/replaced-element-in-table-display-keeps-its-box-on-restyle.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ .row { overflow: hidden; width: 300px; margin-bottom: 10px; }
+ .float { float: left; width: 10px; height: 10px; }
+ .cell { display: table-cell; }
+</style>
+<!-- A replaced element in a table display is laid out as inline, before a restyle of the whole document and after it. -->
+<div class="row"><div class="float"></div><input class="cell" style="width: 100px; height: 20px"></div>
+<div class="row"><div class="float"></div><img class="cell" width="40" height="20" src="../../../Assets/120.png"></div>
+<div class="row"><div class="float"></div><img style="display: table-row" width="40" height="20" src="../../../Assets/120.png"></div>
+<div class="row"><div class="float"></div><img style="display: table" width="40" height="20" src="../../../Assets/120.png"></div>
+<div class="row"><div class="float"></div><canvas class="cell" width="40" height="20"></canvas></div>
+<script>
+ asyncTest(async done => {
+ await new Promise(resolve => window.addEventListener("load", resolve, { once: true }));
+ const sizes = () => [...document.querySelectorAll(".row")].map(row => {
+ const rect = row.lastElementChild.getBoundingClientRect();
+ return `${row.lastElementChild.localName} ${rect.width}x${rect.height}`;
+ });
+ const before = sizes();
+ document.documentElement.style.setProperty("--anything", "1");
+ await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+ const after = sizes();
+ before.forEach((size, index) => println(`${size} -> ${after[index].split(" ")[1]}`));
+ done();
+ });
+</script>