diff --git a/trusted-types/support/navigation-report-only-support.html b/trusted-types/support/navigation-report-only-support.html index 6b85fec81d10e6..791559f7a19bec 100644 --- a/trusted-types/support/navigation-report-only-support.html +++ b/trusted-types/support/navigation-report-only-support.html @@ -5,6 +5,9 @@

Support page for trusted-types-navigation-report-only.*.html tests.

link +
+ +
diff --git a/trusted-types/support/navigation-support.html b/trusted-types/support/navigation-support.html index 3c576e2e799157..47f33c9fb6430c 100644 --- a/trusted-types/support/navigation-support.html +++ b/trusted-types/support/navigation-support.html @@ -5,6 +5,9 @@

Support page for trusted-types-navigation.*.html tests.

link +
+ +
diff --git a/trusted-types/support/navigation-support.js b/trusted-types/support/navigation-support.js index e6b452b82a1433..41dce761def94a 100644 --- a/trusted-types/support/navigation-support.js +++ b/trusted-types/support/navigation-support.js @@ -1,20 +1,24 @@ const kNavigationAttempted = "navigationattempted=1"; function navigateToJavascriptURL(reportOnly) { - const params = new URLSearchParams(location.search); + let params = new URLSearchParams(location.search); + if (!!params.get("defaultpolicy")) { - trustedTypes.createPolicy("default", { - createScript: s => s.replace("continue", "defaultpolicywashere"), - }); + trustedTypes.createPolicy("default", { + createScript: s => { + return s.replace("continue", "defaultpolicywashere") + }, + }); } function bounceEventToOpener(e) { - const msg = {}; - for (const field of ["effectiveDirective", "sample", "type"]) { - msg[field] = e[field]; - } - msg["uri"] = location.href; - window.opener.postMessage(msg, "*"); + const msg = {}; + for (const field of ["effectiveDirective", "sample", "type"]) { + msg[field] = e[field]; + } + + msg["uri"] = location.href; + window.opener.postMessage(msg, "*"); } // If a navigation is blocked by Trusted Types, we expect this window to @@ -29,27 +33,43 @@ function navigateToJavascriptURL(reportOnly) { // Navigate to the non-report-only version of the test. That has the same // event listening setup as this, but is a different target URI. target_script = `location.href='${location.href.replace("-report-only", "") + - (location.href.includes("?") ? "&" : "?") + kNavigationAttempted + "&continue"}';`; + (location.href.endsWith(".html") ? "?" : "&") + kNavigationAttempted + "&continue"}';`; } else { // We'll use a javascript:-url to navigate to ourselves, so that we can // re-use the messageing mechanisms above. target_script = `location.href='${location.href + "&" + kNavigationAttempted}&continue';`; } - const target = `javascript:${target_script}`; - const anchor = document.getElementById("anchor"); - anchor.href = target; + function getAndPreparareNavigationElement(javaScriptURL) { + let target = "_self"; + if (!!params.get("frame")) { + const frame = document.createElement("iframe"); + frame.src = "frame-without-trusted-types.html"; + frames.name = "frame"; + document.body.appendChild(frame); + target = "frame"; + } + + if (!!params.get("form-submission")) { + const submit = document.getElementById("submit"); - if (!!params.get("frame")) { - const frame = document.createElement("iframe"); - frame.src = "frame-without-trusted-types.html"; - frames.name = "frame"; - document.body.appendChild(frame); - anchor.target = "frame"; + // Careful, the IDL attributes are defined in camel-case. + submit.formAction = javaScriptURL; + submit.formTarget = target; + + return submit; + } + + const anchor = document.getElementById("anchor"); + anchor.href = javaScriptURL; + anchor.target = target; + return anchor; } + const navigationElement = getAndPreparareNavigationElement(`javascript:${target_script}`); + // Prevent loops. if (!location.search.includes(kNavigationAttempted)) { - document.addEventListener("DOMContentLoaded", _ => anchor.click()); + document.addEventListener("DOMContentLoaded", _ => navigationElement.click()); } } diff --git a/trusted-types/trusted-types-navigation.html b/trusted-types/trusted-types-navigation.html index be3c19d647a599..74a25fa3480c37 100644 --- a/trusted-types/trusted-types-navigation.html +++ b/trusted-types/trusted-types-navigation.html @@ -6,6 +6,8 @@