Truly independent web browser
LibWeb: Release replaced box styles only after comparing them
Tim Ledbetter9d608a9d0f1a
diff --git a/Libraries/LibWeb/Rust/src/css/style/tests.rs b/Libraries/LibWeb/Rust/src/css/style/tests.rsindex 87ab71b62d8..87a930198d7 100644--- a/Libraries/LibWeb/Rust/src/css/style/tests.rs+++ b/Libraries/LibWeb/Rust/src/css/style/tests.rs@@ -5,8 +5,15 @@*/use super::instrumentation::Counter;+use super::layout_style::DerivedStyleRecord;use super::pipeline::rule_store::{DeclarationBlockID, SheetListStart};use super::*;+use crate::css::animated_overlay::AnimatedOverlay;+use crate::css::computed_value_types::{ComputedSize, SizingValues};+use crate::css::computed_values::{DefaultGroups, StyleGroups};+use crate::css::table_group_builder::group_index;+use crate::layout::node_data::{LayoutStyleRef, NodeKind, NodeSlotId};+use crate::layout::{LayoutNodeArena, StyleRelayout};fn native_rules(source: &str) -> std::rc::Rc<crate::css::rule::NativeRuleList> {use crate::css::css_tokenizer::TokenizerInput;@@ -1278,6 +1285,154 @@ fn records_left_unreachable_under_a_lease_are_reclaimed_once_it_drops() {assert!(!engine.computed_group_sets.final_style_record_is_live(first));}+struct StyleReplacement {+ arena: LayoutNodeArena,+ engine: Box<StyleEngine>,+ target: computed::ComputedStyleTarget,+ row: NodeSlotId,+ base: u64,+ cached_fragment_epoch: u32,+}++impl StyleReplacement {+ fn new() -> Self {+ let mut engine = Box::new(StyleEngine::new());+ let mut nodes = [0];+ engine.allocate_style_nodes(&mut nodes);+ let node = StyleNodeID::from_raw(nodes[0]).unwrap();+ let target = computed::ComputedStyleTarget::new(node, u8::MAX);+ let base = engine.install_test_record(target, None, None, 0).raw();+ let mut arena = LayoutNodeArena::new();+ arena.set_style_engine(StyleEngineHandle::from_raw(&raw mut *engine));+ let row = arena.allocate_unbound();+ arena.stamp_dom_row(row, NodeKind::BlockContainer, Some(node));+ arena.bind_row(row);+ arena.stamp_published_style(row, base);+ Self {+ arena,+ engine,+ target,+ row,+ base,+ cached_fragment_epoch: 0,+ }+ }++ fn overlay(&mut self, groups: &StyleGroups) -> DerivedStyleRecord {+ let overlay = AnimatedOverlay::default();+ let record = self+ .engine+ .computed_group_sets+ .publish_animation_overlay(self.target, 1, HostShared::new(&raw const overlay), Some(groups), false)+ .unwrap()+ .style_record+ .raw();+ let pinned = DerivedStyleRecord::pin(&mut self.engine, record);+ self.engine.computed_group_sets.publish_animation_overlay(+ self.target,+ 0,+ HostShared::new(std::ptr::null()),+ None,+ false,+ );+ assert_eq!(Arc::strong_count(self.engine.host_record(record).unwrap()), 1);+ pinned+ }++ fn cache_layout(&mut self) {+ self.cached_fragment_epoch = self.arena.data(self.row).fragment_cache_epoch;+ }++ fn keeps_cached_layout(&self) -> bool {+ self.arena.data(self.row).fragment_cache_epoch == self.cached_fragment_epoch+ }+}++impl Drop for StyleReplacement {+ fn drop(&mut self) {+ self.arena+ .free_subtree(self.row)+ .destroy_shells_and_invoke_callbacks(&crate::stage::MainThread::for_test());+ self.arena.set_style_engine(StyleEngineHandle::null());+ }+}++fn groups_with_width(width: f64) -> StyleGroups {+ let mut groups = DefaultGroups::default().groups().clone();+ // SAFETY: SIZING names SizingValues, and write_group gives this style its own copy.+ unsafe {+ groups.write_group::<SizingValues>(group_index::SIZING, |sizing| {+ sizing.width = ComputedSize::from_data(&RetainedStyleValueData::from_owned(StyleValueData::Length {+ value: width,+ unit: crate::css::style_compute::px_length_unit(),+ }));+ });+ }+ groups+}++#[test]+fn replacing_a_published_overlay_compares_it_before_releasing_its_last_pin() {+ for changes_layout in [false, true] {+ let mut test = StyleReplacement::new();+ let groups = if changes_layout {+ groups_with_width(100.0)+ } else {+ DefaultGroups::default().groups().clone()+ };+ let previous = test.overlay(&groups);+ drop(groups);+ test.arena.stamp_published_style(test.row, previous.record);+ test.engine.unpin_layout_style_record(previous.record);+ test.cache_layout();+ let payloads = LayoutStyleRef::of_groups(test.engine.style_record_payloads(test.base).unwrap());++ test.arena+ .install_style_record(test.row, test.base, Some(payloads), Some(Some(previous.payloads)));++ assert_eq!(test.arena.node_style_record(test.row), test.base);+ assert_eq!(test.keeps_cached_layout(), !changes_layout);+ assert!(test.engine.host_record(previous.record).is_none());+ }+}++#[test]+fn replacing_and_restoring_samples_keep_the_previous_record_through_comparison() {+ let mut test = StyleReplacement::new();+ let first = test.overlay(&groups_with_width(100.0));+ let host_style = test+ .arena+ .install_sample_beside_anonymous_boxes(test.row, first, StyleRelayout::ByLayoutAffectingGroups)+ .expect("the first sample takes the host's style");+ test.cache_layout();++ let second = test.overlay(&groups_with_width(200.0));+ assert!(+ test.arena+ .install_sample_beside_anonymous_boxes(test.row, second, StyleRelayout::ByLayoutAffectingGroups)+ .is_none()+ );+ assert!(test.engine.host_record(first.record).is_none());+ assert!(test.engine.host_record(second.record).is_some());+ assert!(!test.keeps_cached_layout());++ let same = DerivedStyleRecord::pin(&mut test.engine, second.record);+ test.cache_layout();+ assert!(+ test.arena+ .install_sample_beside_anonymous_boxes(test.row, same, StyleRelayout::ByLayoutAffectingGroups)+ .is_none()+ );+ assert!(test.keeps_cached_layout());+ assert!(test.engine.host_record(second.record).is_some());++ test.arena+ .restore_host_style(test.row, host_style, StyleRelayout::ByLayoutAffectingGroups);+ assert_eq!(test.arena.node_style_record(test.row), test.base);+ assert!(!test.keeps_cached_layout());+ assert!(test.engine.host_record(second.record).is_none());+}+#[test]fn a_reissued_identity_holds_no_construction_facts() {use super::bridge::element_construction_fact::{IS_BODY, IS_EDITING_HOST, IS_IN_USER_AGENT_SHADOW_TREE};diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rsindex 04554eab2e4..1fd888fa031 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs@@ -2334,16 +2334,19 @@ impl LayoutNodeArena {payloads: Option<LayoutStyleRef>,previous_payloads: Option<Option<LayoutStyleRef>>,) {+ let keeps_layout_inputs = match previous_payloads {+ Some(previous) => {+ self.forget_last_successful_position_option_after_fallback_sensitive_change(slot, previous, payloads);+ self.style_move_keeps_layout_inputs(slot, previous, payloads)+ }+ None => true,+ };if self.set_node_style(slot, style_record, payloads) {self.refresh_style_flags(slot);}self.publish_new_size_container_geometry(slot);self.enroll_node_for_svg_paint_resources_sync(slot);- let Some(previous_payloads) = previous_payloads else {- return;- };- self.forget_last_successful_position_option_after_fallback_sensitive_change(slot, previous_payloads, payloads);- if !self.style_move_keeps_layout_inputs(slot, previous_payloads, payloads) {+ if !keeps_layout_inputs {self.invalidate_fragments_and_intrinsic_sizes_of_self_and_ancestors(slot);}}@@ -2723,19 +2726,16 @@ impl LayoutNodeArena {relayout: StyleRelayout,) -> Option<HostStyle> {let index = row.slot_index() as usize;+ let previous = self.style_records[index];let host_style = match self.style_record_pins[index] {- ArenaStylePin::Sampled => {- let previous = self.style_records[index];- self.with_style_engine(|engine| engine.unpin_layout_style_record(previous));- None- }- pin => Some(HostStyle {- record: self.style_records[index],- pin,- }),+ ArenaStylePin::Sampled => None,+ pin => Some(HostStyle { record: previous, pin }),};self.style_record_pins[index] = ArenaStylePin::Sampled;self.show_style(row, self.node_style_node(row), sample, relayout);+ if host_style.is_none() {+ self.with_style_engine(|engine| engine.unpin_layout_style_record(previous));+ }host_style}@@ -2777,7 +2777,6 @@ impl LayoutNodeArena {"a box shows a sample until its host's style is restored");let sample = self.style_records[index];- self.with_style_engine(|engine| engine.unpin_layout_style_record(sample));let HostStyle { record, pin } = host_style;let payloads = self.with_style_store(|engine| {LayoutStyleRef::of_groups(@@ -2793,6 +2792,7 @@ impl LayoutNodeArena {DerivedStyleRecord { record, payloads },relayout,);+ self.with_style_engine(|engine| engine.unpin_layout_style_record(sample));}/// Shows `style` in `row`, whose pin the caller set, as the host's install of a row of a box that styles no anonymous