Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(style): retire inactive document worlds

ldm0a1385e07924f
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs
index f8b52bdc8..8ba6ee146 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs
@@ -58,6 +58,12 @@ impl JsContextHost {
self.note_style_subtree_context_change(document_handle);
self.dom_host_mut()
.mark_subtree_disconnected_preserving_owner_document(document_handle);
+ // The retired child realm exposes no further rendered style surface.
+ // Drop its heavyweight Stylist/source state after disconnecting the DOM;
+ // the global retirement-generation barrier keeps stale
+ // CSSStyleDeclaration wrappers invalid without retaining the world.
+ self.style_engine
+ .retire_document_style_world(document_handle);
}
fn remove_child_browsing_context_current_document_storage(
@@ -82,6 +88,8 @@ impl JsContextHost {
self.note_style_subtree_context_change(document_handle);
self.dom_host_mut()
.mark_subtree_disconnected_preserving_owner_document(document_handle);
+ self.style_engine
+ .retire_document_style_world(document_handle);
Some(document_handle)
}
diff --git a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
index b4c518bfc..45855e192 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
@@ -1625,6 +1625,18 @@ impl JsContextHost {
.computed_style_cache_entry_count_for_document_for_test(document)
}
+ #[cfg(test)]
+ pub(crate) fn active_document_style_world_count_for_test(&self) -> usize {
+ self.style_engine
+ .active_document_style_world_count_for_test()
+ }
+
+ #[cfg(test)]
+ pub(crate) fn document_style_world_is_active_for_test(&self, document: DomHandle) -> bool {
+ self.style_engine
+ .document_style_world_is_active_for_test(document)
+ }
+
#[cfg(test)]
pub(crate) fn retained_style_system_rebuild_count_for_document_for_test(
&self,
diff --git a/moli-renderer-v8/src/native_bridge/context_host/popups.rs b/moli-renderer-v8/src/native_bridge/context_host/popups.rs
index 08a41927e..aca7a2856 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/popups.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/popups.rs
@@ -2627,6 +2627,8 @@ impl JsContextHost {
self.note_style_subtree_context_change(document_handle);
self.dom_host_mut()
.mark_subtree_disconnected_preserving_owner_document(document_handle);
+ self.style_engine
+ .retire_document_style_world(document_handle);
}
pub(crate) fn dispatch_lightweight_popup_window_event<'s>(
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration.rs
index c02b85435..360fbf0f6 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration.rs
@@ -36,8 +36,9 @@ pub(super) use parse::{
pub(super) use properties::{
all_shorthand_applies_to, animation_shorthand_longhands, css_wide_keyword,
font_variant_longhands, is_style_intrinsic_name, known_style_property,
- resolve_style_property_name, shorthand_longhands, supported_declared_property,
- text_decoration_shorthand_longhands, transition_shorthand_longhands,
+ resolve_known_style_property_name, resolve_style_property_name, shorthand_longhands,
+ supported_declared_property, text_decoration_shorthand_longhands,
+ transition_shorthand_longhands,
};
pub(in crate::native_bridge::element) use style_world::style_base_url;
pub(crate) use values::{
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/properties.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/properties.rs
index a0d15f6d9..c6c9214d2 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/properties.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/properties.rs
@@ -232,12 +232,7 @@ fn style_property_exists(
&& !computed_style_default_value(runtime, handle, property).is_empty())
}
-pub(in crate::native_bridge::element::styles) fn resolve_style_property_name(
- runtime: &JsContextHost,
- handle: DomHandle,
- mode: StyleMode,
- raw_key: &str,
-) -> Option<String> {
+fn canonical_property_name_for_named_lookup(raw_key: &str) -> Option<String> {
if raw_key.is_empty() || is_style_intrinsic_name(raw_key) {
return None;
}
@@ -245,8 +240,7 @@ pub(in crate::native_bridge::element::styles) fn resolve_style_property_name(
return Some("float".to_owned());
}
if raw_key.starts_with('-') {
- let property = canonical_style_property_name(raw_key);
- return style_property_exists(runtime, handle, mode, &property).then_some(property);
+ return Some(canonical_style_property_name(raw_key));
}
for prefix in ["moz", "ms", "o"] {
if let Some(suffix) = raw_key.strip_prefix(prefix)
@@ -258,7 +252,23 @@ pub(in crate::native_bridge::element::styles) fn resolve_style_property_name(
return None;
}
}
- let property = canonical_style_property_identifier(raw_key);
+ Some(canonical_style_property_identifier(raw_key))
+}
+
+pub(in crate::native_bridge::element::styles) fn resolve_known_style_property_name(
+ raw_key: &str,
+) -> Option<String> {
+ let property = canonical_property_name_for_named_lookup(raw_key)?;
+ known_style_property(&property).then_some(property)
+}
+
+pub(in crate::native_bridge::element::styles) fn resolve_style_property_name(
+ runtime: &JsContextHost,
+ handle: DomHandle,
+ mode: StyleMode,
+ raw_key: &str,
+) -> Option<String> {
+ let property = canonical_property_name_for_named_lookup(raw_key)?;
style_property_exists(runtime, handle, mode, &property).then_some(property)
}
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/named.rs b/moli-renderer-v8/src/native_bridge/element/styles/named.rs
index 4e51d7d72..b3af85136 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/named.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/named.rs
@@ -4,12 +4,13 @@ use super::super::property_string_value;
use super::declaration::{
StyleMode, cssom_style_property_affected_names_with_pdb,
expand_unresolved_box_shorthand_entries_for_mutation, is_style_intrinsic_name,
- parse_style_property_entries_for_cssom_fallback_write, resolve_style_property_name,
- set_inline_style_property_with_pdb_storage, set_style_entries_if_changed_with_inline_base_url,
- shorthand_longhands, style_base_url, style_entries_for_style_object,
- style_property_count_with_context, style_property_index_exists_with_context,
- style_property_name_at_with_context, style_property_value_for_pseudo_with_context,
- style_property_value_with_context, style_runtime_and_handle_from_object,
+ parse_style_property_entries_for_cssom_fallback_write, resolve_known_style_property_name,
+ resolve_style_property_name, set_inline_style_property_with_pdb_storage,
+ set_style_entries_if_changed_with_inline_base_url, shorthand_longhands, style_base_url,
+ style_entries_for_style_object, style_property_count_with_context,
+ style_property_index_exists_with_context, style_property_name_at_with_context,
+ style_property_value_for_pseudo_with_context, style_property_value_with_context,
+ style_runtime_and_handle_from_object,
};
use super::{
style_object_computation_context, style_object_forces_empty_computed,
@@ -99,10 +100,10 @@ fn live_style_named_property_value_for_handle<'s>(
if is_style_intrinsic_name(raw_key) {
return None;
}
- let property = resolve_style_property_name(runtime, handle, mode, raw_key)?;
if forces_empty {
- return Some(String::new());
+ return resolve_known_style_property_name(raw_key).map(|_| String::new());
}
+ let property = resolve_style_property_name(runtime, handle, mode, raw_key)?;
let context = style_object_computation_context(scope, style);
Some(
if let Some(pseudo) = style_object_pseudo_element(scope, style, mode) {
diff --git a/moli-renderer-v8/src/script_vm.rs b/moli-renderer-v8/src/script_vm.rs
index 1d821fd31..2fd61fe1c 100644
--- a/moli-renderer-v8/src/script_vm.rs
+++ b/moli-renderer-v8/src/script_vm.rs
@@ -2931,6 +2931,20 @@ impl ScriptVm {
.computed_style_cache_entry_count_for_document_for_test(document)
}
+ #[cfg(test)]
+ pub(crate) fn active_document_style_world_count_for_test(&self) -> usize {
+ self._context_host
+ .borrow()
+ .active_document_style_world_count_for_test()
+ }
+
+ #[cfg(test)]
+ pub(crate) fn document_style_world_is_active_for_test(&self, document: DomHandle) -> bool {
+ self._context_host
+ .borrow()
+ .document_style_world_is_active_for_test(document)
+ }
+
#[cfg(test)]
pub(crate) fn computed_style_cache_generation_for_document_for_test(
&self,
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
index 8765b7586..ea9eb99a1 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
@@ -6063,6 +6063,9 @@ fn removed_iframe_clears_child_document_computed_style_cache() {
.expect("child frame computed style setup should evaluate");
assert_eq!(initial, "rgb(0, 128, 0)");
+ let retired_child_document =
+ child_document_handle_for_frame_id(&vm, "style-cache-reattach-frame");
+ assert!(vm.document_style_world_is_active_for_test(retired_child_document));
assert!(vm.computed_style_cache_entry_count_for_document_for_test(document) > 0);
let removed = vm
@@ -6077,6 +6080,7 @@ fn removed_iframe_clears_child_document_computed_style_cache() {
.expect("child frame removal should evaluate");
assert_eq!(removed, "true");
+ assert!(!vm.document_style_world_is_active_for_test(retired_child_document));
assert_eq!(
vm.computed_style_cache_entry_count_for_document_for_test(document),
0
@@ -6229,6 +6233,102 @@ fn held_child_frame_computed_style_is_empty_after_srcdoc_navigation() {
1
);
}
+
+#[test]
+fn repeated_iframe_navigation_retires_each_previous_document_style_world() {
+ let mut vm = new_storage_test_vm("https://child-style-world-retirement.test/");
+ let main_document = vm.document_handle_for_test();
+
+ vm.eval(
+ r#"
+(() => {
+ const frame = document.createElement('iframe');
+ frame.id = 'style-world-churn-frame';
+ frame.srcdoc = '<style>body { color: rgb(0, 128, 0); }</style><body data-generation="0">first</body>';
+ (document.body || document.documentElement || document).appendChild(frame);
+ getComputedStyle(document.documentElement).display;
+})()
+"#,
+ )
+ .expect("initial child style-world navigation should queue");
+ assert!(
+ vm.run_next_child_navigation_commit_body_for_test()
+ .expect("initial child navigation-commit body should succeed")
+ .is_some()
+ );
+ assert_eq!(
+ vm.eval(
+ r#"
+(() => {
+ const frame = document.getElementById('style-world-churn-frame');
+ globalThis.__heldChurnStyle = frame.contentWindow.getComputedStyle(frame.contentDocument.body);
+ return globalThis.__heldChurnStyle.color;
+})()
+"#,
+ )
+ .expect("initial child style should compute"),
+ "rgb(0, 128, 0)"
+ );
+
+ let mut previous_document = child_document_handle_for_frame_id(&vm, "style-world-churn-frame");
+ assert!(vm.document_style_world_is_active_for_test(main_document));
+ assert!(vm.document_style_world_is_active_for_test(previous_document));
+ let steady_active_worlds = vm.active_document_style_world_count_for_test();
+ assert_eq!(steady_active_worlds, 2);
+
+ for generation in 1..=8 {
+ let navigation = format!(
+ r#"
+(() => {{
+ const frame = document.getElementById('style-world-churn-frame');
+ globalThis.__heldChurnStyle = frame.contentWindow.getComputedStyle(frame.contentDocument.body);
+ frame.srcdoc = '<body data-generation="{generation}">next</body>';
+}})()
+"#
+ );
+ vm.eval(&navigation)
+ .expect("replacement child style-world navigation should queue");
+ assert!(
+ vm.run_next_child_navigation_commit_body_for_test()
+ .expect("replacement child navigation-commit body should succeed")
+ .is_some()
+ );
+
+ let current_document = child_document_handle_for_frame_id(&vm, "style-world-churn-frame");
+ assert_ne!(current_document, previous_document);
+ assert!(
+ !vm.document_style_world_is_active_for_test(previous_document),
+ "the previous child Document must leave the active style-world map at commit"
+ );
+ assert_eq!(
+ vm.eval(
+ r#"
+(() => {
+ const frame = document.getElementById('style-world-churn-frame');
+ const held = globalThis.__heldChurnStyle;
+ const stale = `${held.length}:${held.color}`;
+ const fresh = frame.contentWindow.getComputedStyle(frame.contentDocument.body).display;
+ return `${stale}|${fresh}|${frame.contentDocument.body.dataset.generation}`;
+})()
+"#,
+ )
+ .expect("stale and current child styles should remain distinguishable"),
+ format!("0:|block|{generation}")
+ );
+ assert!(
+ !vm.document_style_world_is_active_for_test(previous_document),
+ "reading a held stale declaration must not recreate its retired style world"
+ );
+ assert!(vm.document_style_world_is_active_for_test(current_document));
+ assert_eq!(
+ vm.active_document_style_world_count_for_test(),
+ steady_active_worlds,
+ "iframe navigation churn must keep heavyweight style worlds bounded"
+ );
+ previous_document = current_document;
+ }
+}
+
#[test]
fn computed_style_custom_property_names_use_iframe_viewport() {
let mut vm = new_storage_test_vm("https://child-custom-property-names.test/");
@@ -8475,6 +8575,7 @@ fn popup_held_computed_style_wrapper_is_empty_after_about_blank_navigation() {
popup.location.href = 'about:blank?next';
const replacement = popup.document.createElement('div');
+ replacement.id = 'popup-held-computed-replacement';
replacement.style.cssText = 'color: rgb(44, 55, 66);';
popup.document.body.appendChild(replacement);
@@ -8495,6 +8596,13 @@ fn popup_held_computed_style_wrapper_is_empty_after_about_blank_navigation() {
result,
"rgb(11, 22, 33):true|about:blank?next|true||0|rgb(44, 55, 66)"
);
+ let retired_popup_document =
+ owner_document_handle_for_element_id(&vm, "popup-held-computed-target");
+ let current_popup_document =
+ owner_document_handle_for_element_id(&vm, "popup-held-computed-replacement");
+ assert_ne!(retired_popup_document, current_popup_document);
+ assert!(!vm.document_style_world_is_active_for_test(retired_popup_document));
+ assert!(vm.document_style_world_is_active_for_test(current_popup_document));
}
#[tokio::test]
diff --git a/moli-renderer-v8/src/style_engine/document_world.rs b/moli-renderer-v8/src/style_engine/document_world.rs
index 3f0af89ed..e6f5c539c 100644
--- a/moli-renderer-v8/src/style_engine/document_world.rs
+++ b/moli-renderer-v8/src/style_engine/document_world.rs
@@ -1,4 +1,8 @@
-use std::{cell::RefCell, collections::HashMap, rc::Rc};
+use std::{
+ cell::{Cell, RefCell},
+ collections::HashMap,
+ rc::Rc,
+};
use crate::document_runtime::DomHandle;
@@ -12,7 +16,7 @@ use super::{
linked::LinkedStylesheetSources,
},
source_owner_text::OwnerStyleSheetSources,
- state::StyleDocumentState,
+ state::{StyleDocumentGenerationSnapshot, StyleDocumentState},
};
pub(super) struct DocumentStyleWorld {
@@ -30,6 +34,10 @@ pub(super) struct DocumentStyleWorld {
pub(super) struct DocumentStyleWorlds {
worlds: RefCell<HashMap<DomHandle, Rc<DocumentStyleWorld>>>,
+ /// Monotonic lower bound used when no active world exists. This keeps stale
+ /// computed-style wrappers distinguishable from later worlds without an
+ /// O(navigations) tombstone map keyed by every retired Document.
+ lifecycle_generation_floor: Cell<u64>,
}
impl DocumentStyleWorld {
@@ -68,18 +76,70 @@ impl DocumentStyleWorlds {
pub(super) fn new() -> Self {
Self {
worlds: RefCell::new(HashMap::new()),
+ lifecycle_generation_floor: Cell::new(0),
}
}
pub(super) fn for_document(&self, document: DomHandle) -> Rc<DocumentStyleWorld> {
- if let Some(world) = self.worlds.borrow().get(&document) {
- return Rc::clone(world);
+ if let Some(world) = self.active_world(document) {
+ return world;
}
let world = Rc::new(DocumentStyleWorld::new(document));
self.worlds.borrow_mut().insert(document, Rc::clone(&world));
world
}
+ pub(super) fn active_world(&self, document: DomHandle) -> Option<Rc<DocumentStyleWorld>> {
+ self.worlds.borrow().get(&document).map(Rc::clone)
+ }
+
+ /// Removes one Document's heavyweight style state without making a lookup
+ /// create it first.
+ ///
+ /// The lifecycle generation floor replaces per-Document tombstones. A
+ /// retired child/popup handle is terminal; same-handle `document.open()`
+ /// remains active and uses `clear_for_document_replacement` instead.
+ pub(super) fn retire_document(&self, document: DomHandle) -> bool {
+ let world = self.worlds.borrow_mut().remove(&document);
+ let Some(world) = world else {
+ return false;
+ };
+ // Clear before dropping the map's Rc so a short-lived borrower cannot
+ // keep the retired Stylist or stylesheet sources alive past this
+ // lifecycle boundary.
+ world.clear_for_document_replacement();
+ self.raise_lifecycle_generation_floor(world.document_state.generation_snapshot());
+ true
+ }
+
+ pub(super) fn generation_snapshot_for_document(
+ &self,
+ document: DomHandle,
+ ) -> StyleDocumentGenerationSnapshot {
+ if let Some(generations) = self
+ .worlds
+ .borrow()
+ .get(&document)
+ .map(|world| world.document_state.generation_snapshot())
+ {
+ return generations;
+ }
+ self.lifecycle_generation_snapshot()
+ }
+
+ fn lifecycle_generation_snapshot(&self) -> StyleDocumentGenerationSnapshot {
+ lifecycle_generation_snapshot(self.lifecycle_generation_floor.get())
+ }
+
+ fn raise_lifecycle_generation_floor(&self, generations: StyleDocumentGenerationSnapshot) {
+ self.lifecycle_generation_floor.set(
+ self.lifecycle_generation_floor
+ .get()
+ .max(generations.computed_cache_generation)
+ .max(generations.target_context_epoch),
+ );
+ }
+
pub(super) fn clear_for_document_replacement(&self, document: DomHandle) {
self.for_document(document).clear_for_document_replacement();
}
@@ -130,4 +190,23 @@ impl DocumentStyleWorlds {
})
.collect()
}
+
+ #[cfg(test)]
+ pub(super) fn active_world_count(&self) -> usize {
+ self.worlds.borrow().len()
+ }
+
+ #[cfg(test)]
+ pub(super) fn contains_active_world(&self, document: DomHandle) -> bool {
+ self.worlds.borrow().contains_key(&document)
+ }
+}
+
+fn lifecycle_generation_snapshot(generation: u64) -> StyleDocumentGenerationSnapshot {
+ StyleDocumentGenerationSnapshot {
+ source_set_generation: 0,
+ computed_cache_generation: generation,
+ retained_style_system_generation: 0,
+ target_context_epoch: generation,
+ }
}
diff --git a/moli-renderer-v8/src/style_engine/mod.rs b/moli-renderer-v8/src/style_engine/mod.rs
index a2a61f929..8939d3a5d 100644
--- a/moli-renderer-v8/src/style_engine/mod.rs
+++ b/moli-renderer-v8/src/style_engine/mod.rs
@@ -209,10 +209,19 @@ impl MoliStyleEngine {
.map(|document| self.world_for_document(document))
}
+ pub(in crate::style_engine) fn active_owner_document_world(
+ &self,
+ host: &DomHost,
+ handle: DomHandle,
+ ) -> Option<Rc<DocumentStyleWorld>> {
+ host.owner_document_handle(handle)
+ .and_then(|document| self.document_worlds.active_world(document))
+ }
+
pub(crate) fn computed_cache_generation_for_document(&self, document: DomHandle) -> u64 {
- self.world_for_document(document)
- .document_state
- .computed_cache_generation()
+ self.document_worlds
+ .generation_snapshot_for_document(document)
+ .computed_cache_generation
}
pub(crate) fn computed_style_observation_generations(
@@ -226,7 +235,8 @@ impl MoliStyleEngine {
&self,
document: DomHandle,
) -> Option<StylesheetResourceSnapshot> {
- self.world_for_document(document)
+ self.document_worlds
+ .active_world(document)?
.document_state
.stylesheet_resource_snapshot(document)
}
@@ -240,7 +250,7 @@ impl MoliStyleEngine {
&self,
document: DomHandle,
) -> Option<Rc<FullStyleWorldSnapshot>> {
- let world = self.world_for_document(document);
+ let world = self.document_worlds.active_world(document)?;
world
.document_state
.try_with_retained_style_system(|retained| {
@@ -353,9 +363,9 @@ impl MoliStyleEngine {
}
pub(crate) fn target_context_epoch_for_document(&self, document: DomHandle) -> u64 {
- self.world_for_document(document)
- .document_state
- .target_context_epoch()
+ self.document_worlds
+ .generation_snapshot_for_document(document)
+ .target_context_epoch
}
#[cfg(test)]
@@ -374,7 +384,9 @@ impl MoliStyleEngine {
&self,
document: DomHandle,
) -> usize {
- self.world_for_document(document).pseudo_style_cache.len()
+ self.document_worlds
+ .active_world(document)
+ .map_or(0, |world| world.pseudo_style_cache.len())
}
#[cfg(test)]
@@ -662,6 +674,30 @@ impl MoliStyleEngine {
.clear_for_document_replacement(document);
}
+ /// Retires all style state owned by a Document that left its browsing
+ /// context. Unlike `clear_for_document_replacement`, this removes the world
+ /// itself because child/popup navigation installs a different Document.
+ /// Same-handle `document.open()` replacement continues to use `clear`.
+ pub(crate) fn retire_document_style_world(&mut self, document: DomHandle) -> bool {
+ self.clear_owner_document_indexes_for_document(document);
+ self.dom_adapter.clear_element_data_for_document(document);
+ self.dom_adapter
+ .clear_inline_style_attributes_for_document(document);
+ self.dom_adapter
+ .clear_shadow_cascade_data_for_document(document);
+ self.document_worlds.retire_document(document)
+ }
+
+ #[cfg(test)]
+ pub(crate) fn active_document_style_world_count_for_test(&self) -> usize {
+ self.document_worlds.active_world_count()
+ }
+
+ #[cfg(test)]
+ pub(crate) fn document_style_world_is_active_for_test(&self, document: DomHandle) -> bool {
+ self.document_worlds.contains_active_world(document)
+ }
+
/// Drops the exact target's canonical style and cached pseudo values after
/// it leaves every rendered style context.
///
diff --git a/moli-renderer-v8/src/style_engine/source/mod.rs b/moli-renderer-v8/src/style_engine/source/mod.rs
index 09068a547..77dfad212 100644
--- a/moli-renderer-v8/src/style_engine/source/mod.rs
+++ b/moli-renderer-v8/src/style_engine/source/mod.rs
@@ -785,7 +785,10 @@ impl MoliStyleEngine {
host: &DomHost,
handle: DomHandle,
) -> super::InlineStyleCspState {
- self.owner_document_world(host, handle)
+ // This is a metadata lookup performed while resolving CSSOM property
+ // names. In particular, a held declaration from a retired iframe may
+ // reach it; a read must not recreate that Document's heavyweight world.
+ self.active_owner_document_world(host, handle)
.map(|world| world.inline_style_metadata.csp_state(handle))
.unwrap_or_default()
}
diff --git a/moli-renderer-v8/src/style_engine/tests/subtree_cache.rs b/moli-renderer-v8/src/style_engine/tests/subtree_cache.rs
index fd1041dc3..0d02dd1a4 100644
--- a/moli-renderer-v8/src/style_engine/tests/subtree_cache.rs
+++ b/moli-renderer-v8/src/style_engine/tests/subtree_cache.rs
@@ -7471,6 +7471,73 @@ fn document_replacement_cleanup_preserves_unreplaced_document_world_and_shared_s
);
}
+#[test]
+fn retired_document_world_releases_heavy_state_without_losing_its_generation_barrier() {
+ let mut host = test_host();
+ let document = host.create_detached_html_document();
+ let target = host.create_element("div");
+ assert!(host.append_child(document, target));
+
+ let mut engine = MoliStyleEngine::new();
+ let document_url = url::Url::parse("https://example.test/").unwrap();
+ let inputs = FullStyleWorldSnapshot::default();
+ assert!(
+ engine
+ .computed_style_property_value(
+ &host,
+ &document_url,
+ target,
+ "display",
+ None,
+ &inputs,
+ None,
+ )
+ .is_some()
+ );
+ assert!(engine.dom_adapter.has_element_data(target));
+ assert!(engine.document_style_world_is_active_for_test(document));
+ assert_eq!(engine.active_document_style_world_count_for_test(), 1);
+ let weak_world = std::rc::Rc::downgrade(&engine.world_for_document(document));
+ let published_generation = engine.computed_cache_generation_for_document_for_test(document);
+
+ assert!(engine.retire_document_style_world(document));
+ assert!(
+ weak_world.upgrade().is_none(),
+ "retirement must release the world allocation, not merely remove its contents"
+ );
+ assert!(!engine.document_style_world_is_active_for_test(document));
+ assert_eq!(engine.active_document_style_world_count_for_test(), 0);
+ assert!(!engine.dom_adapter.has_element_data(target));
+
+ let retired_generation = engine.computed_cache_generation_for_document_for_test(document);
+ assert!(
+ retired_generation > published_generation,
+ "retirement must invalidate wrappers published by the old world"
+ );
+ assert_eq!(
+ engine.active_document_style_world_count_for_test(),
+ 0,
+ "a stale wrapper generation read must not resurrect the retired world"
+ );
+ assert!(
+ engine
+ .stylesheet_resource_snapshot_for_document(document)
+ .is_none()
+ );
+ assert!(
+ engine
+ .retained_stylesheet_query_snapshot_for_document(document)
+ .is_none()
+ );
+ assert_eq!(engine.active_document_style_world_count_for_test(), 0);
+
+ assert_eq!(
+ engine.computed_cache_generation_for_document_for_test(document),
+ retired_generation
+ );
+ assert!(!engine.document_style_world_is_active_for_test(document));
+}
+
#[test]
fn detached_retained_rebuild_preserves_active_document_adapter_element_data() {
let mut host = test_host();