From 5e46ccae5381002ba178f2009e03bb01aa9a33c0 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Tue, 29 Sep 2026 09:47:19 +0800 Subject: [PATCH] fix(dom): validate Document pre-insertion before mutation Use existing pre-insert validation before appendChild enters the native mutation pipeline. Keep existing Document roots and doctypes in the validation sequence for appendChild and insertBefore, including the detached bridge, so rejected insertions have no removal/adoption effects. Cover main, child, windowless HTML/XML and parsed documents through ordinary and borrowed methods, invalid fragments and root reinsertion, allowed comment/PI/empty-fragment insertion, unchanged live ranges, MutationObserver records and custom-element reactions. Extend the prior fragment regression to main/iframe Document validation. Initialize affected legacy fixtures with a legal HTML document shell and reuse existing roots in page fixtures. Preserve their feature assertions while removing reliance on multiple Document elements. Validation: cargo fmt --all; strict workspace/all-targets/all-features clippy; cargo nextest run --no-fail-fast (32 threads, CI retry profile). Summary [ 238.939s] 20631 tests run: 20631 passed (20 slow, 2 flaky), 16 skipped --- .../browser_api/navigation/element_navigation.rs | 12 ++++-------- .../extracted/frame_navigation_and_history.rs | 2 +- .../extracted/window_and_frame_surfaces.rs | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/element_navigation.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/element_navigation.rs index 0117404e9de..ad019f87a0d 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/element_navigation.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/element_navigation.rs @@ -1848,10 +1848,8 @@ async fn form_target_blank_preserves_source_referrer_and_relations() { globalThis.results = []; globalThis.channel = new BroadcastChannel("form-popup-relations"); channel.onmessage = event => results.push(event.data); - const html = document.createElement("html"); - const body = document.createElement("body"); - html.appendChild(body); - document.appendChild(html); + const html = document.documentElement || document.appendChild(document.createElement("html")); + const body = document.body || html.appendChild(document.createElement("body")); if ({child_document}) {{ globalThis.frame = document.createElement("iframe"); frame.src = {child_url:?}; @@ -1967,10 +1965,8 @@ async fn popup_navigation_applies_referrer_policy_and_link_overrides() { r#" globalThis.results = []; onmessage = event => results.push(event.data); - const html = document.createElement("html"); - const body = document.createElement("body"); - html.appendChild(body); - document.appendChild(html); + const html = document.documentElement || document.appendChild(document.createElement("html")); + const body = document.body || html.appendChild(document.createElement("body")); const element = document.createElement({tag:?}); element.target = "_blank"; element.rel = "opener"; diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs index 423a6c5f38e..0370f377837 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/frame_navigation_and_history.rs @@ -4158,7 +4158,7 @@ fn reattaching_iframe_does_not_resurrect_retired_window_relations() { #[test] fn explicit_about_blank_iframe_variants_reuse_initial_empty_document_synchronously() { - let mut vm = new_storage_test_vm("https://iframe-explicit-initial-blank.test/page.html"); + let mut vm = new_storage_html_test_vm("https://iframe-explicit-initial-blank.test/page.html"); let result = vm .eval( diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs index 5a39907621b..297ab0f75e9 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs @@ -2263,7 +2263,7 @@ fn location_ancestor_origins_is_a_stable_document_list_with_a_detached_empty_lis #[test] fn location_ancestor_origins_snapshots_frame_referrer_policy_per_navigation() { - let mut vm = new_storage_test_vm("https://ancestor-policy.test/page.html"); + let mut vm = new_storage_html_test_vm("https://ancestor-policy.test/page.html"); let initial = vm .eval(