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): expose popup browsing context names

ldm0a4b2ad7bc10b
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 e68f94185..3cbabd7d1 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/popups.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/popups.rs
@@ -162,6 +162,18 @@ struct LightweightPopupWindowMethodsDeclaration {
close: (),
}
+#[derive(Default, WebApiObject)]
+#[webapi(fragment)]
+struct LightweightPopupWindowNameDeclaration {
+ #[webapi(
+ accessor_property,
+ enumerable,
+ getter = lightweight_popup_window_name_getter,
+ setter = lightweight_popup_window_name_setter
+ )]
+ name: (),
+}
+
#[derive(WebApiObject)]
#[webapi(plain)]
struct LightweightPopupComputedStyleMethodDeclaration<'scope> {
@@ -618,6 +630,17 @@ impl JsContextHost {
true
}
+ fn set_lightweight_popup_window_name(&mut self, popup_id: u64, next: &str) {
+ self.lightweight_popup_window_names
+ .retain(|_, candidate| *candidate != popup_id);
+ if !self.lightweight_popup_is_open(popup_id) {
+ return;
+ }
+ if let Some(name) = trackable_lightweight_popup_window_name(next) {
+ self.lightweight_popup_window_names.insert(name, popup_id);
+ }
+ }
+
pub(crate) fn open_lightweight_popup_window<'s>(
&mut self,
scope: &mut v8::PinScope<'s, '_>,
@@ -732,6 +755,13 @@ impl JsContextHost {
LIGHTWEIGHT_POPUP_ID_SLOT,
popup_id_private_value.into(),
);
+ let initial_window_name =
+ trackable_lightweight_popup_window_name(target_name).unwrap_or_default();
+ let initial_window_name = v8_string(scope, &initial_window_name)?;
+ set_object_slot(scope, window, WINDOW_NAME_SLOT, initial_window_name.into());
+ LightweightPopupWindowNameDeclaration::default()
+ .initialize(scope, window)
+ .ok()?;
install_window_location_history_navigation_runtime_state(
scope,
window,
@@ -755,12 +785,6 @@ impl JsContextHost {
set_object_slot(scope, window, "parent", window.into());
set_object_slot(scope, window, "top", window.into());
set_object_slot(scope, window, "frames", window.into());
- if let Some(name) = trackable_lightweight_popup_window_name(target_name).as_deref()
- && let Some(value) = v8_string(scope, name)
- {
- set_object_slot(scope, window, WINDOW_NAME_SLOT, value.into());
- set_object_slot(scope, window, "name", value.into());
- }
if let Some(opener) = opener {
set_object_slot(scope, window, "opener", opener.into());
install_lightweight_popup_viewport_surface_from_opener(scope, opener, window);
@@ -4217,6 +4241,43 @@ fn install_lightweight_popup_viewport_surface_from_opener<'s>(
}
}
+fn lightweight_popup_window_name_getter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'_, v8::Value>,
+) {
+ let window = args.this();
+ if lightweight_popup_id_from_window(scope, window).is_none() {
+ throw_type_error(scope, "Window.name getter called on incompatible receiver.");
+ return;
+ }
+ let value = window
+ .get(scope, v8str(scope, WINDOW_NAME_SLOT).into())
+ .filter(|value| value.is_string())
+ .unwrap_or_else(|| v8::String::empty(scope).into());
+ rv.set(value);
+}
+
+fn lightweight_popup_window_name_setter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ _rv: v8::ReturnValue<'_, v8::Value>,
+) {
+ let window = args.this();
+ let Some(popup_id) = lightweight_popup_id_from_window(scope, window) else {
+ throw_type_error(scope, "Window.name setter called on incompatible receiver.");
+ return;
+ };
+ let Some(next) = args.get(0).to_string(scope) else {
+ return;
+ };
+ let next_string = next.to_rust_string_lossy(scope);
+ set_object_slot(scope, window, WINDOW_NAME_SLOT, next.into());
+ if let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) {
+ unsafe { &mut *host_ptr }.set_lightweight_popup_window_name(popup_id, &next_string);
+ }
+}
+
fn lightweight_popup_close_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
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 1545f79d2..557e82466 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
@@ -24194,6 +24194,51 @@ fn window_open_about_blank_returns_lightweight_popup_window() {
);
}
+#[test]
+fn window_open_blank_targets_expose_mutable_empty_browsing_context_names() {
+ let mut vm = new_storage_test_vm("https://example.com/");
+
+ let result = vm
+ .eval(
+ r##"
+ (() => {
+ const implicit = open("about:blank#implicit");
+ const empty = open("about:blank#empty", "");
+ const blank = open("about:blank#blank", "_blank");
+ const mixedBlank = open("about:blank#mixed", "_BlAnK");
+ const initialNames = [implicit, empty, blank, mixedBlank]
+ .map(popup => popup.name);
+ const initialTypes = [implicit, empty, blank, mixedBlank]
+ .map(popup => typeof popup.name);
+ let conversions = 0;
+ empty.name = {
+ toString() {
+ conversions++;
+ return "renamed-popup";
+ }
+ };
+ blank.name = 42;
+ const reused = open("about:blank#reused", "renamed-popup");
+ return JSON.stringify({
+ initialNames,
+ initialTypes,
+ conversions,
+ emptyName: empty.name,
+ blankName: blank.name,
+ reused: reused === empty,
+ reusedName: reused.name
+ });
+ })()
+ "##,
+ )
+ .expect("blank-target popup Window.name probe should evaluate");
+
+ assert_eq!(
+ result,
+ r#"{"initialNames":["","","",""],"initialTypes":["string","string","string","string"],"conversions":1,"emptyName":"renamed-popup","blankName":"42","reused":true,"reusedName":"renamed-popup"}"#
+ );
+}
+
#[test]
fn window_open_existing_context_special_targets_never_enter_the_popup_carrier() {
for target in ["_self", "_parent", "_top", "_SeLf", "_PaReNt", "_TOP"] {