Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(renderer): resolve VisualViewport dimensions through its receiver Window

ldm06d20ede8d191
diff --git a/moli-renderer-v8/src/context_bootstrap/navigator_runtime/visual_viewport.rs b/moli-renderer-v8/src/context_bootstrap/navigator_runtime/visual_viewport.rs
index adfa2d8df..c895c8fbf 100644
--- a/moli-renderer-v8/src/context_bootstrap/navigator_runtime/visual_viewport.rs
+++ b/moli-renderer-v8/src/context_bootstrap/navigator_runtime/visual_viewport.rs
@@ -158,18 +158,33 @@ fn visual_viewport_attribute_getter_callback<'s>(
if matches!(
slot,
VISUAL_VIEWPORT_WIDTH_SLOT | VISUAL_VIEWPORT_HEIGHT_SLOT
- ) && let Some(host_ptr) = crate::util::context_host_ptr_from_global_bridge(scope)
- {
- let viewport =
- crate::context_bootstrap::current_window_style_viewport(scope, unsafe { &*host_ptr });
+ ) {
+ // A borrowed getter belongs to its receiver's Window, even when the
+ // function was obtained from another realm's VisualViewport prototype.
+ let context = args
+ .this()
+ .get_creation_context(scope)
+ .unwrap_or_else(|| scope.get_current_context());
+ let scope = &mut v8::ContextScope::new(scope, context);
+ let window = context.global(scope);
+ let current = super::super::window_accessors::window_host_ptr(scope, window).is_some_and(
+ |host_ptr| {
+ super::super::window_accessors::window_current_dispatch_scope(
+ scope,
+ window,
+ unsafe { &*host_ptr },
+ )
+ .is_some()
+ },
+ );
+ if !current {
+ rv.set_double(0.0);
+ return;
+ }
let value = if slot == VISUAL_VIEWPORT_WIDTH_SLOT {
- viewport
- .width
- .unwrap_or(DEFAULT_WINDOW_SURFACE_PROFILE.inner_width)
+ super::super::window_accessors::window_inner_surface_width(scope, window)
} else {
- viewport
- .height
- .unwrap_or(DEFAULT_WINDOW_SURFACE_PROFILE.inner_height)
+ super::super::window_accessors::window_inner_surface_height(scope, window)
};
rv.set_double(value);
return;
diff --git a/moli-renderer-v8/src/context_bootstrap/window_accessors.rs b/moli-renderer-v8/src/context_bootstrap/window_accessors.rs
index e8da9beca..e5ac1a5eb 100644
--- a/moli-renderer-v8/src/context_bootstrap/window_accessors.rs
+++ b/moli-renderer-v8/src/context_bootstrap/window_accessors.rs
@@ -28,8 +28,8 @@ pub(super) use document::{WINDOW_DOCUMENT_SLOT, window_document_getter_template}
pub(crate) use document::{
bind_current_child_window_document, retain_window_document_in_retired_realm,
};
-pub(super) use helpers::window_child_context_handle;
pub(crate) use helpers::{current_window_style_viewport, window_host_ptr};
+pub(super) use helpers::{window_child_context_handle, window_current_dispatch_scope};
pub(super) use interceptors::{
window_indexed_property_descriptor, window_indexed_property_enumerator,
window_indexed_property_getter, window_indexed_property_query, window_named_property_getter,
diff --git a/moli-renderer-v8/src/context_bootstrap/window_accessors/helpers.rs b/moli-renderer-v8/src/context_bootstrap/window_accessors/helpers.rs
index 4a4f9bd8c..f98955816 100644
--- a/moli-renderer-v8/src/context_bootstrap/window_accessors/helpers.rs
+++ b/moli-renderer-v8/src/context_bootstrap/window_accessors/helpers.rs
@@ -106,7 +106,7 @@ pub(crate) fn window_host_ptr(
.or_else(|| context_host_ptr_from_global_bridge(scope))
}
-pub(super) fn window_current_dispatch_scope<'s>(
+pub(in crate::context_bootstrap) fn window_current_dispatch_scope<'s>(
scope: &mut v8::PinScope<'s, '_>,
receiver: v8::Local<'s, v8::Object>,
host: &JsContextHost,
diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs
index 00d6137cc..170a9dc37 100644
--- a/moli-renderer-v8/src/script_vm/tests/mod.rs
+++ b/moli-renderer-v8/src/script_vm/tests/mod.rs
@@ -1,6 +1,7 @@
mod credential_container;
mod credential_json_options;
mod picture_in_picture_interfaces;
+mod visual_viewport_receiver;
mod writable_stream_native_receivers;
use super::post_parse::dynamic_script_execute_is_runnable_before_dom_content_loaded;
use super::{
diff --git a/moli-renderer-v8/src/script_vm/tests/visual_viewport_receiver.rs b/moli-renderer-v8/src/script_vm/tests/visual_viewport_receiver.rs
new file mode 100644
index 000000000..3e19fc913
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/visual_viewport_receiver.rs
@@ -0,0 +1,65 @@
+use super::*;
+
+#[tokio::test(flavor = "current_thread")]
+async fn visual_viewport_borrowed_getters_use_the_receiver_window() {
+ for policy in [
+ moli_page_types::LayoutPolicy::Mock,
+ moli_page_types::LayoutPolicy::OnDemand,
+ ] {
+ let mut vm = new_storage_html_test_vm("https://visual-viewport-receiver.test/");
+ vm.set_layout_policy(policy);
+ vm.eval(
+ r#"
+ globalThis.frame=document.createElement('iframe');
+ frame.style.cssText='width:123px;height:87px;border:0';
+ frame.srcdoc='<body>child</body>';document.body.append(frame);
+ 'ready'
+ "#,
+ )
+ .unwrap();
+ run_child_navigation_commit_and_host_load_for_test(&mut vm, "viewport child setup").await;
+ if policy == moli_page_types::LayoutPolicy::OnDemand {
+ vm.publish_layout_for_test().unwrap();
+ }
+ let builds = vm.layout_pass_observability_for_test().1;
+ let facts = vm.eval(r#"JSON.stringify((()=>{
+ const parentViewport=window.visualViewport;
+ const childViewport=frame.contentWindow.visualViewport;
+ const getter=(viewport,name)=>Object.getOwnPropertyDescriptor(Object.getPrototypeOf(viewport),name).get;
+ globalThis.savedViewport=childViewport;
+ globalThis.childWidth=getter(childViewport,'width');
+ globalThis.childHeight=getter(childViewport,'height');
+ const rows=['width','height'].map(name=>{
+ const parentGetter=getter(parentViewport,name);
+ const childGetter=getter(childViewport,name);
+ return [parentGetter.call(childViewport),childViewport[name],
+ childGetter.call(parentViewport),parentViewport[name]];
+ });
+ return {distinct:parentViewport.width!==childViewport.width,
+ rows,brands:['width','height'].map(name=>{
+ try{getter(parentViewport,name).call({});return 'accepted'}catch(e){return e.name}
+ })};
+ })())"#).unwrap();
+ let facts: serde_json::Value = serde_json::from_str(&facts).unwrap();
+ assert_eq!(
+ facts["distinct"], true,
+ "fixture must have distinct viewports"
+ );
+ assert_eq!(
+ facts["brands"],
+ serde_json::json!(["TypeError", "TypeError"])
+ );
+ for row in facts["rows"].as_array().unwrap() {
+ assert_eq!(row[0], row[1], "parent getter on child: {policy:?}");
+ assert_eq!(row[2], row[3], "child getter on parent: {policy:?}");
+ }
+ assert_eq!(vm.layout_pass_observability_for_test().1, builds);
+ vm.eval("frame.remove(); 'removed'").unwrap();
+ assert_eq!(
+ vm.eval("JSON.stringify([savedViewport.width,savedViewport.height,childWidth.call(savedViewport),childHeight.call(savedViewport)])").unwrap(),
+ "[0,0,0,0]",
+ "a retired child viewport must not borrow the parent dimensions: {policy:?}"
+ );
+ assert_eq!(vm.layout_pass_observability_for_test().1, builds);
+ }
+}