diff --git a/moli-renderer-v8/src/context_bootstrap/events/init.rs b/moli-renderer-v8/src/context_bootstrap/events/init.rs index 0b4695cbc1b..27eabe72bf0 100644 --- a/moli-renderer-v8/src/context_bootstrap/events/init.rs +++ b/moli-renderer-v8/src/context_bootstrap/events/init.rs @@ -30,38 +30,6 @@ pub(in crate::context_bootstrap) fn parse_event_dictionary<'s, T: webidl::WebIdl } } -pub(super) fn init_bool_property<'s>( - scope: &mut v8::PinScope<'s, '_>, - init: Option>, - key: &'static str, - default: bool, -) -> bool { - let Some(object) = init else { - return default; - }; - webidl::legacy_bool_member_or(scope, object, "EventInit", key, default) -} - -pub(super) fn init_string_property<'s>( - scope: &mut v8::PinScope<'s, '_>, - init: Option>, - key: &'static str, - default: &str, -) -> String { - let Some(object) = init else { - return default.to_owned(); - }; - webidl::legacy_string_member_or(scope, object, "EventInit", key, default) -} - -pub(super) fn init_value_property<'s>( - scope: &mut v8::PinScope<'s, '_>, - init: Option>, - key: &'static str, -) -> Option> { - init.and_then(|object| webidl::property_non_undefined(scope, object, key)) -} - pub(super) fn read_event_init<'s>( scope: &mut v8::PinScope<'s, '_>, args: &v8::FunctionCallbackArguments<'s>, diff --git a/moli-renderer-v8/src/context_bootstrap/events/subclasses.rs b/moli-renderer-v8/src/context_bootstrap/events/subclasses.rs index b28c04be33a..e4de24027ea 100644 --- a/moli-renderer-v8/src/context_bootstrap/events/subclasses.rs +++ b/moli-renderer-v8/src/context_bootstrap/events/subclasses.rs @@ -1,5 +1,4 @@ use super::base::{define_event_property, event_type_argument, initialize_event_object_with_type}; -use super::init::{init_bool_property, init_string_property, init_value_property, read_event_init}; use super::kind::EventSubclassKind; use super::*; @@ -9,6 +8,7 @@ mod data; mod error; mod input; mod keyboard; +mod navigation_init; mod payload; mod pointer; mod security_policy; diff --git a/moli-renderer-v8/src/context_bootstrap/events/subclasses/constructor.rs b/moli-renderer-v8/src/context_bootstrap/events/subclasses/constructor.rs index 6a2a2de6d5d..bc5cf13d1e2 100644 --- a/moli-renderer-v8/src/context_bootstrap/events/subclasses/constructor.rs +++ b/moli-renderer-v8/src/context_bootstrap/events/subclasses/constructor.rs @@ -1,5 +1,6 @@ use super::super::message; use super::*; +use crate::context_bootstrap::css_fontface_runtime::FontFaceSetLoadEventInit; use crate::web_api_interfaces; fn event_subclass_kind_from_callback_data( @@ -9,6 +10,146 @@ fn event_subclass_kind_from_callback_data( EventSubclassKind::from_i32(value) } +fn required_arguments(kind: EventSubclassKind) -> i32 { + match kind { + EventSubclassKind::NavigateEvent + | EventSubclassKind::NavigationCurrentEntryChangeEvent + | EventSubclassKind::PromiseRejectionEvent + | EventSubclassKind::FormDataEvent => 2, + _ => 1, + } +} + +// Every kind has a complete derived dictionary. A new kind must select its +// dictionary explicitly; there is no legacy EventInit-only fallback. +enum SubclassInit<'s> { + Basic(basic::BasicEventInit<'s>), + Pointing(pointer::PointerEventInitPayload<'s>), + Values(values::ValueEventInit<'s>), + Interaction(input::InteractionEventInit<'s>), + Payload(payload::PayloadEventInit<'s>), + Font(FontFaceSetLoadEventInit<'s>), + Storage(data::StorageEventInitMembers<'s>), + Keyboard(keyboard::KeyboardEventInit<'s>), + Security(security_policy::SecurityPolicyViolationEventInit), + Message(message::MessageEventInit<'s>), + Error(error::ErrorEventInit<'s>), + CurrentEntry(navigation_init::NavigationCurrentEntryChangeEventInitMembers<'s>), + Navigate(navigation_init::NavigateEventInitMembers<'s>), +} + +impl<'s> SubclassInit<'s> { + fn parse( + scope: &mut v8::PinScope<'s, '_>, + args: &v8::FunctionCallbackArguments<'s>, + kind: EventSubclassKind, + ) -> Option { + Some(match kind { + EventSubclassKind::UiEvent + | EventSubclassKind::FocusEvent + | EventSubclassKind::TextEvent + | EventSubclassKind::CompositionEvent + | EventSubclassKind::CustomEvent => { + Self::Basic(basic::BasicEventInit::parse(scope, args, kind)?) + } + EventSubclassKind::MouseEvent + | EventSubclassKind::WheelEvent + | EventSubclassKind::PointerEvent + | EventSubclassKind::DragEvent => { + Self::Pointing(pointer::PointerEventInitPayload::parse(scope, args, kind)?) + } + EventSubclassKind::ClipboardEvent + | EventSubclassKind::CloseEvent + | EventSubclassKind::CommandEvent + | EventSubclassKind::ToggleEvent + | EventSubclassKind::InterestEvent + | EventSubclassKind::PopStateEvent + | EventSubclassKind::HashChangeEvent + | EventSubclassKind::PageTransitionEvent => { + Self::Values(values::ValueEventInit::parse(scope, args, kind)?) + } + EventSubclassKind::InputEvent + | EventSubclassKind::TouchEvent + | EventSubclassKind::CapturedMouseEvent => { + Self::Interaction(input::InteractionEventInit::parse(scope, args, kind)?) + } + EventSubclassKind::PromiseRejectionEvent + | EventSubclassKind::FormDataEvent + | EventSubclassKind::SubmitEvent + | EventSubclassKind::TrackEvent => { + Self::Payload(payload::PayloadEventInit::parse(scope, args, kind)?) + } + EventSubclassKind::FontFaceSetLoadEvent => Self::Font( + super::super::parse_event_dictionary(scope, args, "FontFaceSetLoadEvent")?, + ), + EventSubclassKind::StorageEvent => { + Self::Storage(data::parse_storage_event_init(scope, args)?) + } + EventSubclassKind::KeyboardEvent => { + Self::Keyboard(keyboard::parse_keyboard_event_init(scope, args)?) + } + EventSubclassKind::SecurityPolicyViolationEvent => Self::Security( + security_policy::parse_security_policy_violation_event_init(scope, args)?, + ), + EventSubclassKind::MessageEvent => { + Self::Message(message::parse_message_event_init(scope, args)?) + } + EventSubclassKind::ErrorEvent => { + Self::Error(error::parse_error_event_init(scope, args)?) + } + EventSubclassKind::NavigationCurrentEntryChangeEvent => Self::CurrentEntry( + navigation_init::parse_current_entry_change_event_init(scope, args)?, + ), + EventSubclassKind::NavigateEvent => { + Self::Navigate(navigation_init::parse_navigate_event_init(scope, args)?) + } + }) + } + + fn event_flags(&self) -> (bool, bool, bool) { + match self { + Self::Basic(init) => init.event_flags(), + Self::Pointing(init) => init.event_flags(), + Self::Values(init) => init.event_flags(), + Self::Interaction(init) => init.event_flags(), + Self::Payload(init) => init.event_flags(), + Self::Font(init) => init.event_flags(), + Self::Storage(init) => init.event_flags(), + Self::Keyboard(init) => init.event_flags(), + Self::Security(init) => init.event_flags(), + Self::Message(init) => init.event_flags(), + Self::Error(init) => init.event_flags(), + Self::CurrentEntry(init) => init.event_flags(), + Self::Navigate(init) => init.event_flags(), + } + } + + fn initialize( + self, + scope: &mut v8::PinScope<'s, '_>, + event: v8::Local<'s, v8::Object>, + ) -> bool { + match self { + Self::Basic(init) => init.initialize(scope, event), + Self::Pointing(init) => init.initialize(scope, event), + Self::Values(init) => init.initialize(scope, event), + Self::Interaction(init) => return init.initialize(scope, event), + Self::Payload(init) => init.initialize(scope, event), + Self::Font(init) => init.initialize(scope, event), + Self::Storage(init) => data::initialize_storage_event(scope, event, init), + Self::Keyboard(init) => keyboard::initialize_keyboard_event(scope, event, init), + Self::Security(init) => init.initialize(scope, event), + Self::Message(init) => message::initialize_message_event(scope, event, init), + Self::Error(init) => init.initialize(scope, event), + Self::CurrentEntry(init) => { + data::initialize_navigation_current_entry_change_event(scope, event, init) + } + Self::Navigate(init) => data::initialize_navigate_event(scope, event, init), + } + true + } +} + fn event_subclass_constructor_callback<'s>( scope: &mut v8::PinScope<'s, '_>, args: v8::FunctionCallbackArguments<'s>, @@ -18,7 +159,6 @@ fn event_subclass_constructor_callback<'s>( throw_type_error(scope, "Invalid event subclass constructor."); return; }; - if !args.is_construct_call() { throw_type_error( scope, @@ -29,12 +169,7 @@ fn event_subclass_constructor_callback<'s>( ); return; } - - if matches!( - kind, - EventSubclassKind::PromiseRejectionEvent | EventSubclassKind::FormDataEvent - ) && args.length() < 2 - { + if required_arguments(kind) == 2 && args.length() < 2 { throw_type_error( scope, &format!( @@ -45,184 +180,15 @@ fn event_subclass_constructor_callback<'s>( ); return; } - let wrapper = args.this(); let Some(event_type) = event_type_argument(scope, &args, kind.constructor_name()) else { return; }; - let init = { - let init_arg = args.get(1); - if init_arg.is_null_or_undefined() || !init_arg.is_object() { - None - } else { - init_arg.to_object(scope) - } - }; - let basic_event_init = if matches!( - kind, - EventSubclassKind::UiEvent - | EventSubclassKind::FocusEvent - | EventSubclassKind::TextEvent - | EventSubclassKind::CompositionEvent - | EventSubclassKind::CustomEvent - ) { - let Some(init) = basic::BasicEventInit::parse(scope, &args, kind) else { - return; - }; - Some(init) - } else { - None - }; - let pointer_event_init = if matches!( - kind, - EventSubclassKind::MouseEvent - | EventSubclassKind::WheelEvent - | EventSubclassKind::PointerEvent - | EventSubclassKind::DragEvent - ) { - let Some(init) = pointer::PointerEventInitPayload::parse(scope, &args, kind) else { - return; - }; - Some(init) - } else { - None - }; - let interaction_event_init = if matches!( - kind, - EventSubclassKind::InputEvent - | EventSubclassKind::TouchEvent - | EventSubclassKind::CapturedMouseEvent - ) { - let Some(init) = input::InteractionEventInit::parse(scope, &args, kind) else { - return; - }; - Some(init) - } else { - None - }; - let payload_event_init = if matches!( - kind, - EventSubclassKind::PromiseRejectionEvent - | EventSubclassKind::FormDataEvent - | EventSubclassKind::SubmitEvent - | EventSubclassKind::TrackEvent - ) { - let Some(init) = payload::PayloadEventInit::parse(scope, &args, kind) else { - return; - }; - Some(init) - } else { - None - }; - let value_event_init = if matches!( - kind, - EventSubclassKind::ClipboardEvent - | EventSubclassKind::CloseEvent - | EventSubclassKind::CommandEvent - | EventSubclassKind::ToggleEvent - | EventSubclassKind::InterestEvent - | EventSubclassKind::PopStateEvent - | EventSubclassKind::HashChangeEvent - | EventSubclassKind::PageTransitionEvent - ) { - let Some(init) = values::ValueEventInit::parse(scope, &args, kind) else { - return; - }; - Some(init) - } else { - None - }; - let font_face_init = if kind == EventSubclassKind::FontFaceSetLoadEvent { - let Some(init) = super::super::parse_event_dictionary::< - crate::context_bootstrap::css_fontface_runtime::FontFaceSetLoadEventInit, - >(scope, &args, "FontFaceSetLoadEvent") else { - return; - }; - Some(init) - } else { - None - }; - let storage_event_init = if kind == EventSubclassKind::StorageEvent { - let Some(init) = data::parse_storage_event_init(scope, &args) else { - return; - }; - Some(init) - } else { - None - }; - let keyboard_event_init = if kind == EventSubclassKind::KeyboardEvent { - let Some(init) = keyboard::parse_keyboard_event_init(scope, &args) else { - return; - }; - Some(init) - } else { - None - }; - let security_policy_init = if kind == EventSubclassKind::SecurityPolicyViolationEvent { - let Some(init) = security_policy::parse_security_policy_violation_event_init(scope, &args) - else { - return; - }; - Some(init) - } else { - None - }; - let message_event_init = if kind == EventSubclassKind::MessageEvent { - let Some(init) = message::parse_message_event_init(scope, &args) else { - return; - }; - Some(init) - } else { - None - }; - let error_init = if kind == EventSubclassKind::ErrorEvent { - let Some(init) = error::parse_error_event_init(scope, &args) else { - return; - }; - Some(init) - } else { - None - }; - let Some((bubbles, cancelable, composed)) = security_policy_init - .as_ref() - .map(security_policy::SecurityPolicyViolationEventInit::event_flags) - .or_else(|| error_init.as_ref().map(error::ErrorEventInit::event_flags)) - .or_else(|| { - keyboard_event_init - .as_ref() - .map(keyboard::KeyboardEventInit::event_flags) - }) - .or_else(|| { - storage_event_init - .as_ref() - .map(data::StorageEventInitMembers::event_flags) - }) - .or_else(|| { - message_event_init - .as_ref() - .map(message::MessageEventInit::event_flags) - }) - .or_else(|| { - basic_event_init - .as_ref() - .map(basic::BasicEventInit::event_flags) - }) - .or_else(|| { - pointer_event_init - .as_ref() - .map(pointer::PointerEventInitPayload::event_flags) - }) - .or_else(|| font_face_init.as_ref().map(crate::context_bootstrap::css_fontface_runtime::FontFaceSetLoadEventInit::event_flags)) - .or_else(|| value_event_init.as_ref().map(values::ValueEventInit::event_flags)) - .or_else(|| interaction_event_init.as_ref().map(input::InteractionEventInit::event_flags)) - .or_else(|| payload_event_init.as_ref().map(payload::PayloadEventInit::event_flags)) - .or_else(|| read_event_init(scope, &args)) - else { + let Some(init) = SubclassInit::parse(scope, &args, kind) else { return; }; - + let (bubbles, cancelable, composed) = init.event_flags(); let event = new_event_state(scope); - initialize_event_object_with_type(scope, event, event_type, bubbles, cancelable); define_event_property( scope, @@ -230,97 +196,9 @@ fn event_subclass_constructor_callback<'s>( "composed", v8::Boolean::new(scope, composed).into(), ); - - match kind { - EventSubclassKind::UiEvent - | EventSubclassKind::FocusEvent - | EventSubclassKind::TextEvent - | EventSubclassKind::CompositionEvent - | EventSubclassKind::CustomEvent => { - basic_event_init - .expect("basic event init is parsed") - .initialize(scope, event); - } - EventSubclassKind::MouseEvent - | EventSubclassKind::WheelEvent - | EventSubclassKind::PointerEvent - | EventSubclassKind::DragEvent => { - pointer_event_init - .expect("pointing event init is parsed") - .initialize(scope, event); - } - EventSubclassKind::ClipboardEvent - | EventSubclassKind::CloseEvent - | EventSubclassKind::CommandEvent - | EventSubclassKind::ToggleEvent - | EventSubclassKind::InterestEvent - | EventSubclassKind::PopStateEvent - | EventSubclassKind::HashChangeEvent - | EventSubclassKind::PageTransitionEvent => value_event_init - .expect("value event init is parsed") - .initialize(scope, event), - EventSubclassKind::InputEvent - | EventSubclassKind::TouchEvent - | EventSubclassKind::CapturedMouseEvent => { - if !interaction_event_init - .expect("interaction event init is parsed") - .initialize(scope, event) - { - return; - } - } - EventSubclassKind::KeyboardEvent => { - keyboard::initialize_keyboard_event( - scope, - event, - keyboard_event_init.expect("KeyboardEvent init should be parsed"), - ); - } - EventSubclassKind::MessageEvent => message::initialize_message_event( - scope, - event, - message_event_init.expect("MessageEvent init should be parsed"), - ), - EventSubclassKind::StorageEvent => { - data::initialize_storage_event( - scope, - event, - storage_event_init.expect("StorageEvent init should be parsed"), - ); - } - EventSubclassKind::ErrorEvent => error_init - .expect("ErrorEvent init is parsed") - .initialize(scope, event), - EventSubclassKind::SecurityPolicyViolationEvent => { - security_policy_init - .expect("SecurityPolicyViolationEvent init is parsed") - .initialize(scope, event); - } - EventSubclassKind::PromiseRejectionEvent - | EventSubclassKind::FormDataEvent - | EventSubclassKind::SubmitEvent - | EventSubclassKind::TrackEvent => { - payload_event_init - .expect("payload event init is parsed") - .initialize(scope, event); - } - EventSubclassKind::NavigationCurrentEntryChangeEvent => { - if !data::initialize_navigation_current_entry_change_event(scope, event, init) { - return; - } - } - EventSubclassKind::NavigateEvent => { - if !data::initialize_navigate_event(scope, event, init) { - return; - } - } - EventSubclassKind::FontFaceSetLoadEvent => { - font_face_init - .expect("font event init is parsed") - .initialize(scope, event); - } + if !init.initialize(scope, event) { + return; } - web_api_interfaces::initialize(scope, event, kind.constructor_name()) .expect("event primary interface should initialize"); set_private_value( @@ -339,18 +217,8 @@ pub(in crate::context_bootstrap) fn build_event_subclass_template<'s>( kind: EventSubclassKind, ) -> v8::Local<'s, v8::FunctionTemplate> { let data: v8::Local<'s, v8::Value> = v8::Integer::new(scope, kind as i32).into(); - let length = if matches!( - kind, - EventSubclassKind::NavigateEvent - | EventSubclassKind::PromiseRejectionEvent - | EventSubclassKind::FormDataEvent - ) { - 2 - } else { - 1 - }; v8::FunctionTemplate::builder(event_subclass_constructor_callback) .data(data) - .length(length) + .length(required_arguments(kind)) .build(scope) } diff --git a/moli-renderer-v8/src/context_bootstrap/events/subclasses/data.rs b/moli-renderer-v8/src/context_bootstrap/events/subclasses/data.rs index 9a971cfc9bc..2091e46d228 100644 --- a/moli-renderer-v8/src/context_bootstrap/events/subclasses/data.rs +++ b/moli-renderer-v8/src/context_bootstrap/events/subclasses/data.rs @@ -130,30 +130,6 @@ struct NavigateEventMethodsDeclaration { scroll: (), } -#[derive(WebApiObject)] -#[webapi(plain, data_properties, enumerable)] -struct NavigationCurrentEntryChangeEventInitDeclaration<'scope> { - from: v8::Local<'scope, v8::Value>, - navigation_type: v8::Local<'scope, v8::Value>, -} - -#[derive(WebApiObject)] -#[webapi(plain, data_properties, enumerable)] -struct NavigateEventInitDeclaration<'scope> { - navigation_type: v8::Local<'scope, v8::String>, - destination: v8::Local<'scope, v8::Value>, - can_intercept: bool, - user_initiated: bool, - hash_change: bool, - signal: v8::Local<'scope, v8::Value>, - form_data: v8::Local<'scope, v8::Value>, - download_request: v8::Local<'scope, v8::Value>, - info: v8::Local<'scope, v8::Value>, - #[webapi(data_property = "hasUAVisualTransition")] - has_ua_visual_transition: bool, - source_element: v8::Local<'scope, v8::Value>, -} - #[derive(Default, webidl::WebIdlDictionary)] #[webidl(prefix = "StorageEventInit", snapshot)] pub(in crate::context_bootstrap::events) struct StorageEventInitMembers<'s> { @@ -215,97 +191,23 @@ pub(in crate::context_bootstrap::events::subclasses) fn initialize_storage_event .expect("StorageEvent state"); } -pub(in crate::context_bootstrap::events::subclasses) fn initialize_navigation_current_entry_change_event< - 's, ->( +pub(super) fn initialize_navigation_current_entry_change_event<'s>( scope: &mut v8::PinScope<'s, '_>, event: v8::Local<'s, v8::Object>, - init: Option>, -) -> bool { - let Some(init) = init else { - throw_type_error( - scope, - "Failed to construct 'NavigationCurrentEntryChangeEvent': NavigationCurrentEntryChangeEventInit.from is required.", - ); - return false; - }; - let Some(from) = init_value_property(scope, Some(init), "from") else { - throw_type_error( - scope, - "Failed to construct 'NavigationCurrentEntryChangeEvent': NavigationCurrentEntryChangeEventInit.from is required.", - ); - return false; - }; - let navigation_type = init_value_property(scope, Some(init), "navigationType") - .unwrap_or_else(|| v8::null(scope).into()); - NavigationCurrentEntryChangeEventInitDeclaration::new(from, navigation_type) - .initialize(scope, event) - .expect("NavigationCurrentEntryChangeEvent init declaration should initialize"); - true + init: super::navigation_init::NavigationCurrentEntryChangeEventInitMembers<'s>, +) { + init.initialize_snapshot(scope, event) + .expect("current entry event state"); } -pub(in crate::context_bootstrap::events::subclasses) fn initialize_navigate_event<'s>( +pub(super) fn initialize_navigate_event<'s>( scope: &mut v8::PinScope<'s, '_>, event: v8::Local<'s, v8::Object>, - init: Option>, -) -> bool { - let Some(init) = init else { - throw_type_error( - scope, - "Failed to construct 'NavigateEvent': NavigateEventInit.destination is required.", - ); - return false; - }; - let Some(destination) = init_value_property(scope, Some(init), "destination") else { - throw_type_error( - scope, - "Failed to construct 'NavigateEvent': NavigateEventInit.destination is required.", - ); - return false; - }; - let Some(signal) = init_value_property(scope, Some(init), "signal") else { - throw_type_error( - scope, - "Failed to construct 'NavigateEvent': NavigateEventInit.signal is required.", - ); - return false; - }; - - let navigation_type = init_string_property(scope, Some(init), "navigationType", "push"); - let can_intercept = init_bool_property(scope, Some(init), "canIntercept", false); - let user_initiated = init_bool_property(scope, Some(init), "userInitiated", false); - let hash_change = init_bool_property(scope, Some(init), "hashChange", false); - let has_ua_visual_transition = - init_bool_property(scope, Some(init), "hasUAVisualTransition", false); - let form_data = init_value_property(scope, Some(init), "formData") - .unwrap_or_else(|| v8::null(scope).into()); - let download_request = init_value_property(scope, Some(init), "downloadRequest") - .unwrap_or_else(|| v8::null(scope).into()); - let info = init - .get(scope, v8str(scope, "info").into()) - .unwrap_or_else(|| v8::undefined(scope).into()); - let source_element = init_value_property(scope, Some(init), "sourceElement") - .unwrap_or_else(|| v8::null(scope).into()); - - let navigation_type = v8_string(scope, &navigation_type).expect("NavigateEvent navigationType"); - NavigateEventInitDeclaration::new( - navigation_type, - destination, - can_intercept, - user_initiated, - hash_change, - signal, - form_data, - download_request, - info, - has_ua_visual_transition, - source_element, - ) - .initialize(scope, event) - .expect("NavigateEvent init declaration should initialize"); + init: super::navigation_init::NavigateEventInitMembers<'s>, +) { + init.initialize_snapshot(scope, event) + .expect("NavigateEvent state"); define_navigate_event_internal_flag(scope, event, true); - - true } fn define_navigate_event_internal_flag( diff --git a/moli-renderer-v8/src/context_bootstrap/events/subclasses/navigation_init.rs b/moli-renderer-v8/src/context_bootstrap/events/subclasses/navigation_init.rs new file mode 100644 index 00000000000..49e4cdc9d3a --- /dev/null +++ b/moli-renderer-v8/src/context_bootstrap/events/subclasses/navigation_init.rs @@ -0,0 +1,93 @@ +use crate::context_bootstrap::events::EventInit; +use crate::{web_api_interfaces, webidl}; + +#[derive(Clone, Copy, webidl::WebIdlEnum)] +#[webidl(name = "NavigationType", rename_all = "kebab-case")] +pub(super) enum NavigationType { + Push, + Replace, + Reload, + Traverse, +} + +// Convert inherited EventInit members first, then derived members in Web IDL +// lexicographic order. Parse the entire dictionary before initializing an event. +#[derive(webidl::WebIdlDictionary)] +#[webidl(prefix = "NavigateEventInit", snapshot)] +pub(super) struct NavigateEventInitMembers<'s> { + #[webidl(inherit)] + base: EventInit, + #[webidl(name = "canIntercept", default = false, snapshot_order = 2)] + pub(super) can_intercept: bool, + #[webidl(required, interface = web_api_interfaces::NavigationDestination, snapshot_order = 1)] + pub(super) destination: v8::Local<'s, v8::Object>, + #[webidl(nullable, converter = "raw", snapshot_order = 7)] + pub(super) download_request: Option, + #[webidl(nullable, interface = web_api_interfaces::FormData, snapshot_order = 6)] + pub(super) form_data: Option>, + #[webidl(name = "hasUAVisualTransition", default = false, snapshot_order = 9)] + pub(super) has_ua_visual_transition: bool, + #[webidl(name = "hashChange", default = false, snapshot_order = 4)] + pub(super) hash_change: bool, + #[webidl(converter = "raw", snapshot_default = v8::undefined(scope).into(), snapshot_order = 8)] + pub(super) info: Option>, + #[webidl(name = "navigationType", converter = "enum", default = NavigationType::Push, snapshot_order = 0)] + pub(super) navigation_type: NavigationType, + #[webidl(required, interface = web_api_interfaces::AbortSignal, snapshot_order = 5)] + pub(super) signal: v8::Local<'s, v8::Object>, + #[webidl(nullable, interface = web_api_interfaces::Element, snapshot_order = 10)] + pub(super) source_element: Option>, + #[webidl(name = "userInitiated", default = false, snapshot_order = 3)] + pub(super) user_initiated: bool, +} + +#[derive(webidl::WebIdlDictionary)] +#[webidl(prefix = "NavigationCurrentEntryChangeEventInit", snapshot)] +pub(super) struct NavigationCurrentEntryChangeEventInitMembers<'s> { + #[webidl(inherit)] + base: EventInit, + #[webidl(required, interface = web_api_interfaces::NavigationHistoryEntry)] + pub(super) from: v8::Local<'s, v8::Object>, + #[webidl(name = "navigationType", nullable, converter = "enum")] + pub(super) navigation_type: Option, +} + +impl NavigateEventInitMembers<'_> { + pub(super) fn event_flags(&self) -> (bool, bool, bool) { + (self.base.bubbles, self.base.cancelable, self.base.composed) + } +} + +impl NavigationCurrentEntryChangeEventInitMembers<'_> { + pub(super) fn event_flags(&self) -> (bool, bool, bool) { + (self.base.bubbles, self.base.cancelable, self.base.composed) + } +} + +#[derive(webidl::WebIdlArgs)] +#[webidl(prefix = "NavigateEvent")] +struct NavigateArgs<'s> { + #[webidl(index = 1, required, dictionary)] + init: NavigateEventInitMembers<'s>, +} + +#[derive(webidl::WebIdlArgs)] +#[webidl(prefix = "NavigationCurrentEntryChangeEvent")] +struct CurrentEntryChangeArgs<'s> { + #[webidl(index = 1, required, dictionary)] + init: NavigationCurrentEntryChangeEventInitMembers<'s>, +} + +pub(super) fn parse_navigate_event_init<'s>( + scope: &mut v8::PinScope<'s, '_>, + args: &v8::FunctionCallbackArguments<'s>, +) -> Option> { + webidl::parse_args::(scope, args).map(|parsed| parsed.init) +} + +pub(super) fn parse_current_entry_change_event_init<'s>( + scope: &mut v8::PinScope<'s, '_>, + args: &v8::FunctionCallbackArguments<'s>, +) -> Option> { + webidl::parse_args::(scope, args).map(|parsed| parsed.init) +} 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 0cfeb12bddd..ee0b205064b 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 @@ -45,6 +45,7 @@ mod message_port_events; mod message_port_lifecycle; mod misc; mod navigation; +mod navigation_event_init; mod notification_init; mod payload_event_init; mod performance; diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/navigate_event.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/navigate_event.rs index 22384f3ba2a..4c564a2af19 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/navigate_event.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation/navigate_event.rs @@ -812,14 +812,12 @@ fn navigate_event_internal_flags_are_not_script_writable() { .eval( r##" (() => { - const destination = { - url: location.href, - key: "", - id: "", - index: 0, - sameDocument: true, - getState() { return null; } - }; + let destination; + navigation.addEventListener("navigate", event => { + destination = event.destination; + event.preventDefault(); + }, {once: true}); + history.pushState(null, "", "#synthetic-event-destination"); const event = new NavigateEvent("navigate", { destination, signal: new AbortController().signal, diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/navigation_event_init.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation_event_init.rs new file mode 100644 index 00000000000..e63d66ba9a3 --- /dev/null +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/navigation_event_init.rs @@ -0,0 +1,13 @@ +use super::*; + +#[test] +fn navigation_event_dictionaries_validate_required_native_payloads_before_state() { + let mut vm = new_storage_html_test_vm("https://navigation-event-dictionary.test/"); + assert_eq!( + vm.eval(include_str!( + "../../../../tests/fixtures/navigation-event-dictionary.js" + )) + .unwrap(), + "true" + ); +} diff --git a/moli-renderer-v8/tests/fixtures/navigation-event-dictionary.js b/moli-renderer-v8/tests/fixtures/navigation-event-dictionary.js new file mode 100644 index 00000000000..483c21b24c4 --- /dev/null +++ b/moli-renderer-v8/tests/fixtures/navigation-event-dictionary.js @@ -0,0 +1,66 @@ +(() => { + const assert = (value, label) => { if (!value) throw Error(label); }; + let destination; + const capture = e => { destination = e.destination; e.preventDefault(); }; + navigation.addEventListener('navigate', capture, {once: true}); + const result = navigation.navigate(location.href + '#dictionary'); + result.committed.catch(() => {}); result.finished.catch(() => {}); + assert(destination, 'genuine destination captured'); + const signal = new AbortController().signal, from = navigation.currentEntry; + const formData = new FormData(), sourceElement = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); + const base = ['bubbles', 'cancelable', 'composed']; + const cases = [ + [NavigationCurrentEntryChangeEvent, ['from', 'navigationType'], {from}], + [NavigateEvent, ['canIntercept', 'destination', 'downloadRequest', 'formData', + 'hasUAVisualTransition', 'hashChange', 'info', 'navigationType', 'signal', 'sourceElement', 'userInitiated'], {destination, signal}] + ]; + for (const [C, own, values] of cases) { + assert(C.length === 2, C.name + ' arity'); + let converted = false, caught; + try { new C({toString() {converted = true; throw 7;}}); } catch (e) { caught = e; } + assert(caught instanceof TypeError && !converted, C.name + ' required dictionary before type conversion'); + const members = [...base, ...own], reads = []; + const options = new Proxy({}, {get(_, key) { + reads.push(key); return key in values ? values[key] : undefined; + }, has() {throw Error('Get required');}}); + new C('audit', options); + assert(reads.join() === members.join(), C.name + ' order: ' + reads); + for (const member of members) for (const sentinel of [{}, null, undefined, 7]) { + reads.length = 0; let caught = Symbol(); + try { new C('audit', new Proxy({}, {get(_, key) { + reads.push(key); if(key === member) throw sentinel; + return key in values ? values[key] : undefined; + }})); } catch(e) { caught = e; } + assert(caught === sentinel, C.name + '/' + member + ' exception identity'); + assert(reads.join() === members.slice(0, members.indexOf(member) + 1).join(), C.name + '/' + member + ' abrupt completion'); + } + for (const value of [1, true, 'init', 1n, Symbol()]) { + let caught; try { new C('audit', value); } catch(e) { caught = e; } + assert(caught instanceof TypeError, C.name + ' primitive dictionary'); + } + for (const navigationType of ['bad', null]) { + if (navigationType === null && C === NavigationCurrentEntryChangeEvent) continue; + let caught; try { new C('audit', {...values, navigationType}); } catch(e) { caught = e; } + assert(caught instanceof TypeError, C.name + ' enum conversion'); + } + } + for (const [C, member, valid, values] of [ + [NavigationCurrentEntryChangeEvent, 'from', from, {}], + [NavigateEvent, 'destination', destination, {signal}], [NavigateEvent, 'signal', signal, {destination}], + [NavigateEvent, 'formData', formData, {destination,signal}], [NavigateEvent, 'sourceElement', sourceElement, {destination,signal}] + ]) { + for (const value of [{}, new Proxy(valid, {})]) { + let caught; try { new C('audit', {...values, [member]: value}); } catch(e) { caught = e; } + assert(caught instanceof TypeError, C.name + '/' + member + ' native brand'); + } + Object.setPrototypeOf(valid, null); + assert(new C('audit', {...values, [member]: valid})[member] === valid, C.name + '/' + member + ' native identity'); + } + const info = {}, utf16 = '\ud800\0\udc00'; + const event = new NavigateEvent('audit', {destination, signal, info, downloadRequest: utf16, formData, sourceElement}); + assert(event.info === info && event.downloadRequest === utf16 && event.formData === formData && event.sourceElement === sourceElement, 'native and any payload'); + assert(new NavigationCurrentEntryChangeEvent('audit', {from}).navigationType === null, 'nullable navigation type default'); + let caught; try {event.intercept();} catch(e) {caught=e;} + assert(caught && caught.name === 'SecurityError', 'synthetic event remains unable to intercept navigation'); + return true; +})()