Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(navigation): prefer the source frame in named target lookup

ldm0772ecc12a3ca
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/lookup.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/lookup.rs
index c6dd50eb6..11f24ae3a 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/lookup.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/lookup.rs
@@ -257,6 +257,17 @@ impl JsContextHost {
key: &str,
document: DomHandle,
) -> Option<DomHandle> {
+ // Navigable lookup searches the current navigable's inclusive subtree.
+ // A same-named descendant must not shadow the source frame itself.
+ if !key.is_empty()
+ && let Some(handle) = self.child_browsing_context_host_for_document_handle(document)
+ && self
+ .child_browsing_contexts
+ .get(&handle)
+ .is_some_and(|entry| entry.matches_browsing_context_name(key))
+ {
+ return Some(handle);
+ }
self.sync_child_browsing_context_subtree(scope, document);
self.child_browsing_context_handle_by_name_in_document_order_from_document(key, document)
}
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 d94c8d304..f9af6e32d 100644
--- a/moli-renderer-v8/src/native_bridge/element/activation/targets.rs
+++ b/moli-renderer-v8/src/native_bridge/element/activation/targets.rs
@@ -509,11 +509,16 @@ pub(in crate::native_bridge) fn navigate_hyperlink_target_browsing_context<'s>(
if let Some(target_name) = target_name
&& special_target.is_none()
{
- return navigate_named_iframe_target(
+ let source_document = unsafe { &*runtime_ptr }
+ .dom_host()
+ .node(source_handle)
+ .and_then(|node| node.owner_document());
+ return navigate_named_iframe_target_from_document(
scope,
runtime_ptr,
target_name,
resolved_url,
+ source_document,
source_element,
) || navigate_hyperlink_popup_target(
scope,
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation.rs
index 44ff9ede1..a0f6410f8 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation.rs
@@ -1,5 +1,82 @@
use super::*;
+#[tokio::test]
+async fn named_element_navigation_prefers_its_source_frame_over_duplicate_names() {
+ for action in ["anchor", "submit", "requestSubmit"] {
+ for name in ["initial", "renamed", "shadowed"] {
+ let server =
+ StaticHttpServer::spawn_with_bodies(vec!["<!doctype html><body>selected".into()])
+ .await;
+ let base = server.base_url().origin().ascii_serialization();
+ let loader = static_http_loader([]);
+ let mut vm = new_storage_page_task_executor_test_vm_with_loader(
+ &format!("{base}/parent"),
+ &loader,
+ );
+ vm.eval(&format!(
+ r#"
+ const earlier = document.createElement('iframe'); earlier.name = 'same';
+ const source = document.createElement('iframe');
+ source.name = {name:?} === 'renamed' ? 'old-name' : 'same';
+ document.body.append(earlier, source);
+ const childDocument = source.contentDocument;
+ const nested = childDocument.createElement('iframe'); nested.name = 'same';
+ childDocument.body.append(nested);
+ globalThis.originalDocument = childDocument;
+ globalThis.nameReads = 0;
+ if ({name:?} === 'renamed') source.contentWindow.name = 'same';
+ if ({name:?} === 'shadowed') Object.defineProperty(source.contentWindow, 'name', {{
+ get() {{ ++nameReads; throw new Error('script-visible name was read'); }}
+ }});
+ globalThis.namedAccessIsChild = source.contentWindow.same === nested.contentWindow;
+ "#
+ ))
+ .unwrap();
+ vm.drain_ready_page_task_executor_turns_for_setup(&loader, 100)
+ .await
+ .unwrap();
+ vm.eval(&format!(
+ r#"
+ const element = childDocument.createElement({action:?} === 'anchor' ? 'a' : 'form');
+ element.target = 'same';
+ if ({action:?} === 'anchor') element.href = '/selected?from=anchor';
+ else {{
+ element.action = '/selected';
+ element.innerHTML = '<input name=from value="{action}">';
+ }}
+ childDocument.body.append(element);
+ if ({action:?} === 'anchor') element.click();
+ else element[{action:?}]();
+ "#
+ ))
+ .unwrap();
+ advance_page_task_executor_until_eval_equals(
+ &mut vm,
+ &loader,
+ "String(source.contentDocument.body?.textContent === 'selected')",
+ "true",
+ &format!("{action}, {name}"),
+ )
+ .await;
+ assert_eq!(
+ vm.eval(
+ "JSON.stringify([source.contentDocument !== originalDocument, \
+ source.contentWindow.length, earlier.contentWindow.location.href, \
+ namedAccessIsChild, nameReads])",
+ )
+ .unwrap(),
+ r#"[true,0,"about:blank",true,0]"#,
+ "{action}, {name}",
+ );
+ assert_eq!(
+ server.finish_targets().await,
+ [format!("/selected?from={action}")],
+ "{action}, {name}",
+ );
+ }
+ }
+}
+
#[test]
fn form_target_blank_reloads_rel_opener_policy_for_each_submission() {
for (rel, expected_exposes_opener) in [