diff --git a/cli/dts/lib.deno.window.d.ts b/cli/dts/lib.deno.window.d.ts index 9515f5b23e..77204fcbd6 100644 --- a/cli/dts/lib.deno.window.d.ts +++ b/cli/dts/lib.deno.window.d.ts @@ -21,6 +21,7 @@ declare class Window extends EventTarget { readonly self: Window & typeof globalThis; onerror: ((this: Window, ev: ErrorEvent) => any) | null; onload: ((this: Window, ev: Event) => any) | null; + onbeforeunload: ((this: Window, ev: Event) => any) | null; onunload: ((this: Window, ev: Event) => any) | null; onunhandledrejection: | ((this: Window, ev: PromiseRejectionEvent) => any) @@ -76,6 +77,8 @@ declare var onerror: ((this: Window, ev: ErrorEvent) => any) | null; /** @category DOM Events */ declare var onload: ((this: Window, ev: Event) => any) | null; /** @category DOM Events */ +declare var onbeforeunload: ((this: Window, ev: Event) => any) | null; +/** @category DOM Events */ declare var onunload: ((this: Window, ev: Event) => any) | null; /** @category Observability */ declare var onunhandledrejection: diff --git a/cli/tests/testdata/run/onload/imported.ts b/cli/tests/testdata/run/onload/imported.ts index 969f97e563..f4157b86f8 100644 --- a/cli/tests/testdata/run/onload/imported.ts +++ b/cli/tests/testdata/run/onload/imported.ts @@ -3,10 +3,11 @@ import { assert } from "../../../../../test_util/std/testing/asserts.ts"; import "./nest_imported.ts"; const handler = (e: Event) => { - assert(!e.cancelable); + assert(e.type === "beforeunload" ? e.cancelable : !e.cancelable); console.log(`got ${e.type} event in event handler (imported)`); }; window.addEventListener("load", handler); +window.addEventListener("beforeunload", handler); window.addEventListener("unload", handler); console.log("log from imported script"); diff --git a/cli/tests/testdata/run/onload/main.out b/cli/tests/testdata/run/onload/main.out index 9b1f454c94..b25d33fa86 100644 --- a/cli/tests/testdata/run/onload/main.out +++ b/cli/tests/testdata/run/onload/main.out @@ -5,6 +5,10 @@ got load event in event handler (nest_imported) got load event in event handler (imported) got load event in event handler (main) got load event in onload function +got beforeunload event in event handler (nest_imported) +got beforeunload event in event handler (imported) +got beforeunload event in event handler (main) +got beforeunload event in onbeforeunload function got unload event in event handler (nest_imported) got unload event in event handler (imported) got unload event in event handler (main) diff --git a/cli/tests/testdata/run/onload/main.ts b/cli/tests/testdata/run/onload/main.ts index 798b8aa7b0..126627e34f 100644 --- a/cli/tests/testdata/run/onload/main.ts +++ b/cli/tests/testdata/run/onload/main.ts @@ -6,12 +6,14 @@ assert(window.hasOwnProperty("onload")); assert(window.onload === null); const eventHandler = (e: Event) => { - assert(!e.cancelable); + assert(e.type === "beforeunload" ? e.cancelable : !e.cancelable); console.log(`got ${e.type} event in event handler (main)`); }; window.addEventListener("load", eventHandler); +window.addEventListener("beforeunload", eventHandler); + window.addEventListener("unload", eventHandler); window.onload = (e: Event) => { @@ -19,6 +21,11 @@ window.onload = (e: Event) => { console.log(`got ${e.type} event in onload function`); }; +window.onbeforeunload = (e: BeforeUnloadEvent) => { + assert(e.cancelable); + console.log(`got ${e.type} event in onbeforeunload function`); +}; + window.onunload = (e: Event) => { assert(!e.cancelable); console.log(`got ${e.type} event in onunload function`); diff --git a/cli/tests/testdata/run/onload/nest_imported.ts b/cli/tests/testdata/run/onload/nest_imported.ts index 351a3cb221..a942d27295 100644 --- a/cli/tests/testdata/run/onload/nest_imported.ts +++ b/cli/tests/testdata/run/onload/nest_imported.ts @@ -2,10 +2,11 @@ import { assert } from "../../../../../test_util/std/testing/asserts.ts"; const handler = (e: Event) => { - assert(!e.cancelable); + assert(e.type === "beforeunload" ? e.cancelable : !e.cancelable); console.log(`got ${e.type} event in event handler (nest_imported)`); }; window.addEventListener("load", handler); +window.addEventListener("beforeunload", handler); window.addEventListener("unload", handler); console.log("log from nest_imported script");