Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(history): preserve popup history across document restoration

ldm053a95cd54343
diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_serialize.rs b/moli-renderer-v8/src/context_bootstrap/navigation_serialize.rs
index d0a41420d..2c29b8cc4 100644
--- a/moli-renderer-v8/src/context_bootstrap/navigation_serialize.rs
+++ b/moli-renderer-v8/src/context_bootstrap/navigation_serialize.rs
@@ -1,7 +1,8 @@
use super::navigation_activation::navigation_activation_value;
use super::navigation_entry::{
history_entries, history_index, navigation_current_entry, navigation_entry_document_id,
- navigation_entry_referrer_policy_value, navigation_entry_url_value,
+ navigation_entry_id_value, navigation_entry_key_value, navigation_entry_referrer_policy_value,
+ navigation_entry_url_value,
};
use super::navigation_entry_state::{
history_entry_state_snapshot, navigation_entry_state_snapshot,
@@ -100,16 +101,10 @@ pub(super) fn serialize_navigation_entry_object<'s>(
entry: v8::Local<'s, v8::Object>,
history_entries: &[NavigationHistorySerializedEntry],
) -> NavigationHistorySerializedEntry {
- let id = get_own_static_property(scope, entry, "id")
- .and_then(|value| value.to_string(scope))
- .map(|value| value.to_rust_string_lossy(scope))
- .filter(|value| !value.is_empty())
+ let id = navigation_entry_id_value(scope, entry)
.map(NavigationHistoryEntryId::from_serialized)
.unwrap_or_else(NavigationHistoryEntryId::allocate);
- let key = get_own_static_property(scope, entry, "key")
- .and_then(|value| value.to_string(scope))
- .map(|value| value.to_rust_string_lossy(scope))
- .filter(|value| !value.is_empty())
+ let key = navigation_entry_key_value(scope, entry)
.map(NavigationHistoryEntryKey::from_serialized)
.unwrap_or_else(NavigationHistoryEntryKey::allocate);
if let Some(snapshot) = history_entries
@@ -230,16 +225,13 @@ pub(super) fn serialize_history_entries<'s>(
.filter(|value| *value >= 0)
.map(|value| value as u32)
.unwrap_or(index);
- let id = get_own_static_property(scope, entry, "id")
- .and_then(|value| value.to_string(scope))
- .map(|value| value.to_rust_string_lossy(scope))
- .filter(|value| !value.is_empty())
+ // Public Navigation getters may hide entries from an inactive view.
+ // Persist their stored identities so restoration still matches the
+ // shared session history, including a popup's forward entries.
+ let id = navigation_entry_id_value(scope, entry)
.map(NavigationHistoryEntryId::from_serialized)
.unwrap_or_else(NavigationHistoryEntryId::allocate);
- let key = get_own_static_property(scope, entry, "key")
- .and_then(|value| value.to_string(scope))
- .map(|value| value.to_rust_string_lossy(scope))
- .filter(|value| !value.is_empty())
+ let key = navigation_entry_key_value(scope, entry)
.map(NavigationHistoryEntryKey::from_serialized)
.unwrap_or_else(NavigationHistoryEntryKey::allocate);
let document_id = navigation_entry_document_id(scope, entry)
diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_traversal_plan.rs b/moli-renderer-v8/src/context_bootstrap/navigation_traversal_plan.rs
index df6132e59..d154ce2df 100644
--- a/moli-renderer-v8/src/context_bootstrap/navigation_traversal_plan.rs
+++ b/moli-renderer-v8/src/context_bootstrap/navigation_traversal_plan.rs
@@ -171,6 +171,14 @@ pub(super) fn history_delta_traversal_plan<'s>(
let owner = runtime_window_owner(scope, history);
let host = unsafe { &mut *crate::util::context_host_ptr_from_global_bridge(scope)? };
let binding = super::session_history::binding(scope, host, owner);
+ // Further requests made through the outgoing popup Document's History
+ // are discarded when that Document is replaced. Do not rebase them onto
+ // the destination's projected history while its resource load waits.
+ if let Some(popup_id) = binding.popup
+ && host.lightweight_popup_has_pending_history_traversal(popup_id)
+ {
+ return None;
+ }
let model = host.session_histories.get_mut(binding.popup).clone();
let source = host
.pending_joint_history_step(&model)
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 e6a7addda..d1d9f16db 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/popups.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/popups.rs
@@ -194,6 +194,7 @@ pub(super) struct PendingLightweightPopupDocumentLoad {
pub(super) target: LightweightPopupDocumentFetchTarget,
pub(super) document_state: LightweightPopupDocumentState,
pub(super) resource_loader: Option<crate::network::navigation::NavigationResourceLoader>,
+ history_traversal: bool,
}
struct LightweightPopupClassicScriptContinuation {
@@ -1436,6 +1437,14 @@ impl JsContextHost {
.any(|pending| pending.target.task().popup_id() == popup_id)
}
+ pub(crate) fn lightweight_popup_has_pending_history_traversal(&self, popup_id: u64) -> bool {
+ self.pending_lightweight_popup_document_loads
+ .values()
+ .any(|pending| {
+ pending.target.task().popup_id() == popup_id && pending.history_traversal
+ })
+ }
+
pub(crate) fn lightweight_popup_is_open(&self, popup_id: u64) -> bool {
self.lightweight_popup_record(popup_id)
.is_some_and(LightweightPopupBrowsingContextRecord::is_open)
@@ -1694,15 +1703,19 @@ impl JsContextHost {
self.queue_lightweight_popup_load_event(navigation_task);
return true;
}
- if self
- .start_lightweight_popup_document_load(
- navigation_task,
- target_url,
- previous_url,
- navigation_state,
- )
- .is_none()
- {
+ if let Some(load_id) = self.start_lightweight_popup_document_load(
+ navigation_task,
+ target_url,
+ previous_url,
+ navigation_state,
+ ) {
+ if let Some(pending) = self
+ .pending_lightweight_popup_document_loads
+ .get_mut(&load_id)
+ {
+ pending.history_traversal = true;
+ }
+ } else {
self.queue_lightweight_popup_load_event(navigation_task);
}
true
@@ -2205,6 +2218,7 @@ impl JsContextHost {
target,
document_state: document_state.clone(),
resource_loader: resource_loader.clone(),
+ history_traversal: false,
},
);
if let Some(snapshot) = local_snapshot {
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
index 050c51af1..82481fce9 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
@@ -25011,6 +25011,147 @@ JSON.stringify({
);
}
+#[tokio::test]
+async fn window_open_popup_history_forward_from_restored_load() {
+ let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
+ let mut vm =
+ new_page_task_executor_test_vm_with_loader("https://example.com/page.html", &loader);
+ vm.eval(
+ r#"
+ globalThis.popupHistoryLoads = [];
+ globalThis.popupHistoryRestoring = false;
+ globalThis.popupFirstUrl = URL.createObjectURL(new Blob([`<!doctype html><script>
+ onload = () => {
+ opener.popupHistoryLoads.push('first');
+ if (opener.popupHistoryRestoring) history.forward();
+ };
+ <\/script>`], {type:'text/html'}));
+ globalThis.popupSecondUrl = URL.createObjectURL(new Blob([`<!doctype html><script>
+ onload = () => opener.popupHistoryLoads.push('second');
+ <\/script>`], {type:'text/html'}));
+ globalThis.historyPopup = open(popupFirstUrl, 'historyForwardPopup');
+ "#,
+ )
+ .expect("popup setup should evaluate");
+ advance_page_task_executor_until_eval_equals(
+ &mut vm,
+ &loader,
+ "popupHistoryLoads.join(',')",
+ "first",
+ "first popup load",
+ )
+ .await;
+ vm.eval("open(popupSecondUrl, 'historyForwardPopup')")
+ .expect("second popup navigation should evaluate");
+ advance_page_task_executor_until_eval_equals(
+ &mut vm,
+ &loader,
+ "popupHistoryLoads.join(',')",
+ "first,second",
+ "second popup load",
+ )
+ .await;
+ vm.eval("popupHistoryRestoring = true; historyPopup.history.back()")
+ .expect("popup back traversal should evaluate");
+ advance_page_task_executor_until_eval_equals(
+ &mut vm,
+ &loader,
+ "popupHistoryLoads.join(',')",
+ "first,second,first,second",
+ "restored popup load should be able to traverse forward",
+ )
+ .await;
+ assert_eq!(
+ vm.eval(
+ "[historyPopup.location.href === popupSecondUrl, historyPopup.history.length].join('|')"
+ )
+ .expect("restored popup history should evaluate"),
+ "true|2"
+ );
+}
+
+#[tokio::test]
+async fn popup_history_back_from_departing_document_keeps_first_traversal() {
+ assert_popup_consecutive_history_back(true).await;
+}
+
+#[tokio::test]
+async fn popup_history_back_from_opener_keeps_first_traversal() {
+ assert_popup_consecutive_history_back(false).await;
+}
+
+async fn assert_popup_consecutive_history_back(from_popup: bool) {
+ let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
+ let mut vm =
+ new_page_task_executor_test_vm_with_loader("https://example.com/page.html", &loader);
+ vm.eval(&format!("globalThis.traverseFromPopup = {from_popup}"))
+ .expect("traversal initiator should evaluate");
+ vm.eval(
+ r#"
+ globalThis.popupTraversalLoads = [];
+ globalThis.popupTraversalUrls = [0, 1, 2].map(index =>
+ URL.createObjectURL(new Blob([`<!doctype html><script>
+ onload = () => {
+ opener.popupTraversalLoads.push(${index});
+ if (${index} === 2 && opener.traverseFromPopup) {
+ history.go(-10);
+ history.back();
+ history.back();
+ }
+ };
+ <\/script>`], {type:'text/html'})));
+ "#,
+ )
+ .expect("popup traversal fixtures should evaluate");
+ for index in 0..3 {
+ vm.eval(&format!(
+ "globalThis.traversalPopup = open(popupTraversalUrls[{index}], 'consecutiveHistoryPopup')"
+ ))
+ .expect("popup navigation should evaluate");
+ let expected = match index {
+ 0 => "0",
+ 1 => "0,1",
+ _ if from_popup => "0,1,2,1",
+ _ => "0,1,2",
+ };
+ advance_page_task_executor_until_eval_equals(
+ &mut vm,
+ &loader,
+ "popupTraversalLoads.join(',')",
+ expected,
+ "popup documents should load in traversal order",
+ )
+ .await;
+ }
+ if !from_popup {
+ vm.eval("traversalPopup.history.back(); traversalPopup.history.back()")
+ .expect("the opener should request both traversals through the outgoing History");
+ advance_page_task_executor_until_eval_equals(
+ &mut vm,
+ &loader,
+ "popupTraversalLoads.join(',')",
+ "0,1,2,1",
+ "only the first traversal from the outgoing popup History should commit",
+ )
+ .await;
+ }
+ assert_eq!(
+ vm.eval("String(traversalPopup.location.href === popupTraversalUrls[1])")
+ .expect("popup traversal destination should evaluate"),
+ "true"
+ );
+ vm.eval("traversalPopup.history.back()")
+ .expect("the restored Document's History should allow another traversal");
+ advance_page_task_executor_until_eval_equals(
+ &mut vm,
+ &loader,
+ "popupTraversalLoads.join(',')",
+ "0,1,2,1,0",
+ "completed popup loads must release the pending traversal",
+ )
+ .await;
+}
+
#[tokio::test]
async fn lightweight_popup_cross_document_navigation_clears_old_onload_handler() {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");