summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/scroll-to-text-fragment/redirects.html
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /testing/web-platform/tests/scroll-to-text-fragment/redirects.html
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/scroll-to-text-fragment/redirects.html')
-rw-r--r--testing/web-platform/tests/scroll-to-text-fragment/redirects.html126
1 files changed, 126 insertions, 0 deletions
diff --git a/testing/web-platform/tests/scroll-to-text-fragment/redirects.html b/testing/web-platform/tests/scroll-to-text-fragment/redirects.html
new file mode 100644
index 0000000000..5ad910affe
--- /dev/null
+++ b/testing/web-platform/tests/scroll-to-text-fragment/redirects.html
@@ -0,0 +1,126 @@
+<!doctype html>
+<title>TextFragment invoked on redirects</title>
+<meta charset=utf-8>
+<link rel="help" href="https://wicg.github.io/ScrollToTextFragment/">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="/resources/testdriver.js"></script>
+<script src="/resources/testdriver-vendor.js"></script>
+<script src="/common/get-host-info.sub.js"></script>
+<script src="/common/utils.js"></script>
+<script src="stash.js"></script>
+
+<!--See comment in scroll-to-text-fragment.html for why these tests have the
+structure they do. -->
+<script>
+// This test ensure correct operation of text-fragments through both HTTP and
+// client side redirects in various scenarios.
+
+// Constructs a URL to either redirect.py or the local client-redirect.html;
+// which will cause an HTTP or client based redirect, respectively, to
+// |to_url|. |type| provides a numeric 30x code to specify an HTTP redirect,
+// "location" for a write to window.location, or "meta" for a <meta> refresh.
+function buildRedirectUrl(to_url, type) {
+ let dest = "";
+ to_url = encodeURIComponent(to_url);
+
+ if (typeof type == "number") {
+ // If the type is a number, it's an HTTP response code, use redirect.py to
+ // respond with an HTTP redirect.
+ const code = type;
+ dest = `${get_host_info().ORIGIN}/common/redirect.py?status=${code}&location=${to_url}`;
+ } else if (type == 'meta' || type == 'location') {
+ // Otherwise we're requesting a client-side redirect, either a <meta> tag
+ // or window.location. Use the client-redirect file to bounce to the
+ // destination.
+ dest = `client-redirect.html?${type}&${to_url}`;
+ }
+ return dest;
+}
+
+// Turns |path| from a relative-to-this-file path into a full URL.
+function relativePathToFull(path) {
+ const pathname = window.location.toString();
+ const base_path = pathname.substring(0, pathname.lastIndexOf('/') + 1);
+ return base_path + path;
+}
+
+const status_codes = [301, 302, 303, 307, 308];
+
+// Test that an HTTP redirect to a URL with a text fragment invokes the
+// fragment.
+for (let code of status_codes) {
+ promise_test(t => new Promise((resolve, reject) => {
+ let key = token();
+
+ const abs_url = relativePathToFull(`redirects-target.html?key=${key}#:~:text=target`);
+ const url = buildRedirectUrl(abs_url, code);
+
+ test_driver.bless('Open a URL with a text fragment directive', () => {
+ window.open(url, '_blank', 'noopener');
+ });
+
+ fetchResults(key, resolve, reject);
+ }).then(data => {
+ assert_equals(data.scrolled, true);
+ }), `Text fragment works from HTTP ${code} redirect.`);
+}
+
+// Test that a URL with a text fragment that causes an HTTP redirect preserves
+// the fragment and invokes it on the destination page.
+for (let code of status_codes) {
+ promise_test(t => new Promise((resolve, reject) => {
+ let key = token();
+
+ const abs_url = relativePathToFull(`redirects-target.html?key=${key}`);
+ const url = buildRedirectUrl(abs_url, code) + "#:~:text=target";
+
+ test_driver.bless('Open a URL with a text fragment directive', () => {
+ window.open(url, '_blank', 'noopener');
+ });
+
+ fetchResults(key, resolve, reject);
+ }).then(data => {
+ assert_equals(data.scrolled, true);
+ }), `Text fragment propagated through HTTP ${code} redirect.`);
+}
+
+// Test that client-side redirects (using script) to a URL with a text fragment
+// cause the text fragment to be invoked.
+for (let type of ['location', 'meta']) {
+ promise_test(t => new Promise((resolve, reject) => {
+ let key = token();
+
+ const to_url = `redirects-target.html?key=${key}#:~:text=target`
+ const url = buildRedirectUrl(to_url, type);
+
+ test_driver.bless('Open a URL with a text fragment directive', () => {
+ window.open(url, '_blank', 'noopener');
+ });
+
+ fetchResults(key, resolve, reject);
+ }).then(data => {
+ assert_equals(data.scrolled, true);
+ }), `Text fragment works on client-side ${type} redirect.`);
+}
+
+// Test that client-side redirects (using script) to a URL with a text fragment
+// cause the text fragment to be invoked only the first time. A further
+// redirect without a user gesture is blocked.
+for (let type of ['location', 'meta']) {
+ promise_test(t => new Promise((resolve, reject) => {
+ let key = token();
+
+ const to_url = `redirects-target.html?twice&key=${key}#:~:text=target`
+ const url = buildRedirectUrl(to_url, type);
+
+ test_driver.bless('Open a URL with a text fragment directive', () => {
+ window.open(url, '_blank', 'noopener');
+ });
+
+ fetchResults(key, resolve, reject);
+ }).then(data => {
+ assert_equals(data.scrolled, false);
+ }), `One text fragment per user gesture allowed in client-side ${type} redirect.`);
+}
+</script>