Skip to content

Commit 06ccb6d

Browse files
authored
chore: Added onbeforeunload to window type definition (#16251)
1 parent ba3d0da commit 06ccb6d

File tree

5 files changed

+19
-3
lines changed

5 files changed

+19
-3
lines changed

cli/dts/lib.deno.window.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ declare class Window extends EventTarget {
2121
readonly self: Window & typeof globalThis;
2222
onerror: ((this: Window, ev: ErrorEvent) => any) | null;
2323
onload: ((this: Window, ev: Event) => any) | null;
24+
onbeforeunload: ((this: Window, ev: Event) => any) | null;
2425
onunload: ((this: Window, ev: Event) => any) | null;
2526
onunhandledrejection:
2627
| ((this: Window, ev: PromiseRejectionEvent) => any)
@@ -76,6 +77,8 @@ declare var onerror: ((this: Window, ev: ErrorEvent) => any) | null;
7677
/** @category DOM Events */
7778
declare var onload: ((this: Window, ev: Event) => any) | null;
7879
/** @category DOM Events */
80+
declare var onbeforeunload: ((this: Window, ev: Event) => any) | null;
81+
/** @category DOM Events */
7982
declare var onunload: ((this: Window, ev: Event) => any) | null;
8083
/** @category Observability */
8184
declare var onunhandledrejection:

cli/tests/testdata/run/onload/imported.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { assert } from "../../../../../test_util/std/testing/asserts.ts";
33
import "./nest_imported.ts";
44

55
const handler = (e: Event) => {
6-
assert(!e.cancelable);
6+
assert(e.type === "beforeunload" ? e.cancelable : !e.cancelable);
77
console.log(`got ${e.type} event in event handler (imported)`);
88
};
99

1010
window.addEventListener("load", handler);
11+
window.addEventListener("beforeunload", handler);
1112
window.addEventListener("unload", handler);
1213
console.log("log from imported script");

cli/tests/testdata/run/onload/main.out

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ got load event in event handler (nest_imported)
55
got load event in event handler (imported)
66
got load event in event handler (main)
77
got load event in onload function
8+
got beforeunload event in event handler (nest_imported)
9+
got beforeunload event in event handler (imported)
10+
got beforeunload event in event handler (main)
11+
got beforeunload event in onbeforeunload function
812
got unload event in event handler (nest_imported)
913
got unload event in event handler (imported)
1014
got unload event in event handler (main)

cli/tests/testdata/run/onload/main.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,26 @@ assert(window.hasOwnProperty("onload"));
66
assert(window.onload === null);
77

88
const eventHandler = (e: Event) => {
9-
assert(!e.cancelable);
9+
assert(e.type === "beforeunload" ? e.cancelable : !e.cancelable);
1010
console.log(`got ${e.type} event in event handler (main)`);
1111
};
1212

1313
window.addEventListener("load", eventHandler);
1414

15+
window.addEventListener("beforeunload", eventHandler);
16+
1517
window.addEventListener("unload", eventHandler);
1618

1719
window.onload = (e: Event) => {
1820
assert(!e.cancelable);
1921
console.log(`got ${e.type} event in onload function`);
2022
};
2123

24+
window.onbeforeunload = (e: BeforeUnloadEvent) => {
25+
assert(e.cancelable);
26+
console.log(`got ${e.type} event in onbeforeunload function`);
27+
};
28+
2229
window.onunload = (e: Event) => {
2330
assert(!e.cancelable);
2431
console.log(`got ${e.type} event in onunload function`);

cli/tests/testdata/run/onload/nest_imported.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import { assert } from "../../../../../test_util/std/testing/asserts.ts";
33

44
const handler = (e: Event) => {
5-
assert(!e.cancelable);
5+
assert(e.type === "beforeunload" ? e.cancelable : !e.cancelable);
66
console.log(`got ${e.type} event in event handler (nest_imported)`);
77
};
88

99
window.addEventListener("load", handler);
10+
window.addEventListener("beforeunload", handler);
1011
window.addEventListener("unload", handler);
1112
console.log("log from nest_imported script");

0 commit comments

Comments
 (0)