Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dispatch_event to take Event or CustomEvent #3938

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion crates/web-sys/src/features/gen_EventTarget.rs
Expand Up @@ -155,7 +155,18 @@ extern "C" {
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `Event`, `EventTarget`*"]
pub fn dispatch_event(this: &EventTarget, event: &Event) -> Result<bool, JsValue>;
pub fn dispatch_event_with_event(this: &EventTarget, event: &Event) -> Result<bool, JsValue>;
#[cfg(feature = "CustomEvent")]
# [wasm_bindgen (catch , method , structural , js_class = "EventTarget" , js_name = dispatchEvent)]
#[doc = "The `dispatchEvent()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `CustomEvent`, `EventTarget`*"]
pub fn dispatch_event_with_custom_event(
this: &EventTarget,
event: &CustomEvent,
) -> Result<bool, JsValue>;
# [wasm_bindgen (catch , method , structural , js_class = "EventTarget" , js_name = removeEventListener)]
#[doc = "The `removeEventListener()` method."]
#[doc = ""]
Expand Down
11 changes: 11 additions & 0 deletions crates/web-sys/tests/wasm/custom_event.js
@@ -0,0 +1,11 @@
export function new_custom_event() {
return new Promise(resolve => {
window.addEventListener("test-custom-event", resolve);
window.dispatchEvent(new CustomEvent("test-custom-event", {
detail: "detail",
bubbles: true,
cancelable: true,
composed: true,
}));
});
}
31 changes: 31 additions & 0 deletions crates/web-sys/tests/wasm/custom_event.rs
@@ -0,0 +1,31 @@
use js_sys::{Object, Promise};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::JsFuture;
use wasm_bindgen_test::*;
use web_sys::CustomEvent;

#[wasm_bindgen(module = "/tests/wasm/custom_event.js")]
extern "C" {
fn new_custom_event() -> Promise;
}

#[wasm_bindgen_test]
async fn custom_event() {
let result = JsFuture::from(new_custom_event()).await.unwrap();
let event = CustomEvent::from(result);
// All DOM interfaces should inherit from `Object`.
assert!(event.is_instance_of::<Object>());
let _: &Object = event.as_ref();

// These should match `new Event`.
assert!(event.bubbles());
assert!(event.cancelable());
assert!(event.composed());
assert_eq!(event.detail().as_string().unwrap(), "detail");

// The default behavior not initially prevented, but after
// we call `prevent_default` it better be.
assert!(!event.default_prevented());
event.prevent_default();
assert!(event.default_prevented());
}
2 changes: 1 addition & 1 deletion crates/web-sys/webidls/enabled/EventTarget.webidl
Expand Up @@ -37,5 +37,5 @@ interface EventTarget {
EventListener listener,
optional (EventListenerOptions or boolean) options);
[Throws, NeedsCallerType]
boolean dispatchEvent(Event event);
boolean dispatchEvent((Event or CustomEvent) event);
};