Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(svg): validate geometry accessors with native brands

ldm0c9a68882505e
diff --git a/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs b/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs
index 9c7906668..2f2dd7857 100644
--- a/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs
+++ b/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs
@@ -1519,28 +1519,28 @@ struct SvgElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGGraphicsElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGGraphicsElement, enumerable, receiver)]
struct SvgGraphicsElementPrototypeAccessorsDeclaration {
#[webapi(accessor_property = "transform", getter = svg_graphics_transform_getter)]
transform: (),
#[webapi(
accessor_property = "requiredExtensions",
- getter = svg_graphics_test_string_list_getter,
+ getter = svg_test_string_list_getter,
data = callback_data_index_value(scope, 0)
)]
required_extensions: (),
#[webapi(
accessor_property = "systemLanguage",
- getter = svg_graphics_test_string_list_getter,
+ getter = svg_test_string_list_getter,
data = callback_data_index_value(scope, 1)
)]
system_language: (),
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGGeometryElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGGeometryElement, enumerable, receiver)]
struct SvgGeometryElementPrototypeAccessorsDeclaration {
#[webapi(accessor_property = "pathLength", getter = svg_geometry_path_length_getter)]
path_length: (),
@@ -1719,7 +1719,7 @@ struct SvgRadialGradientElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGSVGElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGSVGElement, enumerable, receiver)]
struct SvgSvgElementPrototypeAccessorsDeclaration {
#[webapi(accessor_property = "x", getter = svg_svg_animated_length_getter, data = callback_data_index_value(scope, 0))]
x: (),
@@ -1993,7 +1993,7 @@ struct SvgMaskElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGRectElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGRectElement, enumerable, receiver)]
struct SvgRectElementPrototypeAccessorsDeclaration {
#[webapi(accessor_property = "x", getter = svg_rect_animated_length_getter, data = callback_data_index_value(scope, 0))]
x: (),
@@ -2015,7 +2015,7 @@ struct SvgRectElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGCircleElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGCircleElement, enumerable, receiver)]
struct SvgCircleElementPrototypeAccessorsDeclaration {
#[webapi(accessor_property = "cx", getter = svg_circle_animated_length_getter, data = callback_data_index_value(scope, 0))]
cx: (),
@@ -2028,7 +2028,7 @@ struct SvgCircleElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGEllipseElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGEllipseElement, enumerable, receiver)]
struct SvgEllipseElementPrototypeAccessorsDeclaration {
#[webapi(accessor_property = "cx", getter = svg_ellipse_animated_length_getter, data = callback_data_index_value(scope, 0))]
cx: (),
@@ -2044,7 +2044,7 @@ struct SvgEllipseElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGLineElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGLineElement, enumerable, receiver)]
struct SvgLineElementPrototypeAccessorsDeclaration {
#[webapi(accessor_property = "x1", getter = svg_line_animated_length_getter, data = callback_data_index_value(scope, 0))]
x1: (),
@@ -2059,22 +2059,36 @@ struct SvgLineElementPrototypeAccessorsDeclaration {
y2: (),
}
-#[derive(WebApiFunctionTemplate)]
-#[webapi(name = "SVGGraphicsBoxElement", enumerable)]
-struct SvgGraphicsBoxElementPrototypeAccessorsDeclaration {
- #[webapi(accessor_property = "x", getter = svg_box_animated_length_getter, data = callback_data_index_value(scope, 0))]
- x: (),
+macro_rules! define_svg_box_length_accessors {
+ ($declaration:ident, $interface:ident) => {
+ #[derive(WebApiFunctionTemplate)]
+ #[webapi(interface = web_api_interfaces::$interface, enumerable, receiver)]
+ struct $declaration {
+ #[webapi(accessor_property = "x", getter = svg_box_animated_length_getter, data = callback_data_index_value(scope, 0))]
+ x: (),
- #[webapi(accessor_property = "y", getter = svg_box_animated_length_getter, data = callback_data_index_value(scope, 1))]
- y: (),
+ #[webapi(accessor_property = "y", getter = svg_box_animated_length_getter, data = callback_data_index_value(scope, 1))]
+ y: (),
- #[webapi(accessor_property = "width", getter = svg_box_animated_length_getter, data = callback_data_index_value(scope, 2))]
- width: (),
+ #[webapi(accessor_property = "width", getter = svg_box_animated_length_getter, data = callback_data_index_value(scope, 2))]
+ width: (),
- #[webapi(accessor_property = "height", getter = svg_box_animated_length_getter, data = callback_data_index_value(scope, 3))]
- height: (),
+ #[webapi(accessor_property = "height", getter = svg_box_animated_length_getter, data = callback_data_index_value(scope, 3))]
+ height: (),
+ }
+ };
}
+define_svg_box_length_accessors!(
+ SvgImageElementPrototypeAccessorsDeclaration,
+ SVGImageElement
+);
+define_svg_box_length_accessors!(SvgUseElementPrototypeAccessorsDeclaration, SVGUseElement);
+define_svg_box_length_accessors!(
+ SvgForeignObjectElementPrototypeAccessorsDeclaration,
+ SVGForeignObjectElement
+);
+
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::SVGMarkerElement, enumerable, receiver)]
struct SvgMarkerElementPrototypeAccessorsDeclaration {
@@ -2708,21 +2722,27 @@ pub(super) fn install_svg_element_accessor_bindings<'s>(
scope, prototype,
);
}
- "SVGImageElement" | "SVGUseElement" => {
- SvgGraphicsBoxElementPrototypeAccessorsDeclaration::initialize_prototype_template(
+ "SVGImageElement" => {
+ SvgImageElementPrototypeAccessorsDeclaration::initialize_prototype_template(
+ scope, prototype,
+ );
+ SvgUriReferencePrototypeAccessorsDeclaration::initialize_prototype_template(
+ scope, prototype,
+ );
+ SvgImagePreserveAspectRatioPrototypeAccessorsDeclaration::initialize_prototype_template(
+ scope, prototype,
+ );
+ }
+ "SVGUseElement" => {
+ SvgUseElementPrototypeAccessorsDeclaration::initialize_prototype_template(
scope, prototype,
);
SvgUriReferencePrototypeAccessorsDeclaration::initialize_prototype_template(
scope, prototype,
);
- if interface_name == "SVGImageElement" {
- SvgImagePreserveAspectRatioPrototypeAccessorsDeclaration::initialize_prototype_template(
- scope, prototype,
- );
- }
}
"SVGForeignObjectElement" => {
- SvgGraphicsBoxElementPrototypeAccessorsDeclaration::initialize_prototype_template(
+ SvgForeignObjectElementPrototypeAccessorsDeclaration::initialize_prototype_template(
scope, prototype,
);
}
diff --git a/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs b/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs
index 55576f037..d93609b55 100644
--- a/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs
+++ b/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs
@@ -773,17 +773,6 @@ pub(super) fn svg_graphics_transform_getter<'s>(
svg_transform_attribute_getter(scope, args, rv, SVG_GRAPHICS_TRANSFORM_SLOT, "transform");
}
-pub(super) fn svg_graphics_test_string_list_getter<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- args: v8::FunctionCallbackArguments<'s>,
- rv: v8::ReturnValue<'_, v8::Value>,
-) {
- if !require_svg_graphics_element_receiver(scope, args.this(), "SVGTests") {
- return;
- }
- svg_test_string_list_getter(scope, args, rv);
-}
-
pub(super) fn svg_test_string_list_getter<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
@@ -798,7 +787,9 @@ pub(super) fn svg_test_string_list_getter<'s>(
rv.set_undefined();
return;
};
- let receiver = args.this();
+ let Some(receiver) = moli_webapi_declare::web_api_object_target(scope, args.this()) else {
+ return;
+ };
let Some(context) = receiver.get_creation_context(scope) else {
return;
};
@@ -815,14 +806,6 @@ pub(super) fn svg_test_string_list_getter<'s>(
rv.set(list.into());
}
-fn require_svg_graphics_element_receiver<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- receiver: v8::Local<'s, v8::Object>,
- member: &str,
-) -> bool {
- require_svg_interface_receiver(scope, receiver, "SVGGraphicsElement", member)
-}
-
fn require_svg_interface_receiver<'s>(
scope: &mut v8::PinScope<'s, '_>,
receiver: v8::Local<'s, v8::Object>,
@@ -867,7 +850,13 @@ pub(super) fn svg_transform_attribute_getter<'s>(
slot: &str,
attribute: &str,
) {
- let owner = args.this();
+ let Some(owner) = moli_webapi_declare::web_api_object_target(scope, args.this()) else {
+ return;
+ };
+ let Some(context) = owner.get_creation_context(scope) else {
+ return;
+ };
+ let scope = &mut v8::ContextScope::new(scope, context);
if let Some(value) = get_private_value(scope, owner, slot) {
if let Ok(object) = v8::Local::<v8::Object>::try_from(value) {
sync_svg_animated_transform_list_from_owner_attribute(scope, object, owner, attribute);
@@ -885,7 +874,13 @@ pub(super) fn svg_geometry_path_length_getter<'s>(
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
- let owner = args.this();
+ let Some(owner) = moli_webapi_declare::web_api_object_target(scope, args.this()) else {
+ return;
+ };
+ let Some(context) = owner.get_creation_context(scope) else {
+ return;
+ };
+ let scope = &mut v8::ContextScope::new(scope, context);
if let Some(value) = get_private_value(scope, owner, SVG_GEOMETRY_PATH_LENGTH_SLOT) {
if let Ok(object) = v8::Local::<v8::Object>::try_from(value) {
sync_svg_animated_number_from_owner_attribute(scope, object, owner, "pathLength");
diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs
index 5a63c0be2..88bb2353b 100644
--- a/moli-renderer-v8/src/script_vm/tests/mod.rs
+++ b/moli-renderer-v8/src/script_vm/tests/mod.rs
@@ -2198,6 +2198,7 @@ mod storage_dense_name_arrays;
mod streams;
mod svg_filter_interfaces;
mod svg_geometry_methods;
+mod svg_geometry_receivers;
mod svg_length_validation;
mod svg_number_validation;
mod svg_root_factory_receivers;
diff --git a/moli-renderer-v8/src/script_vm/tests/svg_geometry_receivers.js b/moli-renderer-v8/src/script_vm/tests/svg_geometry_receivers.js
new file mode 100644
index 000000000..eb3b9a3dd
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/svg_geometry_receivers.js
@@ -0,0 +1,189 @@
+(() => {
+ const checks = [];
+ const check = (name, callback) => {
+ try { checks.push({name, passed: callback() === true}); }
+ catch (error) { checks.push({name, passed: false, error: String(error)}); }
+ };
+ const invoke = (getter, receiver) => Reflect.apply(getter, receiver, []);
+ const typeError = (getter, receiver, realm) => {
+ try { invoke(getter, receiver); }
+ catch (error) { return error instanceof realm.TypeError; }
+ return false;
+ };
+ const ns = 'http://www.w3.org/2000/svg';
+ const child = document.querySelector('iframe').contentWindow;
+ const lengths = names => names.map(name => [name, 'SVGAnimatedLength', name, 'length']);
+ const graphics = [
+ ['transform', 'SVGAnimatedTransformList', 'transform', 'transform'],
+ ['requiredExtensions', 'SVGStringList', 'requiredExtensions', 'extensions'],
+ ['systemLanguage', 'SVGStringList', 'systemLanguage', 'languages'],
+ ];
+ const geometry = [['pathLength', 'SVGAnimatedNumber', 'pathLength', 'number']];
+ const definitions = [
+ ['g', 'SVGGraphicsElement', graphics],
+ ['path', 'SVGGeometryElement', geometry],
+ ['svg', 'SVGSVGElement', lengths(['x','y','width','height'])],
+ ['rect', 'SVGRectElement', lengths(['x','y','width','height','rx','ry'])],
+ ['circle', 'SVGCircleElement', lengths(['cx','cy','r'])],
+ ['ellipse', 'SVGEllipseElement', lengths(['cx','cy','rx','ry'])],
+ ['line', 'SVGLineElement', lengths(['x1','y1','x2','y2'])],
+ ['image', 'SVGImageElement', lengths(['x','y','width','height'])],
+ ['use', 'SVGUseElement', lengths(['x','y','width','height'])],
+ ['foreignObject', 'SVGForeignObjectElement', lengths(['x','y','width','height'])],
+ ];
+ const reflection = {
+ length: {
+ raw: '12',
+ matches: value => value.baseVal.value === 12 && value.animVal.value === 12,
+ write: value => { value.baseVal.value = 17; },
+ written: value => value.baseVal.value === 17 && value.animVal.value === 17,
+ serialized: '17',
+ },
+ number: {
+ raw: '12.5',
+ matches: value => value.baseVal === 12.5 && value.animVal === 12.5,
+ write: value => { value.baseVal = 17.5; },
+ written: value => value.baseVal === 17.5 && value.animVal === 17.5,
+ serialized: '17.5',
+ },
+ transform: {
+ raw: 'translate(2 3)',
+ matches: value => value.baseVal.numberOfItems === 1 && value.animVal.numberOfItems === 1 &&
+ value.baseVal.getItem(0).matrix.e === 2 && value.animVal.getItem(0).matrix.f === 3,
+ write: value => { value.baseVal.getItem(0).setTranslate(5, 6); },
+ written: value => value.baseVal.numberOfItems === 1 && value.animVal.numberOfItems === 1 &&
+ value.baseVal.getItem(0).matrix.e === 5 && value.animVal.getItem(0).matrix.f === 6,
+ },
+ extensions: {
+ raw: 'urn:a urn:b',
+ matches: value => value.numberOfItems === 2 && value.getItem(0) === 'urn:a' && value.getItem(1) === 'urn:b',
+ write: value => { value.initialize('urn:c'); },
+ written: value => value.numberOfItems === 1 && value.getItem(0) === 'urn:c',
+ serialized: 'urn:c',
+ },
+ languages: {
+ raw: 'en,fr',
+ matches: value => value.numberOfItems === 2 && value.getItem(0) === 'en' && value.getItem(1) === 'fr',
+ write: value => { value.initialize('de'); },
+ written: value => value.numberOfItems === 1 && value.getItem(0) === 'de',
+ serialized: 'de',
+ },
+ };
+ const serializedMatches = (element, property, attribute, kind, behavior) => {
+ if (kind !== 'transform') return element.getAttribute(attribute) === behavior.serialized;
+ // SVG2 serializes the matrix; Chromium currently retains translate syntax.
+ // Check attribute round-trip semantics independently of that spelling.
+ const copy = element.cloneNode(false), list = copy[property].baseVal;
+ if (!element.getAttribute(attribute) || list.numberOfItems !== 1) return false;
+ const matrix = list.getItem(0).matrix;
+ return matrix.a === 1 && matrix.b === 0 && matrix.c === 0 && matrix.d === 1 && matrix.e === 5 && matrix.f === 6;
+ };
+ for (const [world, realm] of [['main', globalThis], ['child', child]]) {
+ for (const [documentKind, doc] of [
+ ['live', realm.document],
+ ['windowless-html', realm.document.implementation.createHTMLDocument('')],
+ ['windowless-xml', new realm.DOMParser().parseFromString('<root/>', 'application/xml')],
+ ]) {
+ for (const [tag, name, properties] of definitions) {
+ const proto = realm[name].prototype;
+ const make = () => doc.createElementNS(ns, tag);
+ for (const [property, type, attribute, kind] of properties) {
+ const prefix = `${world}/${documentKind}/${tag}/${property}`;
+ const descriptor = Object.getOwnPropertyDescriptor(proto, property);
+ const getter = descriptor.get;
+ const real = make();
+ const value = real[property];
+ check(prefix + '/readonly-descriptor', () => typeof getter === 'function' &&
+ descriptor.set === undefined && descriptor.enumerable && descriptor.configurable);
+ check(prefix + '/function-realm', () => Object.getPrototypeOf(getter) === realm.Function.prototype);
+ check(prefix + '/valid-native-receiver', () => invoke(getter, real) === value && value instanceof realm[type]);
+ check(prefix + '/stable-tear-off', () => invoke(getter, real) === invoke(getter, real));
+ check(prefix + '/clone', () => invoke(getter, real.cloneNode(false)) instanceof realm[type]);
+ check(prefix + '/import', () => invoke(getter, doc.importNode(real, false)) instanceof realm[type]);
+ check(prefix + '/bypass-author-property', () => {
+ const element = make(), original = element[property]; let reads = 0;
+ Object.defineProperty(element, property, {get() { reads++; throw 42; }});
+ return invoke(getter, element) === original && reads === 0;
+ });
+ const other = realm === globalThis ? child : globalThis;
+ const foreign = Object.getOwnPropertyDescriptor(other[name].prototype, property).get;
+ check(prefix + '/borrowed-foreign-getter', () => invoke(foreign, real) === value);
+ check(prefix + '/borrowed-foreign-getter-cold', () => {
+ const element = make(), first = invoke(foreign, element);
+ return first === element[property] && Object.getPrototypeOf(first) === realm[type].prototype;
+ });
+ check(prefix + '/foreign-error-realm', () => typeError(foreign, {}, other));
+ const revoked = Proxy.revocable(real, {}); revoked.revoke();
+ let traps = 0;
+ const author = new Proxy(real, {
+ get() { traps++; throw 42; },
+ getPrototypeOf() { traps++; throw 42; },
+ has() { traps++; throw 42; },
+ });
+ const wrongTag = tag === 'image' ? 'use' : tag === 'use' ? 'foreignObject' : 'marker';
+ const wrong = doc.createElementNS(ns, wrongTag);
+ Object.setPrototypeOf(wrong, proto);
+ for (const [label, receiver] of [
+ ['undefined', undefined], ['null', null], ['boolean', true], ['number', 42],
+ ['symbol', Symbol('receiver')], ['plain', {}], ['interface-prototype', proto],
+ ['forged-prototype', Object.create(proto)], ['inherit-real', Object.create(real)],
+ ['author-proxy', author], ['revoked-proxy', revoked.proxy],
+ ['wrong-svg', doc.createElementNS(ns, wrongTag)],
+ ['html-namespace', doc.createElementNS('http://www.w3.org/1999/xhtml', tag)],
+ ['prototype-swapped-wrong-svg', wrong],
+ ]) {
+ check(prefix + '/receiver/' + label, () => typeError(getter, receiver, realm) && traps === 0);
+ }
+ check(prefix + '/retained-value-after-invalid-calls', () => real[property] === value);
+ const behavior = reflection[kind];
+ check(prefix + '/attribute-forward-reflection', () => {
+ const element = make(), original = element[property];
+ element.setAttribute(attribute, behavior.raw);
+ return original === element[property] && behavior.matches(original);
+ });
+ check(prefix + '/value-writeback', () => {
+ const element = make(); element.setAttribute(attribute, behavior.raw);
+ const original = element[property]; behavior.write(original);
+ return original === element[property] && behavior.written(original) &&
+ serializedMatches(element, property, attribute, kind, behavior);
+ });
+ check(prefix + '/single-native-attribute-mutation', () => {
+ const element = make(); element.setAttribute(attribute, behavior.raw);
+ const original = element[property];
+ const observer = new realm.MutationObserver(() => {});
+ observer.observe(element, {attributes: true, attributeOldValue: true});
+ try {
+ behavior.write(original);
+ const records = observer.takeRecords();
+ return records.length === 1 && records[0].target === element && records[0].attributeName === attribute &&
+ records[0].attributeNamespace === null && records[0].oldValue === behavior.raw;
+ } finally { observer.disconnect(); }
+ });
+ }
+ }
+ for (const [name, tags, properties] of [
+ ['SVGGraphicsElement', ['svg','g','defs','symbol','use','switch','path','rect','circle','ellipse','line',
+ 'polyline','polygon','text','tspan','textPath','image','foreignObject','a'], graphics],
+ ['SVGGeometryElement', ['path','rect','circle','ellipse','line','polyline','polygon'], geometry],
+ ]) {
+ for (const tag of tags) {
+ for (const [property, type] of properties) {
+ const element = doc.createElementNS(ns, tag);
+ const getter = Object.getOwnPropertyDescriptor(realm[name].prototype, property).get;
+ const prefix = `${world}/${documentKind}/inheritance/${name}/${tag}/${property}`;
+ check(prefix + '/native-subclass', () => invoke(getter, element) === element[property]);
+ check(prefix + '/producer-realm', () => Object.getPrototypeOf(invoke(getter, element)) === realm[type].prototype);
+ check(prefix + '/prototype-change-preserves-brand', () => {
+ const original = invoke(getter, element);
+ Object.setPrototypeOf(element, realm.Object.prototype);
+ return invoke(getter, element) === original;
+ });
+ }
+ }
+ }
+ }
+ }
+ globalThis.__uiEventResults = {complete: true, total: checks.length,
+ passed: checks.filter(row => row.passed).length, checks};
+ return true;
+})()
diff --git a/moli-renderer-v8/src/script_vm/tests/svg_geometry_receivers.rs b/moli-renderer-v8/src/script_vm/tests/svg_geometry_receivers.rs
new file mode 100644
index 000000000..63ae8708e
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/svg_geometry_receivers.rs
@@ -0,0 +1,122 @@
+use super::*;
+
+#[test]
+fn svg_geometry_getters_validate_native_receivers_and_reflect_windowless_values() {
+ let mut vm = new_storage_page_task_executor_test_vm("https://svg-geometry-receivers.test/");
+ vm.eval("document.body.innerHTML = '<iframe></iframe>'")
+ .unwrap();
+ vm.eval(include_str!("svg_geometry_receivers.js")).unwrap();
+ assert_eq!(vm.eval("__uiEventResults.complete").unwrap(), "true");
+ assert_eq!(vm.eval("__uiEventResults.total").unwrap(), "7368");
+ assert_eq!(
+ vm.eval("JSON.stringify(__uiEventResults.checks.filter(row => !row.passed))")
+ .unwrap(),
+ "[]"
+ );
+}
+
+#[test]
+fn svg_geometry_getters_accept_registered_native_proxies_in_the_producer_realm() {
+ let mut vm = new_storage_page_task_executor_test_vm("https://svg-geometry-proxies.test/");
+ vm.eval(
+ r#"
+ document.body.innerHTML = '<iframe></iframe>';
+ globalThis.childWindow = document.querySelector('iframe').contentWindow;
+ globalThis.definitions = [
+ ['g', 'SVGGraphicsElement', ['transform','requiredExtensions','systemLanguage']],
+ ['path', 'SVGGeometryElement', ['pathLength']],
+ ['svg', 'SVGSVGElement', ['x','y','width','height']],
+ ['rect', 'SVGRectElement', ['x','y','width','height','rx','ry']],
+ ['circle', 'SVGCircleElement', ['cx','cy','r']],
+ ['ellipse', 'SVGEllipseElement', ['cx','cy','rx','ry']],
+ ['line', 'SVGLineElement', ['x1','y1','x2','y2']],
+ ['image', 'SVGImageElement', ['x','y','width','height']],
+ ['use', 'SVGUseElement', ['x','y','width','height']],
+ ['foreignObject', 'SVGForeignObjectElement', ['x','y','width','height']],
+ ];
+ const detached = childWindow.document.implementation.createHTMLDocument('');
+ for (const [tag] of definitions) {
+ globalThis[tag] = detached.createElementNS('http://www.w3.org/2000/svg', tag);
+ }
+ "#,
+ )
+ .unwrap();
+ let context_ptr = &vm.page_default_context as *const _;
+ vm.with_context_scope_by_ptr_and_checkpoint_for_test(context_ptr, |scope, _| {
+ let global = scope.get_current_context().global(scope);
+ for (tag, proxy_name) in [
+ ("g", "gProxy"),
+ ("path", "pathProxy"),
+ ("svg", "svgProxy"),
+ ("rect", "rectProxy"),
+ ("circle", "circleProxy"),
+ ("ellipse", "ellipseProxy"),
+ ("line", "lineProxy"),
+ ("image", "imageProxy"),
+ ("use", "useProxy"),
+ ("foreignObject", "foreignObjectProxy"),
+ ] {
+ let key = crate::util::v8str(scope, tag);
+ let object = global.get(scope, key.into()).unwrap();
+ let object = v8::Local::<v8::Object>::try_from(object).unwrap();
+ let handler = crate::util::new_null_prototype_object(scope);
+ let proxy = v8::Proxy::new(scope, object, handler).unwrap();
+ moli_webapi_declare::register_web_api_proxy(scope, proxy).unwrap();
+ let key = crate::util::v8str(scope, proxy_name);
+ assert_eq!(
+ global.create_data_property(scope, key.into(), proxy.into()),
+ Some(true)
+ );
+ }
+ Ok(())
+ })
+ .unwrap();
+ assert_eq!(
+ vm.eval(
+ r#"(() => {
+ for (const [tag, name, properties] of definitions) {
+ const element = globalThis[tag], native = globalThis[tag + 'Proxy'];
+ let traps = 0;
+ const author = new Proxy(native, {get() {traps++;throw 42;}, getPrototypeOf() {traps++;throw 42;}});
+ const revoked = Proxy.revocable(native, {}); revoked.revoke();
+ for (const property of properties) {
+ const getter = Object.getOwnPropertyDescriptor(window[name].prototype, property).get;
+ const value = getter.call(native);
+ if (value !== element[property] || value !== getter.call(element) ||
+ Object.getPrototypeOf(value) !== childWindow[value.constructor.name].prototype) {
+ throw Error(name + '.' + property + ' native proxy state/realm');
+ }
+ for (const receiver of [author, revoked.proxy, Object.create(native)]) {
+ let error;
+ try {getter.call(receiver);} catch (caught) {error = caught;}
+ if (!(error instanceof TypeError) || traps !== 0) throw Error(property + ' author proxy accepted');
+ }
+ }
+ }
+ for (const property of ['transform','requiredExtensions','systemLanguage']) {
+ const getter = Object.getOwnPropertyDescriptor(SVGGraphicsElement.prototype, property).get;
+ if (getter.call(rectProxy) !== rect[property]) throw Error('native graphics subclass');
+ }
+ const pathLength = Object.getOwnPropertyDescriptor(SVGGeometryElement.prototype, 'pathLength').get;
+ if (pathLength.call(rectProxy) !== rect.pathLength) throw Error('native geometry subclass');
+ const number = pathLength.call(pathProxy);
+ number.baseVal = 2.5;
+ if (path.getAttribute('pathLength') !== '2.5' || number !== path.pathLength) throw Error('native number writeback');
+ const transform = Object.getOwnPropertyDescriptor(SVGGraphicsElement.prototype, 'transform').get;
+ g.setAttribute('transform', 'translate(1 2)');
+ const animated = transform.call(gProxy), base = animated.baseVal;
+ const item = base.getItem(0);
+ item.setTranslate(3, 4);
+ const reparsed = g.cloneNode(false).transform.baseVal.getItem(0).matrix;
+ if (!g.getAttribute('transform') || reparsed.a !== 1 || reparsed.b !== 0 || reparsed.c !== 0 ||
+ reparsed.d !== 1 || reparsed.e !== 3 || reparsed.f !== 4 || transform.call(gProxy) !== animated ||
+ animated.baseVal !== base || base.getItem(0) !== item || animated.animVal.getItem(0).matrix.f !== 4) {
+ throw Error('native transform writeback or retained base item');
+ }
+ return true;
+ })()"#,
+ )
+ .unwrap(),
+ "true"
+ );
+}