From ccd00395af2d7ff7bbfcf45dad85ac8a2dcc275f Mon Sep 17 00:00:00 2001 From: ldm0 Date: Thu, 8 Oct 2026 01:07:42 +0800 Subject: [PATCH] fix(cssom): validate document flat-tree style targets --- .../src/native_bridge/element/styles.rs | 68 ++++++----- .../script_vm/tests/dom_xhr/computed_style.rs | 1 + .../dom_xhr/computed_style/cssom_flat_tree.js | 111 ++++++++++++++++++ .../dom_xhr/computed_style/cssom_flat_tree.rs | 76 ++++++++++++ 4 files changed, 227 insertions(+), 29 deletions(-) create mode 100644 moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_flat_tree.js create mode 100644 moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_flat_tree.rs diff --git a/moli-renderer-v8/src/native_bridge/element/styles.rs b/moli-renderer-v8/src/native_bridge/element/styles.rs index 67732b57e37..c0211ec90e4 100644 --- a/moli-renderer-v8/src/native_bridge/element/styles.rs +++ b/moli-renderer-v8/src/native_bridge/element/styles.rs @@ -123,13 +123,6 @@ fn style_object_forces_empty_computed<'s>( unsafe { &*runtime_ptr }.retire_computed_style_for_inactive_handle(*handle); return true; } - if let Some(value) = - get_private_value(scope, style, STYLE_DECLARATION_TARGET_EMPTY_COMPUTED_SLOT) - && value.is_boolean() - && target_context_epoch_is_current(scope, unsafe { &*runtime_ptr }, style) - { - return value.boolean_value(scope); - } let empty = match bridge_handle { BridgeHandle::ComputedStyle(handle, ref descriptor) => match descriptor.target { ComputedStyleTargetKey::ChildFrame(_) => { @@ -143,7 +136,7 @@ fn style_object_forces_empty_computed<'s>( empty } ComputedStyleTargetKey::PopupDocument(document_handle) => { - !popup_document_target_is_current(unsafe { &*runtime_ptr }, handle, document_handle) + !popup_document_target_is_active(unsafe { &*runtime_ptr }, handle, document_handle) } ComputedStyleTargetKey::Dynamic => { computed_style_target_context(scope, unsafe { &*runtime_ptr }, handle, None) @@ -158,7 +151,7 @@ fn style_object_forces_empty_computed<'s>( empty } -fn popup_document_target_is_current( +fn popup_document_target_is_active( runtime: &PageScriptHost, target: DomHandle, popup_document: DomHandle, @@ -168,6 +161,7 @@ fn popup_document_target_is_current( .node(target) .and_then(crate::dom::native::Node::owner_document); target_document == Some(popup_document) + && computed_style_target_is_in_document_flat_tree(runtime, target, popup_document) && runtime .lightweight_popup_id_for_document_handle(popup_document) .is_some_and(|popup_id| runtime.lightweight_popup_is_open(popup_id)) @@ -492,6 +486,9 @@ pub(crate) fn computed_style_target_context( let Some(document) = host.dom_host().owner_document_handle(handle) else { return ComputedStyleTargetContext::EmptyForDetached; }; + if !computed_style_target_is_in_document_flat_tree(host, handle, document) { + return ComputedStyleTargetContext::EmptyForDetached; + } if let Some(frame_handle) = computed_style_target_child_frame_handle(scope, host, handle) .or_else(|| { computed_style_target_requesting_child_frame_handle( @@ -508,7 +505,7 @@ pub(crate) fn computed_style_target_context( rendered, }; } - if computed_style_target_is_in_flat_tree(host, handle) { + if document == host.document_handle() { ComputedStyleTargetContext::ActiveDocument } else if host .lightweight_popup_id_for_document_handle(document) @@ -692,6 +689,9 @@ fn document_snapshot_style_context( .with_read_document(Some(document)), }) } else { + if !computed_style_target_is_in_document_flat_tree(host, handle, document) { + return None; + } let frame_handle = host.child_browsing_context_host_for_document_handle(document)?; if style_property_value(host, frame_handle, StyleMode::Computed, "display") == "none" { return Some(DocumentSnapshotStyleComputation::HiddenChildFrame); @@ -781,6 +781,10 @@ fn iframe_handle_viewport_with_depth( ) } +fn computed_style_target_is_in_flat_tree(host: &PageScriptHost, handle: DomHandle) -> bool { + computed_style_target_is_in_document_flat_tree(host, handle, host.document_handle()) +} + fn computed_style_target_is_in_document_flat_tree( host: &PageScriptHost, handle: DomHandle, @@ -788,23 +792,7 @@ fn computed_style_target_is_in_document_flat_tree( ) -> bool { let mut current = handle; loop { - if current == document { - return host - .dom_host() - .node(current) - .is_some_and(crate::dom::native::Node::is_document); - } - match computed_style_flat_tree_step(host, current) { - ControlFlow::Break(_) => return false, - ControlFlow::Continue(parent) => current = parent, - } - } -} - -fn computed_style_target_is_in_flat_tree(host: &PageScriptHost, handle: DomHandle) -> bool { - let mut current = handle; - loop { - match computed_style_flat_tree_step(host, current) { + match computed_style_flat_tree_step_for_document(host, current, document) { ControlFlow::Break(connected) => return connected, ControlFlow::Continue(parent) => current = parent, } @@ -814,16 +802,27 @@ fn computed_style_target_is_in_flat_tree(host: &PageScriptHost, handle: DomHandl fn computed_style_flat_tree_step( host: &PageScriptHost, current: DomHandle, +) -> ControlFlow { + computed_style_flat_tree_step_for_document(host, current, host.document_handle()) +} + +fn computed_style_flat_tree_step_for_document( + host: &PageScriptHost, + current: DomHandle, + document: DomHandle, ) -> ControlFlow { #[cfg(test)] snapshot_tests::note_flat_tree_step(); + if let Some(slot) = host.dom_host().assigned_slot_for_node(current) { + return ControlFlow::Continue(slot); + } let Some(parent) = host .dom_host() .node(current) .and_then(crate::dom::native::Node::parent_node) else { return ControlFlow::Break( - current == host.dom_host().document_handle() + current == document && host .dom_host() .node(current) @@ -838,7 +837,18 @@ fn computed_style_flat_tree_step( } if host.dom_host().shadow_root_handle(parent).is_some() && computed_style_node_is_slotable_for_flat_tree(host, current) - && host.dom_host().assigned_slot_for_node(current).is_none() + { + return ControlFlow::Break(false); + } + if host + .dom_host() + .node(parent) + .is_some_and(|node| node.is_html_element_named("slot")) + && host.dom_host().containing_shadow_root(parent).is_some() + && !host + .dom_host() + .assigned_nodes_for_slot_with_options(parent, false) + .is_empty() { return ControlFlow::Break(false); } 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 f822d3787f7..18ed83fed76 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 @@ -74,6 +74,7 @@ mod css_transforms; mod css_unparsed; mod css_variable_fonts; mod cssom_realm; +mod cssom_flat_tree; mod nested_document_invalidation; mod properties_and_selectors; mod stylesheet_and_document_lifecycle; diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_flat_tree.js b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_flat_tree.js new file mode 100644 index 00000000000..503ac14060b --- /dev/null +++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_flat_tree.js @@ -0,0 +1,111 @@ +(() => { + const checks = []; + const record = (name, verify) => { + try { + checks.push({name, passed: verify() === true}); + } catch (error) { + checks.push({name, passed: false, error: String(error)}); + } + }; + const frame = document.createElement('iframe'); + document.body.appendChild(frame); + const popup = window.open('about:blank', '_blank'); + const owners = [window, frame.contentWindow, popup]; + try { + for (const [ownerIndex, owner] of owners.entries()) { + const doc = owner.document; + const callers = ownerIndex === 1 ? [window, owner] : [owner]; + const styles = (target, pseudo) => callers.map(caller => caller.getComputedStyle(target, pseudo)); + const observe = (name, declarations, connected) => { + for (const [index, declaration] of declarations.entries()) { + const label = `owner ${ownerIndex}/${name}/caller ${index}`; + record(label + '/length', () => connected ? declaration.length > 0 : declaration.length === 0); + record(label + '/color', () => declaration.color === (connected ? 'rgb(1, 2, 3)' : '')); + record(label + '/font', () => declaration.fontSize === (connected ? '19px' : '')); + record(label + '/custom', () => declaration.getPropertyValue('--flat-token') === (connected ? 'local' : '')); + record(label + '/item', () => connected ? declaration.item(0) !== '' : declaration.item(0) === ''); + record(label + '/cssText', () => declaration.cssText === ''); + } + }; + const create = () => { + const target = doc.createElement('div'); + target.style.cssText = 'color: rgb(1, 2, 3); font-size: 19px; --flat-token: local'; + return target; + }; + for (const [kind, target] of [['HTML', create()], ['SVG', doc.createElementNS('http://www.w3.org/2000/svg', 'svg')]]) { + target.style.cssText = 'color: rgb(1, 2, 3); font-size: 19px; --flat-token: local'; + const detached = styles(target); + observe(kind + '/detached', detached, false); + doc.body.appendChild(target); + const connected = styles(target); + observe(kind + '/inserted held', detached, true); + observe(kind + '/inserted fresh', connected, true); + target.remove(); + observe(kind + '/removed held', connected, false); + observe(kind + '/removed fresh', styles(target), false); + doc.body.appendChild(target); + observe(kind + '/reinserted detached held', detached, true); + observe(kind + '/reinserted connected held', connected, true); + target.remove(); + } + const host = doc.createElement('section'); + doc.body.appendChild(host); + const root = host.attachShadow({mode: 'closed'}); + const light = create(), descendant = create(); + light.appendChild(descendant); + host.appendChild(light); + const lightHeld = styles(light), descendantHeld = styles(descendant); + observe('unslotted light', lightHeld, false); + observe('unslotted descendant', descendantHeld, false); + const slot = doc.createElement('slot'); + root.appendChild(slot); + observe('assigned light held', lightHeld, true); + observe('assigned descendant held', descendantHeld, true); + const fallback = create(), fallbackDescendant = create(); + fallback.appendChild(fallbackDescendant); + slot.appendChild(fallback); + const fallbackHeld = styles(fallback), fallbackDescendantHeld = styles(fallbackDescendant); + observe('inactive fallback', fallbackHeld, false); + observe('inactive fallback descendant', fallbackDescendantHeld, false); + light.remove(); + observe('active fallback held', fallbackHeld, true); + observe('active fallback descendant held', fallbackDescendantHeld, true); + host.appendChild(light); + observe('inactive fallback again', fallbackHeld, false); + observe('inactive fallback descendant again', fallbackDescendantHeld, false); + slot.remove(); + observe('unassigned light held', lightHeld, false); + observe('unassigned descendant held', descendantHeld, false); + const nested = doc.createElement('article'); + const nestedRoot = nested.attachShadow({mode: 'open'}); + nested.appendChild(slot); + root.appendChild(nested); + observe('slot outside flat tree', lightHeld, false); + observe('slot descendant outside flat tree', descendantHeld, false); + nestedRoot.appendChild(doc.createElement('slot')); + observe('slot enters flat tree', lightHeld, true); + observe('slot descendant enters flat tree', descendantHeld, true); + host.remove(); + observe('removed shadow host light', lightHeld, false); + observe('removed shadow host descendant', descendantHeld, false); + doc.body.appendChild(host); + observe('reinserted shadow host light', lightHeld, true); + observe('reinserted shadow host descendant', descendantHeld, true); + host.remove(); + const fragment = doc.createDocumentFragment(), fragmentTarget = create(); + fragment.appendChild(fragmentTarget); + const fragmentHeld = styles(fragmentTarget); + observe('fragment', fragmentHeld, false); + doc.body.appendChild(fragment); + observe('fragment insertion held', fragmentHeld, true); + fragmentTarget.remove(); + } + } finally { + popup.close(); + frame.remove(); + } + const passed = checks.filter(row => row.passed).length; + globalThis.__cssomFlatTreeResults = {complete: true, total: checks.length, passed, checks}; + globalThis.__uiEventResults = globalThis.__cssomFlatTreeResults; + return true; +})() diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_flat_tree.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_flat_tree.rs new file mode 100644 index 00000000000..915d8733f6d --- /dev/null +++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_flat_tree.rs @@ -0,0 +1,76 @@ +use super::*; + +#[test] +fn computed_style_live_declarations_track_document_flat_tree_membership() { + let mut vm = new_storage_page_task_executor_test_vm("https://computed-style-flat-tree.test/"); + vm.eval(include_str!("cssom_flat_tree.js")) + .expect("document flat-tree transitions should evaluate"); + assert_eq!(vm.eval("__cssomFlatTreeResults.complete").unwrap(), "true"); + assert_eq!( + vm.eval("JSON.stringify(__cssomFlatTreeResults.checks.filter(row => !row.passed))") + .unwrap(), + "[]" + ); + assert_eq!( + vm.eval("__cssomFlatTreeResults.total === 864 && __cssomFlatTreeResults.passed === 864") + .unwrap(), + "true" + ); +} + +#[cfg(feature = "devtools")] +#[test] +fn inspector_computed_style_tracks_child_flat_tree_membership() { + let mut vm = new_storage_page_task_executor_test_vm("https://inspector-flat-tree.test/"); + vm.eval( + r#" +const frame = document.createElement('iframe'); +document.body.appendChild(frame); +const childDocument = frame.contentDocument; +globalThis.__inspectorFlatTarget = childDocument.createElement('div'); +__inspectorFlatTarget.id = 'inspector-flat-target'; +__inspectorFlatTarget.style.color = 'rgb(1, 2, 3)'; +globalThis.__inspectorFlatHost = childDocument.createElement('section'); +childDocument.body.appendChild(__inspectorFlatHost); +__inspectorFlatHost.attachShadow({mode: 'open'}); +"#, + ) + .expect("child inspector fixture should initialize"); + let target = element_handle_by_id(&vm, "inspector-flat-target"); + + for (mutation, active) in [ + ("void 0", false), + ( + "__inspectorFlatHost.ownerDocument.body.appendChild(__inspectorFlatTarget)", + true, + ), + ( + "__inspectorFlatHost.appendChild(__inspectorFlatTarget)", + false, + ), + ( + "__inspectorFlatHost.shadowRoot.appendChild(__inspectorFlatHost.ownerDocument.createElement('slot'))", + true, + ), + ("__inspectorFlatTarget.remove()", false), + ] { + vm.eval(mutation) + .expect("flat-tree mutation should succeed"); + let properties = vm + .computed_style_properties_for_inspector_handle(target) + .expect("an existing child element must keep its inspector identity"); + if active { + assert!( + properties + .iter() + .any(|(name, value)| name == "color" && value == "rgb(1, 2, 3)"), + "computed values were missing after {mutation}" + ); + } else { + assert!( + properties.is_empty(), + "inactive element exposed computed values after {mutation}" + ); + } + } +}