From fd91c25a887463cdc6e14ecdeefb569d1a94d3e7 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Thu, 3 Sep 2026 17:05:58 +0800 Subject: [PATCH] fix(history): preserve URL for empty state updates --- .../browser_api/navigation/history_api.rs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/history_api.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/history_api.rs index be29027acea..b516c87acbf 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/history_api.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/history_api.rs @@ -208,6 +208,47 @@ fn history_activity_is_checked_after_argument_conversion() { assert_eq!(result, ["SecurityError:1"; 6].join("|")); } +#[test] +fn history_mutation_empty_url_preserves_current_document_url() { + let mut vm = new_storage_test_vm("https://example.com/path/page.html?query=value#initial"); + + let result = vm + .eval( + r#" + (() => { + const root = document.documentElement || + document.appendChild(document.createElement("html")); + const head = document.head || + root.insertBefore(document.createElement("head"), root.firstChild); + const base = document.createElement("base"); + base.href = "https://example.com/different/base/"; + head.appendChild(base); + + const before = location.href; + history.pushState({ kind: "push" }, "", ""); + const afterPush = location.href; + history.replaceState({ kind: "replace" }, "", ""); + + return JSON.stringify({ + before, + afterPush, + afterReplace: location.href, + baseURI: document.baseURI, + length: history.length, + state: history.state.kind, + entryUrl: navigation.currentEntry.url + }); + })() + "#, + ) + .expect("empty history URL should preserve the current document URL"); + + assert_eq!( + result, + r#"{"before":"https://example.com/path/page.html?query=value#initial","afterPush":"https://example.com/path/page.html?query=value#initial","afterReplace":"https://example.com/path/page.html?query=value#initial","baseURI":"https://example.com/different/base/","length":2,"state":"replace","entryUrl":"https://example.com/path/page.html?query=value#initial"}"# + ); +} + #[test] fn history_state_preserves_structured_clone_values_not_representable_as_json() { let mut vm = new_storage_test_vm("https://example.com/base");