Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(webidl): expose UI event attributes on native prototypes

ldm08dc3d29abb60
diff --git a/moli-renderer-v8/src/context_bootstrap/event_template.rs b/moli-renderer-v8/src/context_bootstrap/event_template.rs
index 28da36009..653379171 100644
--- a/moli-renderer-v8/src/context_bootstrap/event_template.rs
+++ b/moli-renderer-v8/src/context_bootstrap/event_template.rs
@@ -1,4 +1,3 @@
-use super::events::event_value_attribute_getter;
use super::{
event_document::{document_create_event_callback, document_has_focus_callback},
event_legacy::{
@@ -18,14 +17,15 @@ use super::{
event_return_value_setter_function, event_src_element_getter_function,
event_stop_immediate_propagation_callback, event_stop_propagation_callback,
event_target_getter_function, event_time_stamp_getter_function, event_type_getter_function,
- focus_event_related_target_getter_function, form_data_event_form_data_getter_function,
- message_event_init_callback, mouse_event_related_target_getter_function,
- pointer_event_get_predicted_events_callback, submit_event_agent_invoked_getter_function,
- submit_event_respond_with_callback, submit_event_submitter_getter_function,
- track_event_track_getter_function, ui_event_pseudo_target_getter_function,
+ event_value_attribute_getter, focus_event_related_target_getter_function,
+ form_data_event_form_data_getter_function, message_event_init_callback,
+ mouse_event_related_target_getter_function, pointer_event_get_predicted_events_callback,
+ submit_event_agent_invoked_getter_function, submit_event_respond_with_callback,
+ submit_event_submitter_getter_function, track_event_track_getter_function,
+ ui_event_pseudo_target_getter_function, ui_event_which_getter_function,
},
selection_surface::document_get_selection_callback,
- specs::{ConstructorKind, ConstructorSpec},
+ specs::ConstructorSpec,
};
use crate::web_api_interfaces;
use crate::{native_bridge::document, window_host};
@@ -166,6 +166,13 @@ struct FormDataEventTemplateAccessorsDeclaration {
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::UIEvent, enumerable, receiver)]
struct UiEventTemplateMethodsDeclaration {
+ #[webapi(accessor_property = "view", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "view"))]
+ view: (),
+ #[webapi(accessor_property = "detail", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "detail"))]
+ detail: (),
+ #[webapi(accessor_property = "which", getter = ui_event_which_getter_function, data = crate::util::v8str(scope, "which"))]
+ which: (),
+
#[webapi(method = "initUIEvent", length = 1, callback = ui_event_init_callback)]
init_ui_event: (),
@@ -189,6 +196,9 @@ struct FocusEventTemplateAccessorsDeclaration {
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::TextEvent, enumerable, receiver)]
struct TextEventTemplateMethodsDeclaration {
+ #[webapi(accessor_property = "data", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "data"))]
+ data: (),
+
#[webapi(method = "initTextEvent", length = 1, callback = text_event_init_callback)]
init_text_event: (),
}
@@ -196,6 +206,9 @@ struct TextEventTemplateMethodsDeclaration {
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::CompositionEvent, enumerable, receiver)]
struct CompositionEventTemplateMethodsDeclaration {
+ #[webapi(accessor_property = "data", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "data"))]
+ data: (),
+
#[webapi(
method = "initCompositionEvent",
length = 1,
@@ -211,6 +224,19 @@ struct CustomEventTemplateMethodsDeclaration {
init_custom_event: (),
}
+#[derive(WebApiFunctionTemplate)]
+#[webapi(interface = web_api_interfaces::InputEvent, enumerable, receiver)]
+struct InputEventTemplateAccessorsDeclaration {
+ #[webapi(accessor_property, getter = event_value_attribute_getter, data = crate::util::v8str(scope, "data"))]
+ data: (),
+
+ #[webapi(accessor_property, getter = event_value_attribute_getter, data = crate::util::v8str(scope, "isComposing"))]
+ is_composing: (),
+
+ #[webapi(accessor_property, getter = event_value_attribute_getter, data = crate::util::v8str(scope, "inputType"))]
+ input_type: (),
+}
+
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::StorageEvent, enumerable, receiver)]
struct StorageEventTemplateMethodsDeclaration {
@@ -225,6 +251,51 @@ struct StorageEventTemplateMethodsDeclaration {
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::KeyboardEvent, enumerable, receiver)]
struct KeyboardEventTemplateMethodsDeclaration {
+ #[webapi(constant = "DOM_KEY_LOCATION_STANDARD", value = 0u32)]
+ dom_key_location_standard: (),
+
+ #[webapi(constant = "DOM_KEY_LOCATION_LEFT", value = 1u32)]
+ dom_key_location_left: (),
+
+ #[webapi(constant = "DOM_KEY_LOCATION_RIGHT", value = 2u32)]
+ dom_key_location_right: (),
+
+ #[webapi(constant = "DOM_KEY_LOCATION_NUMPAD", value = 3u32)]
+ dom_key_location_numpad: (),
+
+ #[webapi(accessor_property = "key", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "key"))]
+ key: (),
+
+ #[webapi(accessor_property = "code", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "code"))]
+ code: (),
+
+ #[webapi(accessor_property = "location", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "location"))]
+ location: (),
+
+ #[webapi(accessor_property = "ctrlKey", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "ctrlKey"))]
+ ctrl_key: (),
+
+ #[webapi(accessor_property = "shiftKey", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "shiftKey"))]
+ shift_key: (),
+
+ #[webapi(accessor_property = "altKey", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "altKey"))]
+ alt_key: (),
+
+ #[webapi(accessor_property = "metaKey", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "metaKey"))]
+ meta_key: (),
+
+ #[webapi(accessor_property = "repeat", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "repeat"))]
+ repeat: (),
+
+ #[webapi(accessor_property = "isComposing", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "isComposing"))]
+ is_composing: (),
+
+ #[webapi(accessor_property = "charCode", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "charCode"))]
+ char_code: (),
+
+ #[webapi(accessor_property = "keyCode", getter = event_value_attribute_getter, data = crate::util::v8str(scope, "keyCode"))]
+ key_code: (),
+
#[webapi(method = "initKeyboardEvent", length = 1, callback = keyboard_event_init_callback)]
init_keyboard_event: (),
@@ -357,13 +428,6 @@ struct SecurityPolicyViolationEventTemplateAccessorsDeclaration {
column_number: (),
}
-#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::MessageEvent, enumerable, receiver)]
-struct MessageEventTemplateMethodsDeclaration {
- #[webapi(method = "initMessageEvent", length = 1, callback = message_event_init_callback)]
- init_message_event: (),
-}
-
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::ErrorEvent, enumerable, receiver)]
struct ErrorEventTemplateAccessorsDeclaration {
@@ -383,13 +447,11 @@ struct ErrorEventTemplateAccessorsDeclaration {
error: (),
}
-fn install_event_base_bindings<'s>(
- scope: &mut v8::PinScope<'s, '_, ()>,
- template: v8::Local<'s, v8::FunctionTemplate>,
-) {
- let proto = template.prototype_template(scope);
- EventBaseTemplateMethodsDeclaration::initialize_template(scope, template);
- EventBaseTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
+#[derive(WebApiFunctionTemplate)]
+#[webapi(interface = web_api_interfaces::MessageEvent, enumerable, receiver)]
+struct MessageEventTemplateMethodsDeclaration {
+ #[webapi(method = "initMessageEvent", length = 1, callback = message_event_init_callback)]
+ init_message_event: (),
}
struct EventTemplateDeclaration {
@@ -415,16 +477,41 @@ fn install_declaration<'s, D: WebApiFunctionTemplateDeclaration>(
D::initialize_prototype_template(scope, prototype);
}
+// Installation and wrapper placement share the same native declarations. Public
+// prototype mutations cannot change which fields are exposed as own properties.
fn event_template_declaration(interface: &str) -> Option<EventTemplateDeclaration> {
- match interface {
- "ErrorEvent" => Some(EventTemplateDeclaration::new::<
- ErrorEventTemplateAccessorsDeclaration,
- >()),
- "SecurityPolicyViolationEvent" => Some(EventTemplateDeclaration::new::<
+ Some(match interface {
+ "ErrorEvent" => EventTemplateDeclaration::new::<ErrorEventTemplateAccessorsDeclaration>(),
+ "SecurityPolicyViolationEvent" => EventTemplateDeclaration::new::<
SecurityPolicyViolationEventTemplateAccessorsDeclaration,
- >()),
- _ => None,
- }
+ >(),
+ "MessageEvent" => EventTemplateDeclaration::new::<MessageEventTemplateMethodsDeclaration>(),
+ "Event" => EventTemplateDeclaration::new::<EventBaseTemplateMethodsDeclaration>(),
+ "UIEvent" => EventTemplateDeclaration::new::<UiEventTemplateMethodsDeclaration>(),
+ "FocusEvent" => EventTemplateDeclaration::new::<FocusEventTemplateAccessorsDeclaration>(),
+ "TextEvent" => EventTemplateDeclaration::new::<TextEventTemplateMethodsDeclaration>(),
+ "CompositionEvent" => {
+ EventTemplateDeclaration::new::<CompositionEventTemplateMethodsDeclaration>()
+ }
+ "CustomEvent" => EventTemplateDeclaration::new::<CustomEventTemplateMethodsDeclaration>(),
+ "InputEvent" => EventTemplateDeclaration::new::<InputEventTemplateAccessorsDeclaration>(),
+ "StorageEvent" => EventTemplateDeclaration::new::<StorageEventTemplateMethodsDeclaration>(),
+ "KeyboardEvent" => {
+ EventTemplateDeclaration::new::<KeyboardEventTemplateMethodsDeclaration>()
+ }
+ "MouseEvent" => EventTemplateDeclaration::new::<MouseEventTemplateMethodsDeclaration>(),
+ "WheelEvent" => EventTemplateDeclaration::new::<WheelEventTemplateConstantsDeclaration>(),
+ "PointerEvent" => EventTemplateDeclaration::new::<PointerEventTemplateMethodsDeclaration>(),
+ "CloseEvent" => EventTemplateDeclaration::new::<CloseEventTemplateAccessorsDeclaration>(),
+ "TrackEvent" => EventTemplateDeclaration::new::<TrackEventTemplateAccessorsDeclaration>(),
+ "SubmitEvent" => EventTemplateDeclaration::new::<SubmitEventTemplateAccessorsDeclaration>(),
+ "FormDataEvent" => {
+ EventTemplateDeclaration::new::<FormDataEventTemplateAccessorsDeclaration>()
+ }
+ "EventTarget" => EventTemplateDeclaration::new::<EventTargetTemplateMethodsDeclaration>(),
+ "Document" => EventTemplateDeclaration::new::<DocumentEventTemplateMethodsDeclaration>(),
+ _ => return None,
+ })
}
pub(super) fn event_has_prototype_attribute(interface: &str, property: &str) -> bool {
@@ -446,89 +533,15 @@ pub(super) fn install_event_template_bindings<'s>(
template: v8::Local<'s, v8::FunctionTemplate>,
spec: ConstructorSpec,
) {
- if spec.kind == ConstructorKind::Event {
- install_event_base_bindings(scope, template);
+ let interface = spec.interface.name();
+ super::events::install_value_event_template_bindings(scope, template, interface);
+ super::events::install_device_event_template_bindings(scope, template, interface);
+ if interface == "Document" {
+ let prototype = template.prototype_template(scope);
+ document::install_document_prototype_methods(scope, prototype);
}
- super::events::install_value_event_template_bindings(scope, template, spec.interface.name());
- super::events::speech::install(scope, template, spec.interface.name());
- super::events::install_device_event_template_bindings(scope, template, spec.interface.name());
-
- if let Some(declaration) = event_template_declaration(spec.interface.name()) {
+ super::events::speech::install(scope, template, interface);
+ if let Some(declaration) = event_template_declaration(interface) {
(declaration.install)(scope, template);
- return;
- }
-
- match spec.interface.name() {
- "MessageEvent" => {
- let proto = template.prototype_template(scope);
- MessageEventTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
- }
- "UIEvent" => {
- let proto = template.prototype_template(scope);
- UiEventTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
- }
- "FocusEvent" => {
- let proto = template.prototype_template(scope);
- FocusEventTemplateAccessorsDeclaration::initialize_prototype_template(scope, proto);
- }
- "TextEvent" => {
- let proto = template.prototype_template(scope);
- TextEventTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
- }
- "CompositionEvent" => {
- let proto = template.prototype_template(scope);
- CompositionEventTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
- }
- "CustomEvent" => {
- let proto = template.prototype_template(scope);
- CustomEventTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
- }
- "StorageEvent" => {
- let proto = template.prototype_template(scope);
- StorageEventTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
- }
- "KeyboardEvent" => {
- let proto = template.prototype_template(scope);
- KeyboardEventTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
- }
- "MouseEvent" => {
- let proto = template.prototype_template(scope);
- MouseEventTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
- }
- "WheelEvent" => {
- let proto = template.prototype_template(scope);
- WheelEventTemplateConstantsDeclaration::initialize_template(scope, template);
- WheelEventTemplateConstantsDeclaration::initialize_prototype_template(scope, proto);
- }
- "PointerEvent" => {
- let proto = template.prototype_template(scope);
- PointerEventTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
- }
- "CloseEvent" => {
- let proto = template.prototype_template(scope);
- CloseEventTemplateAccessorsDeclaration::initialize_prototype_template(scope, proto);
- }
- "TrackEvent" => {
- let proto = template.prototype_template(scope);
- TrackEventTemplateAccessorsDeclaration::initialize_prototype_template(scope, proto);
- }
- "SubmitEvent" => {
- let proto = template.prototype_template(scope);
- SubmitEventTemplateAccessorsDeclaration::initialize_prototype_template(scope, proto);
- }
- "FormDataEvent" => {
- let proto = template.prototype_template(scope);
- FormDataEventTemplateAccessorsDeclaration::initialize_prototype_template(scope, proto);
- }
- "EventTarget" => {
- let prototype = template.prototype_template(scope);
- EventTargetTemplateMethodsDeclaration::initialize_prototype_template(scope, prototype);
- }
- "Document" => {
- let proto = template.prototype_template(scope);
- document::install_document_prototype_methods(scope, proto);
- DocumentEventTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
- }
- _ => {}
}
}
diff --git a/moli-renderer-v8/src/context_bootstrap/events.rs b/moli-renderer-v8/src/context_bootstrap/events.rs
index 3f79270c7..d0df2f19b 100644
--- a/moli-renderer-v8/src/context_bootstrap/events.rs
+++ b/moli-renderer-v8/src/context_bootstrap/events.rs
@@ -502,3 +502,6 @@ pub(in crate::context_bootstrap) use message::message_event_init_callback;
pub(in crate::context_bootstrap) use wrappers::event_value_attribute_getter;
pub(crate) use subclasses::construct_original_error_event;
+
+mod ui_properties;
+pub(in crate::context_bootstrap) use ui_properties::ui_event_which_getter_function;
diff --git a/moli-renderer-v8/src/context_bootstrap/events/ui_properties.rs b/moli-renderer-v8/src/context_bootstrap/events/ui_properties.rs
new file mode 100644
index 000000000..e9eb09e3c
--- /dev/null
+++ b/moli-renderer-v8/src/context_bootstrap/events/ui_properties.rs
@@ -0,0 +1,19 @@
+use super::event_backing;
+
+pub(in crate::context_bootstrap) fn ui_event_which_getter_function<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'_, v8::Value>,
+) {
+ // The declaration's generated receiver check requires a native UIEvent.
+ // Older native UI event producers do not store the legacy which member.
+ let state = event_backing(scope, args.this());
+ let Some(value) = state.get(scope, args.data()) else {
+ return;
+ };
+ if value.is_undefined() {
+ rv.set_uint32(0);
+ } else {
+ rv.set(value);
+ }
+}
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/event_prototype_attributes.js b/moli-renderer-v8/src/script_vm/tests/browser_api/event_prototype_attributes.js
new file mode 100644
index 000000000..8a2c2d249
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/event_prototype_attributes.js
@@ -0,0 +1,192 @@
+(() => {
+ const rows = [], defaultWhich = {};
+ const check = (name, run) => {
+ try {
+ if (run() !== true) throw new Error('assertion failed');
+ rows.push({name, passed: true});
+ } catch (error) {
+ rows.push({name, passed: false, error: String(error), stack: error.stack});
+ }
+ };
+ const other = document.getElementById('child').contentWindow;
+ const typeError = (run, realm = globalThis) => {
+ try { run(); } catch (error) {
+ return error instanceof realm.TypeError &&
+ (realm === globalThis || !(error instanceof TypeError));
+ }
+ return false;
+ };
+ const own = (object, name) => Object.prototype.hasOwnProperty.call(object, name);
+ const make = (name, realm = globalThis) => {
+ if (name === 'TextEvent') {
+ const event = realm.document.createEvent(name);
+ event.initTextEvent('event', false, false, null, 'text');
+ return event;
+ }
+ return new realm[name]('event', {
+ view: realm, detail: 3, data: 'text', inputType: 'insertText', isComposing: true,
+ key: 'K', code: 'KeyK', location: 2, ctrlKey: true, shiftKey: true,
+ altKey: true, metaKey: true, repeat: true, charCode: 75, keyCode: 75
+ });
+ };
+ const declarations = [
+ ['Event', ['type', 'target', 'currentTarget', 'eventPhase', 'bubbles', 'cancelable',
+ 'defaultPrevented', 'composed', 'srcElement', 'timeStamp', 'cancelBubble', 'returnValue']],
+ ['UIEvent', ['view', 'detail', 'which']],
+ ['FocusEvent', ['relatedTarget']],
+ ['InputEvent', ['data', 'inputType', 'isComposing']],
+ ['KeyboardEvent', ['key', 'code', 'location', 'ctrlKey', 'shiftKey', 'altKey',
+ 'metaKey', 'repeat', 'isComposing', 'charCode', 'keyCode']],
+ ['CompositionEvent', ['data']],
+ ['TextEvent', ['data']]
+ ];
+ for (const [name, attributes] of declarations) {
+ for (const attribute of attributes) {
+ const descriptor = Object.getOwnPropertyDescriptor(globalThis[name].prototype, attribute);
+ check(`${name}.${attribute} prototype descriptor`, () => {
+ const mutable = name === 'Event' && ['cancelBubble', 'returnValue'].includes(attribute);
+ return !!descriptor && descriptor.enumerable && descriptor.configurable &&
+ typeof descriptor.get === 'function' && descriptor.get.length === 0 &&
+ descriptor.get.name === `get ${attribute}` &&
+ (mutable ? typeof descriptor.set === 'function' : descriptor.set === undefined);
+ });
+ check(`${name}.${attribute} instance inherits accessor`, () => {
+ const event = make(name);
+ return !own(event, attribute) && attribute in event &&
+ Object.is(descriptor.get.call(event), event[attribute]);
+ });
+ check(`${name}.${attribute} genuine receiver ignores public prototype`, () => {
+ const event = make(name), value = event[attribute];
+ Object.setPrototypeOf(event, null);
+ return Object.is(descriptor.get.call(event), value);
+ });
+ check(`${name}.${attribute} borrowed foreign receiver`, () => {
+ const event = make(name, other);
+ return Object.is(descriptor.get.call(event), event[attribute]);
+ });
+ check(`${name}.${attribute} rejects forged and proxy receivers without traps`, () => {
+ const event = make(name), revocable = Proxy.revocable(event, {});
+ let traps = 0;
+ const proxy = new Proxy(event, {
+ get() {traps++;}, getPrototypeOf() {traps++;}, has() {traps++;}
+ });
+ revocable.revoke();
+ return [{}, globalThis[name].prototype, Object.create(globalThis[name].prototype),
+ Object.create(event), proxy, revocable.proxy].every(
+ value => typeError(() => descriptor.get.call(value))) && traps === 0;
+ });
+ check(`${name}.${attribute} foreign getter throws in callee realm`, () => {
+ const getter = Object.getOwnPropertyDescriptor(other[name].prototype, attribute).get;
+ return typeError(() => getter.call({}), other) &&
+ typeError(() => getter.call(new Proxy(make(name), {})), other);
+ });
+ }
+ }
+ for (const name of ['UIEvent', 'FocusEvent', 'InputEvent', 'KeyboardEvent',
+ 'CompositionEvent', 'TextEvent', 'MouseEvent', 'WheelEvent', 'PointerEvent', 'TouchEvent']) {
+ check(`${name} inherits base and UI attributes`, () => {
+ const event = name === 'TextEvent' ? make(name) : new globalThis[name]('event');
+ defaultWhich[name] = event.which;
+ return ['type', 'view', 'detail', 'which'].every(attribute => !own(event, attribute)) &&
+ typeof event.which === 'number' &&
+ Object.getOwnPropertyDescriptor(UIEvent.prototype, 'which').get.call(event) === event.which;
+ });
+ }
+ check('non-mouse UI event classes default which to zero', () =>
+ ['UIEvent', 'FocusEvent', 'InputEvent', 'KeyboardEvent', 'CompositionEvent', 'TextEvent']
+ .every(name => defaultWhich[name] === 0));
+ for (const [suffix, value] of [['STANDARD', 0], ['LEFT', 1], ['RIGHT', 2], ['NUMPAD', 3]]) {
+ const name = `DOM_KEY_LOCATION_${suffix}`;
+ for (const object of [KeyboardEvent, KeyboardEvent.prototype]) {
+ check(`${object === KeyboardEvent ? 'KeyboardEvent' : 'KeyboardEvent.prototype'}.${name}`, () => {
+ const d = Object.getOwnPropertyDescriptor(object, name);
+ return !!d && d.value === value && d.enumerable && !d.writable && !d.configurable;
+ });
+ }
+ check(`KeyboardEvent instance inherits ${name}`, () => {
+ const event = make('KeyboardEvent');
+ return event[name] === value && !own(event, name);
+ });
+ }
+ for (const [interfaceName, attribute, wrong] of [
+ ['UIEvent', 'view', new Event('event')],
+ ['KeyboardEvent', 'key', new UIEvent('event')],
+ ['CompositionEvent', 'data', new InputEvent('event')],
+ ['TextEvent', 'data', new CompositionEvent('event')],
+ ['InputEvent', 'isComposing', new KeyboardEvent('event')],
+ ['FocusEvent', 'relatedTarget', new MouseEvent('event')]
+ ]) {
+ check(`${interfaceName}.${attribute} rejects unrelated genuine interface`, () =>
+ typeError(() => Object.getOwnPropertyDescriptor(globalThis[interfaceName].prototype, attribute).get.call(wrong)));
+ }
+ check('UI view retains original foreign Window identity', () => {
+ const event = new UIEvent('event', {view: other});
+ return event.view === other &&
+ Object.getOwnPropertyDescriptor(other.UIEvent.prototype, 'view').get.call(event) === other;
+ });
+ check('isTrusted remains an own unforgeable accessor', () => {
+ const event = make('KeyboardEvent'), d = Object.getOwnPropertyDescriptor(event, 'isTrusted');
+ return !!d && typeof d.get === 'function' && d.set === undefined && d.enumerable &&
+ !d.configurable && !own(Event.prototype, 'isTrusted') && event.isTrusted === false;
+ });
+ check('InputEvent payload and composing state remain unchanged', () => {
+ const event = make('InputEvent');
+ return event.data === 'text' && event.inputType === 'insertText' && event.isComposing === true;
+ });
+ check('KeyboardEvent values remain unchanged', () => {
+ const event = make('KeyboardEvent');
+ return event.key === 'K' && event.code === 'KeyK' && event.location === 2 &&
+ event.ctrlKey && event.shiftKey && event.altKey && event.metaKey && event.repeat &&
+ event.isComposing && event.charCode === 75 && event.keyCode === 75;
+ });
+ check('frozen wrapper still exposes legacy initialization from private state', () => {
+ const event = Object.freeze(new UIEvent('before', {view: window, detail: 7}));
+ event.initUIEvent('after', true, true, window, 11);
+ return event.type === 'after' && event.bubbles && event.cancelable && event.view === window && event.detail === 11;
+ });
+ check('prototype replacement is observable without running author code during construction', () => {
+ const prototype = KeyboardEvent.prototype;
+ const d = Object.getOwnPropertyDescriptor(prototype, 'key');
+ let reads = 0, writes = 0;
+ try {
+ Object.defineProperty(prototype, 'key', {
+ get() {reads++; return 'public';}, set() {writes++;}, configurable: true
+ });
+ const event = new KeyboardEvent('event', {key: 'native'});
+ if (reads || writes || own(event, 'key') || d.get.call(event) !== 'native') return false;
+ return event.key === 'public' && reads === 1 && writes === 0;
+ } finally { Object.defineProperty(prototype, 'key', d); }
+ });
+ check('deleted prototype attribute is not replaced with an own getter', () => {
+ const d = Object.getOwnPropertyDescriptor(CompositionEvent.prototype, 'data');
+ try {
+ delete CompositionEvent.prototype.data;
+ const event = new CompositionEvent('event', {data: 'native'});
+ return !own(event, 'data') && event.data === undefined && d.get.call(event) === 'native';
+ } finally { Object.defineProperty(CompositionEvent.prototype, 'data', d); }
+ });
+ check('private event state ignores Object.prototype pollution', () => {
+ const d = Object.getOwnPropertyDescriptor(Object.prototype, 'which');
+ try {
+ Object.defineProperty(Object.prototype, 'which', {get() {throw new Error('polluted');}, configurable: true});
+ return new FocusEvent('event').which === 0 && new InputEvent('event').which === 0;
+ } finally {
+ if (d) Object.defineProperty(Object.prototype, 'which', d);
+ else delete Object.prototype.which;
+ }
+ });
+ check('dispatch ignores author shadows of type and defaultPrevented', () => {
+ const target = document.createElement('div'), event = new UIEvent('native', {cancelable: true});
+ let calls = 0;
+ target.addEventListener('native', e => {calls++; e.preventDefault();});
+ Object.defineProperty(event, 'type', {get() {throw new Error('public type');}});
+ Object.defineProperty(event, 'defaultPrevented', {value: false});
+ return target.dispatchEvent(event) === false && calls === 1 && event.defaultPrevented === false &&
+ Object.getOwnPropertyDescriptor(Event.prototype, 'defaultPrevented').get.call(event) === true;
+ });
+ globalThis.__uiEventResults = {
+ rows, passed: rows.filter(row => row.passed).length, total: rows.length,
+ isSecureContext, origin: location.origin, defaultWhich
+ };
+ return rows.every(row => row.passed);
+})()
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/event_prototype_attributes.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/event_prototype_attributes.rs
new file mode 100644
index 000000000..04ba8e9c7
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/event_prototype_attributes.rs
@@ -0,0 +1,18 @@
+use super::*;
+
+#[test]
+fn event_prototype_attributes_use_native_interface_brands_and_private_state() {
+ let mut vm = new_storage_html_test_vm("https://event-prototype-webidl.test/");
+ vm.eval("document.body.innerHTML = '<iframe id=child></iframe>';")
+ .unwrap();
+ let result = vm
+ .eval(include_str!("event_prototype_attributes.js"))
+ .unwrap();
+ assert_eq!(
+ result,
+ "true",
+ "{}",
+ vm.eval("JSON.stringify(__uiEventResults.rows.filter(row=>!row.passed))")
+ .unwrap()
+ );
+}
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage/event_dispatch_and_targets.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage/event_dispatch_and_targets.rs
index 6b6936c9e..e75dfc6cb 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage/event_dispatch_and_targets.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/events_selection_storage/event_dispatch_and_targets.rs
@@ -1535,7 +1535,7 @@ fn event_trusted_slot_ignores_legacy_reflection_and_spoofing() {
assert_eq!(
result,
- r#"{"accessorDescriptors":["returnValue:function:get returnValue:0:function:set returnValue:1:true:true:true","cancelBubble:function:get cancelBubble:0:function:set cancelBubble:1:true:true:true","isTrusted:function:get isTrusted:0:undefined:undefined:undefined:true:false:true"],"keys":"type,target,srcElement,currentTarget,defaultPrevented,bubbles,cancelable,isTrusted,composed,eventPhase","returnValueBefore":true,"returnValueAfter":false,"defaultPreventedAfterReturnValue":true,"cancelBubbleBefore":false,"cancelBubbleAfter":true,"reinitializedShape":{"keys":"type,target,srcElement,currentTarget,defaultPrevented,bubbles,cancelable,isTrusted,composed,eventPhase","accessors":["returnValue:function:get returnValue:0:function:set returnValue:1:true:true:true","cancelBubble:function:get cancelBubble:0:function:set cancelBubble:1:true:true:true","isTrusted:function:get isTrusted:0:undefined:undefined:undefined:true:false:true"],"type":"after","bubbles":true,"cancelable":true,"defaultPrevented":false,"trusted":false},"plainOwnBefore":false,"plainTrustedAfterSpoof":false,"plainSpoofedOwnValue":true,"fakeTrusted":"TypeError","dispatched":true,"trustedDispatch":{"ownBefore":false,"trustedAfterSpoof":true,"spoofedOwnValue":false}}"#
+ r#"{"accessorDescriptors":["returnValue:function:get returnValue:0:function:set returnValue:1:true:true:true","cancelBubble:function:get cancelBubble:0:function:set cancelBubble:1:true:true:true","isTrusted:function:get isTrusted:0:undefined:undefined:undefined:true:false:true"],"keys":"isTrusted","returnValueBefore":true,"returnValueAfter":false,"defaultPreventedAfterReturnValue":true,"cancelBubbleBefore":false,"cancelBubbleAfter":true,"reinitializedShape":{"keys":"isTrusted","accessors":["returnValue:function:get returnValue:0:function:set returnValue:1:true:true:true","cancelBubble:function:get cancelBubble:0:function:set cancelBubble:1:true:true:true","isTrusted:function:get isTrusted:0:undefined:undefined:undefined:true:false:true"],"type":"after","bubbles":true,"cancelable":true,"defaultPrevented":false,"trusted":false},"plainOwnBefore":false,"plainTrustedAfterSpoof":false,"plainSpoofedOwnValue":true,"fakeTrusted":"TypeError","dispatched":true,"trustedDispatch":{"ownBefore":false,"trustedAfterSpoof":true,"spoofedOwnValue":false}}"#
);
}
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/history_worlds.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/history_worlds.rs
index 6b1af1a37..bd850a74b 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/history_worlds.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/history_worlds.rs
@@ -17,8 +17,8 @@ fn event_field_getters_are_shared_within_a_realm_without_capturing_instances() {
for (const name of Object.keys(event)) getters.add(own(event, name).get);
if (event.type !== String(i) || event.detail.value !== i) throw new Error('captured instance');
}
- const type = own(first, 'type'), detail = own(first, 'detail');
- const childType = own(child, 'type');
+ const type = own(Event.prototype, 'type'), detail = own(first, 'detail');
+ const childType = own(other.Event.prototype, 'type');
let illegal = false;
try { childType.get.call({}); }
catch (error) { illegal = error instanceof other.TypeError; }
@@ -35,7 +35,8 @@ fn event_field_getters_are_shared_within_a_realm_without_capturing_instances() {
return JSON.stringify({
getterCount: getters.size,
fieldCount: Object.keys(first).length,
- base: type.get === own(new Event('base'), 'type').get,
+ base: !own(first, 'type') && !own(new Event('base'), 'type') &&
+ type.get.call(first) === first.type && type.get.call(new Event('base')) === 'base',
realm: type.get instanceof Function && childType.get instanceof other.Function &&
childType.get !== type.get && !(childType.get instanceof Function),
borrowed: type.get.call(child) === 'child' && detail.get.call(child) === child.detail,
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/media.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/media.rs
index e62648ef0..579070ba7 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/media.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/media.rs
@@ -3097,7 +3097,7 @@ async fn match_media_change_event_uses_event_prototype_and_declared_properties()
assert_eq!(
result,
- r#"[{"tag":"[object MediaQueryListEvent]","ctor":"MediaQueryListEvent","protoCtor":"MediaQueryListEvent","keys":"type,target,srcElement,currentTarget,defaultPrevented,bubbles,cancelable,isTrusted,composed,eventPhase","type":"change","media":"(prefers-color-scheme: dark)","matches":true,"mediaEnumerable":false,"matchesEnumerable":false,"targetIsMql":true,"currentTargetIsMql":true,"bubbles":false,"isTrusted":true,"cancelable":false}]"#
+ r#"[{"tag":"[object MediaQueryListEvent]","ctor":"MediaQueryListEvent","protoCtor":"MediaQueryListEvent","keys":"isTrusted","type":"change","media":"(prefers-color-scheme: dark)","matches":true,"mediaEnumerable":false,"matchesEnumerable":false,"targetIsMql":true,"currentTargetIsMql":true,"bubbles":false,"isTrusted":true,"cancelable":false}]"#
);
}
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/events_errors.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/events_errors.rs
index 43316d8c0..16d1b44f0 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/events_errors.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/events_errors.rs
@@ -172,7 +172,7 @@ fn host_dispatched_events_ignore_user_replaced_event_constructor() {
assert_eq!(
result,
- r#"[{"type":"DOMContentLoaded","trusted":true,"target":true,"currentTarget":true,"bubbles":true,"cancelable":false,"intrinsicPrototype":true,"intrinsicInstance":true,"ownMethods":[],"inheritedMethods":true,"ownCancelBubble":false,"ownReturnValue":false,"ownTimeStamp":false,"ownIsTrusted":true,"keys":["type","target","srcElement","currentTarget","defaultPrevented","bubbles","cancelable","isTrusted","composed","eventPhase"]}]"#
+ r#"[{"type":"DOMContentLoaded","trusted":true,"target":true,"currentTarget":true,"bubbles":true,"cancelable":false,"intrinsicPrototype":true,"intrinsicInstance":true,"ownMethods":[],"inheritedMethods":true,"ownCancelBubble":false,"ownReturnValue":false,"ownTimeStamp":false,"ownIsTrusted":true,"keys":["isTrusted"]}]"#
);
}
#[test]
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/mod.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/mod.rs
index 92f3a3884..9adfe25c6 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/mod.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/mod.rs
@@ -75,3 +75,5 @@ mod message_event_init;
mod window_current_event_private;
mod video_codec_support;
+
+mod event_prototype_attributes;