Truly independent web browser
LibWeb: Don’t restyle a box that its new display replaces
sideshowbarker4a2a8600b17a
diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp b/Libraries/LibWeb/CSS/CSSStyleProperties.cppindex 2afa603f287..5f2aa90259c 100644--- a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp+++ b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp@@ -1342,10 +1342,14 @@ RefPtr<StyleValue const> CSSStyleProperties::style_value_for_computed_property(L};auto color_resolution_style = ColorResolutionStyle::for_layout_node(layout_node);+ // A logical property maps by the element's own writing mode and direction, which its box may not show yet: a box+ // awaiting the layout tree update that replaces it keeps the style it was built for.if (property_is_logical_alias(property_id)) {+ auto style = element.computed_style(pseudo_element);+ VERIFY(style);return style_value_for_computed_property(layout_node,- map_logical_alias_to_physical_property(property_id, LogicalAliasMappingContext { layout_node.writing_mode(), layout_node.direction() }));+ map_logical_alias_to_physical_property(property_id, LogicalAliasMappingContext { style->writing_mode(), style->direction() }));}// A limited number of properties have special rules for producing their "resolved value".diff --git a/Libraries/LibWeb/Layout/Node.cpp b/Libraries/LibWeb/Layout/Node.cppindex 522162477cb..7e5ba799aab 100644--- a/Libraries/LibWeb/Layout/Node.cpp+++ b/Libraries/LibWeb/Layout/Node.cpp@@ -401,8 +401,26 @@ bool style_record_installed_by_render_state(NodeWithStyle const& node, CSS::Stylreturn style_record_identity.value() != 0 && RustFFI::render_state_installed_style_record(node.document_host(), Node::slot_id(&node)) == style_record_identity.value();}+// A box keeps the style it was built for when the new record's display asks for another kind of box: the layout tree+// update that the display change marks builds that box from the element's record, and a layout that runs before the+// build would otherwise lay out a box of one kind by the style of another. The element holds the new record from now+// on, so the box pins the one it keeps until the build replaces it. A record the render side installed in the box+// already passed its own check.+bool NodeWithStyle::keeps_style_until_rebuilt(CSS::StyleRecordID style_record_identity)+{+ if (!m_style_record || m_has_layout_derived_style || style_record_installed_by_render_state(*this, style_record_identity))+ return false;+ auto const style = document().style_computer().install_style(held_read(), style_record_identity);+ if (!style || RustFFI::layout_box_can_show_style(kind(), m_style_record, style.groups()))+ return false;+ pin_style_record_for_cxx_consumers();+ return true;+}+void NodeWithStyle::apply_style(CSS::StyleRecordID style_record_identity){+ if (keeps_style_until_rebuilt(style_record_identity))+ return;// The render side installed the record in this node's row ahead of the host, with everything below that follows// from it in the layout tree: the node adopts it as its own.bool const installed_by_render_state = style_record_installed_by_render_state(*this, style_record_identity);@@ -575,6 +593,9 @@ void NodeWithStyle::set_style_record_identity(CSS::StyleRecordID style_record_idreturn;}+ if (keeps_style_until_rebuilt(style_record_identity))+ return;+bool should_repin_style_record = m_style_record_pinned_for_cxx_consumers;release_pinned_style_record();m_background_images.clear();diff --git a/Libraries/LibWeb/Layout/Node.h b/Libraries/LibWeb/Layout/Node.hindex be5d8dd3bf6..e6d4390cbac 100644--- a/Libraries/LibWeb/Layout/Node.h+++ b/Libraries/LibWeb/Layout/Node.h@@ -547,6 +547,7 @@ public:void release_pinned_style_record();private:+ bool keeps_style_until_rebuilt(CSS::StyleRecordID);virtual bool is_node_with_style() const final { return true; }void publish_style_record_to_node_data();diff --git a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs b/Libraries/LibWeb/Rust/src/layout/tree_builder.rsindex ae1f8069922..9bfb29b0983 100644--- a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs@@ -3950,6 +3950,30 @@ pub extern "C" fn layout_node_kind_is_replaced_box(kind: NodeKind) -> bool {node_facts::kind_is_replaced_box(kind)}+/// Whether a box of `kind`, built for an element whose style was `built_for`, can show the style `style` as it is. A box+/// whose kind its display decided can't show a display that asks for another kind of box: the layout tree update that+/// the display change marks replaces it, and until then it keeps the style it was built for.+///+/// # Safety+/// Each of `built_for` and `style` is null or the groups of a style record the caller keeps alive for the call.+#[unsafe(no_mangle)]+pub unsafe extern "C" fn layout_box_can_show_style(+ kind: NodeKind,+ built_for: *const std::ffi::c_void,+ style: *const std::ffi::c_void,+) -> bool {+ use super::node_data::LayoutStyleRef;+ // SAFETY: Guaranteed by the caller.+ let (Some(built_for), Some(style)) = (unsafe { LayoutStyleRef::from_raw(built_for) }, unsafe {+ LayoutStyleRef::from_raw(style)+ }) else {+ return true;+ };+ // SAFETY: Guaranteed by the caller.+ let (built_for, style) = unsafe { (built_for.view().display(), style.view().display()) };+ node_kind_for_display(built_for) != Some(kind) || node_kind_for_display(style) == Some(kind)+}+#[unsafe(no_mangle)]pub extern "C" fn layout_node_kind_is_svg_box(kind: NodeKind) -> bool {node_facts::kind_is_svg_box(kind)diff --git a/Tests/LibWeb/Text/expected/css/style-engine/display-change-keeps-box-style-until-rebuild.txt b/Tests/LibWeb/Text/expected/css/style-engine/display-change-keeps-box-style-until-rebuild.txtnew file mode 100644index 00000000000..d41b98db711--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/style-engine/display-change-keeps-box-style-until-rebuild.txt@@ -0,0 +1,3 @@+class change: display: inline+shadow host stylesheet: display: inline+logical property: border-inline-start-style: dasheddiff --git a/Tests/LibWeb/Text/input/css/style-engine/display-change-keeps-box-style-until-rebuild.html b/Tests/LibWeb/Text/input/css/style-engine/display-change-keeps-box-style-until-rebuild.htmlnew file mode 100644index 00000000000..23833b958ed--- /dev/null+++ b/Tests/LibWeb/Text/input/css/style-engine/display-change-keeps-box-style-until-rebuild.html@@ -0,0 +1,46 @@+<!DOCTYPE html>+<script src="../../include.js"></script>+<style>+ .grid { display: grid; }+ .leaf { display: block; width: 20px; height: 10px; }+</style>+<div class="grid"><div><span id="styled" class="grid"><span class="leaf"></span><div class="leaf"></div></span></div></div>+<div class="grid"><div><span id="host"><span class="leaf"></span><div class="leaf"></div></span></div></div>+<div class="grid"><div><span id="logical" class="grid" style="border-left: 1px solid; border-right: 1px dashed"><span class="leaf"></span></span></div></div>+<script>+ function check(name, element, changeDisplay) {+ element.getBoundingClientRect();+ internals.updateStyle();+ // The element's grid box waits for the layout tree update to replace it with an inline box. Neither the+ // display change nor another change after it may restyle the grid box as inline in the meantime.+ changeDisplay();+ internals.updateStyle();+ element.style.color = "green";+ internals.updateStyle();+ println(`${name}: display: ${getComputedStyle(element).display}`);+ element.getBoundingClientRect();+ }++ test(() => {+ const styled = document.getElementById("styled");+ check("class change", styled, () => styled.classList.remove("grid"));++ // The host is a grid only through its own shadow root's stylesheet, so emptying the sheet makes it inline.+ const host = document.getElementById("host");+ const sheet = new CSSStyleSheet();+ sheet.replaceSync(":host { display: grid; }");+ const shadow = host.attachShadow({ mode: "open" });+ shadow.adoptedStyleSheets = [sheet];+ shadow.innerHTML = "<slot></slot>";+ check("shadow host stylesheet", host, () => sheet.replaceSync(""));++ // A logical property resolves by the element's own direction while its box waits to be rebuilt.+ const logical = document.getElementById("logical");+ logical.getBoundingClientRect();+ internals.updateStyle();+ logical.classList.remove("grid");+ logical.style.direction = "rtl";+ internals.updateStyle();+ println(`logical property: border-inline-start-style: ${getComputedStyle(logical).getPropertyValue("border-inline-start-style")}`);+ });+</script>