Truly independent web browser
LibWeb: Decide relayout of derived-style boxes against their old style
Aliaksandr Kalenik7577e34b98e6
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_changes.rs b/Libraries/LibWeb/Rust/src/layout/layout_changes.rsindex 028ff5d63df..7da94dd2191 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_changes.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_changes.rs@@ -307,10 +307,12 @@ impl LayoutChange {arena.set_node_needs_compositor_animation_frame(node, kind, value);}Self::SetNodeStyle { node, record, payloads } => {- // A style the arena derived is replaced only to be derived again, which decides the relayout itself.- let previous_payloads = (arena.slot_is_live(node) && !arena.node_style_record_is_derived(node))- .then(|| arena.data(node).style.get());- arena.install_style_record(node, record, payloads, previous_payloads);+ if arena.slot_is_live(node) && arena.node_style_record_is_derived(node) {+ arena.install_style_record_in_place_of_derived_style(node, record, payloads);+ } else {+ let previous_payloads = arena.slot_is_live(node).then(|| arena.data(node).style.get());+ arena.install_style_record(node, record, payloads, previous_payloads);+ }}Self::PinBoundBoxStyleRecordForDetachment {style_node,diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rsindex 2ffabd569e3..d3a0bfda061 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs@@ -1092,6 +1092,7 @@ pub(crate) struct LayoutNodeArena {/// is a pin of its own beside the arena's, and it names the record it took rather than whichever/// record the row holds when it goes.style_records_pinned_by_host: Vec<u64>,+ derived_styles_shown_before_host_installs: HashMap<NodeSlotId, DerivedStyleRecord>,/// The StyleNodeID of the element or text node each row is bound to, or of the element it is/// generated for. Rows carrying one identity are chained through `next_rows_with_same_style_node`/// from `first_rows_by_style_node`, so retiring an identity reaches every row that carries it,@@ -1257,6 +1258,7 @@ impl LayoutNodeArena {image_resources_owed_records: Vec::new(),style_record_pins: Vec::new(),style_records_pinned_by_host: Vec::new(),+ derived_styles_shown_before_host_installs: HashMap::default(),style_nodes: Vec::new(),next_rows_with_same_style_node: Vec::new(),first_rows_by_style_node: RowsByStyleNode::default(),@@ -1574,6 +1576,9 @@ impl LayoutNodeArena {if host_pinned_style_record != 0 {arena_pinned_style_records.push(host_pinned_style_record);}+ if let Some(shown) = self.derived_styles_shown_before_host_installs.remove(&slot) {+ arena_pinned_style_records.push(shown.record);+ }self.unlink_children_of_node_being_freed(slot);if let Some(reset) = self.free_unlinked_slot(slot) {paintable_row_resets.push(reset);@@ -2329,6 +2334,55 @@ impl LayoutNodeArena {}}+ pub(crate) fn install_style_record_in_place_of_derived_style(+ &mut self,+ slot: NodeSlotId,+ style_record: u64,+ payloads: Option<LayoutStyleRef>,+ ) {+ let index = slot.slot_index() as usize;+ debug_assert!(self.style_record_pins[index] == ArenaStylePin::Derived);+ let shown = DerivedStyleRecord {+ record: self.style_records[index],+ payloads: self+ .data(slot)+ .style+ .get()+ .expect("a derived style row holds its style"),+ };+ self.style_record_pins[index] = ArenaStylePin::None;+ let holds_an_older_shown_style = match self.derived_styles_shown_before_host_installs.entry(slot) {+ std::collections::hash_map::Entry::Occupied(_) => true,+ std::collections::hash_map::Entry::Vacant(entry) => {+ entry.insert(shown);+ false+ }+ };+ if holds_an_older_shown_style {+ self.with_style_engine(|engine| engine.unpin_layout_style_record(shown.record));+ }+ self.install_style_record(slot, style_record, payloads, None);+ }++ fn holds_derived_style_shown_before_host_install(&self, slot: NodeSlotId) -> bool {+ !self.derived_styles_shown_before_host_installs.is_empty()+ && self.derived_styles_shown_before_host_installs.contains_key(&slot)+ }++ fn decide_relayout_against_derived_style_shown_before_host_install(&mut self, slot: NodeSlotId) {+ if self.derived_styles_shown_before_host_installs.is_empty() {+ return;+ }+ let Some(shown) = self.derived_styles_shown_before_host_installs.remove(&slot) else {+ return;+ };+ if !self.style_move_keeps_layout_inputs(slot, Some(shown.payloads), self.data(slot).style.get()) {+ self.invalidate_fragments_and_intrinsic_sizes_of_self_and_ancestors(slot);+ self.set_needs_layout_update(slot, true);+ }+ self.with_style_engine(|engine| engine.unpin_layout_style_record(shown.record));+ }+/// Whether the box `slot` keeps the layout it cached when it shows `new` in place of `previous` (see/// [`crate::css::computed_values::style_payloads_layout_inputs_equal`]).pub(crate) fn style_move_keeps_layout_inputs(@@ -3562,7 +3616,10 @@ impl LayoutNodeArena {);return;}- let shown_record = self.node_style_record(slot);+ let shown_record = self+ .derived_styles_shown_before_host_installs+ .get(&slot)+ .map_or_else(|| self.node_style_record(slot), |shown| shown.record);let damage = self.with_style_engine(|engine| engine.element_record_damage(originating, true, shown_record, derived.record));if self.apply_reinherited_style_record(host_calls, slot, derived) {@@ -3669,6 +3726,11 @@ impl LayoutNodeArena {}pub(crate) fn reinherit_anonymous_descendants(&mut self, host_calls: HostCalls<'_>, node: NodeSlotId) {+ self.reinherit_own_derived_style_and_anonymous_descendants(host_calls, node);+ self.decide_relayout_against_derived_style_shown_before_host_install(node);+ }++ fn reinherit_own_derived_style_and_anonymous_descendants(&mut self, host_calls: HostCalls<'_>, node: NodeSlotId) {if self.node_style_record(node) == 0 {return;}@@ -3765,8 +3827,8 @@ impl LayoutNodeArena {derived: DerivedStyleRecord,) -> bool {let previous_payloads = self.data(slot).style.get();- let changes_layout_affecting_style =- !self.style_move_keeps_layout_inputs(slot, previous_payloads, Some(derived.payloads));+ let changes_layout_affecting_style = !self.holds_derived_style_shown_before_host_install(slot)+ && !self.style_move_keeps_layout_inputs(slot, previous_payloads, Some(derived.payloads));self.replace_arena_pinned_style_record(slot, derived);if changes_layout_affecting_style {self.invalidate_fragments_and_intrinsic_sizes_of_self_and_ancestors(slot);diff --git a/Tests/LibWeb/Text/expected/first-letter-in-child-block-relayout-after-pseudo-element-style-change.txt b/Tests/LibWeb/Text/expected/first-letter-in-child-block-relayout-after-pseudo-element-style-change.txtnew file mode 100644index 00000000000..36873b07428--- /dev/null+++ b/Tests/LibWeb/Text/expected/first-letter-in-child-block-relayout-after-pseudo-element-style-change.txt@@ -0,0 +1,2 @@+queried: computed font-size 30px, letter laid out like the 30px reference: true, letter parent as tall as the reference's: true+transitioned: computed font-size 30px, letter laid out like the 30px reference: true, letter parent as tall as the reference's: truediff --git a/Tests/LibWeb/Text/input/first-letter-in-child-block-relayout-after-pseudo-element-style-change.html b/Tests/LibWeb/Text/input/first-letter-in-child-block-relayout-after-pseudo-element-style-change.htmlnew file mode 100644index 00000000000..fe24f629ecf--- /dev/null+++ b/Tests/LibWeb/Text/input/first-letter-in-child-block-relayout-after-pseudo-element-style-change.html@@ -0,0 +1,60 @@+<!DOCTYPE html>+<script src="include.js"></script>+<style>+ body {+ margin: 0;+ }+ .letter-parent {+ color: green;+ }+ #queried {+ container-type: inline-size;+ width: 200px;+ }+ @container (min-width: 300px) {+ #queried::first-letter {+ font-size: 30px;+ }+ }+ #transitioned::first-letter {+ font-size: 16px;+ transition: font-size 1000s steps(1, jump-start);+ }+ #transitioned.big::first-letter {+ font-size: 30px;+ }+ #reference::first-letter {+ font-size: 30px;+ }+</style>+<div id="queried"><div class="letter-parent">First line</div></div>+<div id="transitioned"><div class="letter-parent">First line</div></div>+<div id="reference"><div class="letter-parent">First line</div></div>+<script>+ function firstLetterRect(element) {+ const text = element.querySelector(".letter-parent").firstChild;+ const range = new Range();+ range.setStart(text, 0);+ range.setEnd(text, 1);+ return range.getBoundingClientRect();+ }++ function describe(element) {+ const letter = firstLetterRect(element);+ const reference = firstLetterRect(document.getElementById("reference"));+ const letterParentHeight = element.querySelector(".letter-parent").getBoundingClientRect().height;+ const referenceLetterParentHeight = document.querySelector("#reference .letter-parent").getBoundingClientRect().height;+ println(`${element.id}: computed font-size ${getComputedStyle(element, "::first-letter").fontSize}, letter laid out like the 30px reference: ${letter.width === reference.width && letter.height === reference.height}, letter parent as tall as the reference's: ${letterParentHeight === referenceLetterParentHeight}`);+ }++ asyncTest(async done => {+ await animationFrame();+ queried.style.width = "300px";+ transitioned.classList.add("big");+ await animationFrame();+ await animationFrame();+ describe(queried);+ describe(transitioned);+ done();+ });+</script>