From 68bc65b155aaee6b8fbad35af0fc726575b6d539 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Thu, 10 Nov 2022 15:58:49 +0000 Subject: [PATCH] Bug 1795635 [wpt PR 36488] - Test that javascript: URL navigation does not fire beforeunload, a=testonly Automatic update from web-platform-tests Test that javascript: URL navigation does not fire beforeunload The current spec fires beforeunload, but the rewrite in https://github.com/whatwg/html/pull/6315 does not. -- wpt-commits: e5144d4daa5979805e0e1360c2bc69abf6825bff wpt-pr: 36488 --- .../javascript-url-no-beforeunload.window.js | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 testing/web-platform/tests/html/browsers/browsing-the-web/navigating-across-documents/javascript-url-no-beforeunload.window.js diff --git a/testing/web-platform/tests/html/browsers/browsing-the-web/navigating-across-documents/javascript-url-no-beforeunload.window.js b/testing/web-platform/tests/html/browsers/browsing-the-web/navigating-across-documents/javascript-url-no-beforeunload.window.js new file mode 100644 index 000000000000..47e8f11797dc --- /dev/null +++ b/testing/web-platform/tests/html/browsers/browsing-the-web/navigating-across-documents/javascript-url-no-beforeunload.window.js @@ -0,0 +1,80 @@ +// META: script=../resources/helpers.js + +for (const stringCompletion of [false, true]) { + const testNameSuffix = stringCompletion ? ": string completion" : ": undefined completion"; + + testNoBeforeunload( + { testRunnerWindow: "top", stringCompletion }, + async (t, urlToSet) => { + const iframe = await addIframe(); + iframe.contentWindow.location.href = urlToSet; + + return iframe.contentWindow; + }, + `Navigating an iframe via location.href to a javascript: URL must not fire beforeunload${testNameSuffix}` + ); + + testNoBeforeunload( + { testRunnerWindow: "top", stringCompletion }, + async (t, urlToSet) => { + const iframe = await addIframe(); + iframe.src = urlToSet; + + return iframe.contentWindow; + }, + `Navigating an iframe via src="" to a javascript: URL after insertion must not fire beforeunload${testNameSuffix}` + ); + + testNoBeforeunload( + { testRunnerWindow: "opener", stringCompletion }, + async (t, urlToSet) => { + const w = await openWindow("/common/blank.html", t); + w.location.href = urlToSet; + + return w; + }, + `Navigating an opened window via location.href to a javascript: URL must not fire beforeunload${testNameSuffix}` + ); + + + testNoBeforeunload( + { testRunnerWindow: "opener", stringCompletion }, + async (t, urlToSet) => { + const w = await openWindow("../resources/has-iframe.html", t); + w.frames[0].onbeforeunload = t.unreached_func("beforeunload must not fire on the iframe"); + w.location.href = urlToSet; + + return w; + }, + `Navigating an opened window with an iframe via location.href to a javascript: URL must not fire beforeunload on the iframe${testNameSuffix}` + ); +} + +function testNoBeforeunload({ testRunnerWindow, stringCompletion }, setupAndNavigateFunc, description) { + promise_test(async t => { + t.add_cleanup(() => { + delete window.resolveTestPromise; + }); + + const ranPromise = new Promise(resolve => { + window.resolveTestPromise = resolve; + }); + + const urlToSet = makeURL({ testRunnerWindow, stringCompletion }); + const w = await setupAndNavigateFunc(t, urlToSet); + w.onbeforeunload = t.unreached_func("beforeunload must not fire"); + + await ranPromise; + if (stringCompletion) { + await waitForMessage(w); + } + }, description); +} + +function makeURL({ testRunnerWindow, stringCompletion }) { + const completion = stringCompletion ? + `"a string";` : + `undefined;`; + + return `javascript:window.${testRunnerWindow}.resolveTestPromise();${completion};`; +} -- 2.11.4.GIT