Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(window): resolve parent targets from the receiver

ldm0edaa12ed8921
diff --git a/moli-renderer-v8/src/context_bootstrap/window_runtime/dialogs.rs b/moli-renderer-v8/src/context_bootstrap/window_runtime/dialogs.rs
index eb28ae310..1ad059145 100644
--- a/moli-renderer-v8/src/context_bootstrap/window_runtime/dialogs.rs
+++ b/moli-renderer-v8/src/context_bootstrap/window_runtime/dialogs.rs
@@ -245,8 +245,18 @@ pub(crate) fn window_open_callback<'s>(
target @ (SpecialBrowsingContextTarget::Parent | SpecialBrowsingContextTarget::Top),
) = special_target
{
+ let source_window = if window_receiver_child_handle(scope, args.this()).is_some() {
+ args.this()
+ } else {
+ entered_window
+ };
let navigation_url = if parsed.raw_url.is_empty() { "" } else { &url };
- match navigate_existing_browsing_context_target(scope, host_ptr, target, navigation_url) {
+ match navigate_existing_browsing_context_target(
+ scope,
+ source_window,
+ target,
+ navigation_url,
+ ) {
Some(window) => rv.set(window.into()),
None => rv.set(v8::null(scope).into()),
}
diff --git a/moli-renderer-v8/src/native_bridge/element/activation/targets.rs b/moli-renderer-v8/src/native_bridge/element/activation/targets.rs
index 80a0d6e44..abd33bc88 100644
--- a/moli-renderer-v8/src/native_bridge/element/activation/targets.rs
+++ b/moli-renderer-v8/src/native_bridge/element/activation/targets.rs
@@ -523,7 +523,7 @@ fn navigate_special_target_from_window<'s>(
pub(crate) fn navigate_existing_browsing_context_target<'s>(
scope: &mut v8::PinScope<'s, '_>,
- runtime_ptr: *mut JsContextHost,
+ source_window: v8::Local<'s, v8::Object>,
target: SpecialBrowsingContextTarget,
resolved_url: &str,
) -> Option<v8::Local<'s, v8::Object>> {
@@ -532,9 +532,6 @@ pub(crate) fn navigate_existing_browsing_context_target<'s>(
SpecialBrowsingContextTarget::Blank,
"a new-context target cannot use existing-context navigation"
);
- let dispatch_scope = unsafe { &*runtime_ptr }.entered_owner_dispatch_scope(scope);
- let source_window =
- browsing_context_window_for_dispatch_scope(scope, runtime_ptr, dispatch_scope)?;
navigate_special_target_from_window(scope, source_window, Some(target), resolved_url)
}
@@ -589,21 +586,22 @@ pub(crate) fn navigate_target_browsing_context<'s>(
)
)
{
+ let dispatch_scope = unsafe { &*runtime_ptr }.entered_owner_dispatch_scope(scope);
+ let Some(source_window) =
+ browsing_context_window_for_dispatch_scope(scope, runtime_ptr, dispatch_scope)
+ else {
+ return false;
+ };
return match special_target {
- Some(target) => {
- navigate_existing_browsing_context_target(scope, runtime_ptr, target, resolved_url)
- .is_some()
- }
- None => {
- let dispatch_scope = unsafe { &*runtime_ptr }.entered_owner_dispatch_scope(scope);
- let Some(source_window) =
- browsing_context_window_for_dispatch_scope(scope, runtime_ptr, dispatch_scope)
- else {
- return false;
- };
- navigate_special_target_from_window(scope, source_window, None, resolved_url)
- .is_some()
- }
+ Some(target) => navigate_existing_browsing_context_target(
+ scope,
+ source_window,
+ target,
+ resolved_url,
+ )
+ .is_some(),
+ None => navigate_special_target_from_window(scope, source_window, None, resolved_url)
+ .is_some(),
};
}
if special_target == Some(SpecialBrowsingContextTarget::Blank) {
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/popup_window.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/popup_window.rs
index af6f17cb3..2040e7fad 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/popup_window.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/popup_window.rs
@@ -44,6 +44,69 @@ fn window_open_empty_url_preserves_special_target_document() {
assert!(vm.take_pending_location_navigation_with_seed().is_none());
}
+#[test]
+fn window_open_parent_target_uses_the_receiver_browsing_context() {
+ for depth in [2, 3] {
+ for target in ["_parent", "_PaReNt"] {
+ for borrowed in [false, true] {
+ for url in ["", "https://window-open-parent.test/destination"] {
+ let mut vm = new_parsed_test_vm(
+ "https://window-open-parent.test/source",
+ "<!doctype html><body></body>",
+ );
+ let description =
+ format!("depth={depth} target={target} borrowed={borrowed} url={url:?}");
+ let result = vm
+ .eval(&format!(
+ r#"(() => {{
+ let receiver = window;
+ for (let i = 0; i < {depth}; ++i) {{
+ const frame = receiver.document.createElement('iframe');
+ receiver.document.body.appendChild(frame);
+ frame.srcdoc = '<body></body>';
+ receiver = frame.contentWindow;
+ }}
+ const expected = receiver.parent;
+ const parentHref = expected.location.href;
+ const receiverHref = receiver.location.href;
+ const seen = [];
+ navigation.onnavigate = event => {{
+ seen.push('top');
+ event.preventDefault();
+ }};
+ const open = {borrowed} ? window.open : receiver.open;
+ const opened = open.call(receiver, {url:?}, {target:?});
+ return JSON.stringify([
+ expected !== window,
+ opened === expected,
+ opened === window,
+ expected.location.href === ({url:?} || parentHref),
+ receiver.location.href === receiverHref,
+ location.href === 'https://window-open-parent.test/source',
+ seen
+ ]);
+ }})()"#
+ ))
+ .expect(&description);
+ assert_eq!(
+ serde_json::from_str::<serde_json::Value>(&result).unwrap(),
+ serde_json::json!([true, true, false, true, true, true, []]),
+ "{description}"
+ );
+ assert!(
+ vm.take_pending_popup_activations().is_empty(),
+ "{description}"
+ );
+ assert!(
+ vm.take_pending_location_navigation_with_seed().is_none(),
+ "{description}"
+ );
+ }
+ }
+ }
+ }
+}
+
#[tokio::test]
async fn window_open_lightweight_named_reuse_updates_dom_opener_synchronously() {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();