Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(geometry): complete DOMRect construction and factories

ldm0640d37f6ed3e
diff --git a/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs b/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs
index 3531f1f07..05225f73d 100644
--- a/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs
+++ b/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs
@@ -376,6 +376,11 @@ pub(in crate::context_bootstrap) fn build_constructor_template<'s>(
.length(0)
.build(scope)
}
+ ConstructorKind::DomRectReadOnly => v8::FunctionTemplate::builder(
+ super::super::dom_rect::dom_rect_readonly_constructor_callback,
+ )
+ .length(0)
+ .build(scope),
ConstructorKind::DomRect => {
v8::FunctionTemplate::builder(super::super::dom_rect::dom_rect_constructor_callback)
.length(0)
diff --git a/moli-renderer-v8/src/context_bootstrap/dom_rect.rs b/moli-renderer-v8/src/context_bootstrap/dom_rect.rs
index 37632caef..c9457d8aa 100644
--- a/moli-renderer-v8/src/context_bootstrap/dom_rect.rs
+++ b/moli-renderer-v8/src/context_bootstrap/dom_rect.rs
@@ -25,6 +25,22 @@ struct DomRectObjectDeclaration {
height: f64,
}
+#[derive(WebApiObject)]
+#[webapi(interface = "DOMRectReadOnly")]
+struct DomRectReadOnlyObjectDeclaration {
+ #[webapi(slot = DOM_RECT_BRAND_SLOT, init = true)]
+ brand: (),
+
+ #[webapi(slot = DOM_RECT_X_SLOT)]
+ x: f64,
+ #[webapi(slot = DOM_RECT_Y_SLOT)]
+ y: f64,
+ #[webapi(slot = DOM_RECT_WIDTH_SLOT)]
+ width: f64,
+ #[webapi(slot = DOM_RECT_HEIGHT_SLOT)]
+ height: f64,
+}
+
#[derive(WebApiFunctionTemplate)]
#[webapi(name = "DOMRectReadOnly")]
struct DomRectReadOnlyPrototypeDeclaration {
@@ -125,6 +141,28 @@ struct DomRectPrototypeDeclaration {
height: (),
}
+#[derive(WebApiFunctionTemplate)]
+#[webapi(name = "DOMRectReadOnly")]
+struct DomRectReadOnlyConstructorDeclaration {
+ #[webapi(
+ static_method = "fromRect",
+ length = 0,
+ callback = dom_rect_readonly_from_rect_callback
+ )]
+ from_rect: (),
+}
+
+#[derive(WebApiFunctionTemplate)]
+#[webapi(name = "DOMRect")]
+struct DomRectConstructorDeclaration {
+ #[webapi(
+ static_method = "fromRect",
+ length = 0,
+ callback = dom_rect_from_rect_callback
+ )]
+ from_rect: (),
+}
+
#[derive(WebApiObject)]
#[webapi(interface = "Object")]
struct DomRectJsonDeclaration {
@@ -167,6 +205,58 @@ struct DomRectConstructorArgs {
height: f64,
}
+#[derive(webidl::WebIdlArgs)]
+#[webidl(prefix = "DOMRectReadOnly")]
+struct DomRectReadOnlyConstructorArgs {
+ #[webidl(default = 0.0)]
+ x: f64,
+ #[webidl(default = 0.0)]
+ y: f64,
+ #[webidl(default = 0.0)]
+ width: f64,
+ #[webidl(default = 0.0)]
+ height: f64,
+}
+
+#[derive(Clone, Copy, Default, webidl::WebIdlDictionary)]
+#[webidl(prefix = "DOMRectInit")]
+struct DomRectInit {
+ #[webidl(default = 0.0)]
+ x: f64,
+ #[webidl(default = 0.0)]
+ y: f64,
+ #[webidl(default = 0.0)]
+ width: f64,
+ #[webidl(default = 0.0)]
+ height: f64,
+}
+
+pub(super) fn dom_rect_readonly_constructor_callback<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'_, v8::Value>,
+) {
+ if !args.is_construct_call() {
+ throw_type_error(
+ scope,
+ "Failed to construct 'DOMRectReadOnly': Please use the 'new' operator.",
+ );
+ return;
+ }
+ let Some(parsed) = webidl::parse_args::<DomRectReadOnlyConstructorArgs>(scope, &args) else {
+ return;
+ };
+ initialize_dom_rect_readonly_object(
+ scope,
+ args.this(),
+ parsed.x,
+ parsed.y,
+ parsed.width,
+ parsed.height,
+ );
+ rv.set(args.this().into());
+}
+
pub(super) fn dom_rect_constructor_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
@@ -205,6 +295,18 @@ pub(crate) fn build_dom_rect_object<'s>(
.expect("DOMRect declaration should bind")
}
+fn build_dom_rect_readonly_object<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ x: f64,
+ y: f64,
+ width: f64,
+ height: f64,
+) -> v8::Local<'s, v8::Object> {
+ DomRectReadOnlyObjectDeclaration::new(x, y, width, height)
+ .bind(scope)
+ .expect("DOMRectReadOnly declaration should bind")
+}
+
fn initialize_dom_rect_object<'s>(
scope: &mut v8::PinScope<'s, '_>,
object: v8::Local<'s, v8::Object>,
@@ -218,6 +320,19 @@ fn initialize_dom_rect_object<'s>(
.expect("DOMRect declaration should initialize object");
}
+fn initialize_dom_rect_readonly_object<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ object: v8::Local<'s, v8::Object>,
+ x: f64,
+ y: f64,
+ width: f64,
+ height: f64,
+) {
+ DomRectReadOnlyObjectDeclaration::new(x, y, width, height)
+ .bind_into(scope, object)
+ .expect("DOMRectReadOnly declaration should initialize object");
+}
+
pub(in crate::context_bootstrap) fn install_dom_rect_template_bindings<'s>(
scope: &mut v8::PinScope<'s, '_, ()>,
template: v8::Local<'s, v8::FunctionTemplate>,
@@ -226,15 +341,61 @@ pub(in crate::context_bootstrap) fn install_dom_rect_template_bindings<'s>(
let prototype = template.prototype_template(scope);
match interface_name {
"DOMRectReadOnly" => {
+ DomRectReadOnlyConstructorDeclaration::initialize_template(scope, template);
DomRectReadOnlyPrototypeDeclaration::initialize_prototype_template(scope, prototype);
}
"DOMRect" => {
+ DomRectConstructorDeclaration::initialize_template(scope, template);
DomRectPrototypeDeclaration::initialize_prototype_template(scope, prototype);
}
_ => {}
}
}
+fn dom_rect_readonly_from_rect_callback<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'_, v8::Value>,
+) {
+ let Some(init) = dom_rect_init_arg(scope, &args, "DOMRectReadOnly.fromRect") else {
+ return;
+ };
+ rv.set(build_dom_rect_readonly_object(scope, init.x, init.y, init.width, init.height).into());
+}
+
+fn dom_rect_from_rect_callback<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'_, v8::Value>,
+) {
+ let Some(init) = dom_rect_init_arg(scope, &args, "DOMRect.fromRect") else {
+ return;
+ };
+ rv.set(build_dom_rect_object(scope, init.x, init.y, init.width, init.height).into());
+}
+
+fn dom_rect_init_arg<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: &v8::FunctionCallbackArguments<'s>,
+ prefix: &'static str,
+) -> Option<DomRectInit> {
+ if args.length() == 0 || args.get(0).is_undefined() {
+ return Some(DomRectInit::default());
+ }
+ match webidl::parse_dictionary::<DomRectInit>(
+ scope,
+ args.get(0),
+ webidl::Context::argument(prefix, 1),
+ ) {
+ Ok(Some(init)) => Some(init),
+ Ok(None) => Some(DomRectInit::default()),
+ Err(error) => {
+ webidl::throw_error(scope, &error);
+ None
+ }
+ }
+}
+
fn dom_rect_writable_getter_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
@@ -353,26 +514,42 @@ fn dom_rect_readonly_attribute_value<'s>(
DomRectReadonlyAttribute::Top => {
let y = dom_rect_slot(object, scope, DOM_RECT_Y_SLOT);
let height = dom_rect_slot(object, scope, DOM_RECT_HEIGHT_SLOT);
- y.min(y + height)
+ dom_rect_min(y, y + height)
}
DomRectReadonlyAttribute::Right => {
let x = dom_rect_slot(object, scope, DOM_RECT_X_SLOT);
let width = dom_rect_slot(object, scope, DOM_RECT_WIDTH_SLOT);
- x.max(x + width)
+ dom_rect_max(x, x + width)
}
DomRectReadonlyAttribute::Bottom => {
let y = dom_rect_slot(object, scope, DOM_RECT_Y_SLOT);
let height = dom_rect_slot(object, scope, DOM_RECT_HEIGHT_SLOT);
- y.max(y + height)
+ dom_rect_max(y, y + height)
}
DomRectReadonlyAttribute::Left => {
let x = dom_rect_slot(object, scope, DOM_RECT_X_SLOT);
let width = dom_rect_slot(object, scope, DOM_RECT_WIDTH_SLOT);
- x.min(x + width)
+ dom_rect_min(x, x + width)
}
}
}
+fn dom_rect_min(lhs: f64, rhs: f64) -> f64 {
+ if lhs.is_nan() || rhs.is_nan() {
+ f64::NAN
+ } else {
+ lhs.min(rhs)
+ }
+}
+
+fn dom_rect_max(lhs: f64, rhs: f64) -> f64 {
+ if lhs.is_nan() || rhs.is_nan() {
+ f64::NAN
+ } else {
+ lhs.max(rhs)
+ }
+}
+
fn dom_rect_slot<'s>(
object: v8::Local<'s, v8::Object>,
scope: &mut v8::PinScope<'s, '_>,
diff --git a/moli-renderer-v8/src/context_bootstrap/specs/registry.rs b/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
index 7450445e0..a871a6842 100644
--- a/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
+++ b/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
@@ -1204,7 +1204,7 @@ const CONSTRUCTOR_SPECS_AFTER_STREAMS: &[ConstructorSpec] = &[
ConstructorSpec {
name: "DOMRectReadOnly",
parent: None,
- kind: ConstructorKind::Illegal,
+ kind: ConstructorKind::DomRectReadOnly,
},
ConstructorSpec {
name: "DOMRect",
diff --git a/moli-renderer-v8/src/context_bootstrap/specs/types.rs b/moli-renderer-v8/src/context_bootstrap/specs/types.rs
index 3c1f2c1cd..d295aeffb 100644
--- a/moli-renderer-v8/src/context_bootstrap/specs/types.rs
+++ b/moli-renderer-v8/src/context_bootstrap/specs/types.rs
@@ -102,6 +102,7 @@ pub(in crate::context_bootstrap) enum ConstructorKind {
SpeechSynthesisUtterance,
WebSocketError,
WebSocketStream,
+ DomRectReadOnly,
DomRect,
DomPoint,
DomMatrix,
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/misc.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/misc.rs
index b7704c496..69d068d10 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/misc.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/misc.rs
@@ -1665,6 +1665,125 @@ fn geometry_rect_objects_expose_domrect_to_json() {
);
}
+#[test]
+fn dom_rect_readonly_is_constructible_and_to_json_uses_the_function_realm() {
+ let mut vm = new_storage_test_vm("https://domrect-readonly-constructor.test/");
+
+ vm.eval(
+ r#"
+(() => {
+ const root = document.documentElement ||
+ document.appendChild(document.createElement("html"));
+ const body = document.body || root.appendChild(document.createElement("body"));
+ const frame = document.createElement("iframe");
+ frame.id = "domrect-readonly-realm";
+ body.appendChild(frame);
+})()
+"#,
+ )
+ .expect("DOMRectReadOnly child frame should be created");
+ materialize_single_child_default_realm_for_test(&mut vm, "DOMRectReadOnly toJSON child Realm");
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const other = document.getElementById("domrect-readonly-realm").contentWindow;
+ const rect = new DOMRectReadOnly(1, 2, 3, 4);
+ const json = other.DOMRectReadOnly.prototype.toJSON.call(rect);
+ let callError;
+ try {
+ other.DOMRectReadOnly();
+ } catch (error) {
+ callError = [error.name, error instanceof other.TypeError].join(":");
+ }
+ return [
+ rect instanceof DOMRectReadOnly,
+ rect instanceof DOMRect,
+ [rect.x, rect.y, rect.width, rect.height, rect.top, rect.right,
+ rect.bottom, rect.left].join(","),
+ Object.getPrototypeOf(json) === other.Object.prototype,
+ Object.getPrototypeOf(json) === Object.prototype,
+ JSON.stringify(json),
+ callError
+ ].join("|");
+})()
+"#,
+ )
+ .expect("DOMRectReadOnly constructor and cross-Realm toJSON should evaluate");
+
+ assert_eq!(
+ result,
+ "true|false|1,2,3,4,2,4,6,1|true|false|{\"x\":1,\"y\":2,\"width\":3,\"height\":4,\"top\":2,\"right\":4,\"bottom\":6,\"left\":1}|TypeError:true"
+ );
+}
+
+#[test]
+fn dom_rect_bounds_propagate_nan_components() {
+ let mut vm = new_storage_test_vm("https://domrect-nan-bounds.test/");
+
+ let result = vm
+ .eval(
+ r#"
+(() => [DOMRect, DOMRectReadOnly].every(Rect => {
+ const width = new Rect(0, 0, NaN, 0).toJSON();
+ const height = new Rect(0, 0, 0, NaN).toJSON();
+ return Number.isNaN(width.left) && Number.isNaN(width.right) &&
+ Number.isNaN(height.top) && Number.isNaN(height.bottom);
+}))()
+"#,
+ )
+ .expect("DOMRect NaN bounds should evaluate");
+
+ assert_eq!(result, "true");
+}
+
+#[test]
+fn dom_rect_from_rect_factories_create_their_declared_interface_in_the_function_realm() {
+ let mut vm = new_storage_test_vm("https://domrect-from-rect.test/");
+
+ vm.eval(
+ r#"
+(() => {
+ const root = document.documentElement ||
+ document.appendChild(document.createElement("html"));
+ const body = document.body || root.appendChild(document.createElement("body"));
+ const frame = document.createElement("iframe");
+ frame.id = "domrect-from-rect-realm";
+ body.appendChild(frame);
+})()
+"#,
+ )
+ .expect("DOMRect factory child frame should be created");
+ materialize_single_child_default_realm_for_test(&mut vm, "DOMRect factory child Realm");
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const other = document.getElementById("domrect-from-rect-realm").contentWindow;
+ const mutable = other.DOMRect.fromRect({x: "1", y: 2, width: 3, height: 4});
+ const readonly = other.DOMRectReadOnly.fromRect(mutable);
+ const empty = other.DOMRectReadOnly.fromRect(null);
+ return [
+ other.DOMRect.fromRect.length,
+ other.DOMRectReadOnly.fromRect.length,
+ mutable instanceof other.DOMRect,
+ mutable instanceof DOMRect,
+ readonly instanceof other.DOMRectReadOnly,
+ readonly instanceof other.DOMRect,
+ readonly instanceof DOMRectReadOnly,
+ [readonly.x, readonly.y, readonly.width, readonly.height].join(","),
+ [empty.x, empty.y, empty.width, empty.height].join(",")
+ ].join("|");
+})()
+"#,
+ )
+ .expect("DOMRect factories should evaluate");
+
+ assert_eq!(result, "0|0|true|false|true|false|false|1,2,3,4|0,0,0,0");
+}
+
#[test]
fn geometry_domrect_private_slots_ignore_reflection_and_spoofing() {
let mut vm = new_storage_test_vm("https://geometry-domrect-private-slots.test/");